Second position entry is not triggered
Author: kkrisz
Creation Date: 7/31/2013 6:59 AM
profile picture

kkrisz

#1
Hello,

I'm trying to build a strategy which handles two different position in the same time although it is not clearly pyramiding. My problem is that the first position enters correctly, but the second one is not triggered even when the prices reaches the stop level.

According to my debug printout the second position is initialized (BuyAtStop returns the position object) but the backtesting engine doesn't take it into consideration.

Any idea?

Thank you in advance,
Krisztian


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

Gamba

#2
Maybe your position-sizing methods allows only one trade per symbol?
I had an similarly issue e few days ago.
profile picture

Eugene

#3
QUOTE:
My problem is that the first position enters correctly, but the second one is not triggered even when the prices reaches the stop level.


I. I'd be rather surprised if it worked as you want it to work. It's a mistake to use IsLastPositionActive to manage multiple positions. Check out the WealthScript Programming Guide, Programming Trading Strategies > Multi-Position Strategies > MP Strategy Template for a design pattern applicable to your task.

II. Another serious problem is peeking into the future:

CODE:
Please log in to see this code.


In other words, if not all 4 positions have been closed, your strategy is ready to buy at limit on the same bar where the stop order triggered. For a proper approach to this problem, please see this FAQ:

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

III. A cosmetic issue. These superfluous directives make the code harder to read than it should be:

CODE:
Please log in to see this code.


If that ExitAtStop has actually exited from S1_pos, then the corresponding variable is already null and explicitly setting it to null is not required.
profile picture

kkrisz

#4
Eugene, Gamba,

Thanks for the answers. I will fix my strategy accordingly.
profile picture

kkrisz

#5
Hi again Eugene,

I'm trying to implement your considerations but I'm still having the pending problem. First of all:

I.: understood, I started to change to stratgy to MP template.
II.: it is also clear, although most probably the peeking did not effect my performance because my STOP and LIMIT orders are relatively far, so there was no single bar reaching both of them.
III.: superfluous directives cleaned up

Not talking about the three point you mentioned, I still have the problem with the second long/short position: they are not entering into trade. If I debug my code I can see the second long/short position is entered (the position object is created, L2_pos.EntryPrice contains the correct price) but this position is not displayed on the chart and not taken into considertion at backtesting...

As you can see I'm trying to implement the logic of the real market, when a placed Buy order can mean the entry of a long position or the exit of a short one. Unfortunately I didn't find such template for WL so far. That's why my solution was only this:

CODE:
Please log in to see this code.


Can you recommend a better practice to impelement such logic?

profile picture

Eugene

#6
Let's see the complete rewritten script first.
profile picture

kkrisz

#7
Hi Eugene,

Here is the changed code.

Please note, that I'm aware of the possible peeking, but it's not a problem right now as a single bar will never touch a STOP and LIMIT price at the same time thanks to the low timeframe. Also I'm not using the for loop of the MP template, as I have only 2 long and 2 short entries, therefore I found the administraction simplier this way.

As I mentioned previously I'm trying to implement a logic similar to the real word, when a placed Buy order can mean the entry of a long position or the exit of a short one.

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

Eugene

#8
CODE:
Please log in to see this code.


With this you're falling into another peeking trap, potentially making your strategy function not as designed. For an explanation, please see the WealthScript Programming Guide, Programming Trading Strategies > Peeking > Trading based on Position Active Status.

I think a little fix could do it:
CODE:
Please log in to see this code.


QUOTE:
Not talking about the three point you mentioned, I still have the problem with the second long/short position: they are not entering into trade.


Not sure what do you mean. After a slight tweak (see above) and changing the hardcoded time values to fit the US markets, I get lots of L2/S2 positions on any symbol I tried e.g. AAPL, QQQ:



This depends on symbol
profile picture

kkrisz

#9
Eugene,

Here is an example of my problem:



This screenshot is an example trading day with 15min bars. The lower red line represtents S1-short-stop, the upper red line is S2-short-stop. As you can see S1 short selling stop was executed, you can see the sell symbol there. On the other hand when the price crossed S2-sell-stop point, the backtesting engine doesn't indicate a sell-short, although acording to the PrintDebug output the position object is not null anymore:

QUOTE:

2013-06-04 1645
16:45
L1_pos= L2_pos= S1_pos=WealthLab.Position S2_pos=WealthLab.Position
profile picture

Eugene

#10
I think it happens because your code is bloody peeking:

CODE:
Please log in to see this code.


In the real world, you can't know the next bar's Close until the bar is closed! Let alone execute an order based on that price. To correct it, use the following:
CODE:
Please log in to see this code.


I just tweaked pivots w/o scrutinizing your code but it seems to work:


profile picture

kkrisz

#11
Thanks for the help again Euguene, but unfortunately this did not solve the problem.

I fixed the peeking you identified. Your proposed fix didn't work well, because the backtester engine continously entered to new positions over the stoploss line. Finally I found this type of code working:
CODE:
Please log in to see this code.


Althougt the peeking is fixed, the symptome is still the same: the second position doesn't enter, although the position object was inititialized: my previously attached image is still valid. Because of the hardcoded tick values, this code only works on forex pairs.

profile picture

Eugene

#12
As your approach to coding this strategy does not look intuitive to me, I decided to rewrite it from scratch. It's NOT complete as I very well could have misinterpreted your rules (they're not available in plain English) but I hope it's easier for you to tweak the rules and fix entry/exit thresholds if you follow this design pattern:

CODE:
Please log in to see this code.


Notes:

1. Requires installed Community Components.
2. Note how each Position is marked with profit target using Position.AutoProfitLevel.
profile picture

kkrisz

#13
Eugene,

Thank you very much for the effort, I really appreciate it. Although this code is not exactly covering my strategy but I definitely learnt a lot of your programming style.

I consider this problem solved.
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).