Support Board
Date/Time: Sat, 23 Nov 2024 12:27:46 +0000
[Programming Help] - Trade Result -8998
View Count: 244
[2024-08-20 17:58:06] |
PS - Posts: 40 |
Hello, I have a strategy I am back testing that keeps on getting TRade result error -8998. I saw on some other posts that this means it is trying to send an order during "full recalculation" but I am unclear on exactly what that means or how to resolve it. Below is the section of my code that contains order entry logic. This error is occurring when I run a replay backtest on Standard Replay mode. It is the only strategy / indicator on the chart. I've used arrow drawings to indicate where a buy / sell signal "should" take place, and those arrows are all being placed correctly on the chart. It is simply a matter of getting the actual orders to be sent. I'm not entirely sure if this is deemed as programming help since the only issue here is resolving an order error command. Thank you in advance for your assistance. // Trading logic
bool enable_trading = true; // Enable trading int trade_quantity = 1; // Trade 1 contract if (enable_trading) { s_SCPositionData PositionData; sc.GetTradePosition(PositionData); double current_position = PositionData.PositionQuantity; // Check for short trades for (auto& zone : active_resistance_zones) { if (!zone.traded && high >= zone.start_price && high <= zone.end_price && sc.Close[sc.Index - 1] < zone.start_price) { SCString logMessage; logMessage.Format("Sell conditions met: Start Price: %f, End Price: %f, Close Price: %f, Time: %s", zone.start_price, zone.end_price, close, sc.FormatDateTime(sc.BaseDateTimeIn[sc.Index]).GetChars()); sc.AddMessageToLog(logMessage, 0); float stop_price = zone.end_price + 3.0f; if (sc.UpdateStartIndex==0) return; s_SCNewOrder NewOrder; NewOrder.OrderQuantity = trade_quantity; NewOrder.OrderType = SCT_ORDERTYPE_MARKET; NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED; NewOrder.AttachedOrderStop1Type = SCT_ORDERTYPE_STOP; // Specify the type of the attached stop order NewOrder.Stop1Offset = std::abs(stop_price - close); // Set the stop offset int Result = sc.SellEntry(NewOrder); SCString traderesult; traderesult.Format("Trade result = %i", Result); sc.AddMessageToLog(traderesult, 0); // Mark the zone as traded zone.traded = true; // Draw a red arrow for sell entry s_UseTool SellArrow; SellArrow.Clear(); SellArrow.ChartNumber = sc.ChartNumber; SellArrow.DrawingType = DRAWING_MARKER; SellArrow.MarkerType = MARKER_ARROWDOWN; SellArrow.MarkerSize = 8; SellArrow.Color = RGB(255, 0, 0); // Red color SellArrow.BeginIndex = sc.Index; SellArrow.BeginValue = close; sc.UseTool(SellArrow); } } // Check for long trades for (auto& zone : active_support_zones) { if (!zone.traded && low >= zone.end_price && low <= zone.start_price && sc.Close[sc.Index - 1] > zone.start_price) { SCString logMessage; logMessage.Format("Buy conditions met: Start Price: %f, End Price: %f, Close Price: %f, Time: %s", zone.start_price, zone.end_price, close, sc.FormatDateTime(sc.BaseDateTimeIn[sc.Index]).GetChars()); sc.AddMessageToLog(logMessage, 0); float stop_price = zone.end_price - 3.0f; if (sc.UpdateStartIndex==0) return; s_SCNewOrder NewOrder; NewOrder.OrderQuantity = trade_quantity; NewOrder.OrderType = SCT_ORDERTYPE_MARKET; NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED; NewOrder.AttachedOrderStop1Type = SCT_ORDERTYPE_STOP; // Specify the type of the attached stop order NewOrder.Stop1Offset = std::abs(1 * (stop_price - close)); // Set the stop offset int Result = sc.BuyEntry(NewOrder); SCString traderesult; traderesult.Format("Trade result = %i", Result); sc.AddMessageToLog(traderesult, 0); // Mark the zone as traded zone.traded = true; // Draw a green arrow for buy entry s_UseTool BuyArrow; BuyArrow.Clear(); BuyArrow.ChartNumber = sc.ChartNumber; BuyArrow.DrawingType = DRAWING_MARKER; BuyArrow.MarkerType = MARKER_ARROWUP; BuyArrow.MarkerSize = 8; BuyArrow.Color = RGB(0, 255, 0); // Green color BuyArrow.BeginIndex = sc.Index; BuyArrow.BeginValue = close; sc.UseTool(BuyArrow); } } // Trailing stop logic if (current_position < 0) // Short position { for (auto& zone : active_support_zones) { if (sc.Close[sc.Index] > zone.start_price) // If a support zone forms and we are in a short position, exit { int Result = sc.FlattenAndCancelAllOrders(); SCString traderesult; traderesult.Format("Exiting short position due to support zone formation, Result = %i", Result); sc.AddMessageToLog(traderesult, 0); break; } } } else if (current_position > 0) // Long position { for (auto& zone : active_resistance_zones) { if (sc.Close[sc.Index] < zone.end_price) // If a resistance zone forms and we are in a long position, exit { int Result = sc.FlattenAndCancelAllOrders(); SCString traderesult; traderesult.Format("Exiting long position due to resistance zone formation, Result = %i", Result); sc.AddMessageToLog(traderesult, 0); break; } } } } |
[2024-08-20 19:01:59] |
User431178 - Posts: 540 |
Check for sc.IsFullRecalculation, and if so do not perform the trading action. ACSIL Interface Members - Variables and Arrays: sc.IsFullRecalculation As you are using autolooping (inferred by the use of sc.Index), the code below is not doing anything useful. if (sc.UpdateStartIndex==0) return; ACSIL Interface Members - Variables and Arrays: sc.UpdateStartIndex Instead, you could use if (sc.IsFullRecalculation) return; Date Time Of Last Edit: 2024-08-20 19:02:43
|
[2024-08-20 19:27:03] |
PS - Posts: 40 |
Yes, I am using Autoloop. I had put that sc.UpdateStartIndex in there because I someone post it as a solution to a similar problem, and thought it may work. I don't quite understand what exactly the full recalculation is and why it yields this error. I have very similar strategies I've backtested before but this is the first one that is yielding this issue. Could you explain what exactly is going on? Also, I tried replacing the lines you said to remove with the IsFullRecalculation, and I'm still getting this error. Date Time Of Last Edit: 2024-08-20 19:29:30
|
[2024-08-20 19:55:33] |
User431178 - Posts: 540 |
I don't quite understand what exactly the full recalculation is and why it yields this error. I have very similar strategies I've backtested before but this is the first one that is yielding this issue. Working with ACSIL Arrays and Understanding Looping: When the Study Function is Called Could you explain what exactly is going on?
It is simple, do not use any of the order entry / exit functions whilst a recalculation is in progress.Obviously the code above is not the entire study, have you debugged at all, do you know which code is being executed that leads to the error message? |
[2024-08-22 16:39:06] |
PS - Posts: 40 |
The Sierra documentation says that a full recalculation occurs anytime the following happens: A study will be fully calculated/recalculated when it is added to a chart, any time its Input settings are changed, another study is added or removed from a chart, when the Study Window is closed with OK or the settings are applied. Or under other conditions which can cause a full recalculation.
Other conditions include: When the chart the study is applied to is being referenced by another chart and the other chart has been fully recalculated, the chart data loaded, or a historical data download finishes. For more information, refer to References to Other Charts and Tagging. When there is a rebuilding of the internal Trades List in a chart, if it is being used, which can occur when changing the Trade Account on the chart or enabling or disabling Trade Simulation Mode. I don't have anything in my code that would cause this. Additionally, after putting the sc.IsFullRecalculation check prior to my trading logic, the error is still occurring. Any ideas? |
[2024-08-22 17:15:32] |
User431178 - Posts: 540 |
Any ideas?
Yes, either post the code in it's entirety (or send via direct message) or debug it yourself. Somewhere there must be code being executed during recalculation. |
[2024-08-22 18:02:11] |
PS - Posts: 40 |
Sent you a DM request.
|
To post a message in this thread, you need to log in with your Sierra Chart account: