Login Page - Create Account

Support Board


Date/Time: Fri, 03 May 2024 05:24:05 +0000



sc.CancelOrder is causing CPU exception when a user is looking at another chartbook

View Count: 952

[2019-10-25 13:31:16]
@sstfrederik - Posts: 403
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);
        }
      }
    }
  }

}


[2019-10-25 13:54:36]
Sierra Chart Engineering - Posts: 104368
We are checking on this now.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
[2019-10-25 21:13:52]
Sierra Chart Engineering - Posts: 104368
Really this problem does not make sense that it would be occurring and we are not able to reproduce it either. There must be some other cause.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
[2019-10-25 21:57:46]
@sstfrederik - Posts: 403
I know it does not make sense but I see it happen. Nobody likes exceptions. But this needs to be solved.

This issue is only present when the chartbook the study is loaded on that has a CancelOrder function call is not an active window.

I will include some pics and two chartbooks and dll so you can reproduce this.
attachmentChartbook50.Cht - Attached On 2019-10-25 21:55:51 UTC - Size: 30.46 KB - 265 views
attachmentChartbook51.Cht - Attached On 2019-10-25 21:55:59 UTC - Size: 21.17 KB - 275 views
Private File
[2019-10-25 21:58:41]
@sstfrederik - Posts: 403
Here are the pics.

This happens in replay as well as in live conditions.

PS the order Id are different because I had to run this again for screenshot purposes.
Date Time Of Last Edit: 2019-10-25 22:04:03
imagecancelorder-issue-1.png / V - Attached On 2019-10-25 21:58:09 UTC - Size: 134.28 KB - 244 views
imagecancelorder-issue-2.png / V - Attached On 2019-10-25 21:58:21 UTC - Size: 142.75 KB - 246 views
imagecancelorder-issue-3.png / V - Attached On 2019-10-25 21:58:32 UTC - Size: 120.3 KB - 237 views
[2019-10-26 12:56:56]
@sstfrederik - Posts: 403
Here is the 64 bit dll.
Private File
[2019-10-26 13:48:18]
@sstfrederik - Posts: 403
I did some more testing. It seems to be specific to my configuration. Unsure what is causing this and hope you can investigate further when you test with my Sierra3.cfg file (see attached).

If this is caused by some global setting it will be useful to know in order to prevent or fix.

Thanks for your time.

Frederik
Private File
[2019-10-28 10:31:09]
Sierra Chart Engineering - Posts: 104368
We saw the 3 orders originating from Chartbook 50 get submitted, and later canceled after about two minutes while looking at Chartbook number 51 and no exceptions. This is using your global configuration file (Sierra3.CFG).

We recommend that you restart Sierra Chart and do not use any custom study other than this one custom study that is very simple and test again.

And we also want you to use the latest prerelease.

Update: Maybe the problem is related to an incorrect DisplayOrder for Inputs:
ACSIL Interface Members - sc.Input Array: Controlling the Display Order of Inputs in the Study Settings Window

Make sure you are using a valid range for those values.

Once there is a memory corruption occurring, any type of problem can occur that is completely unrelated.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
Date Time Of Last Edit: 2019-10-28 11:25:46
[2019-10-28 11:48:54]
@sstfrederik - Posts: 403
Can you acknowledge this problem is happening on your end as well with the non-pre release SC version 1997 and below?

Acknowledge the problem and let me know what the cause is. Any comparison between a clean cfg and the one I posted?

The pre-release does not have this issue at the moment. It for sure is not the display order inputs. The test code used only has one input value, you can check yourself.
[2019-10-29 06:14:33]
Sierra Chart Engineering - Posts: 104368
We really cannot spend further time on this. And we do not think anything changed between the versions related to this, other than adding a safety check related to the sc.Input[]. DisplayOrder.

When you are running testing, did you restart Sierra Chart and are you not using any other custom studies? And to be completely safe you would have to remove all other DLL files from the Sierra Chart Data folder. Try doing that and test on 1997 again.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
Date Time Of Last Edit: 2019-10-29 06:16:00
[2019-10-29 12:59:36]
@sstfrederik - Posts: 403
I ran that test on a clean install of SC 1997 (again), same result. I newly installed SC 1997 and only changed the sierra3.cfg. Removed all other dll's in data folder before testing, only ran the posted chartbooks of which chartbook 50 has one dll as posted with source code above. This is not my first week using SC either.

My conclusion: there is some setting (as my sierra3.cfg is different from the default one) causing this (it only happens when using my sierra3.cfg not on the default cfg).

You changed something in pre-release as that is working fine and has no issues.

My point is: if anyone runs an auto strategy on an earlier SC version and happens to have some kind of setting set (like in my case) there is the potential their order will not get cancelled due to this issue. This can not be acceptable and users should know about this.

Can't you run a diff on the cfg files and check on potential causes? I will be happy to do the testing for you.

As I sell an Autotrading study it would be helpful to be able to tell my clients which setting could cause a possible issue with the cancel order interaction. Fortunately none of my clients have reported having this issue this yet.

I guess right now I all I can tell is upgrade, SC fixed this thing in pre-release.
[2019-10-31 00:31:25]
Sierra Chart Engineering - Posts: 104368
We are very busy and cannot spend more time on this. The only thing we are aware of is a problem involving the DisplayOrder with sc.Input because there was not a safety check if it was set to an invalid value. This is all that has changed we have not fixed anything else. We have not been aware of any stability issue recently.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account