Equity curve simulation

Hi guys, I need a little help here... I'm trying to code something I want to test out... what Im trying to do is a simple entry ( engulfing ) to buy + stoploss and take profit, I'm trying to simulate the equity curve, I managed to simulate the entries and exits... but I don't know how to calculate the equity curve, I want to simulate the equity curve because I want to apply a moving average to it, and whenever the equity curve gets below the moving average, stop trading, and start trading again Code button (use to enter/paste AFL formula)once the equity curve its back above the equity moving average

if( Status("Action") == actionIndicator) // equity curve simulation
{

Charttype2 = ParamList("tipoChart","Simulation|noSimulation");

if(charttype2 == "Simulation")
{ 

eq2=Open;

buysim[ 0 ] = eq2[ 0 ]; sellsim[ 0 ] = eq2[ 0 ]; inbuy[ 0 ] = False; eqsim[0]=0;
buysim[ 1 ] = eq2[ 1 ]; sellsim[ 1 ] = eq2[ 1 ]; inbuy[ 1 ] = False; eqsim[1]=0;

inbuy=False; SL=0;TP=0; in_equity=0;

for( i = 2; i < BarCount; i++ )
{
   if((Close[i-1] > High[i-2]) AND inbuy==False)  // buy signal simulation
   { 
   buysim[ i ] = eq2[i];
   inbuy=True;
   SL = Open[i] - (Open[i]*0.03); 
   TP = Open[i] + (Open[i]*0.03);
   }
   
   if((High[i] > TP OR Low[i] < SL) AND inbuy==True)  // sell signal simulation
   {
   sellsim[ i ] = eq2[i];
   inbuy=False;
   }  

}


buysim=ExRem(buysim,sellsim);
sellsim=ExRem(sellsim,buysim);

// HERE ADD CODE TO SIMULATE EQUITY CURVE...!!!!!!!!!!!!!

 

Plot(eqsim,"eqsim",colorGreen,styleLine); 
Plot(eq2,"eq2",colorWhite,styleLine); 
PlotShapes(IIf(buysim,shapeUpArrow,Null),colorWhite,0,eq2);
PlotShapes(IIf(sellsim,shapeDownArrow,Null),colorBlue,0,eq2);
}

if(charttype2 == "noSimulation"){  }
}








SetOption("ActivateStopsImmediately", True);
SetPositionSize(1,spsShares);
BuyPrice = O;
Buy = Ref(C,-1) > Ref(H,-2);

SellPrice=O;
Sell = 0;

ApplyStop(stopTypeLoss,stopModePoint,ValueWhen(Buy,Open*0.03,1),1,0);
ApplyStop(stopTypeProfit,stopModePoint,ValueWhen(Buy,Open*0.03,1),1,0);


if( Status("Action") == actionIndicator)
{

ChartType = ParamList("Chart Type", "Price Chart|EquityLine|none");
eq = Equity(1,-1);
eqstop = MA(eq,200);

if ( ChartType == "Price Chart")
{

intrade = Flip(Buy,Sell);
stoplevel = ValueWhen(Buy,O-O*0.03,1);
profitlevel = ValueWhen(Buy,O+O*0.03,1);	
Plot(IIf(intrade,stoplevel,Null),"SL",colorRed);
Plot(IIf(intrade,profitlevel,Null),"TP",colorGreen);
PlotShapes(IIf(Buy,shapeUpArrow,Null),colorWhite,0,BuyPrice);
PlotShapes(IIf(Sell,shapeDownArrow,Null),colorBlue,0,SellPrice);
Plot( C, "Price", colorDefault, styleCandle );

}

if ( ChartType == "EquityLine")
{

Plot(eq,"eq",colorWhite,styleLine);
Plot(eqstop,"eq_stop",colorRed,styleLine);

}

if ( ChartType == "none")
{}


}









First of all your code is incorrect on several levels.

Also loop is not required at all.

ApplyStop is used improperly. You don't need ValueWhen(Buy..) and amount array.
Instead of mode point simply use mode percent and amount being just percentage value.

ApplyStop(stopTypeLoss,stopModePercent,3,1,0);
ApplyStop(stopTypeProfit,stopModePercent,3,1,0);

etc.


You would have to go to CBT (custom backtester) mid level...
Besides of other things see bo.Equity (bar by bar equity)

Thank you fxshrat :slight_smile: