Need a function to simulate a trade

 

Here's what I'm looking for:

A function to test how a trade would have went in the past, kind of like a strategy-tester on-the-fly, if you know what I

mean.


This is what I've written so far:


double simulateTrade(datetime time,bool isBuy,double SL,double TP)
{
double PL;


return (PL);
}


so basically if I want to find out how a buy order would have went at 2009-05-01 with a SL of 40 and TP of 70 Id call


double PL=simulateTrade(2009-05-01,true,40,70



It would return the negative number if a loss, positive if a win and 0 if neither the SL or TP were hit.


Appreciate any help on this!

 

My advice would be to stop now before you do yourself an injury.


CB

 

won't profit/loss depend on the price level the trade is supposedly placed?

 
abstract_mind:

won't profit/loss depend on the price level the trade is supposedly placed?

I just needed something rough, anyway I wrote it myself. Hope this helps someone

double simulateTrade(string symbol,int timeframe,datetime time,bool isBuy,double SL,double TP)
{
double PL=0;
int barShift=iBarShift(symbol,timeframe,time,false);
double startPrice=iOpen(symbol,timeframe,barShift);

while(barShift>=0 && PL==0)
{
if(isBuy){
if(iLow(symbol,timeframe,barShift)<=startPrice-(SL*Point)){
PL=-(SL*Point);
}
if(iHigh(symbol,timeframe,barShift)>=startPrice+(TP*Point)){
PL=TP*Point;
}
}else{
if(iHigh(symbol,timeframe,barShift)>=startPrice+(SL*Point)){
PL=-(SL*Point);
}
if(iLow(symbol,timeframe,barShift)<=startPrice-(TP*Point)){
PL=TP*Point;
}
}
barShift--;
}
return (PL);
}

 
FrinkFX:

I just needed something rough, anyway I wrote it myself. Hope this helps someone

double simulateTrade(string symbol,int timeframe,datetime time,bool isBuy,double SL,double TP)
{
double PL=0;
int barShift=iBarShift(symbol,timeframe,time,false);
double startPrice=iOpen(symbol,timeframe,barShift);

while(barShift>=0 && PL==0)
{
if(isBuy){
if(iLow(symbol,timeframe,barShift)<=startPrice-(SL*Point)){
PL=-(SL*Point);
}
if(iHigh(symbol,timeframe,barShift)>=startPrice+(TP*Point)){
PL=TP*Point;
}
}else{
if(iHigh(symbol,timeframe,barShift)>=startPrice+(SL*Point)){
PL=-(SL*Point);
}
if(iLow(symbol,timeframe,barShift)<=startPrice-(TP*Point)){
PL=TP*Point;
}
}
barShift--;
}
return (PL);
}

It is quite rough. You don't take into account the spread also.

Reason: