Login Page - Create Account

Support Board


Date/Time: Sun, 05 May 2024 14:23:11 +0000



Post From: Simple ACSIL example to scale in to a trade

[2016-05-04 02:42:04]
Sierra Chart Engineering - Posts: 104368
You will need to use the following code.

This code requires version 1406 which will be out today or tomorrow.

Our other code example we gave above has been updated to this one.

SCSFExport scsf_TradingScaleInExample(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];

  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: Scale In";

    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;

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

    TargetValue.Name = "Target Offset in Ticks";
    TargetValue.SetFloat(10.0f);

    StopValue.Name = "Stop Offset in Ticks";
    StopValue.SetFloat(10.0f);

    sc.StudyDescription = "This study function is an example of how to use the ACSIL Trading Functions and increase the Trade Position quantity by scaling in.";

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

    sc.FreeDLL = 0;

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

    sc.AllowMultipleEntriesInSameDirection = true;
    sc.MaximumPositionAllowed = 2;
    sc.SupportReversals = false;

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

    sc.AllowOppositeEntryWithOpposingPositionOrOrders = false;

    //This must be true for Scale In to work
    sc.SupportAttachedOrdersForTrading = true;

    sc.CancelAllOrdersOnEntriesAndReversals= false;
    sc.AllowEntryWithWorkingOrders = true;
    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;
  }

  sc.SupportTradingScaleIn = true;
  sc.SupportTradingScaleOut = false;

  if (!Enabled.GetYesNo())
    return;


  // Get the Trade Position data
  s_SCPositionData PositionData;
  sc.GetTradePosition(PositionData) ;


  int& r_BuyEntryInternalOrderID = sc.GetPersistentInt(1);

  // This Boolean needs to be set to true when the condition for your initial entry has been met. This trading example does not actually define any entry condition but has code to enter a buy order when an entry condition has occurred.
  bool ConditionForInitialBuyEntry = true;
  bool ConditionForSecondBuyEntry = true;

  // Buy when the last price crosses the moving average from below.
  if (ConditionForInitialBuyEntry && PositionData.PositionQuantity == 0
    && !PositionData.WorkingOrdersExist)
  {

    // Create an s_SCNewOrder object.
    s_SCNewOrder NewOrder;
    NewOrder.OrderQuantity = 1;
    NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
    NewOrder.Target1Offset = TargetValue.GetFloat() * sc.TickSize;
    NewOrder.Stop1Offset = StopValue.GetFloat() * sc.TickSize;

    int 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];
    }
  }
  else if (ConditionForSecondBuyEntry
    && PositionData.PositionQuantity == 1
    && !PositionData.NonAttachedWorkingOrdersExist
  )
  {

    // Create an s_SCNewOrder object.
    s_SCNewOrder NewOrder;
    NewOrder.OrderQuantity = 1;
    NewOrder.OrderType = SCT_ORDERTYPE_MARKET;

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

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