Login Page - Create Account

Support Board


Date/Time: Sun, 29 Dec 2024 06:28:38 +0000



Post From: Missmatch execution times...

[2016-01-15 19:19:09]
Sierra Chart Engineering - Posts: 104368
The below function contains a code example to get the details of an order submitted using the Internal Order ID. This code is near the bottom of the function.

We added the ability to get the most recent fill price, so this will not compile until the next release which will be out today or tomorrow.

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];

  SCSubgraphRef SimpMovAvgSubgraph = sc.Subgraph[4];

  SCInputRef Enabled = sc.Input[0];
  SCInputRef TargetValue = sc.Input[1];
  SCInputRef StopValue = sc.Input[2];


  if (sc.SetDefaults)
  {
    // Set the study configuration and defaults.

    sc.GraphName = "Trading Example";

    BuyEntrySubgraph.Name = "Buy Entry";
    BuyEntrySubgraph.DrawStyle = DRAWSTYLE_ARROWUP;
    BuyEntrySubgraph.PrimaryColor = RGB(0, 255, 0);
    BuyEntrySubgraph.LineWidth = 2;
    BuyEntrySubgraph.DrawZeros = false;

    BuyExitSubgraph.Name = "Buy Exit";
    BuyExitSubgraph.DrawStyle = DRAWSTYLE_ARROWDOWN;
    BuyExitSubgraph.PrimaryColor = RGB(255, 128, 128);
    BuyExitSubgraph.LineWidth = 2;
    BuyExitSubgraph.DrawZeros = false;

    SellEntrySubgraph.Name = "Sell Entry";
    SellEntrySubgraph.DrawStyle = DRAWSTYLE_ARROWDOWN;
    SellEntrySubgraph.PrimaryColor = RGB(255, 0, 0);
    SellEntrySubgraph.LineWidth = 2;
    SellEntrySubgraph.DrawZeros = false;

    SellExitSubgraph.Name = "Sell Exit";
    SellExitSubgraph.DrawStyle = DRAWSTYLE_ARROWUP;
    SellExitSubgraph.PrimaryColor = RGB(128, 255, 128);
    SellExitSubgraph.LineWidth = 2;
    SellExitSubgraph.DrawZeros = false;

    SimpMovAvgSubgraph.Name = "Simple Moving Average";
    SimpMovAvgSubgraph.DrawStyle = DRAWSTYLE_LINE;
    SimpMovAvgSubgraph.DrawZeros = false;

    Enabled.Name = "Enabled";
    Enabled.SetYesNo(0);

    TargetValue.Name = "Target Value";
    TargetValue.SetFloat(2.0f);

    StopValue.Name = "Stop Value";
    StopValue.SetFloat(1.0f);

    sc.AutoLoop = 1;
    sc.GraphRegion = 0;

    // During development set this flag to 1, so the DLL can be modified. When development is completed, set it to 0 to improve performance.
    sc.FreeDLL = 0;

    //Any of the following variables can also be set outside and below the sc.SetDefaults code block

    sc.AllowMultipleEntriesInSameDirection = false;
    sc.MaximumPositionAllowed = 10;
    sc.SupportReversals = false;

    // This is false by default. Orders will go to the simulation system always.
    sc.SendOrdersToTradeService = false;

    sc.AllowOppositeEntryWithOpposingPositionOrOrders = false;
    sc.SupportAttachedOrdersForTrading = false;
    sc.CancelAllOrdersOnEntriesAndReversals= true;
    sc.AllowEntryWithWorkingOrders = false;
    sc.CancelAllWorkingOrdersOnExit = false;

    // Only 1 trade for each Order Action type is allowed per bar.
    sc.AllowOnlyOneTradePerBar = true;

    //This needs to be set to true when a trading study uses trading functions.
    sc.MaintainTradeStatisticsAndTradesData = true;


    return;
  }

  if (!Enabled.GetYesNo())
    return;
  
  SCFloatArrayRef Last = sc.Close;
  
  // Calculate the moving average
  sc.SimpleMovAvg(Last, SimpMovAvgSubgraph, sc.Index, 10);
  

  // Get the Trade Position data to be used for position exit processing.
  s_SCPositionData PositionData;
  sc.GetTradePosition(PositionData) ;


  float LastTradePrice = sc.Close[sc.Index];

  int& r_BuyEntryInternalOrderID = sc.GetPersistentInt(1);

  // Create an s_SCNewOrder object.
  s_SCNewOrder NewOrder;
  NewOrder.OrderQuantity = 1;
  NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
  NewOrder.TextTag = "Trading example tag";
  int Result;

  // Buy when the last price crosses the moving average from below.
  if (sc.CrossOver(Last, SimpMovAvgSubgraph) == CROSS_FROM_BOTTOM && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
  {
      Result = sc.BuyEntry(NewOrder);
      //If there has been a successful order entry, then draw an arrow at the low of the bar.
      if (Result > 0)
      {
        r_BuyEntryInternalOrderID = NewOrder.InternalOrderID;
        BuyEntrySubgraph[sc.Index] = sc.Low[sc.Index];
      }
  }
  
  
  // When there is a long position AND the Last price is less than the price the Buy Entry was filled at minus Stop Value, OR there is a long position AND the Last price is greater than the price the Buy Entry was filled at plus the Target Value.
  else if (PositionData.PositionQuantity > 0
    && (LastTradePrice <= PositionData.AveragePrice - StopValue.GetFloat() ||
    LastTradePrice >= PositionData.AveragePrice + TargetValue.GetFloat()))
  {
    Result = sc.BuyExit(NewOrder);

    //If there has been a successful order entry, then draw an arrow at the high of the bar.
    if (Result > 0)
    {
      BuyExitSubgraph[sc.Index] = sc.High[sc.Index];
    }
  }
  
  
  // Sell when the last price crosses the moving average from above.
  else if (sc.CrossOver(Last, SimpMovAvgSubgraph) == CROSS_FROM_TOP && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
  {
    Result = sc.SellEntry(NewOrder);
    
    // If there has been a successful order entry, then draw an arrow at the high of the bar.
    if (Result > 0)
    {
      SellEntrySubgraph[sc.Index] = sc.High[sc.Index];
    }
  }
  
  // When there is a short position AND the Last price is greater than the price the Sell Entry was filled at plus the Stop Value, OR there is a short position AND the Last price is less than the price the Sell Entry was filled at minus the Target Value.
  else if (PositionData.PositionQuantity < 0
    && (LastTradePrice >= PositionData.AveragePrice + StopValue.GetFloat() ||
    LastTradePrice <= PositionData.AveragePrice - TargetValue.GetFloat()))
  {
    Result = sc.SellExit(NewOrder);
    
    // If there has been a successful order entry, then draw an arrow at the low of the bar.
    if (Result > 0)
    {
      SellExitSubgraph[sc.Index] = sc.Low[sc.Index];
    }
  }

  //Example to check the status of the order using the internal order ID remembered
  s_SCTradeOrder TradeOrder;
  sc.GetOrderByOrderID(r_BuyEntryInternalOrderID, TradeOrder);

  // Order has been found.
  if (TradeOrder.InternalOrderID == r_BuyEntryInternalOrderID)
  {
    bool IsOrderFilled = TradeOrder.OrderStatusCode == SCT_OSC_FILLED;
    double FillPrice = TradeOrder.LastFillPrice;
  }
}

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: 2016-01-17 22:21:37