My ZigZag strategy incorrectly allows multiple positions
Author: marcal
Creation Date: 11/22/2014 10:19 PM
profile picture

marcal

#1
Something in my code logic is making it "seem" as if the LastPositionActive logic is allowing multiple positions on the same symbol on the same day.

From the beginning:
The premise I'm investigating is using 2 ZigZag series. Sell at peak of zigzag_1, Buy at trough of zigzag_2. I run backtests on Dow30, scale=daily, data range= 1 year, position size raw, fixed $5000. In the backtest results, trades list, I see several positions entered on the same day and same symbol and exited same day. (appears as if exact same trade)

Intuitively I suspect this has something to do with my use of the same bars index in 2 different series with respect to the persistance of the lastpeakbar/lasttroughbar properties until a new peak/trough occurs.

If you could find a few minutes to make any suggestions in correcting my logic I would be grateful.

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

LenMoz

#2
Most likely because you're buying at bar Zig_2.lastTroughBar +1, which is in the past and stays the same for multiple bars, hence multiple trades on the same date. Your strategy is also "peeking" by buying in the past.

There's nothing wrong with IsLastPositionActive.
profile picture

Eugene

#3
To further stress LenMoz's point, you should buy at bar+1 and rewrite your entry/exit conditions accordingly to get rid of the peeking.
profile picture

marcal

#4
@Gents, thanks for your replies. I think you both reiterate what I was trying to convey originally. Unfortunately with my rudimentary programming skills I haven't yet learned how to accomplish your suggestions. I'll continue searching and exploring published strategies in hopes of recognizing, and then learning from correctly structured techniques that may apply to this particular situation.

Also let me extend an apology if my poor choice of words failed at giving an implication that there was something wrong with IsLastPositionActive. I sincerely tried to avoid any such implication. As one reads through the forum one gets an inkling perception that there is, among the forum regulars/moderators, a heightened sensitivity to any reference to it in any context. I expect there is some history behind it, but a newbie can't know that.
profile picture

Eugene

#5
Hi Mark,

The problem you're facing comes from using different parameter values for buy/sell ZigZags. Your code doesn't take into consideration the order of peaks and troughs. Hence the frantic buying/selling sprees which would not happen if you took a single ZigZag for both the entry and the exit. Note my changes highlighted with...
QUOTE:
// THIS IS THE KEY


CODE:
Please log in to see this code.


QUOTE:
Also let me extend an apology if my poor choice of words failed at giving an implication that there was something wrong with IsLastPositionActive. I sincerely tried to avoid any such implication. As one reads through the forum one gets an inkling perception that there is, among the forum regulars/moderators, a heightened sensitivity to any reference to it in any context. I expect there is some history behind it, but a newbie can't know that.

No problem. This is why I renamed the thread so as not to give an incorrect impression of a new bug in WealthScript. It's very common for new users to think that they've just found a bug but Wealth-Lab is a mature product so there's a very low chance of a bug in the LastPositionActive logic.
profile picture

marcal

#6
Thank you sir,
It works of course.(Don't know if it will make any money but that's another topic).
But now I have to study why.
Zig_2.lastTroughBar > Zig_1.lastPeakBar ) // THIS IS THE KEY -- This makes sense, but previously I couldn't understand why it would be needed since both Zigs were within the same for loop and the same bar index. It appeared to me as if they were looking at different bars and for lack of a better term not 'synchronized'. (I think I've seen that term synchronize used in another context so not to be confused with the other usage)

if ( (Zig_2.lastTroughBar == 0 -- I don't understand this.Does this have to do with bar 0 being determined by the date range of the backtest and not the bar where a peak or trough occurred or something along those lines? And if the for loop index starts at 1 how can this ever be true?

if ( (Zig_2.lastTroughBar == 0 || Zig_2.lastTroughBar == 0) || -- And I really don't understand why you would OR it with itself? Is the result not the same as just saying if ( (Zig_2.lastTroughBar == 0)

I have some other questions about the workings of the peak/trough series and it's forward looking aspects but I'll leave that for another thread.
btw: I've experimented with this idea in another trading software package with some degree of hope that it might work. I think it may need a second bar of confirmation that a reverse has indeed occurred and some fundamentals conditions. Or in your opinion, is this just beating on a dead horse.
profile picture

Eugene

#7
QUOTE:
why it would be needed since both Zigs were within the same for loop and the same bar index.

But they occur at very different bars. Your exit is faster (2$) than entry (5%). In this context please keep in mind that "troughDetected", according to its documentation, remains true until a new trough is detected.

As to the other questions, for some reason first detection of trough happened at bar 0. Cone may provide further insight when he has time (and he's busy these days).