Login Page - Create Account

Support Board


Date/Time: Wed, 12 Feb 2025 11:56:37 +0000



Post From: Breakeven with trail stop

[2020-10-16 14:09:48]
User837393 - Posts: 140
I found it, but isn't this already compliled ? I can'T find this into the study list ...

SCSFExport scsf_TradingExample2WithAdvancedAttachedOrders(SCStudyInterfaceRef sc)
{
  // Define references to the Subgraphs and Inputs for easy reference
  SCSubgraphRef Subgraph_BuyEntry = sc.Subgraph[0];
  SCSubgraphRef Subgraph_SellEntry = sc.Subgraph[2];
  SCSubgraphRef Subgraph_SimpMovAvg = sc.Subgraph[4];

  SCInputRef Input_Enabled = sc.Input[0];


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

    sc.GraphName = "Trading Example: 2 With Advanced Attached Orders";

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

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

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

    Input_Enabled.Name = "Enabled";
    Input_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. It demonstrates more advanced use of Attached Orders. A new entry cannot occur until the Targets and Stops have been filled or canceled. 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 = 5;
    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;

    return;
  }

  if (!Input_Enabled.GetYesNo())
    return;


  SCFloatArrayRef Last = sc.Close;


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


  // Create an s_SCNewOrder object.
  s_SCNewOrder NewOrder;
  NewOrder.OrderQuantity = 2;
  NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
  NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
  //NewOrder.Price1;// This needs to be set if using a limit or stop order type.

  //Define the Attached Orders to be attached to the main Market order
  //Target 1
  NewOrder.Target1Offset = 4*sc.TickSize;
  NewOrder.AttachedOrderTarget1Type = SCT_ORDERTYPE_LIMIT;

  //Target 2
  NewOrder.Target2Offset = 12*sc.TickSize;
  NewOrder.AttachedOrderTarget2Type = SCT_ORDERTYPE_LIMIT;

  //Common Stop
  NewOrder.StopAllOffset = 8*sc.TickSize;
  NewOrder.AttachedOrderStopAllType = SCT_ORDERTYPE_TRIGGERED_TRAILING_STOP_LIMIT_3_OFFSETS;
  NewOrder.TriggeredTrailStopTriggerPriceOffset=8*sc.TickSize;
  NewOrder.TriggeredTrailStopTrailPriceOffset=4*sc.TickSize;

  //Set up a move to breakeven action for the common stop. When Target 1 is filled, the order will be moved to breakeven +1
  NewOrder.MoveToBreakEven.Type = MOVETO_BE_ACTION_TYPE_OCO_GROUP_TRIGGERED;
  NewOrder.MoveToBreakEven.BreakEvenLevelOffsetInTicks = 1;
  NewOrder.MoveToBreakEven.TriggerOCOGroup= OCO_GROUP_1;


  // Buy when the last price crosses the moving average from below.
  if (sc.CrossOver(Last, Subgraph_SimpMovAvg) == CROSS_FROM_BOTTOM && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
  {
    int Result = (int)sc.BuyEntry(NewOrder);
    if (Result > 0) //If there has been a successful order entry, then draw an arrow at the low of the bar.
    {
      Subgraph_BuyEntry[sc.Index] = sc.Low[sc.Index];
    }
  }


  // Sell when the last price crosses the moving average from above.
  else if (sc.CrossOver(Last, Subgraph_SimpMovAvg) == CROSS_FROM_TOP && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
  {
    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.
    {
      Subgraph_SellEntry[sc.Index] = sc.High[sc.Index];

    }
  }
}