Possible issue with stop loss Logic in Wealth-Lab?
Author: thetraderman
Creation Date: 4/11/2011 12:22 PM
profile picture

thetraderman

#1
I have been working at this for a long time and I think I convinced myself that there is a bug in WL 6.x with regards to stop logic. I only saw it once but and it is repeatable which I think it should be brought up. The simplified code is shown below. Essentially p.Active does not always go from true to false after a stop order is executed. Correct me if I’m wrong but wealthlab takes care of whether positions are opened and how many. Once a stop order is executed the count should be reduced by one and the Active flag set to false if the count is zero. It is as if the internal semaphore logic is not working properly.
Below is an output log of the count and flag after the stop logic is executed. Notice that the count requires two iterations for it to be set back to zero.
I would be happy to log any additional data if it helps to resolve this issue.

STOP LOSS on LONG Detected at Bar 379
04-11-2011 13:17:00 Stop Loss Triggered for Long Position 380
Stop Long ActivePositions.Count 1 p.Active True
STOP LOSS on LONG Detected at Bar 381
04-11-2011 13:17:00 Stop Loss Triggered for Long Position 382
Stop Long ActivePositions.Count 0 p.Active False


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

Cone

#2
If you're not going to post a working example (and even if you do), please click the "CODE" button and put your code between the tags. It's an extra step that we won't have to do for you. I'll take a look now...
profile picture

thetraderman

#3
Sorry about that. I'll be more careful next time.
profile picture

Cone

#4
How does the log correspond to the code that you posted?
In other words, I don't see the debug statements that print "STOP LOSS on LONG Detected" or "Stop Long ActivePositions.Count". The order of execution is the key here and I can't determine the order.

Note that when looping through ActivePositions, you don't need to check if(p.Active) - it will be otherwise p wouldn't be in the list.

profile picture

Eugene

#5
I've always wondered, why do users leave the threads they started and post a reply on the SAME ISSUE in a new thread?

Can a stop be activated more than once for the same trade?

It only creates extra trouble to jump between the threads.

Please help solve this mystery for me.
profile picture

thetraderman

#6
Check the last few lines. Understanding that I don't need p.Active, does it hurt to have it in?

CODE:
Please log in to see this code.



profile picture

Cone

#7
Maybe you didn't do it on purpose, but you're give me code and results that cannot possibly match. You put "STOP LOSS on LONG Detected" in the short exit block, which should be followed by "Stop Loss Triggered for Short Position", but your results say "Stop Loss Triggered for Long Position". We're not going to be able to help you with your code like this.
profile picture

thetraderman

#8
Let me take a look again. To make it simple I took the actual code which is really very large and removed a lot of stuff I didn't think you needed. I possibly mixed things up in the process. Please give a litte time to re-look and re-attach.

Thanks for your help.

profile picture

thetraderman

#9
Hopefully this listing will be OK. It is accurate to what I am doing. Thanks again for your help.

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

Cone

#10
// PrintDebug("Stop Long ActivePositions.Count " + ActivePositions.Count + " p.Active " + p.Active);
This statement is commented out, but must have not been when you ran the script. I can't tell if it should be in the "else" modifier or after the "if" statement completely.

Really, this is a mess you're giving me, and I can't help you troubleshoot it like this. I'm quite sure that Wealth-Lab's stop logic works and either the problem is in your code or you're .

Further, in your log you've assumed you're working with the same position, but it seems clear that you're not. Add code to uniquely identify the properties of the position that you're testing - for example, the .Shares, .EntryBar and, .BasisPrice.

When you write complex scripts, you get complex problems, and I'm quite sure that's where your problem lies.
profile picture

thetraderman

#11
When troubleshooting, I comment and un-comment debug statements as I go. If you see the exact text but with a comment, it wasn't commented when I sent you the print out. In reality it is just a bunch of if-else statements. If you look at the code I can't see how executing a stop or a sell would require several loops for WL to finally complete the transaction. Although it looks complicated it really isn't. There is a check if there are open positions. If not then depending on the business rules I either go long or short. WL should keep track of that. The next time around if I have an open position depending on whether I went long or short I either exit at a stop or at normal exit with a profit again depending on the business rules.

I understand you are probably busy trying to answer everyone's questions but before I lay out money I need to make sure the system works as it should. Is there another facility for me to contact someone for additional help? I wouldn’t ask if I didn’t feel I exhausted all other possibilities.

Thanks for your help.
profile picture

Cone

#12
If CoverAt/SellAtStop returns true, the Position that you passed to the signal is closed.

Since you have a dispute, you can create a support ticket, which is a private discussion with me or Eugene. I HIGHLY encourage you to create a watered-down script, 25 lines or less, that demonstrates what you think is a problem. I think the exercise may show you what you're doing wrong in your complex script. All complex logic boils down to a trading decision - buy or sell, short or cover.

Here's a model example. Starting a 50 bars from the end of the chart, it buys immediately (when no positions are active), holds the position for at least 5 bars and then sells on stop at the low of the current bar. Please run it and look at the results and notice that even though a Position is sold on the next bar (bar + 1), the Position is immediately indicated to be inactive after the signal is executed.

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

thetraderman

#13
I'll look it over. Thanks.
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).