Issues with MA, last bar shift on iMA function???

 

Hey, 

 

Just new to this forum and would need some help with 2 things

 

I'm using a iMA function to get the MA value but don't really understand one part of it which is pretty essential. 

1) The lastBarShift, as I understood it, this excludes the candle being formed right now and instead looks at index-spot 1 which would be the latest closed candle right?
I only want to take action on closed candle and not one that is forming so the shift should be 1? Or am I wrong?

2) As I use the lastBarShift, at what point in time will the MA-average be calculated for? For instance, using 15-timeframe and 2 period with a shift of 1 and it's currently 12:15, does this mean that the iMA-value is calculated for 12:00?

Really appreciate any help. Thanks, code below. 

 

------- CODE------ (have left out init and deinit)

string symbol = NULL;

int timeFrame = 0;

int period = 1;

int period2 = 2;

int maShift = 1;

int lastBarShift = 1;

double imaTest;

double imaTest2;



void start()

  {

imaTest = iMA(symbol,timeFrame,period, maShift,MODE_SMA,PRICE_MEDIAN,lastBarShift);

imaTest2 = iMA(symbol,timeFrame,period2, maShift,MODE_SMA,PRICE_MEDIAN,lastBarShift);

Alert("iMA 1:",imaTest, "iMA 2:", imaTest2);

}
 

Forum on trading, automated trading systems and testing trading strategies


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.


 

Yes lastbarshift set to 1 will give you the value for the most recent closed bar.

However with maShift set to 1 the ma is shifted 1 bar to the right, so you are actually getting the value for the bar previous to that.

 

Thanks, I just found my post again with some help from Keith. Not easy being the new kid on the street ;)

 

Keith, if I set maShift to 0, then this should give me the most recent closed bar right if I also have lastbarshift set to 1?  
That's the first thing I want do, all calculations on the previous closed bar but also, what if I wanted to do this on every tick with the iMA-function, basically doing it live on the forming bar instead of waiting for bars to close, would this be accomplisehd by setting both  lastbarshift and maShift to 0? Basically, if I want to trade when the MA's actually cross each other live, will this work ?


 

Big thanks for the help.

 

Regards from a snowy Sweden. 

 

Unless there is a specific reason to have the MA offset from the bars, maShift should always be 0.

Then lastbarshift at 0 will give the value for the current bar, at 1, the most recent closed bar.

Reason: