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.
Size:
Color:
Maybe your position-sizing methods allows only one trade per symbol?
I had an similarly issue e few days ago.
Size:
Color:
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.
Size:
Color:
Eugene, Gamba,
Thanks for the answers. I will fix my strategy accordingly.
Size:
Color:
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?
Size:
Color:
Let's see the complete rewritten script first.
Size:
Color:
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.
Size:
Color:
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
Size:
Color:
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
Size:
Color:
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:
Size:
Color:
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.
Size:
Color:
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.
Size:
Color:
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.
Size:
Color: