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); } } }
CODE:It does not. The trailing code is the normal logic for everything after the entry bar (same bar).
Does the trailingstoploss code need to be in this section as well?
Probably the issue for that trade was that there's a condition for assigning the AutoStopLossPrice. That won't happen unless:
(OBVxLong[idx] >= EMAofOBVLong[idx])
Right but that condition is met during the trade.
As long as you were in Auto-Trade and there were no errors, bad data, connection breaks, etc... then it should have worked. Without more data it's not worth pursuing here.
If you're in WL9, check the OMLogs in the User Data folder >> \OMLogs\by date
If you're in WL9, check the OMLogs in the User Data folder >> \OMLogs\by date
Your Response
Post
Edit Post
Login is required