How to make "virtual trades" and track their results?
Author: maximgl
Creation Date: 8/25/2019 4:13 PM
profile picture

maximgl

#1
Let's say that I have a strategy that I run on a large number of stocks. I want it to enable/disable trading on a specific stock depending on past results:
1. if last 5 trades on that stock were losses, temporary stop actual trading on that stock, but keep doing "virtual trades" to maintain the statistics.
2. If last 5 "virtual trades" on that stock were gains, resume actual trading on that stock.

I could think of 2 approaches:
1. Track IsActiveTrading within a strategy code. When active trading is disabled, track simulated entry and exit prices on the next bar to maintain the statistics. The problem with this approach is that it gets quite complicated when using Stops/Limits, it requires extra code to account for slippage etc.
2. Use PosSizer, possibly PositionOptions and .Tag field to set position size to a really small value for virtual trading. The problem is that .Tag does not seem to work with Fixed size and it would still do some tiny trades.

Is there a better way of doing it?
profile picture

Eugene

#2
Let me throw a couple more ideas for you to experiment with:

1. You might also have some success with ClearPositions in SSB (single symbol backtest) mode, see example in the QuickRef. Note that it won't work in Multi-Symbol Backtest (MSB) mode so the strategy logic has to be reworked according to this design pattern:

Limit Monthly Drawdown / Portfolio Equity Tracker

2. Another idea is:

Interacting Dynamically with Portfolio Level Equity

Despite it mentioning just the equity curve, the technique is not limited to it. The SystemResults object contains everything you need to track the last N trades i.e.
CODE:
Please log in to see this code.


The idea is that instead of SystemResults.EquityCurve (used in the Wiki sample code) you'd simply work with said property, looping by positions.
profile picture

KGo

#3
If using the percent equity tag or SetShareSize those values can be reset to zero after the bar loop is completed. After the bar loop all entry prices, exit prices and NetProfitPercent are known in the Positions object to determine if a position should be active. If it should not be active simply reset SetShareSize or Positions[..].Tag to zero.
CODE:
Please log in to see this code.
profile picture

maximgl

#4
KGo, thank you for your reply. I've used your code as a base and I am playing with setting Position[x].Tag = 0 - trade is not registered, but NetProfitPercent data seems to be available. It looks promising, thank you for your idea.

Eugene, thank you for your recommendations too - although KGo's approach seems to be simpler, the Wiki article seems to be very interesting.
profile picture

KGo

#5
You're welcome. Glad it's helpful.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).