Login Page - Create Account

Support Board


Date/Time: Sun, 08 Sep 2024 00:32:39 +0000



[Programming Help] - Issues about sc.CancelAllOrders()

View Count: 165

[2024-07-10 14:53:20]
Tony - Posts: 502
Hi,

I am trying to cancel stop order when there is no position by using sc.CancelAllOrders(), everything works great until I press "Insert" key to reload and recalculate the chart, the stop order disappeared even I still had a position, so I tried to fix the issue by doing this:


  if (sc.IsChartDataLoadingInChartbook() || sc.IsFullRecalculation || sc.ChartIsDownloadingHistoricalData(sc.ChartNumber)) {
    // do something
  }

  else {
    s_SCPositionData PositionData;
    sc.GetTradePosition(PositionData);

    if (PositionData.AveragePrice == 0.0) {
      sc.CancelAllOrders();
    }
  }

But the stop order still got canceled after reload and recalculation while I had an open position.

Wonder what I had missed,

Thanks!

P.S.
Originally, the stop order also got canceled after I remove and add this custom study, that part has been fixed by adding the code showed above.
Date Time Of Last Edit: 2024-07-10 15:25:58
[2024-07-10 15:09:35]
ForgivingComputers.com - Posts: 928
// Generally you want to return in this conditional:
if (sc.IsFullRecalculation || sc.ChartIsDownloadingHistoricalData(sc.ChartNumber)) {
return;
}

// Then check if in a position and working orders
s_SCPositionData PositionData;
sc.GetTradePosition(PositionData);
int Open_Position = PositionData.PositionQuantity;

if (!Open_Position && PositionData.WorkingOrdersExist) {
sc.CancelAllOrders();
}



[2024-07-10 15:20:16]
Tony - Posts: 502
Hi bradh,

Thanks for your reply, I tried to return that condition, I still have the same issue.

One thing had been fixed is that after I remove/add this study or release/load DLL, the stop order will not be canceled, which is what I wanted.
[2024-07-10 15:35:27]
User431178 - Posts: 495
As well as

PositionData.WorkingOrdersExist

there is also

PositionData.NonAttachedWorkingOrdersExist

Which seems might be appropriate?
[2024-07-10 17:10:03]
ForgivingComputers.com - Posts: 928
The first one is all working orders, the second is only Non-Attached Orders, so I would use the first one.
[2024-07-10 19:46:09]
Tony - Posts: 502
Hi bradh and User431178,

Thanks so much for the feedback, unfortunately I tried all the possible solutions, none of them works.

I get around the issue by delaying the open-position check for a few loops right after reload / recalculation:

static int LoopCount {0};
LoopCount++;

So this number will be increased by 1 per loop through the lifespan of the study, but will be reset to 0 every time reload or recalculate happened.

And open-position check only happens when the value of LoopCount is greater than a certain number, i.e. 3

Now the stop order will never be canceled when there is an open position, as I expected.

I think I misunderstood with either of these 3 function / variable:
sc.IsChartDataLoadingInChartbook()
sc.IsFullRecalculation
sc.ChartIsDownloadingHistoricalData(sc.ChartNumber)

I hope engineering team will have a better solution, my code looks ugly now.
Date Time Of Last Edit: 2024-07-11 05:44:45

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

Login

Login Page - Create Account