Calculate EMA in strategy for longer time period

Moderator: admin

Calculate EMA in strategy for longer time period

Postby zilcho » Fri Dec 23, 2016 5:25 am

I am sure this has been done many times and I will kick myself for not being able to figure it out on my own, but I have searched and tried many different methods and cannot get this to work. I would simply like to have a 200 day moving average for the instrument my strategy is running on available for calculations. For example, my strategy is running on AUDUSD on a 5 minute time frame but I need the strategy to make trading decisions based on the current prices relativity to the 200 day EMA. My code:

Code: Select all
...
local MA;
local ma_source = nil;
local sourceData;
local loading;
...

function Prepare(nameOnly)
...
   ma_source = instance.source;
   sourceData = core.host:execute("getSyncHistory", ma_source:instrument(), "D1", ma_source:isBid(), 200, 200, 100);
    loading = true;
    MA = core.indicators:create("EMA", sourceData.close, 200);
...
end
...
function ExtUpdate(id, source, period)
...
   MA:update(core.UpdateLast );
   core.host:trace(string.format("Value of 200 day MA:=%f", MA.DATA));
...
end
...


When I run this in the debugger, I get an error "182: attempt to index upvalue 'ma_source' (a nil value)". I believe trying to use this method does not work because I need to check for loading using the function "AsyncOperationFinished" which is not called when using "ExtUpdate" function as opposed to just "Update". However I need to use "ExtUpdate" for other reasons.

If someone could layout for me in the simplest terms how to access the value of a 200 day moving average in a strategy running on a lower time frame chart, I would greatly appreciate it.
zilcho
 
Posts: 7
Joined: Thu Jul 21, 2016 7:06 pm

Re: Calculate EMA in strategy for longer time period

Postby Apprentice » Sat Dec 24, 2016 6:10 am

Can you provide strategy code.
Will fix it for you.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Calculate EMA in strategy for longer time period

Postby zilcho » Sat Dec 24, 2016 5:52 pm

Apprentice, I have sent it to you in PM.
zilcho
 
Posts: 7
Joined: Thu Jul 21, 2016 7:06 pm


Return to Indicator Development

Who is online

Users browsing this forum: No registered users and 8 guests