Using Stoploss in backtesting

I want to use Stoploss in Back Testing for LONG and SHORT both.
You may refer the following code code.

Please suggest how to use STOPLOSS.

Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

//want Stoploss

//want Stoploss


I also see a number in parenthesis in the trade column. Can anyone throw some light.


There are multiple examples in the forum already. Have you searched?
You may use ApplyStop() function for stop loss/ take profit.

Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

myma = MA(C,14);
Plot( myma, "MA14", colorRed, styleLine );


Buy = Cross(C,myma);
Short = Cross(myma,C);

Sell = 0; // 0 or False means exit only by ApplyStop
Cover = 0; 

ApplyStop( stopTypeLoss, stopModePercent, percent_amount = 2, ExitAtStop = 1 );
ApplyStop( stopTypeProfit, stopModePercent, percent_amount = 3,  ExitAtStop = 1 );

Other examples

See here

"Sell = number * price_array" in your upper code of post #1 is not proper signal condition. Same applies to your Cover assignment.


Thank you so much. I appreciate your valuable input.

Please let me know how to code custom target..
Target for a LONG order = O+ 0.618*(Ref(H,-1)-Ref(L,-1));

Look here for example

1 Like

Thanks again.
I went through the link provided by you.

Here it says how to set custom STOPLOSS by using
stopLevelLong, stopAmountLong, StopAmount and ApplyStop.
This is definitely needed.

My other requirement is to set CUSTOM target for LONG and Short order.

Please suggest.

Moreover I used the code modified by you, system is not showing any result. In the chart I see the Buy condition has been fulfilled few times.

I am using 3 minutes interval.

Report has been set @ Detailed level.

When Backtest is run, no record is in the result

Chart shows multiple crossovers:

The code of post #2 works if everything is in line!

The thing is... you have to adjust your position size variable!
It is simply too high compared to your set initial equity of backtest settings! ( I only left PositionSize value in there in post #2 because you have set it to that value in your code of post #1 and I thought you would know what it means).


This sets position value. Look at your initial equity setting, it is same size! Does that make sense? No. Position value is not equal to initial equity.

Instead of PostionSize variable I suggest to you to use SetPositionSize() function instead. It provides better readability.


SetOption( "IntialEquity", 1000000 );
SetPositionSize( 100, spsPercentOfEquity);

or set any other option within SetPositionSize(). But do not set spsValue to same size as initial equity.

Also set days to 2 recent days (or more) instead of one for testing.

Also play around with percent_amount variable within ApplyStop functions of 2nd post of this thread. If you set too high values then you will get less trades.

All is there in this thread to get you going (there already is target example in post #2. In addition there is AmiBroker knowledge base example(s). This is not spoon feeding thread. Do some own homework first. So far I only see demand, demand... for demand cases send resolution request to fxshrat at gmail dot com (for example).

1 Like

As suggested by you, I did change the position size as follows:

SetPositionSize(1000, spsPercentOfEquity);

Also changed the range of data to few days instead of one day. Still no data is coming.
Earlier I used to get some result (gave screenshot).

You have set spsPercentOfEquity to 1000% instead of 100%!
Have I set it to 1000% above of your last post? Look carefully again.
Read the documentation of SetPositionSize for other options. Have you? It is linked in the post above your last one also!

3 min timeframe, 1 recent days, test symbols SPY


And with different settings



I tried with 100, 70, 50 or 10 still no result.

It seems I am almost there. Please suggest possible places I need to check.

Here is the screenshot:

All in one place (some modification as suggested).

One reason for no output can be if a symbol has no volume and there has been set trade size limit to value other than 0 and disable trade size limit not being checked.


Check for volume data at Symbol - Quote editor of menu bar

Then there is no result output.

That is one example of no output. But I am not sitting in front of your PC.

Other reason, too low trade size limit if there is volume.


Finally I ran the Backtest for all symbols for a range of 3 days.
Still no result. Volume should not be an issue.

It seems, something else I am missing ...

Problem resolved by fxshrat