Page 1 of 1

Shared Lua library between Indicator and Strategy

PostPosted: Fri Mar 01, 2019 6:11 pm
by fxcodebase_007
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 :)

Re: Shared Lua library between Indicator and Strategy

PostPosted: Mon Mar 04, 2019 5:59 pm
by fxcodebase_007
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.

Re: Shared Lua library between Indicator and Strategy

PostPosted: Tue Mar 26, 2019 7:29 pm
by imxuf92
Is there any way to compile a shared lua file?

The debugger doesn't like the syntax of a standalone module...

Re: Shared Lua library between Indicator and Strategy

PostPosted: Wed Mar 27, 2019 1:36 pm
by Apprentice
You can also use this method

Example from stategy...
dofile(core.app_path() .. "\\external_library.lua");

Re: Shared Lua library between Indicator and Strategy

PostPosted: Thu Jun 10, 2021 7:29 am
by fxcodebase_007
Thank you Apprentice