# 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;

for( i = 2; i < BarCount; i++ )
{
{
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];
}

}

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

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

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

SetOption("ActivateStopsImmediately", True);
SetPositionSize(1,spsShares);

SellPrice=O;
Sell = 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")
{

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