Login Page - Create Account

Support Board


Date/Time: Mon, 10 Mar 2025 02:53:58 +0000



Sample code for sc.SetAttachedOrders

View Count: 2017

[2022-01-18 16:41:09]
User99735 - Posts: 234
Can you provide sample code for sc.SetAttachedOrders usage. Nothing available in documentation or in studes*.cpp in ACS_Source folder.
[2022-01-18 20:27:54]
TedMar - Posts: 190
hi, u can check this link Automated Trading From an Advanced Custom Study: Example Trading Systems and Code
[2022-01-19 03:10:06]
User99735 - Posts: 234
Example code on that page has no sample for sc.SetAttachedOrders.

Am looking for a solution in ACSIL for - If two exit orders Limit and stop have been created separately ( without using the s_SCNewOrder structure Target1 / Stop1 etc ), how to make them OCO?
[2022-01-19 09:30:15]
TedMar - Posts: 190

....

if (!sc.IsFullRecalculation && !sc.DownloadingHistoricalData)
{


s_SCPositionData PositionData;
sc.GetTradePosition(PositionData);
        
int& RememberedOrderID = sc.GetPersistentInt(31);
int& Target1OrderID = sc.GetPersistentInt(32);
int& Stop1OrderID = sc.GetPersistentInt(33);


s_SCNewOrder NewOrder;
NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
NewOrder.OCOGroup1Quantity = 1;
NewOrder.OrderQuantity = 1;

NewOrder.Stop1Offset = 10 * sc.TickSize;
NewOrder.Target1Price = 15 * sc.TickSize;


int Result = (int)sc.BuyEntry(NewOrder, BarIndex); // if Signal to buy
          if (Result > 0)
          {
            
            
            RememberedOrderID = NewOrder.InternalOrderID;

            // Remember the order IDs for subsequent modification and cancellation
             Target1OrderID = NewOrder.Target1InternalOrderID;
            Stop1OrderID = NewOrder.Stop1InternalOrderID;
          }
}
.....



Date Time Of Last Edit: 2022-01-19 09:35:46
[2022-01-19 10:44:05]
User431178 - Posts: 612
Example code on that page has no sample for sc.SetAttachedOrders.

Am looking for a solution in ACSIL for - If two exit orders Limit and stop have been created separately ( without using the s_SCNewOrder structure Target1 / Stop1 etc ), how to make them OCO?

If you read https://www.sierrachart.com/index.php?page=doc/ACSILTrading.html#scSetAttachedOrders, you would already know that this function has nothing to do with submitting orders.

Instead look at https://www.sierrachart.com/index.php?page=doc/ACSILTrading.html#scSubmitOCOOrder, maybe cancel and replace your existing exit orders with an OCO pair....
[2022-01-19 16:01:19]
User99735 - Posts: 234
TedMar,
The sample code provided creates a buy position as well as the attached OCO exit stop / limit sell orders. My query is to only create the OCO exit stop / limit sell orders without creating the position.

User431178,
sc.SubmitOCOOrder() is useful for creating what is popularly known as breakout orders: SCT_ORDERTYPE_OCO_BUY_STOP_SELL_STOP, SCT_ORDERTYPE_OCO_BUY_STOP_LIMIT_SELL_STOP_LIMIT, SCT_ORDERTYPE_OCO_BUY_LIMIT_SELL_LIMIT, ie both the orders in opposite directions ie Buy Stop / Sell Stop etc.

SC_Support,
Please respond.
[2022-01-19 16:17:52]
User431178 - Posts: 612
Again, cancel and replace your existing exit orders with an OCO pair.

SCT_ORDERTYPE_OCO_LIMIT_STOP
SCT_ORDERTYPE_OCO_LIMIT_STOP_LIMIT
[2022-01-20 01:37:12]
User99735 - Posts: 234
User431178: Documentation for sc.SubmitOCOOrder() -
"Description: This function is for submitting OCO (Order Cancels Order) orders. it is only to be used when s_SCNewOrder::OrderType is set to one of the following: SCT_ORDERTYPE_OCO_BUY_STOP_SELL_STOP, SCT_ORDERTYPE_OCO_BUY_STOP_LIMIT_SELL_STOP_LIMIT, SCT_ORDERTYPE_OCO_BUY_LIMIT_SELL_LIMIT."
[2022-01-20 04:30:57]
User431178 - Posts: 612
Yes that's true, but once again, the solution is to cancel and replace your existing exit orders with an OCO pair.

SCT_ORDERTYPE_OCO_LIMIT_STOP
SCT_ORDERTYPE_OCO_LIMIT_STOP_LIMIT

Is it so difficult to work out how to do this?
[2022-01-20 04:39:15]
User99735 - Posts: 234
Using which function? sc.SubmitOCOOrder() or sc.BuyEntry / sc.BuyOrder?
[2022-01-21 13:44:59]
1+1=10 - Posts: 270

Am looking for a solution in ACSIL for - If two exit orders Limit and stop have been created separately ( without using the s_SCNewOrder structure Target1 / Stop1 etc ), how to make them OCO?

I don’t think you can do this in ACSIL. Why not create the child OCO orders using s_SCNewOrder when you enter the trade?
[2022-01-21 14:19:38]
User431178 - Posts: 612
I don’t think you can do this in ACSIL

Why not?
Find the relevant orders, cancel them, replace with OCO.

sc.SubmitOCOOrder() or sc.BuyEntry / sc.BuyOrder?
Seeing as you already said that sc.SubmitOCOOrder() could not be used (my bad) - then surely you know the answer to your question already?
Did you test this yet?
Date Time Of Last Edit: 2022-01-21 14:20:41
[2022-01-21 14:30:35]
User99735 - Posts: 234
Find the relevant orders, cancel them, replace with OCO.
Using sc.BuyOrder

Using sc.BuyOrder is also answered, it will open a entry order / position also with the OCO exit orders.
If the entry order / position can be avoided with sc.BuyOrder, please elaborate.
[2022-01-21 14:57:55]
1+1=10 - Posts: 270
Assuming you’re already in a position, then yes, you can cancel the original target/stop orders and use. (If the entry order is still active then note that this OCO Target/Stop pair will not be attached to the entry order.):


s_SCNewOrder NewOrder;
NewOrder.OrderType = SCT_ORDERTYPE_OCO_LIMIT_STOP

// Fill in other properties using instructions below. Then sc.BuyOrder(NewOrder);

Notes: Use s_NewOrder::Price1 to set the Limit price and s_NewOrder::Price2 to set the Stop price. Use sc.BuyOrder() or sc.SellOrder to submit the order when using this order type. All of the standard Auto Trade Management logic applies when using this order type, so you may want to use Unmanaged Automated Trading when submitting this type of order, so there are no restrictions.
[2022-01-21 15:43:45]
User431178 - Posts: 612
Using sc.BuyOrder is also answered, it will open a entry order / position also with the OCO exit orders.

This is just flat wrong and clearly you did not even test it.
[2022-01-21 21:43:15]
User99735 - Posts: 234
With NewOrder.OrderType = SCT_ORDERTYPE_OCO_LIMIT_STOP and sc.SellOrder returns Error Code -8, "Unsupported order type"
[2022-01-21 22:16:44]
1+1=10 - Posts: 270
The note about how to use that order type with sc.BuyOrder/sc.SellOrder comes from the SC documentation:
Automated Trading From an Advanced Custom Study: Order Type Constants

Will you paste your entire order submitting relevant code? Please make sure click the Code button and put the code between the blocks so it will be formatted well for readability.
[2022-01-22 11:03:31]
User431178 - Posts: 612
I have no problem with this code on sim this morning, as evidenced by attached log.
This is only a minimal demonstration of concept, clearly there is work to be done identifying the correct orders in different situations and there is no error handling or safety check of any kind.


if (cancelAndReplace)
{
  auto stopOrder = s_SCTradeOrder();
  auto targetOrder = s_SCTradeOrder();

  auto index{ 0 };
  auto orderDetails = s_SCTradeOrder();

  while (sc.GetOrderByIndex(index, orderDetails) != SCTRADING_ORDER_ERROR)
  {
    index++;

    if (orderDetails.OrderStatusCode != SCT_OSC_OPEN)
      continue;

    if (orderDetails.OrderType.CompareNoCase("Limit") == 0)
      targetOrder = orderDetails;
    else if (orderDetails.OrderType.CompareNoCase("Stop") == 0)
      stopOrder = orderDetails;

    if (stopOrder.InternalOrderID > 0
      && targetOrder.InternalOrderID > 0)
      break;
  }

  if (stopOrder.OrderStatusCode == SCT_OSC_OPEN
    && targetOrder.OrderStatusCode == SCT_OSC_OPEN)
  {
    if (sc.CancelOrder(stopOrder.InternalOrderID)
      && sc.CancelOrder(targetOrder.InternalOrderID))
    {
      auto newOrder = s_SCNewOrder();

      newOrder.OrderType = SCT_ORDERTYPE_OCO_LIMIT_STOP;
      newOrder.OrderQuantity = targetOrder.OrderQuantity;
      newOrder.Price1 = targetOrder.Price1;
      newOrder.Price2 = stopOrder.Price1;

      if (targetOrder.BuySell == BSE_BUY)
      {
        const auto result = static_cast<int32_t>(sc.BuyOrder(newOrder));

        if (result < 0)
          sc.AddMessageToTradeServiceLog(sc.GetTradingErrorTextMessage(result), 0, 1);

      }
      else if (targetOrder.BuySell == BSE_SELL)
      {
        const auto result = static_cast<int32_t>(sc.SellOrder(newOrder));

        if (result < 0)
          sc.AddMessageToTradeServiceLog(sc.GetTradingErrorTextMessage(result), 0, 1);
      }
    }
  }
}

attachmentTradeActivityLogExport_2022-01-22.txt - Attached On 2022-01-22 10:59:08 UTC - Size: 3.4 KB - 203 views
[2022-01-22 12:08:42]
1+1=10 - Posts: 270
Your code looks fine and well-organized.

I searched “Unsupported Order Type” on the support forum. Apparently, some Trading Services support more order types than others. For instance, SC/TT routing did not support Market-If-Touched but Rithmic did:
https://www.sierrachart.com/SupportBoard.php?do=Search&Query=Unsupported+order+type&Submit_Search=Search

You should consider starting a new support thread that hopefully SC will response to asking if SCT_ORDERTYPE_OCO_LIMIT_STOP is supported on your trading service.

Also, any chance you can explain, without revealing the important part of your strategy why you can’t use attached orders? If you want to start trades without a target/stop just set the attached orders 10000 ticks away from current price and then modify them closer later.
[2022-01-22 12:35:07]
User431178 - Posts: 612
@ACSIL Rocks
I am not the OP, but shared the code to show that the cancel and replace method can work.
As to whether 'SCT_ORDERTYPE_OCO_LIMIT_STOP' is not supported, I would be surprised if that were the case.
[2022-01-22 12:51:06]
1+1=10 - Posts: 270
@User431178
My apologies. I should have paid more attention to the screen names. It was very kind of you to post an example for the OP.

One interesting bullet point in the Introduction to Teton is:
"If you are using CQG, OCO and Bracket orders are not managed properly with that service. And Bracket orders are not server-side. This is all substandard. CQG users need to move to the Teton Order Routing service for properly managed OCO and Bracket orders."

If the OP is using CQG perhaps that is why SCT_ORDERTYPE_OCO_LIMIT_STOP" won't work for them.

Sierra Chart Teton Futures Order Routing: Introduction
Date Time Of Last Edit: 2022-03-02 17:01:11
[2022-03-02 16:48:01]
User99735 - Posts: 234
SC Engineering Support,
The example given in Posts: 18 with OrderType = SCT_ORDERTYPE_OCO_LIMIT_STOP, works for the 'current/default' account.

If the TradeAccount is set to some other account, function sc.BuyOrder() / sc.BuyOrder() returns error -8: "Unsupported Order Type".

Please advise.
Date Time Of Last Edit: 2022-03-02 18:32:27
[2022-03-04 16:04:22]
User99735 - Posts: 234
SC Engineering Support, Any update on post #22.
[2022-03-04 23:25:53]
Sierra Chart Engineering - Posts: 104368
Yes that would be true in regards to post #22 if that is the error you get. It is unsupported.
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