I'm running into an issue with a daily strategy I developed. The code includes CloseAtTrailingStop using a PercentHL as the type.
Today I entered a short position that unfortunately stopped out in the first 15 minutes. The price went above the 3% s/l target. However, for some reason the strategy ignored the high that was printed in the first 15 minutes when I ran at the end of the day, even though the bar clearly shows it went above the trailing stop target. It is now assuming that I am still in the trade.
Below is the code.
Today I entered a short position that unfortunately stopped out in the first 15 minutes. The price went above the 3% s/l target. However, for some reason the strategy ignored the high that was printed in the first 15 minutes when I ran at the end of the day, even though the bar clearly shows it went above the trailing stop target. It is now assuming that I am still in the trade.
Below is the code.
CODE:
if (((currentprice - entryprice) / entryprice) >= Pctgain.AsDouble / 100) { Backtester.CancelationCode = 1; CloseAtTrailingStop(foundPosition0, TrailingStopType.PercentHL, sllongaboveV2.AsDouble, "Sell at " + sllongaboveV2.AsDouble + "% trailing stop loss after 10% gain"); WriteToDebugLog(bars.DateTimes + "Pct gain " + gainnum + "; current price " + currentprice + "; entry price " + entryprice); } else { Backtester.CancelationCode = 1; CloseAtTrailingStop(foundPosition0, TrailingStopType.PercentHL, Parameters[7].AsDouble, "Sell at " + Parameters[7].AsDouble + "% trailing stop loss"); } }
Rename
Your code snippet isn't showing where you programmed the same-day stop loss.
A same-bar stop must assign the stop price to the entry Transaction's AutoStopLossPrice member. It's best to do that in the AssignAutoProfitTargetPrice method, where you get a callback for the real executionPrice.
A same-bar stop must assign the stop price to the entry Transaction's AutoStopLossPrice member. It's best to do that in the AssignAutoProfitTargetPrice method, where you get a callback for the real executionPrice.
This is the code in the AssignAutoStopTargetPrice. Does the trailingstoploss code need to be in this section as well?
CODE:
public override void AssignAutoStopTargetPrices(Transaction t, double basisPrice, double executionPrice) { double price = 0; int idx = GetCurrentIndex(t.Bars); if (OBVxLong[idx] >= EMAofOBVLong[idx]) { if (t.PositionTag == 1) { price = executionPrice * (1.0 - Parameters[13].AsDouble / 100.0); t.AssignAutoProfitTargetPrice(price); } if (t.PositionTag == 1) { price = executionPrice * (1.0 + Parameters[14].AsDouble / 100.0); t.AssignAutoStopLossPrice(price); } } }
Your Response
Post
Edit Post
Login is required