Login Page - Create Account

Support Board


Date/Time: Fri, 31 Jan 2025 00:21:43 +0000



Post From: Acsil TP Management Assistance

[2019-03-27 16:57:00]
User920967 - Posts: 62
This is the code we are using to test shorts only. Error is the same as pic in post 1, mismatching of TP with parent order.

We are thinking either 1) we are missing some sort of setting within sierra, or 2)we must use sellexit to achieve our desired outcome.

Thanks in advance...very much appreciated




// The top of every source code file must include this line
#include "sierrachart.h"

// For reference, refer to this page:
// Advanced Custom Study Interface and Language (ACSIL)

// This line is required. Change the text within the quote
// marks to what you want to name your group of custom studies.
SCDLLName("xxxx Studies")

SCSFExport scsf_TradingExampleWithStopAllAttachedOrdersDirectlyDefined(SCStudyInterfaceRef sc) {
  // Define references to the Subgraphs and Inputs for easy reference
  SCSubgraphRef BuyEntrySubgraph = sc.Subgraph[0];
  SCSubgraphRef SellEntrySubgraph = sc.Subgraph[2];
  SCSubgraphRef SimpMovAvgSubgraph = sc.Subgraph[4];
  SCInputRef Enabled = sc.Input[0];


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

    sc.GraphName = "Trading Example: With Hardcoded Attached Orders (uses Stop All)";

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


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


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

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

    sc.StudyDescription = "This study function is an example of how to use the ACSIL Trading Functions. This example uses Attached Orders defined directly within this function. This function will display a simple moving average and perform a Buy Entry when the Last price crosses the moving average from below and a Sell Entry when the Last price crosses the moving average from above. A new entry cannot occur until the Target or Stop has been hit. When an order is sent, a corresponding arrow will appear on the chart to show that an order was sent. This study will do nothing until the Enabled Input is set to Yes.";

    sc.AllowMultipleEntriesInSameDirection = true;
    sc.MaximumPositionAllowed = 6;
    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; // This can be false in this function because we specify Attached Orders directly with the order which causes this to be considered true when submitting an order.

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

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

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

    return;
  }

  if (!Enabled.GetYesNo())
    return;


  SCFloatArrayRef Last = sc.Close;

  // Use persistent variables to remember attached order IDs so they can be modified or canceled.
  int& Target1OrderID = sc.GetPersistentInt(0);
  int& Target2OrderID = sc.GetPersistentInt(1);
  int& StopAllOrderID = sc.GetPersistentInt(2);

  int & ExecuteModifyOrder = sc.GetPersistentInt(3);

  // Calculate the moving average
  sc.SimpleMovAvg(Last, SimpMovAvgSubgraph, sc.Index, 10);

  if (sc.IsFullRecalculation)
    return;

  // Create an s_SCNewOrder object.
  s_SCNewOrder NewOrder;
  NewOrder.OrderQuantity = 2;
  NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
  NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;

  //Specify Targets and Stops
  NewOrder.Target1Offset = 8*sc.TickSize;
  NewOrder.Target2Offset = 111*sc.TickSize;
  NewOrder.StopAllOffset = 35*sc.TickSize;
  
  
  // 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) {
    
  }
  
  // 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)
  {
    //NewOrder.Price1 = Last[sc.Index] - 2*sc.TickSize;
    int Result = (int)sc.SellEntry(NewOrder);
    if (Result > 0) //If there has been a successful order entry, then draw an arrow at the high of the bar.
    {
      SellEntrySubgraph[sc.Index] = sc.High[sc.Index];
      ExecuteModifyOrder =1;

      // Remember the order IDs for subsequent modification and cancellation
      Target1OrderID = NewOrder.Target1InternalOrderID;
      Target2OrderID = NewOrder.Target2InternalOrderID;
      StopAllOrderID = NewOrder.StopAllInternalOrderID;
    }

  }
}


Date Time Of Last Edit: 2019-03-27 18:00:13