call c# from lua indicator

Use all the fxcodebase indicator in your application!

Moderator: admin

call c# from lua indicator

Postby Zodiac » Fri Dec 31, 2010 9:26 pm

Hi. I'm wondering if anyone's figured out how to use c# dll's in the marketscope indicators. I'm looking at LuaInterface http://luaforge.net/docman/index.php?gr ... guage_id=1

I'd like to try to use the encog neural network library written in c#.

the other option is I'm going to try to write some basic neural net algorithms for lua and i'll post them but I'm still horribly new with lua and not much better yet at neural nets.
Zodiac
 
Posts: 6
Joined: Wed Dec 15, 2010 3:50 am

Re: call c# from lua indicator

Postby Nikolay.Gekht » Sat Jan 01, 2011 1:19 pm

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.
Nikolay.Gekht
FXCodeBase: Base Builder
 
Posts: 1222
Joined: Wed Dec 16, 2009 6:39 pm
Location: Cary, NC

Re: call c# from lua indicator

Postby Zodiac » Mon Jan 03, 2011 12:04 pm

thanks man. That would be awesome to see some code for how to get started. I'm really excited to get some form of neural net going, even if its simple and slow at first.

Related question, when I export data from marketscope, I only get 300 periods but I really need thousands to train one of these NN's. The more, the better. Will I have to start logging ticks to a database? About a week or two worth of 1minute bars would be good enough I think.
Zodiac
 
Posts: 6
Joined: Wed Dec 15, 2010 3:50 am

Re: call c# from lua indicator

Postby Victor.Tereschenko » Tue Jan 04, 2011 6:44 am

2) Use direct context switching (you'll need to develop a small interoperation library in C++).
I have such an example:
Lua indicator code
Code: Select all
-- Indicator profile initialization routine
-- Defines indicator profile properties and indicator parameters
function Init()
    indicator:name("Lua-C# sample");
    indicator:description("Illustrates use of C# dll from Lua");
    indicator:requiredSource(core.Bar);
    indicator:type(core.Indicator);
   
    indicator.parameters:addColor("clr", "Color", "", core.rgb(255, 0, 0));
    indicator.parameters:addInteger("width", "Width", "", 1, 1, 5);
    indicator.parameters:addInteger("style", "Style", "", core.LINE_SOLID);
    indicator.parameters:setFlag("style", core.FLAG_LINE_STYLE);
end

-- Indicator instance initialization routine
-- Processes indicator parameters and creates output streams

local source = nil;
local first = 0;
local Color = nil;
local Width = nil;
local Style = nil;
local host = nil;

-- Routine
function Prepare()
    host = core.host;
    source = instance.source;
    first = source:first();
    require("LuaDll");
    local name = profile:id();
    instance:name(name);
   
    out = instance:addStream("Out", core.Line, name, "Out", instance.parameters.clr, first)
    out:setWidth(instance.parameters.width);
    out:setStyle(instance.parameters.style);
end

local errorLoad = false;

-- Indicator calculation routine
function Update(period)
    if (period >= first) then
    core.host:trace(source.open[period]);
        out[period] = LuaDll.CalcValue(source.open[period], source.high[period], source.low[period], source.close[period]);
    end
end

C++/CLI
Code: Select all
// LuaDll.h

#pragma once

using namespace Calculation; // in C# dll

extern "C"
{
#include "..\Lua\src\lua.h"
#include "..\Lua\src\lualib.h"
#include "..\Lua\src\lauxlib.h"
}

bool getNumber(lua_State *L, int index, double& value)
{
    if (lua_isnumber(L, index) == 0)
        return false;
    value = lua_tonumber(L, index);
    return true;
}

int calcValue(lua_State *L)
{
    // get passed parameters
    double open, high, low, close;
    if (!getNumber(L, 1, open))
        return luaL_error(L, "Incorrect open value");
    if (!getNumber(L, 2, high))
        return luaL_error(L, "Incorrect high value");
    if (!getNumber(L, 3, low))
        return luaL_error(L, "Incorrect low value");
    if (!getNumber(L, 4, close))
        return luaL_error(L, "Incorrect close value");

    // call C# method
    double result = Calculator::Calculate(open, high, low, close);
    // push result into the Lua stack
    lua_pushnumber(L, result);

    int numberOfValuesReturned = 1;
    return numberOfValuesReturned;
}

static const luaL_reg lib_functions[] =
{
    {"CalcValue", calcValue},
    {NULL, NULL}
};

extern "C" __declspec(dllexport)
int luaopen_LuaDll(lua_State *L)
{
    luaL_register(L, "LuaDll", lib_functions);
    return 1;
}

It's impossible to attach a file in this part of the forum. Let me know if you need all sample project files, I'll upload them somewhere.
Last edited by Victor.Tereschenko on Thu Jan 20, 2011 11:55 pm, edited 1 time in total.
“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: 120
Joined: Fri Nov 19, 2010 8:55 am

Re: call c# from lua indicator

Postby Zodiac » Thu Jan 13, 2011 2:18 pm

That helps a lot. Thanks. I found another post you made on the dailyfx forums too with the project so I've been able to figure it out.

I have a feedforward neural network made up in encog and I'm ready to see if I can get it to work. It's not trained very well yet but that's not the point here. Training can take days.
Zodiac
 
Posts: 6
Joined: Wed Dec 15, 2010 3:50 am

Re: call c# from lua indicator

Postby navid_mo » Thu Mar 31, 2011 2:14 pm

Thanks Victor for the hint. Any chance you can point me to your src/project files? I am having a trouble compiling http://luaforge.net/projects/luainterface/ with .NET 4 (VS 2010) ... here is my email navid__at__navid__dot__ca ... I can host the files as well that others can download. Thank you, Navid
navid_mo
 
Posts: 1
Joined: Wed Mar 30, 2011 3:07 pm

Re: call c# from lua indicator

Postby Dosihris » Wed Nov 09, 2011 5:09 pm

Hey,

i try to call a c# method from lua, but its not working. i use .net 4 and vs2010. i tried your example. can you show another short example with version one, the com visible .net assembly?
are there other ways to just call a simple .net method from LUA???

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

Re: call c# from lua indicator

Postby Victor.Tereschenko » Thu Nov 10, 2011 6:07 am

Dosihris wrote:Hey,

i try to call a c# method from lua, but its not working. i use .net 4 and vs2010. i tried your example. can you show another short example with version one, the com visible .net assembly?
are there other ways to just call a simple .net method from LUA???

Nic

Try out this sample. Does it work on your enviroment? If not, please post an error message when call your C# method.
Attachments
LuaDll.zip
(204.05 KiB) Downloaded 881 times
Victor.Tereschenko
FXCodeBase: Confirmed User
 
Posts: 120
Joined: Fri Nov 19, 2010 8:55 am

Re: call c# from lua indicator

Postby Dosihris » Fri Nov 11, 2011 2:32 pm

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
Dosihris
 
Posts: 4
Joined: Wed Nov 09, 2011 5:01 pm

Re: call c# from lua indicator

Postby Dosihris » Fri Nov 11, 2011 3:13 pm

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
Dosihris
 
Posts: 4
Joined: Wed Nov 09, 2011 5:01 pm

Next

Return to C++/.NET API

Who is online

Users browsing this forum: No registered users and 1 guest