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.

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

Buy=Cross(C,MA(C,14));
Sell=1.03*C;
//want Stoploss
//LongSL=C*.98;

Short=Cross(MA(C,14),C);
Cover=0.97*C;
//want Stoploss
//ShortSL=c*1.02;

PositionSize=100000;

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

image

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

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

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

PositionSize=100000;

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 http://www.amibroker.com/kb/2014/09/24/how-to-identify-which-signal-triggers/

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

4 Likes

Thank you so much. I appreciate your valuable input.

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

Look here for example
http://www.amibroker.com/kb/2014/10/17/using-price-levels-with-applystop-function/

1 Like

Thanks again.
I went through the link provided by you.
http://www.amibroker.com/kb/2014/10/17/using-price-levels-with-applystop-function/

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.
image

Report has been set @ Detailed level.
image

When Backtest is run, no record is in the result
image

Chart shows multiple crossovers:
image

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).

PositionSize=100000;

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.

Example:

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:

SetOption("InitialEquity",1000000);
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).
image

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

27

And with different settings

2018-08-12_150525

2 Likes

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:
image

All in one place (some modification as suggested).
image

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.

050

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

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.

Or....

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 ...
image

Problem resolved by fxshrat