I'd like to implement a sl breakeven in my strategies. I use AssignAutoStopTargetPrices to assign a stop loss immediately after opening a trade. I wrote the following code:
I get these weird results
How can this happen and how to move a sl to breakeven correctly?
CODE:
if (foundPosition0.Profit / foundPosition0.EntryPrice >= Parameters[2].AsDouble / 100) { ClosePosition(foundPosition0, OrderType.Stop, foundPosition0.EntryPrice, "Breakeven"); }
I get these weird results
How can this happen and how to move a sl to breakeven correctly?
Rename
Can you post the entire strategy?
Shouldn't you be doing a comparison of type double rather than integer that is currently happening?
CODE:
//if (foundPosition0.Profit / foundPosition0.EntryPrice >= Parameters[2].AsDouble / 100) if (foundPosition0.Profit / foundPosition0.EntryPrice >= Parameters[2].AsDouble / 100d)
Glitch, I don't see an option to attach a strategy here. Anyway this kind of weirdness begins when I add this block to any strategy. For example, I took 2minutesystem from wl published, converted to C# and added even more simple condition to the end of "else" block
Eugene, Isn't it double now? As far as I know if either operand is a double, the result will be floating point
CODE:
if (foundPosition0.ProfitPercent >= 0.05 ){ ClosePosition(foundPosition0, OrderType.Stop, foundPosition0.EntryPrice, "Breakeven"); }
Eugene, Isn't it double now? As far as I know if either operand is a double, the result will be floating point
QUOTE:
Eugene, Isn't it double now? As far as I know if either operand is a double, the result will be floating point
No, this code is definitely doing an undesired integer division there.
May be it's because "Profit" not returning unrealized profits in real time. From ApiReference:
In any case, then nothing should have happened, and here it is distorted the results in the statistics
May be something like this is right?
Upd: It seems the last option works as expected
Upd 2: It doesn't distort the statistics, but I'm not sure it works like breakeven
QUOTE:
Returns the profit of the Position, with commissions deducted. This property is mainly meant for use in Performance Visualizers, not during backtesting. If you need to obtain a Position's profit during a backtest, use ProfitAsOf instead.
In any case, then nothing should have happened, and here it is distorted the results in the statistics
May be something like this is right?
CODE:
if (foundPosition0.ProfitPctAsOf(bars.Count - 1) >= 0.05 ){ ClosePosition(foundPosition0, OrderType.Stop, foundPosition0.EntryPrice, "Breakeven"); }
Upd: It seems the last option works as expected
Upd 2: It doesn't distort the statistics, but I'm not sure it works like breakeven
We're getting closer. Sometimes it works as expected, sometimes like this
What should not happen, because on the first bar the trade is opened, on the second the sl is changed based on the breakeven, and only on the third it can exit at the stop.
What should not happen, because on the first bar the trade is opened, on the second the sl is changed based on the breakeven, and only on the third it can exit at the stop.
Yes, it's working in an unexpected manner because you're taking the value on the last bar. Instead take it at the actual bar being processed i.e. "idx".
Should be:
Should be:
CODE:
if (foundPosition0.ProfitPctAsOf(idx) >= 0.05 ){
Are there any plans to implement a breakeven as a block?
Now in my current algorithm I save the value in the variable if the stop loss was triggered, so that on the next bar during the recalculation it would not return to the original stop loss again. This works well intraday, but on daily I have to save the value to a file, otherwise the value is lost when I restart the program. Is there a more elegant solution without saving to a file?
Now in my current algorithm I save the value in the variable if the stop loss was triggered, so that on the next bar during the recalculation it would not return to the original stop loss again. This works well intraday, but on daily I have to save the value to a file, otherwise the value is lost when I restart the program. Is there a more elegant solution without saving to a file?
Saving a value to a file looks overkill. Can we see some code to understand the logic?
CODE:Where Parameters[3].AsDouble is the value after which it is set to breakeven.
if (Parameters[3].AsDouble > 0 && foundPosition0.ProfitPctAsOf(idx) >= Parameters[3].AsDouble){ ClosePosition(foundPosition0, OrderType.Stop, foundPosition0.EntryPrice, "Breakeven"); } else { // Original Stoploss
The problem is that if a breakeven order has been activated and the price rolls back a little bit and does not hit a stop breakeven order, then on the next bar the original stop is set
It's just a matter of logic. But if you don't mind using the intrabar high to trigger the stop instead of the Close used by ProfitPctAsOf(), just change that to MFEPctAsOf() and it will do what you want.
CODE:
if (Parameters[3].AsDouble > 0 && foundPosition0.MFEPctAsOf(idx) >= Parameters[3].AsDouble)
Please implement breakeven as a block
> Can't you can already implement it with the PowerPack condition "Position P/L"?
As far as I understand, this way we'll have the same problem as the solution in post 11. I.e. if the price will slowly go down, then on the next bar when the profit will be 0.8% the stop condition will not work. I didn't find MFEPctAsOf as an indicator for something like this.
As far as I understand, this way we'll have the same problem as the solution in post 11. I.e. if the price will slowly go down, then on the next bar when the profit will be 0.8% the stop condition will not work. I didn't find MFEPctAsOf as an indicator for something like this.
At least it's possible to "Open as C# coded strategy" and follow Cone's advice in Post #11 and change ProfitPctAsOf to MFEPctAsOf.
>and change ProfitPctAsOf to MFEPctAsOf
Is there a way currently to get MFEPctAsOf in blocks?
Is there a way currently to get MFEPctAsOf in blocks?
We don't have a Block that can deliver it currently, I'll tag this as a #FeatureRequest so vote for it.
Available now in PowerPack Build 34.
Your Response
Post
Edit Post
Login is required