Login Page - Create Account

Support Board


Date/Time: Sat, 10 May 2025 18:15:04 +0000



Post From: learning to code a simple strategy

[2016-01-25 05:12:51]
User61576 - Posts: 446
THANKS!
btw here is what i have done after reading the docs and took the example of the TradingExample1WithAdvancedAttachedOrders:


/*==========================================================================*/
SCSFExport scsf_TradingExample1WithAdvancedAttachedOrders(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];

  //settings that can be changed from within study settings in SC
  SCInputRef Enabled = sc.Input[0];
  SCInputRef Target1_percentage = sc.Input[1];
  SCInputRef Target2_percentage = sc.Input[2];
  SCInputRef stop_percentage = sc.Input[3];
  SCInputRef entry_hour = sc.Input[4];
  SCInputRef exit_hour = sc.Input[5];
  SCInputRef contracts = sc.Input[6];


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

    sc.GraphName = "Trading ADI With Advanced Attached Orders";

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

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

    SimpMovAvgSubgraph.Name = "Simple Moving Average";
    SimpMovAvgSubgraph.DrawStyle = DRAWSTYLE_LINE;
    SimpMovAvgSubgraph.PrimaryColor = RGB(255,255,0);
    SimpMovAvgSubgraph.LineWidth = 2;
    SimpMovAvgSubgraph.DrawZeros = false;

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

    sc.StudyDescription = "TEST. This study will do nothing until the 'Enabled' Input is set to Yes.";

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

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

    sc.AllowOppositeEntryWithOpposingPositionOrOrders = 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.SupportAttachedOrdersForTrading = false;

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


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


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

  //~Define the Attached Orders to be attached to the main Market order
  //TODO Target 1 - need to adjust according to the entry price * target1_percentage
  NewOrder.Target1Offset = sc.GetOrderFillEntry()*(1+target1_percentage);
  NewOrder.AttachedOrderTarget1Type = SCT_ORDERTYPE_LIMIT;

  //~Target 2
  NewOrder.Target1Offset = sc.GetOrderFillEntry()*(1+target2_percentage);
  NewOrder.AttachedOrderTarget2Type = SCT_ORDERTYPE_LIMIT;

  //~Common Step Trailing Stop
  //TODO - set a trailing according to the stop_percentage
  NewOrder.StopAllOffset = sc.GetOrderFillEntry()*(1-stop_percentage);
  NewOrder.AttachedOrderStopAllType = SCT_ORDERTYPE_STEP_TRAILING_STOP_LIMIT;
  //problem???? how to set the updated leg as the stop % from the first target
  NewOrder.TrailStopStepPriceAmount = (sc.GetOrderFillEntry()*(1+target1_percentage)) -;

  // Common Trailing Stop. Comment the section above and uncomment this section to use a simple trailing stop.
  //NewOrder.StopAllOffset = 8*sc.TickSize;
  //NewOrder.AttachedOrderStopAllType = SCT_ORDERTYPE_TRAILING_STOP;
  
  //TODO buy if time is 10:00 and yesterdays close was positive
  if (get.time= 10:00) && sc.GetBar(?????????) > 0)
  {
    int 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];
    }
  }


  // Sell when the time is 10:00 and yesterday's close is negative
  else if (if (time= 10:00) && sc.GetBar(?????????) < 0)
  {
    int 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];

    }
  }
  //Problem: how do i add flat at end of day?
}

Date Time Of Last Edit: 2016-01-25 05:37:41