I want to add conditional-trailing-stop-limit orders to a strategy. I was thinking of something like this:
CODE:
Please log in to see this code.
So, I'm trying to code, e.g., "if there's an open long (short) position and price has been 7% higher (lower) than the entry price since the position was opened, then institute a trailing stop limit order with a limit-price equal to 4% lower (higher) than the highest (lowest) price realized since the position was opened.
However this code is not functioning properly (it compiles but has run errors) I think the problem is in the indexer
bar-p.EntryBar , but my wealthlab skills are not up to the challenge. I also suspect the logic is wrong as my mind has not yet internalized wealthlab.
Is there anywhere I can look at existing code that implements conditional-stop-limit or conditional-stop-loss orders?
thanks in advance
Size:
Color:
Check out ExitAtAutoTrailingStop, SellAtAutoTrailingStop and CoverAtAutoTrailingStop in the QuickRef (hit F11).
Size:
Color:
P.S. Also, "Position Object" > HighestHighAsOfBar (LowestLowAsOfBar) which return the highest (lowest) price registered in the Position, as of the specified bar number.
Size:
Color:
Thanks. I am confused on the logic, or flow control, of these statements and was hoping you could clarify:
CODE:
Please log in to see this code.
So, the
SellAtAutoTrailingStop appears in a negated-boolean expression, implying if that
SellAtAutoTrailingStop expression is False then flow is passed to the "10% stop loss". How can the
SellAtAutoTrailingStop be executed if it only appears in the boolean expression? I guess my understanding of C#, or programming in general, is coming to the surface as I thought that boolean expressions are tested but are not assigning values or otherwise being executed. Is that not right?
In the
HighestHighAsOfBar example,
SellAtAutoTrailingStop is used not within a boolean, but rather as:
CODE:
Please log in to see this code.
So, in this second example I can see how the
SellAtTrailingStop is executed but it is not clear to me in the first example.
Also, both of these are single position examples; anything I would need to do differently for multi-position?
thank you.
Size:
Color:
Boolean expression returns the result of an operation. If closing the Position with SellAtAutoTrailingStop was successful, it's assigned true, otherwise (if false), the flow control is passed to the next code line.
You can use trading methods "as is" or, at the same time, as boolean variables if it's required. There's no logic conflict. I'm sure, in any C# 101 book you'll find it explained.
P.S. Think of it this way: any Windows application you launch returns an integer code on its exit (e.g. 0 - The operation completed successfully etc.).
Size:
Color: