Shared Lua library between Indicator and Strategy

Section for discussions related to indicators, use of indicators, and building of trading stategies using indicators.

Moderator: admin

Shared Lua library between Indicator and Strategy

Postby fxcodebase_007 » Fri Mar 01, 2019 6:11 pm

Hi

Apologies if this question has been answered here already, as I was unable to find one.

I have written a custom Indicator and custom strategy, but I am having to repeat the same code in Indicator and Strategy. I need the same piece of code to be called in Indicator and Strategy.

I learned that Lua uses "require" to load methods from another file, but does this Lua file need to be installed like the strategy and indicators are installed?

Is there a way to use REQUIRE so I can create a common Lua library and call it in both Indicator and Strategy so that I can avoid code duplication?

Thanks
Fx007 :)
fxcodebase_007
 
Posts: 8
Joined: Wed Jun 06, 2018 4:22 am

Re: Shared Lua library between Indicator and Strategy

Postby fxcodebase_007 » Mon Mar 04, 2019 5:59 pm

Found a solution. Posting it here so that it might help someone else who hit the same challenge.

1. Created a folder called "customlib" in the following path
D:\Gehtsoft\IndicoreSDK3\customlib

2. Inside the folder, created customlib.lua with following contents (replace Math method with your own methods)

Code: Select all
local custlib = {}

function custlib.Math( v1, v2 )
 return v1 + v2
end

return custlib



3. In my Prepare() method of Indicator added following line

Code: Select all
package.path = package.path .. ';customlib/?.lua';



4. Called the target method named Math() from Update() method of Indicator:

Code: Select all
local tradeLibrary = require("customlib");
print(tradeLibrary.Math(1,6));



NOTE: The above steps were followed while testing the Strategy and Indicator from Lua Debugger.
fxcodebase_007
 
Posts: 8
Joined: Wed Jun 06, 2018 4:22 am

Re: Shared Lua library between Indicator and Strategy

Postby imxuf92 » Tue Mar 26, 2019 7:29 pm

Is there any way to compile a shared lua file?

The debugger doesn't like the syntax of a standalone module...
imxuf92
 
Posts: 26
Joined: Sat Jan 23, 2016 6:55 pm

Re: Shared Lua library between Indicator and Strategy

Postby Apprentice » Wed Mar 27, 2019 1:36 pm

You can also use this method

Example from stategy...
dofile(core.app_path() .. "\\external_library.lua");
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36258
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Shared Lua library between Indicator and Strategy

Postby fxcodebase_007 » Thu Jun 10, 2021 7:29 am

Thank you Apprentice
fxcodebase_007
 
Posts: 8
Joined: Wed Jun 06, 2018 4:22 am


Return to Discussions

Who is online

Users browsing this forum: No registered users and 2 guests