Calculate EMA in strategy for longer time period
Posted: 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:
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.
- 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.