SplitPosition only for backtesting?
Author: wolftrade
Creation Date: 12/18/2017 5:29 PM
profile picture

wolftrade

#1
I coded a successful strategy using SplitPosition to take partial profits at a target. This works fine in backtesting.

However, when I observed it in Streaming mode, the target alerts aren't generated because SplitPosition uses peeking. So, is SplitPosition only for backtesting?

For examples:
1) The WLPProgramming Guide, p. 61 has SplitPosition as okay to peek when looking at bar+1 to see if the target is reached. The position is split and a limit order is entered for the bar+1.

2) http://www2.wealth-lab.com/WL5WIKI/kbSplitPosition.ashx. This SplitPosition example uses a test of Low[bar+1] which also peeks to see if the exit condition is met.

Can SplitPosition logic be used in live trading with a strategy?
If so, how would I modify my code, which is as follows.

Thank you very much for your insight.


CODE:
Please log in to see this code.
profile picture

Eugene

#2
Hi Spencer,
QUOTE:
Can SplitPosition logic be used in live trading with a strategy?

Yes of course.

QUOTE:
2) http://www2.wealth-lab.com/WL5WIKI/kbSplitPosition.ashx. This SplitPosition example uses a test of Low[bar+1] which also peeks to see if the exit condition is met.

As you can see it does so only for backtesting with
CODE:
Please log in to see this code.


QUOTE:
So, is SplitPosition only for backtesting?

You're putting the cart before the horse. The culprit is not SplitPosition but your trading logic which cannot be realistically traded on Daily data as it uses Stop and Limit orders on the same bar. You'll find some helpful pointers and workarounds in this FAQ:

I want to test a strategy that buys and sells with stop/limit orders on the same bar.
profile picture

wolftrade

#3
Eugene,

Thank you for the reference link.

I looked at the reference for stop/limit orders and now include the intraday priority since my strategy uses 10 minute intraday bars.

CODE:
Please log in to see this code.


If I understand correctly, I don't have a same bar problem since I'm using Intraday data. And, my peeking question is addressed since my main loop uses Bars.Count:

CODE:
Please log in to see this code.


while my Target exit conditions use Bars.Count - 1 and High[bar + 1]:

CODE:
Please log in to see this code.


Is this correct that I'm not peeking, so it can be used in real time?

For reference, my entry logic is:

CODE:
Please log in to see this code.


Thank you.

profile picture

Eugene

#4
QUOTE:
If I understand correctly, I don't have a same bar problem since I'm using Intraday data.

Not quite. The idea is that you can't use stop and limit realistically on the same bar unless you peek at a lower bar scale.

QUOTE:
I looked at the reference for stop/limit orders and now include the intraday priority since my strategy uses 10 minute intraday bars.

For Daily bars it's OK to step down but not when using the same bar scale since the 10-minute bar is just like a Daily bar. You'd need to peek at a lower bar scale.

Also, why not simply use ExitAt... with Position.AllPositions instead of looping by ActivePositions (let alone Positions)? Please check out the QuickRef for more on this.
CODE:
Please log in to see this code.


The same applies to SellAtLimit(bar + 1, open, StopPrice, "2nd/not 1st?"); below.
profile picture

wolftrade

#5
Eugene,

Thank you for the advice. I switched to using ExitAt... with Position.AllPositions as you suggested.

However, I'm still puzzled by the logic to have both Stop and Profit Target alerts generated at the same time. It seems that if I have to peek from my 10 minute bars down to a lower time frame, then I might as well use that lower time frame data all throughout.

For example, I noticed when streaming for the day, a limit exit that was taken is not indicated on the chart until after the bar is complete. So in order to generate that limit exit before the bar is complete, I will need to peek at a lower time frame, such as the 1 minute bar. But even at this 1 minute scale, I will have to monitor the program every minute for a signal that will have been taken by the program up to a minute before it is recorded on the chart.

Also, there is an EOD data error thrown since bar+1 (used in the peeking) doesn't exist yet during real time streaming. I believe this is the cause of the error. if( bar < Bars.Count - 1 && target1 <= High[bar + 1] )

Is there some other logic that does not require peeking at a lower time frame so the Stop and Target alerts (for the respective split amounts of the position) are given at the same time, before the bar is drawn and the trade is hit? I researched how I could have AutoProfitLevel declared for the same bar exit, but this exits the full position not a split position.

Thank you again for your patience as I grow to understand the alert logic. Everything works as expected in backtest, it's just the live part that alludes me.

Happy holidays to you.

I refactored the code after seeing additional examples in the reference guides.

CODE:
Please log in to see this code.
profile picture

Eugene

#6
QUOTE:
For example, I noticed when streaming for the day, a limit exit that was taken is not indicated on the chart until after the bar is complete

Exactly. It happens according to this FAQ describing how the process works: Is it necessary to have access to intra-bar tick data to daytrade with Wealth-Lab?

Happy holidays to you too!
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).