Login Page - Create Account

Support Board


Date/Time: Fri, 27 Dec 2024 18:33:23 +0000



Post From: Opening Stock Option position from a Stock Chart

[2015-12-09 21:24:14]
Sierra Chart Engineering - Posts: 104368
Enable Trade >> Trade Simulation Mode On.

Test the following trading system on a chart of AAPL.

The study has two Inputs. Set the Enabled Input to Yes. Set the Reference Chart to reference the chart the trading system is applied to.

What symbol is the submitted order for?

SCSFExport scsf_TradingExampleOrdersForDifferentSymbolAndAccount(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 ReferenceChartForPrice = sc.Input[1];

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

    sc.GraphName = "Trading Example: Orders for Different Symbol and Account";

    sc.StudyDescription = "This function demonstrates submitting, modifying and canceling an order for a different symbol than the chart the trading study is applied to";

    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);
    Enabled.SetDescription("This input enables the study and allows it to function. Otherwise, it does nothing.");

    ReferenceChartForPrice.Name = "Reference Chart For Price";
    ReferenceChartForPrice.SetChartNumber(0);

    // This is false by default. Orders will be simulated.
    sc.SendOrdersToTradeService = false;

    // These variables are not used when trading another Symbol and/or Trade Account compared to what the chart is set to.
    sc.AllowMultipleEntriesInSameDirection = true;
    sc.MaximumPositionAllowed = 10000;
    sc.SupportReversals = true;
    sc.AllowOppositeEntryWithOpposingPositionOrOrders = true;
    sc.SupportAttachedOrdersForTrading = false;
    sc.UseGUIAttachedOrderSetting = false;
    sc.CancelAllOrdersOnEntriesAndReversals= true;
    sc.AllowEntryWithWorkingOrders = true;
    sc.CancelAllWorkingOrdersOnExit = true;

    //
    sc.AllowOnlyOneTradePerBar = false;

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

    sc.ReceiveNotificationsForChangesToOrdersPositionsForAnySymbol = true;

    sc.AutoLoop = true;
    sc.GraphRegion = 0;

    sc.FreeDLL = 0;

    return;
  }

  //These must be outside of the sc.SetDefaults code block since they have a dependency upon an actual chart object existing. They are not used in this example.
  sc.SupportTradingScaleIn = 0;
  sc.SupportTradingScaleOut = 0;


  int& r_BarIndexOfOrder = sc.GetPersistentInt(1);
  int& r_InternalOrderID = sc.GetPersistentInt(2);
  int& r_PerformedOrderModification = sc.GetPersistentInt(3);

  if (!Enabled.GetYesNo())
  {
    r_BarIndexOfOrder = 0;
    r_InternalOrderID = 0;
    r_PerformedOrderModification = 0;

    return;
  }

  if ( sc.LastCallToFunction )
    return;//nothing to do

  if (sc.ChartIsDownloadingHistoricalData(ReferenceChartForPrice.GetChartNumber())
    || sc.IsFullRecalculation
    //|| sc.ServerConnectionState != SCS_CONNECTED
    )
    return;

  // Run the below code only on the last bar
  if (sc.Index < sc.ArraySize-1)
    return;

  SCFloatArray LastPriceArrayFromChartToTrade;
  sc.GetChartArray
    ( ReferenceChartForPrice.GetChartNumber()
    , SC_LAST
    , LastPriceArrayFromChartToTrade
    );

  if (LastPriceArrayFromChartToTrade.GetArraySize() == 0)
    return;

  int LastIndexOfArrayFromChartToTrade = LastPriceArrayFromChartToTrade.GetArraySize() - 1;

  const char* SymbolToTrade = "AAPL-OPT-20151211-118-C-SMART-100-USD";//"NQZ5";


  s_SCPositionData PositionData;
  sc.GetTradePositionForSymbolAndAccount(PositionData, SymbolToTrade, sc.SelectedTradeAccount);
  if (PositionData.PositionQuantity == 0 && PositionData.WorkingOrdersExist == 0 && r_InternalOrderID == 0)
  {
    // Create an s_SCNewOrder object.
    s_SCNewOrder NewOrder;
    NewOrder.OrderQuantity = 1;
    NewOrder.OrderType = SCT_ORDERTYPE_LIMIT;
    NewOrder.TimeInForce = SCT_TIF_DAY;
    NewOrder.Price1
      = LastPriceArrayFromChartToTrade[LastIndexOfArrayFromChartToTrade] - 10 * sc.TickSize;
    
    //Specify a Target and a Stop with 8 tick offsets.
//Only 1 Target and 1 Stop can be supported when trading
//a Symbol and Trade Account different than the chart the
//trading system study is applied to.
    NewOrder.Target1Offset = 8*sc.TickSize;
    NewOrder.Stop1Offset = 8*sc.TickSize;

    // If this is left at the default of 0, then it will be automatically set.
    NewOrder.OCOGroup1Quantity = 1;

    NewOrder.Symbol = SymbolToTrade;
    NewOrder.TradeAccount = sc.SelectedTradeAccount;

    int Result = sc.BuyOrder(NewOrder);
    
    if (Result > 0)
    {
      r_BarIndexOfOrder = sc.Index;
      r_InternalOrderID = NewOrder.InternalOrderID;
    }
  }
  else if (r_InternalOrderID != 0)
  {
    s_SCTradeOrder ExistingOrderDetails;
    if (sc.GetOrderByOrderID(r_InternalOrderID, ExistingOrderDetails))
    {
      //If original submitted order is still working.
      if (IsWorkingOrderStatus(ExistingOrderDetails.OrderStatusCode) )
      {
        //When 2 new bars added to chart since the original order submission
        if (r_PerformedOrderModification == 0 && sc.Index > r_BarIndexOfOrder + 1)
        {
          //Modify the order
          s_SCNewOrder ModifyOrder;
          ModifyOrder.InternalOrderID = r_InternalOrderID;
          ModifyOrder.Price1 = LastPriceArrayFromChartToTrade[LastIndexOfArrayFromChartToTrade] - 12 * sc.TickSize;

          if (sc.ModifyOrder(ModifyOrder) > 0)
            r_PerformedOrderModification = 1;
        }
        //When 3 new bars added to chart since the original order submission
        else if (sc.Index > r_BarIndexOfOrder + 2)
        {
          //Cancel the order
          sc.CancelOrder(r_InternalOrderID);
        }
      }
    }
  }

}

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: 2015-12-09 21:25:40