Controlling the number of positions available using an intraday profit stop

I'm trying to set up an intraday profit stop to work with multiple positions. If the profit stop is triggered at the open, I want a position to become available to take a new trade. If the stop is triggered in the middle of that day, a new position shouldn't become available until the next day open. In the sample code below, on the stop tab of the settings, if I put a check in "Activate stops immediately (trigger stops AFTER regular signals)" it will reduce my number of 5 positions available for both a stop at the open or an intraday stop. If I uncheck the "Activate stops immediately" all 5 positions will be available regardless of the stops that were triggered.

To clarify, say for example a buy is made a the open on day #1 at $10. That same day, the price never exceeds the 3% setting for the profit target. On day #2 the stock opens at $11 so the profit stop should trigger and now I want a position to become available that same morning. However say on day #2, the stock opens at $10 but in the middle of the day, it exceeds the profit target of 3% at $10.30. I don't want a position to come available to trade until the open of day #3. Is there a way I can do that?

This is a sample of the code that I was using:

MaxPositions = 5; //Optimize("MaxPositions", 14, 2, 25, 1);
SetOption("MaxOpenPositions", MaxPositions); 
SetPositionSize( 100 / MaxPositions , spsPercentOfEquity);

SetTradeDelays( 1, 1, 1, 1 );
BuyPrice = ShortPrice = SellPrice = CoverPrice = Open;

Buy = Cross( Close, MA(Close, 100 ) );
Sell = Cross( MA(Close, 100),Close);
ApplyStop( stopTypeProfit, stopModePercent,3,1);

Short = Cover = 0;

@Marcel I believe the only way to achieve this is with the CBT. As @Tomasz has stated many times, entering or exiting at the open price does not confer any timing information... the price is just the price because the Open price could have also occurred at another time during the day.

2 Likes

As written in the ApplyStop docs: http://www.amibroker.com/f?applystop there are several scenarios that are supported and should use certain settings. Specifically "activate stops immediately" is mentioned in the docs several times and only such settings are correct and only such scenarios are supported out of the box. Plus new versions have "Scenario" combo box that contains presets that setup things correctly.

Specifically if you enter on OPEN as explained precisely in the manual, you should be using "ActivateStopsImmediately" set to TRUE, because that is what is written in the manual.

image

To do what you want you don't necessarily need custom backtester, because you could simply add a check that tracks if open price triggers your stop condition and generate regular sell.

But of course you can use CBT if you wish too.

3 Likes

ExitAtStop = 1 works fine for me but I haven't been able to get ExitAtStop = 2 to work properly nor can I find an example of a code that will work for me. This is my code where I use ExitAtStop = 1:

//SYNTAX ApplyStop( type, mode, amount, exitatstop, volatile = False, ReEntryDelay = 0, ValidFrom = 0, ValidTo = -1 )

SetTradeDelays(1, 1, 1, 1 );
BuyPrice = SellPrice = Open;

Buy = C > MA(C,10);
Sell = 0;
ApplyStop(stopTypeLoss, stopModePercent, 3, 1, True, 1);
ApplyStop(stopTypeProfit, stopModePercent, 3, 1, True, 1);

Short = Cover = 0; 

And this is my code where I tried to use ExitAtStop = 2 without success:

//SYNTAX ApplyStop( type, mode, amount, exitatstop, volatile = False, ReEntryDelay = 0, ValidFrom = 0, ValidTo = -1 )

SetTradeDelays(1, 1, 1, 1 );
BuyPrice = SellPrice = Open;

Buy = C > MA(C,10);
Sell = 0;
ApplyStop(stopTypeLoss, stopModePercent, 3, 2, True, 1);
ApplyStop(stopTypeProfit, stopModePercent, 3, 2, True, 1);

Short = Cover = 0; 

I tried the second code with and without "ActivateStopsImmediately" set to TRUE but I couldn't get it to work. Is that the correct application of ExitAtStop = 2 What I was hoping to do, was to for instance in a stop loss, to sell at the next day open price when the previous day H-L range hits stop

I'm still hoping for a response to my second question. I'm looking for a working example of ExitAtStop = 2

You could have bigger chances to get reply if you explained precisely what "does not work". We don't see your computer.

Thank you @Tomasz for looking at my question. Here's a video of what I see. I can get ApplyAtStop = 1 to work, but when I change it to ApplyAtStop =2 I can't get it to work. Is that the correct way to use it?

This Youtube might be easier to see than the last one that I posted. I read that the start of some Youtube videos are blurry because they are experimenting with some new auto resolution features:

Hi @Marcel,

Did you manage to sort out your issue with using ApplyStop ExitAtStop = 2?

Hi @TrendSurfer, sorry, I never was able to figure it out nor did I get an answer to my question. Either I'm not reading the instructions correctly or ExitAtStop = 2 doesn't work. I've searched but I can't find an example of a working code where ExitAtStop = 2 is used.
https://www.amibroker.com/guide/afl/applystop.html

There is no way @Tomasz would allow that to be the case!

Post up your current settings as to see where the conflict is.

@Trendsurfer, in the video that I posted above, it shows the settings that I used. You can try the code that I posted above and you'll see that ExitAtStop = 1 will work but ExitAtStop = 2 won't work.

@Marcel,
You seem to be running an AB version being one released after AB 6.22. Correct?

Something has changed in between AmiBroker 6.22 beta and 6.25 beta seemingly causing the difference.
If running your exitatstop = 2 sample code in AB versions prior to AB 6.25 then there is multi-row output with maxloss and profit stop trade exits in single security BT as well as in portfolio backtest. Beginning with AB 6.25 there is single line (open trade) output using your exitatstop = 2 sample code.

When looking at the devlog of AmiBroker 6.25 then there has been implemented new feature there

AFL: ApplyStop now has 8th argument: ActivationFloor that defines the amount of profit (in dollars or percents, according to stopmode) that must be exceeded before stop is activated

ActivationFloor does not seem to be documented in AppyStop() function reference of user guide of AB 6.30 yet. Also I think it should be 9th argument not 8th.

AB 6.25+

SYNTAX ApplyStop( type, mode, amount, exitatstop, volatile = False, ReEntryDelay = 0, ValidFrom = 0, ValidTo = -1, ActivationFloor = 0 )

Try running your code in AB 6.20, for example. Report back if you see difference then. You should have multirow output with maxloss and profit stop trade exits in result list then (6.20,6.10, 6.00,...).

4 Likes

@fxshrat, yes, you're right, thank you so much! I was trying to run it on AB 6.28 or 6.29. I downgraded one computer to AB 6.20 and it works as expected now. When I set ExitAtStop = 2, I see the trades occurring on the next day. I think from now on, I'll keep one computer on the official release version so if I get a similar problem, I can check on the other computer.

Only today I have found that there was an unintended change in the code that caused ExitAtStop = 2 mode together with backtestRegular not to generate any exits in 6.25.0...6.30.0. This will need to be addressed in next update.

There are 2 workarounds as of now:

  • use SetBacktestMode( backtestRegularRaw );
    OR
  • use ExitAtStop = 1

General reminder: any issues with given version should be reported to either:

This forum receives tons of "noise traffic" and I can't follow each and every random thread for issues that get buried in some Nth reply.

If you fail to report issues to proper channel they will never be known at all and never will have chance to get fixed.