Prevent Overlapping Trades (Portfolio Testing - Applystop)

Hi All,

I have a question regarding backtesting MaxOpenPositions and its interaction with ApplyStop. I'm testing on a portfolio of equities. When I set the MaxOpenPositions = 1;, I'm noticing that on some candles a position is opened despite already having a position in the portfolio. This occurs when a profit target would be hit that day in the old position. It's buying at the open price and selling the old positions at the profit target.

This is not how I want to execute my strategy, for all intents and purposes, I want to contribute 100% of my equity into the single position, which means there's no additional funds to open the new trade at the opening price.

Here's a sequence of trades where this occurs.
image
In the first trade, TSLA is purchased on Feb 19, then sold intraday on Feb 20. meanwhile the backtester assumes a fill in NVDA on Feb 20 at the open price.

"Allow same bar entry/exit" is unchecked my understanding is this option is meant only to control entries/exits of a single asset on the same bar, not of a portfolio.

Does you have any ideas on how to suspend entry signals based on max positions at candle open?

1 Like

Check ApplyStop DOCUMENTATION
http://www.amibroker.com/f?applystop

There are different scenarios listed in docs and ONLY those scenarios are supported. Read carefullly. "Allow same bar exit" affects only regular exits (via Sell/Cover), not stops.
Other settings affect stops. Again read the ApplyStop docs.

Read also this
http://www.amibroker.com/guide/h_portfolio.html

And use SettlementDelay if you don't funds from sale to be used the same bar.

Thank you @Tomasz.

It doesn't seem that the case I want is covered, but I think SetOption("SettlementDelay", 1 ); will get me close enough to what I want to test.

I don't want to activate the stops on the entry bar, but the bar after. Hold for a single bar then activate stops. if these stops were my only exit then settlement delay would prevent reentry, but I do have regular exit parameters that could trigger a sell at open. In the real-world use case I have been closing a position and opening one at the same time via OTO order. Settlement delay doesn't allow for the OTO orders to take effect.

Thanks for the support, even if Amibroker can't exactly backtest what I want to do, it does seem that it can get me close enough to evaluate the significance of my signals.

It can!

For full control of your exit logic use a 'For Loop'.

See how-to-plot-a-trailing-stop-in-the-price-chart for structure example.

If you then still need a higher level of control use the CBT.

Look at the documentation for ApplyStop again. There is a ValidFrom parameter that allows you to specify the bar on which the stop becomes active. You may also find SetOption("HoldMinBars") useful.

Above all, you failed to deliver basic information that is REQUIRED to get help.
Namely - the FORMULA and the settings

All your posts do not provide all necessary details to give you an answer. Please follow this advice: How to ask a good question

I don't want to activate the stops on the entry bar, but the bar after. Hold for a single bar then activate stops

It is possible. You need to understand that ActivateStopsImmediately setting affects two things not one. It is not only that it activates ability to stop the same bar, BUT ALSO it affects the ORDER in which stops are handled. When it is set to TRUE, stops are executed AFTER entry signals. That allows to exit by stop on the same bar, but it also means that on SUBSEQUENT bars stops will be executed BEFORE entry signals. On the other hand, if it is turned off stops will be executed BEFORE entry signals. It is all documented in ApplyStop documentation. Apparently you got this setting wrong.

One must understand that everything that occurs inside single bar is essentially happening in one discrete point of time. Regardless of price there is NO timing information inside single bar. Open price can be equal to Close, Open price may be crossed many times during the bar and assumptions that one price occurs before another are false. For these reasons there are settings that control what should be executed before others in case of many things happening inside single bar.

Also keep in mind that you should be backtestRegularRAW mode, not backtestRegular if you want to simulate what (apparently from limited info) you are trying to do.
The difference is that in RAW mode stops are simulated LATER (at portfolio backtest phase). AFL Function Reference - SETBACKTESTMODE

Without formula, I don't know what you are doing. Hence it is essential to provide FORMULA if you want help.

Anyway, based on limited info here is what you should do:

  1. To execute stops AFTER signals, you should turn ON "ActivateStopsImmediately" (see docs for ApplyStop scenario 3)
  2. To prevent exit at the exact entry bar you can use HoldMinBars, or ValidFrom argument in ApplyStop
  3. You should use backtestRegularRaw mode

Next time, include the formula. Guessing games are waste of time.

By the way your case is covered and in fact present in pre-defined scenarios in the settings:

image

Whenever possible, everybody should just use pre-defined scenarios because they provide correct combinations of settings.

3 Likes

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.