Login Page - Create Account

Support Board


Date/Time: Sat, 27 Apr 2024 22:24:51 +0000



Post From: Auto Trade ACSIL: how to enter at bar close

[2013-12-08 20:44:55]
User11748 - Posts: 14
I am using "scsf_TradingExample" proposed as an example by SC in tradingsystem.cpp

I have made no modification, except adding
SCDLLName("Test Strategy");
at the beginning.

Full code below.

As shown on the screenshot, the strategy enters at the open of the bar after.

For the purpose of backtesting, is it possible to have it enter at the close of the current bar (the one on which the signal is activated)?

Thanks in advance,

Nicolas


#include "sierrachart.h"


SCDLLName("Test Strategy");

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.StudyDescription = "This study function is an example of how to use the ACSIL Trading Functions. 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 = 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;

    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;

    return;
  }




  if (!Enabled.GetYesNo())
    return;
  
  SCFloatArrayRef Last = sc.Close;
  
  // Simple moving average in the 5th subgraph.
  sc.SimpleMovAvg(Last, SimpMovAvgSubgraph, sc.Index, 10);
  

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


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

  // Create an s_SCNewOrder object.
  s_SCNewOrder NewOrder;
  NewOrder.OrderQuantity = 1;
  NewOrder.OrderType = SCT_MARKET;
  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 (Result > 0) //If there has been a successful order entry, then draw an arrow at the low of the bar.
        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(Result>0) //If there has been a successful order entry, then draw an arrow at the high of the bar.
      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(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];
  }
  
  // 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(Result>0) //If there has been a successful order entry, then draw an arrow at the low of the bar.
      SellExitSubgraph[sc.Index] = sc.Low[sc.Index];
  }

}