Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Adding trailing stop to secondary data series

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Adding trailing stop to secondary data series

    Hello,

    I have a relatively simple strategy that I am backtesting, and would like to produce the most accurate results using tick data exits.

    I understand it is necessary to add a secondary data series for intrabar granularity, Im just having trouble doing so.

    My primary data series is NQ futures 400 tick bars.
    My secondary data series is NQ futures 1 tick data.

    If possible, I would like to enter conditionally on the primary data series, so once a 400 tick bar closes, meeting my conditions, I will enter long on the open of the following bar. This is a non issue.

    However, when trailing out of my position, I would like to set my trail stop according to the secondary data series, tick data, in order to test these stops more precisely as it would run real time.

    I am aware that EnterLong and SetTrailStop do not work for secondary data series.

    My question is how would I add this trailstop command to the secondary data series, if BarsInProgress == 1, while keeping everything else the same, still entering conditionally if BarsInProgress == 0 ?

    If that is not possible, could you direct me where to find the position entry and exit commands for secondary data series, since the usual EnterLong and SetTrailStop only work for primary?

    I will post the code below for further detail:


    IsInstantiatedOnEachOptimizationIteration = true;
    }
    else if (State == State.Configure)
    {
    AddDataSeries("NQ 12-18", Data.BarsPeriodType.Tick, 1, Data.MarketDataType.Last);

    }
    else if (State.DataLoaded);
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 2)
    return;

    // Set 3
    if (((R - (Low[0])) > (2.3 * (V - R)) )
    && ((R - (Low[0])) > (2.7 * (High[0] - (V))))
    && ((High[0] - (Low[0])) > 2.5)
    && (Low[0] < Low[1])
    && (Low[0] < Low[2])
    && (Low[0] < Low[3])
    && (Low[0] < Low[4])
    && (Low[0] < Low[5]))
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), @"LONG");
    SetStopLoss(@"LONG", CalculationMode.Ticks, 13, true);
    SetProfitTarget(@"LONG", CalculationMode.Ticks, 19);
    }

    // Set 4
    if (((High[0] - (V)) > (2.3 * (V - R)))
    && ((High[0] - (V)) > (2.7 * (R - (Low[0]))))
    && ((High[0] - (Low[0])) > 2.5)
    && (High[0] > High[1])
    && (High[0] > High[2])
    && (High[0] > High[3])
    && (High[0] > High[4])
    && (High[0] > High[5]))
    {
    EnterShort(Convert.ToInt32(DefaultQuantity), @"SHORT");
    SetProfitTarget(@"SHORT", CalculationMode.Ticks, 19)
    SetStopLoss(@"SHORT", CalculationMode.Ticks, 13, true);
    }

    }
    }
    }


    This works fine, I would just like to get rid of my SetProfitTarget and SetStopLoss for both long and short conditions, and instead add a trail stop to the secondary data series to serve as an exit for both.
    Last edited by murraym324; 11-01-2018, 07:36 PM.

    #2
    Hello murraym324,

    Thanks for your post.

    You can create your own trailing stop in code that can be adjusted on every tick of the secondary series.
    To accomplish this, you would need to segment your code by BarsInProgress and use the advanced order method overloads.
    In general terms it would be something like:

    if (BarsInProgress == 0)
    {
    if (your entry conditions long)
    {
    EnterLong(0,1, "Long")
    }

    // same for short side
    }

    if (BarsInProgress == 1)
    {
    if (Positions[0].MarketPosition == MarketPosition.Long)
    {
    // your code to first set an initial stop using ExitLongStopMarket(0, true, 1, Positions[0]AveragePrice - 20 * TickSize, "Long","") (or limit if you prefer)
    }
    //same concept for the short side

    You would use bool values so that you only set the initial stop once then your code would need to compare the current price (Closes[0][0] to Positions[0].AveragePrice - 20*TickSize to decide if and when to adjust the stop order.


    Below is detailed level of code to help you see this:

    The control bool variables are firstTime (initially true) and setBE(initially false). These would need to be reset before placing the next entry order.

    An initial stop is set first, then when price is at entry price plus 10 ticks the stop is moved to the entry price (Breakeven), next the stop is moved up 1 tick for every 5 ticks of profit.

    I've included print statements to help see what is happening in each section.

    if (BarsInProgress == 1 && Positions[0].MarketPosition == MarketPosition.Long)
    {
    if (firstTime)
    {
    Print (Times[0][0]+" firstTime"+" Init stop at: "+(Positions[0].AveragePrice - 20 * TickSize).ToString());
    ExitLongStopMarket(0, true, 1, Positions[0].AveragePrice - 20 * TickSize, "","Long");
    firstTime = false;
    }

    if (!firstTime && !setBE)
    {
    if (Closes[0][0] >= Positions[0].AveragePrice + 10 * TickSize);
    {
    Print (Times[0][0]+ " set BE");
    ExitLongStopMarket(0, true, 1, Positions[0].AveragePrice, "","Long");
    setBE = true;
    stopLevel = Positions[0].AveragePrice;
    }
    }
    if (!firstTime && setBE)
    {
    if (Closes[0][0] > stopLevel + 5 * TickSize)
    {
    Print (Times[0][0] + " adjusting stop to: "+stopLevel);
    stopLevel = stopLevel + 1 * TickSize; // increment 5 to 1
    ExitLongStopMarket(0, true, 1, stopLevel, "","Long");
    }
    }
    }


    Please note this example code is provided as is and has not been tested and is intended as an educational example only.

    References:



    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thank you for that insight.. Your solution worked smoothly. Appreciate your time.
      Last edited by murraym324; 11-02-2018, 10:37 AM.

      Comment


        #4
        Hello,

        Im having some troubles with this script. When backtesting, selecting NQ 3000 tick bars as the primary series, I am seeing the following discrepancies.

        First, the maximum loss is $50. This is not possible considering my initial stop is placed 20 ticks below entry, which is $100 on NQ.
        Additionally, the avg time in market in -9.63 min. Im not sure how this can be a negative value.

        Ive made sure all of my entry logic is placed under BarsInProgress == 0
        While my exit strategy is under BarsInProgress == 1

        Ive attached the script for reference.
        Attached Files

        Comment


          #5
          Hello

          Thanks for your reply.

          It appears you are working with my colleague Alan on the very same subject on https://ninjatrader.com/support/foru...ng-on-strategy Please do not engage us on the same subject in multiple threads to avoid confusion, thanks for your understanding.
          Paul H.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by gm7_forum, Today, 05:51 PM
          0 responses
          1 view
          0 likes
          Last Post gm7_forum  
          Started by cre8able, Today, 03:20 PM
          1 response
          9 views
          0 likes
          Last Post cre8able  
          Started by fiddich, Today, 05:25 PM
          0 responses
          3 views
          0 likes
          Last Post fiddich
          by fiddich
           
          Started by gemify, 11-11-2022, 11:52 AM
          6 responses
          804 views
          2 likes
          Last Post ultls
          by ultls
           
          Started by ScottWalsh, Today, 04:52 PM
          0 responses
          4 views
          0 likes
          Last Post ScottWalsh  
          Working...
          X