Support Board
Date/Time: Mon, 03 Feb 2025 08:08:15 +0000
Post From: sc.CancelOrder is causing CPU exception when a user is looking at another chartbook
[2019-10-25 13:31:16] |
@sstfrederik - Posts: 405 |
Hi, I have been investigating a CPU exception and have reduced it to the sc.CancelOrder function. This only happens when the user is looking at another chartbook while the study is active on a chart in a different chartbook. I used the below code from one of your code examples to replicate the issue. Study was build with remote compiler on SC version 1987. Can you investigate this please. Thanks. Frederik #include "sierrachart.h"
SCDLLName("SomeTest") SCSFExport scsf_TradingExample(SCStudyInterfaceRef sc) { //Define references to the Subgraphs and Inputs for easy reference SCSubgraphRef BuyEntrySubgraph = sc.Subgraph[0]; SCSubgraphRef BuyExitSubgraph = sc.Subgraph[1]; SCSubgraphRef SellEntrySubgraph = sc.Subgraph[2]; SCSubgraphRef SellExitSubgraph = sc.Subgraph[3]; SCInputRef Enabled = sc.Input[0]; if (sc.SetDefaults) { // Set the study configuration and defaults. sc.GraphName = "Trading Example"; sc.StudyDescription = "CPU exception when cancel order is executed and user is looking at another chartbook"; BuyEntrySubgraph.Name = "Buy Entry"; BuyEntrySubgraph.DrawStyle = DRAWSTYLE_ARROW_UP; BuyEntrySubgraph.PrimaryColor = RGB(0, 255, 0); BuyEntrySubgraph.LineWidth = 2; BuyEntrySubgraph.DrawZeros = false; BuyExitSubgraph.Name = "Buy Exit"; BuyExitSubgraph.DrawStyle = DRAWSTYLE_ARROW_DOWN; BuyExitSubgraph.PrimaryColor = RGB(255, 128, 128); BuyExitSubgraph.LineWidth = 2; BuyExitSubgraph.DrawZeros = false; SellEntrySubgraph.Name = "Sell Entry"; SellEntrySubgraph.DrawStyle = DRAWSTYLE_ARROW_DOWN; SellEntrySubgraph.PrimaryColor = RGB(255, 0, 0); SellEntrySubgraph.LineWidth = 2; SellEntrySubgraph.DrawZeros = false; SellExitSubgraph.Name = "Sell Exit"; SellExitSubgraph.DrawStyle = DRAWSTYLE_ARROW_UP; SellExitSubgraph.PrimaryColor = RGB(128, 255, 128); SellExitSubgraph.LineWidth = 2; SellExitSubgraph.DrawZeros = false; Enabled.Name = "Enabled"; Enabled.SetYesNo(0); Enabled.SetDescription("This input enables the study and allows it to function. Otherwise, it does nothing."); // This is false by default. Orders will be simulated. sc.SendOrdersToTradeService = false; // These variables are not used when trading another Symbol and/or Trade Account compared to what the chart is set to. sc.AllowMultipleEntriesInSameDirection = true; sc.MaximumPositionAllowed = 20000; sc.SupportReversals = true; sc.AllowOppositeEntryWithOpposingPositionOrOrders = true; sc.SupportAttachedOrdersForTrading = false; sc.UseGUIAttachedOrderSetting = false; sc.CancelAllOrdersOnEntriesAndReversals= true; sc.AllowEntryWithWorkingOrders = true; sc.CancelAllWorkingOrdersOnExit = true; // sc.AllowOnlyOneTradePerBar = false; //This needs to be set to true when a trading study uses trading functions. sc.MaintainTradeStatisticsAndTradesData = true; sc.ReceiveNotificationsForChangesToOrdersPositionsForAnySymbol = true; sc.AutoLoop = true; sc.GraphRegion = 0; return; } //These must be outside of the sc.SetDefaults code block since they have a dependency upon an actual chart object existing. They are not used in this example. sc.SupportTradingScaleIn = 0; sc.SupportTradingScaleOut = 0; int& r_BarIndexOfOrder = sc.GetPersistentInt(1); int& r_InternalOrderID = sc.GetPersistentInt(2); int& r_PerformedOrderModification = sc.GetPersistentInt(3); if (!Enabled.GetYesNo()) { r_BarIndexOfOrder = 0; r_InternalOrderID = 0; r_PerformedOrderModification = 0; return; } if (sc.LastCallToFunction) return;//nothing to do if (sc.ChartIsDownloadingHistoricalData(sc.ChartNumber) || sc.IsFullRecalculation || sc.ServerConnectionState != SCS_CONNECTED ) return; // Run the below code only on the last bar if (sc.Index < sc.ArraySize - 1) return; s_SCPositionData PositionData; sc.GetTradePosition(PositionData); if (PositionData.PositionQuantity == 0 && PositionData.WorkingOrdersExist == 0 && r_InternalOrderID == 0) { // Create an s_SCNewOrder object. s_SCNewOrder NewOrder; NewOrder.OrderQuantity = 1; NewOrder.OrderType = SCT_ORDERTYPE_LIMIT; NewOrder.TimeInForce = SCT_TIF_DAY; NewOrder.Price1 = sc.Low[sc.Index] - 100 * sc.TickSize; //Specify a Target and a Stop with 8 tick offsets. Only 1 Target and 1 Stop can be supported when trading a Symbol and Trade Account different than the chart the trading system study is applied to. NewOrder.Target1Offset = 8*sc.TickSize; NewOrder.Stop1Offset = 8*sc.TickSize; // If this is left at the default of 0, then it will be automatically set. NewOrder.OCOGroup1Quantity = 1; int Result = static_cast<int>(sc.BuyOrder(NewOrder)); if (Result > 0) { r_BarIndexOfOrder = sc.Index; r_InternalOrderID = NewOrder.InternalOrderID; } } else if (r_InternalOrderID != 0) { s_SCTradeOrder ExistingOrderDetails; if (sc.GetOrderByOrderID(r_InternalOrderID, ExistingOrderDetails)) { //If original submitted order is still working. if (IsWorkingOrderStatus(ExistingOrderDetails.OrderStatusCode) ) { if (sc.Index >= r_BarIndexOfOrder + 2) { //Cancel the order sc.CancelOrder(r_InternalOrderID); } } } } } |