Login Page - Create Account

Support Board


Date/Time: Sun, 09 Mar 2025 20:50:25 +0000



[Programming Help] - Orders not Cancelling (fast replay) + Checking if existing working order or filled order

View Count: 686

[2022-03-12 09:55:07]
SelimEker - Posts: 13
Hi everyone !

I know asking for programming might not be really popular here. But i need your input on this bit of code that I use to cancel my orders.

This bit of code seems to have some problem, whenever i do a replay at high speed. Any ideas why ?

Subgraph_BuyFill is basically saying if the Buy Conditions are filled (if == 1)
ActiveScalpPresent is a bool (global variable that i use to say if I have already entered a position.
The rest is self explanatory i imagine.

Also. Any ideas on how to check (programatically) whether there is already an order (that is placed (not filled) or in the market until exited) instead of using my global variable ?


if (EnabledCancel.GetYesNo())
  {
    s_SCTradeOrder ActiveScalpBuyOrder;
    sc.GetOrderByOrderID(BuyInternalOrderID, ActiveScalpBuyOrder);

    if ((Subgraph_BuyFill[sc.Index] != 1) && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED
      && ActiveScalpPresent == true && sc.GetOrderByOrderID(BuyInternalOrderID, ActiveScalpBuyOrder) != SCTRADING_ORDER_ERROR && ActiveScalpBuyOrder.OrderStatusCode != SCT_OSC_FILLED)
    {
      sc.CancelOrder(BuyInternalOrderID);
      ActiveScalpPresent = false;
      SCString DataString;
      DataString.Format("Buy Order Canceled due to condition changed, Scalp Is Available to use Now !");
      sc.AddMessageToLog(DataString, 1);
    }
  }

Date Time Of Last Edit: 2022-03-12 09:55:39
[2022-03-12 10:21:03]
User431178 - Posts: 612
Any ideas on how to check (programatically) whether there is already an order (that is placed (not filled) or in the market until exited) instead of using my global variable ?

Did you consider using trade position information?
Automated Trading From an Advanced Custom Study: Getting Trade Position Data


This bit of code seems to have some problem, whenever i do a replay at high speed. Any ideas why ?

Not a solution, but you could add some debug logging to the cancel order function and see what the error code is.


auto result = sc.CancelOrder(BuyInternalOrderID);

if (result < 0)
sc.AddMessageToTradeServiceLog(sc.GetTradingErrorTextMessage(result), 0, 1);
else
{
ActiveScalpPresent = false;
SCString DataString;
DataString.Format("Buy Order Canceled due to condition changed, Scalp Is Available to use Now !");
sc.AddMessageToLog(DataString, 1);
}

[2022-03-12 10:40:56]
SelimEker - Posts: 13
Thank you for the prompt answer !


Did you consider using trade position information?
Automated Trading From an Advanced Custom Study: Getting Trade Position Data

I have looked into it sometime ago but i was confused on the quantity side of things as sell orders are -1 and that there is also the attached orders that can or doesn't count.
I think i will have to check it out again :)

Not a solution, but you could add some debug logging to the cancel order function and see what the error code is.

I will check this out ! I think the issue might be related to the global variable as well..
[2022-03-12 15:27:39]
1+1=10 - Posts: 270
Hi SelimEker,

Backtesting and replays in SC can be tricky. I don't use that functionality so sadly can't provide must help. But as to your other concern:

Also. Any ideas on how to check (programatically) whether there is already an order (that is placed (not filled) or in the market until exited) instead of using my global variable ?

Yes, User431178's idea of using trade position data is most ideal. You want something like this:

s_SCPositionData position_data;
sc.GetTradePosition(position_data);

// CANCEL BUY OR SELL ORDERS
if (position_data.NonAttachedWorkingOrdersExist)
{

s_SCTradeOrder entry_order;
if (
sc.GetOrderByOrderID (scalp_entry_order_id, entry_order) != SCTRADING_ORDER_ERROR
&& entry_order.FilledQuantity < entry_order.OrderQuantity
&& IsWorkingOrderStatusIgnorePendingChildren(entry_order.OrderStatusCode)
)
{
// This strategy canceled entry buy limit orders as soon as the bid price
// was > 2 ticks above our buy limit order price. You could modify the
// the cancellation reason(s) below:

bool should_cancel_buy_trade = entry_order.BuySell == BSE_BUY &&
(last_bid_price - entry_order.Price1 > 1.9 * sc.TickSize);

bool should_cancel_sell_trade = entry_order.BuySell == BSE_SELL &&
(entry_order.Price1 - last_asl_price > 1.9 * sc.TickSize);


if (should_cancel_buy_trade || should_cancel_sell_trade)
{
sc.CancelOrder(scalp_entry_order_id);
}
}
}

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

Login

Login Page - Create Account