How can I get close price shifted back in a simulation?

 

Hi guys,

Just starting to build my EA after some months of research, I came to a point where I need to get the close price shifted back, lets say 5 days ago. I know that Close[5] is good enough but this wont work in a simulation since it will take the actual date as the start. It's really important for me to get this value in the simulation, is there a way to achieve this?.


Cheers!

 

Save the respective time's price in a variable and use it when you need it

Lets say EMA1 crossed EMA60 then save it in an array the price and in another array the time and test it if its bigger the time difference from the current time than 5 days then ...

OR

If you put a trade at that point you can check the trade's opening time and differentiate it from the current server time and if its bigger than 5 day then ... whatever you want to do with it.

OR


If you need the info without anything happening just the info then save all Close[0] prices in an array of like 100.000.000 slots, so a

double PRICEACTION[100000000];
datetime TIME[100000000];

PRICEACTION[i]=Close[0];
TIME[i]=TimeCurrent() ;

for(int i=1;i<=100000000;i++){
if(TIME[i]*60*60*24*5<TimeCurrent()){

//PRICEACTION[i] is your searched price
break;
 }
}

should do it.

 
Hi thanks for reply. Actually I need to find the max value between openPrice and closePrice. I will try to adapt your code to this
 
Qu4trO:

Hi guys,

Just starting to build my EA after some months of research, I came to a point where I need to get the close price shifted back, lets say 5 days ago. I know that Close[5] is good enough but this wont work in a simulation since it will take the actual date as the start. It's really important for me to get this value in the simulation, is there a way to achieve this?.

I'm a bit confused as to what you are asking.

First, Close[5] will give you the close price for the fifth closed bar on the current chart.

Second, lets say the current chart is a 1 hour chart and you want to know the close price for the fifth preceding day. You can use iClose() to access and get the price from the daily chart:

FifthPrecedingDayClosePrice = iClose(Symbol(), PERIOD_D1, 5);

Third, you can use iBarShift() to find the bar by time and then access that bar's close price:

datetime some_time=D'2013.08.23 12:00';
int shift=iBarShift(Symbol(), PERIOD_H1, some_time);
Print ("Close for ", TimeToStr(some_time), ": ", DoubleToStr(iClose(Symbol(), PERIOD_H1, shift), Digits));

Remember to test for error 4066 when using "i" functions (like iClose()). See here.

I believe that Close[], iClose(), and iBarShift() work in the Strategy Tester.

 

Hi Thirteen, you are so right about the iClose, it works! I try it before but it didn´t work I must been missing something.


Thank you so much for your help guys, now I can continue :D

 
Qu4trO:

Hi Thirteen, you are so right about the iClose, it works! I try it before but it didn´t work I must been missing something.


Thank you so much for your help guys, now I can continue :D


Well mine is actually if you want to save the data and to use it on lower timeframes to calculate long time data.But thirteen's looks more advanced i think you should use the iBARSHIFT one that is what you need.


Plot one on open price and another on close price and then use MathMax() to return the biggest one

 
Proximus:

Save the respective time's price in a variable and use it when you need it

Lets say EMA1 crossed EMA60 then save it in an array the price and in another array the time and test it if its bigger the time difference from the current time than 5 days then ...

Do you really need to use all that memory you have allocated for those arrays ? in the Strategy Tester you can only go back approx 100 bars from the start date . . .
 
Qu4trO: I need to get the close price shifted back, lets say 5 days ago. I know that Close[5] is good enough

Close[5] is 5 bars ago. It will only be 5 days ago on the D1 chart. iClose(NULL, PERIOD_D1, 5) is the close 5 days ago on the D1 chart.

Now if you want the close price 5 days * 24 hours * 3600 seconds ago, on your chart, find the correct bar

string   PriceToStr(double p){   return( DoubleToStr(p, Digits) );            }

#define HR2400 86400       // 24 * 3600
int      TimeOfDay(datetime when){  return( when % HR2400          );         }
datetime DateOfDay(datetime when){  return( when - TimeOfDay(when) );         }
//datetime Today(){                   return(DateOfDay( TimeCurrent() ));       }
//datetime Tomorrow(){                return(Today() + HR2400);                 }
//datetime Yesterday(){               return( iTime(NULL, PERIOD_D1, 1) );      }
///////////////////////////////////////////////////////////////////////////////
#define DAYS5 432000 // 5 * 24 * 3600
datetime now  = TimeCurrent(), // or Time[0]
         Tm5d =  now - DAYS5   // now minus 5 days
int iBar = iBarShift(NULL,0, Tm5d);

Print("Close at " + TimeToStr(Time[iBar], TIME_DATE|TIME_MINUTES|TIME_SECONDS)
     +" was "     + PriceToStr(Close[iBar])
     );