call c# from lua indicator

Use all the fxcodebase indicator in your application!

Moderator: admin

Re: call c# from lua indicator

Postby Victor.Tereschenko » Wed Nov 16, 2011 5:21 am

Dosihris wrote:Hi Victor,

thank you very much for this sample. i will test it in the next hours. I have already seen that there is a lua c++ project, another c++ project to initialize lua and a .net project that calculates the real values. well, i haven't testet this sample yet, but do you know a way to communicate from lua to a c# project without c++ wrapper projects? Somebody wrote it should be possible with a com visible .net assembly. do you know anything about this way?

Nic

I'm not sure, whether it's possible to call C# method from Lua via COM. I'll try to google it when I have more "free" time.
Dosihris wrote:Hi Victor,

i have testet your example. I can compile it and it works. Can you help me again with some parameters? I want to transfer some parameter from the indicator, some numeric values to c#... i want to evaluate the numbers and then create buy or sell orders with the .net api...

Thanks for your help

Nic

If you need an example of how to pass a parameter into your C# methods try to modify my sample like this:
Code: Select all
--drawLine-sample.lua

function Update(period)
...
        local fromDate, fromLevel, toDate, toLevel = LuaDll:getLineCoordinates(source.close[period]);
...
end

Code: Select all
//LuaDll.h
int getLineCoordinates(lua_State *L)
{
    // first parameter starts with 2. 1 contains called table/"object" (like "this" in C#)
    if (!lua_isnumber(L, 2))
        return luaL_error(L, "The first parameter must be a number");
    double level = lua_tonumber(L, 2);
...
    double lineStartLevel = LineCoordinatesCalculator::GetLineStartLevel(level);//call of C# method with a parameter
    double lineEndLevel = LineCoordinatesCalculator::GetLineEndLevel(level);//call of C# method with a parameter
...
“There are only three sports: bullfighting, motor racing, and mountaineering; all the rest are merely games.” (c) Ernest Hemingway
Victor.Tereschenko
FXCodeBase: Confirmed User
 
Posts: 144
Joined: Fri Nov 19, 2010 8:55 am

Re: call c# from lua indicator

Postby Dosihris » Wed Nov 16, 2011 6:31 am

cool. this works. now i can transfer all my data to c#. thank you. Looks a bit complicated, but it works... thanks a lot.

Have a nice day

Nic
Dosihris
 
Posts: 4
Joined: Wed Nov 09, 2011 5:01 pm

Re: call c# from lua indicator

Postby vittorio » Wed Aug 08, 2012 4:17 pm

very good code ;)

Sorry if I open old post but I have a question, i try to get through instrument in use(ex. EUR/USD), from lua to c# but i'm not good whit C++ and i have problem. can u help me pls.
in first sry my orrible english...and my possible confusion i'm newbe :oops:

this is what i do
Code: Select all
--drawLine-sample.lua

function Prepare()
...
        local nameInstrLong = LuaDll:getLineCoordinates(source.instrument());
...
end

it's right in Prepare() or I put line in Update section?

and in LuaDll.h how can do? I try this
Code: Select all
//LuaDll.h
string getLineCoordinates(lua_State *L)
{
...
   string nomeStrum= LineCoordinatesCalculator::GetInstrument(???);

GetInstrument(???) :) Itry whit L but this give me this error...

----
Errore 3 error C2664: 'Calculation::LineCoordinatesCalculator::GetInstrument': impossibile convertire il parametro 1 da 'lua_State' a 'System::String^' e:\my progetti\forex\luadll\luadll\LuaDll.h 18 1 LuaDll
-----
I understand why but not understand what I must use.

in c# function is..
Code: Select all
public static void GetInstrument(string instrument){...}


the partial code is only indicative for example
vittorio
 
Posts: 3
Joined: Thu Jul 19, 2012 5:41 am

Re: call c# from lua indicator

Postby Victor.Tereschenko » Thu Aug 16, 2012 11:23 pm

vittorio wrote:very good code ;)

Sorry if I open old post but I have a question, i try to get through instrument in use(ex. EUR/USD), from lua to c# but i'm not good whit C++ and i have problem. can u help me pls.
in first sry my orrible english...and my possible confusion i'm newbe :oops:
...

You've made a misprint: you should use source:instrument() (via ":")
Code: Select all
--drawLine-sample.lua

function Prepare()
...
        -- source:instrument() instead of source.instrument() !!!!!!!!!!!!!!
        local nameInstrLong = LuaDll:getLineCoordinates(source:instrument());
...
end

You can get a string from "L" via lua_tostring + pass it to the System::String constructor.
The oppoisite direction (passing string C# -> Lua) is more complex: use MarshalNetToStdString + lua_pushstring(L, s.c_str());
Code: Select all
//LuaDll.h
#using "../Calculation/bin/Debug/Calculation.dll"
#include <string> // NEW!!!!!!!!!
using namespace Calculation;
#pragma once
...
void MarshalNetToStdString(System::String^ s, std::string& os)
{
    using System::IntPtr;
    using System::Runtime::InteropServices::Marshal;

    const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer( );
    os = chars;
    Marshal::FreeHGlobal(IntPtr((void*)chars));
}

int getLineCoordinates(lua_State *L)
{
    const char* str = lua_tostring(L, 2); // 2 - first parameter!
    System::String^ nomeStrum = LineCoordinatesCalculator::GetInstrument(gcnew System::String(str));
    std::string s;
    MarshalNetToStdString(nomeStrum, s);
    lua_pushstring(L, s.c_str());
    return 1; // 1 value returned
}
“There are only three sports: bullfighting, motor racing, and mountaineering; all the rest are merely games.” (c) Ernest Hemingway
Victor.Tereschenko
FXCodeBase: Confirmed User
 
Posts: 144
Joined: Fri Nov 19, 2010 8:55 am

Re: call c# from lua indicator

Postby vittorio » Sun Sep 23, 2012 2:56 pm

thanks that's just what I needed

asap I try to complete my program...thx a lot :D
vittorio
 
Posts: 3
Joined: Thu Jul 19, 2012 5:41 am

Re: call c# from lua indicator

Postby TheStudent » Mon Jul 11, 2016 1:14 am

Has anybody tried using suggestion 1 below, using the "luacom" interface?

Nikolay.Gekht wrote:LuaInterface is developed to call Lua from .NET application and the reverse interface (calling .NET application back) works only when the Lua is initially called from Lua.

However, there is at least two variants on how to use it:

1) Slow but simple: use luacom (Lua interoperation with COM/Ole Automation) and then make your assemble properly COM visible.
2) Use direct context switching (you'll need to develop a small interoperation library in C++).

I'll try to find a day to prepare the example at least for the first case.

However, I'm very interesting in your work, so, if you really want to publish your solution here - you can count on me with any help, templates, examples, consulting.
TheStudent
 
Posts: 2
Joined: Fri May 27, 2016 7:19 pm

Previous

Return to C++/.NET API

Who is online

Users browsing this forum: No registered users and 5 guests