Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Creating Own Trailing

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

    Creating Own Trailing

    Hello NT-Team,

    I want to create my own Trailing that is able to trail on the 1-minute data, while the Strategy is running on the 30-minute frame.

    I had both periodes added in the Initialize() block. And I know how to access it (with BarsInProgress)

    But I don't know how to update the SL of a running Order. It doesn't work with SetStopLoss (I think it sets only the SL for future orders?!).

    I am using the "Managed Approach"

    Thanks

    #2
    Hello Ludwig,

    Thank you for your post.

    The BarsInProgress is accessed in the following manner:
    • The Primary bar series (the instrument you select for the strategy on the Strategies tab of the Control Center or in the Chart) is accessed as "BarsInProgress == 0"
    • The Secondary bar (the first Add()) series is referenced as "BarsInProgress == 1"
    • The Tertiary bar (the second Add()) series is referenced by "BarsInProgress == 2"
    • etc.

    For an example on using Multiple Instruments in a Strategy please go to Tools > Edit NinjaScript > Strategy > SampleMultiInstrument > OK.

    Concerning the SetStopLoss(), is this method called in the Initialize() method or the OnBarUpdate() method?
    Should you wish to dynamically change the stop loss price while in an open position you must call the SetStopLoss() from the OnBarUpdate() method.
    For information on SetStopLoss() please visit the following link: http://www.ninjatrader.com/support/h...etstoploss.htm

    Please let me know if the SetStopLoss() method is called from within the OnBarUpdate() method and you are still unable to dynamically update the stop loss.
    Last edited by NinjaTrader_PatrickH; 08-09-2012, 01:46 PM.

    Comment


      #3
      Thanks Patrick,

      the SetStoppLoss() is called first in the Initialize() to set the first SL and also in the OnBarUpdate() to change the SL in an existing open position.

      Does SetStopLoss() generally effect on existing Positions?

      And yes, I am still unable to update the SL on my existing position.

      Comment


        #4
        Hello Ludwig,

        Thank you for your response.

        The SetStopLoss() will only be called for positions in your strategy. If this is a position outside the strategy then unfortunately the SetStopLoss() method will not update the Stop Loss for the existing position.

        If you are updating the SetStopLoss() method for your strategy position, please advise what conditions you are using in the OnBarUpdate() method to update the SetStopLoss() method.

        For an example of modifying the price of Stop Loss and Profit Target orders please visit the following link: http://www.ninjatrader.com/support/f...ead.php?t=3222

        I look forward to assisting you further.

        Comment


          #5
          Here is my Code:

          Code:
          protected override void Initialize()
                  {
          			Add(PeriodType.Minute, 30);
          			Add(PeriodType.Minute, 1);  
          						
          			CalculateOnBarClose = true;
          						
          			SetStopLoss(CalculationMode.Ticks, Trail);
          			
          			EntriesPerDirection = 3;
          			EntryHandling = EntryHandling.AllEntries; 
                  }
          
          .
          .
          .
          
           protected override void OnBarUpdate()
                  {			
          	
          			// --- Signal -----------------------------------------------------------------------------------
          .
          .
          .
          				
          			// --- Trailing ---------------------------------------------------
          			if(BarsInProgress == 2)		
          			{
          				if (Position.MarketPosition == MarketPosition.Flat)
          				{
          					SetStopLoss(CalculationMode.Ticks, Trail);
          				}
          				else if (Position.MarketPosition == MarketPosition.Long)
          				{
          					lastStopLevel = Math.Max(Low[1], lastStopLevel);
          					if (Low[0] > lastStopLevel)	
          					{
          						SetStopLoss("Long Candle",CalculationMode.Ticks, Low[0] - Trail,false);
          						DrawDot(CurrentBar.ToString(),false,0,Low[0]-Trail*TickSize,Color.Yellow);
          					}	
          				}
          				else if (Position.MarketPosition == MarketPosition.Short)
          				{
          					lastStopLevel = Math.Min(High[1], lastStopLevel);
          					if (High[0] < lastStopLevel)		
          					{
          						SetStopLoss("Short Candle",CalculationMode.Ticks, High[0] + Trail,false);
          						DrawDot(CurrentBar.ToString(),false,0,High[0]+Trail*TickSize,Color.Yellow);
          					}
          				} 
          			}  
          			
                  }

          Comment


            #6
            SetStopLoss("Long Candle",CalculationMode.Ticks, Low[0] - Trail,false);
            according your marketposition.flat condition, I assume trail is defined in ticks, however in the long condition stoploss, which is also in ticks, you set it as Low[0]-Trail, which is a price value minus a tick value, which will be a lot of ticks !
            You should set the stoploss as CalculationMode.Price to Low[0]-Trail*TickSize

            Marco

            Comment


              #7
              Hello Ludwig,

              Marco is correct on this matter.

              The conflict is that the CalculationMode is Ticks yet a price value is being used:
              SetStopLoss('Long Candle',CalculationMode.Ticks, Low[0] - Trail,false);
              SetStopLoss('Short Candle',CalculationMode.Ticks, High[0] + Trail,false);

              You will need to change to CalculationMode.Price as Marco details or use a tick based double for the value when using CalculationMode.Ticks.

              Please let me know if I may be of further assistance.

              Comment


                #8
                Thanks for your help,

                I have another question. I am in the Backtester, if I set the Data series Type to Minute and the Value to 30, is the Stategy able to access 1-minute Data?

                I added the 1-minute date in the Initialize().

                Comment


                  #9
                  Yes Ludwig, with data Add()ed you can then also programmatically access it in the strategy script - however it would need to be coded to take advantage of that. Just calling the Add() would not be enough.

                  For a complete working example, you can check into our SampleMultiTime strategy shipped per default with NT.

                  Comment


                    #10
                    Ludwig,

                    I saw this yesterday but forgot to mention; you reported you run your strategy on a 30min chart and calculate the trailing stop from a 1min dataseries. In the initialize section you add both the 1min and 30 min dataseries.

                    In my understanding this is overkill; if you run the strategy on a 30min chart,this 30min dataseries will automatically be your primary dataseries, so no need to add it again in initialize. You should only add the 1min dataseries in initialize

                    So then 30min onbarUpdate will be triggered in BarsInProgress == 0, and 1min will be in BarsInProgress == 1

                    please correct me if I'm wrong.

                    Comment


                      #11
                      Thanks for your help.

                      I added only the 1-minute inside the strategy. sorry ;-)
                      And I use the main signal inside a BarsInProgress == 0 block, for the 30-minute candles.
                      The trailing works inside the BarsInProgress == 1 block, for the 1-minute TF.

                      The Backtest inside the StrategyAnalyzer is set to 30-minute timeframe. Is the Strategy generally able to use 1-minute candles, if the backtest is set to 30-min. tf?

                      Comment


                        #12
                        Hello Ludwig,

                        I would like to provide some additional information here.

                        You can in fact use another bar series in Backtests. You can view an example of using an intra-bar granularity in your strategy at the following link: http://www.ninjatrader.com/support/f...ead.php?t=6652

                        Please let me know if I may be of further assistance.

                        Comment


                          #13
                          I added only the 1-minute inside the strategy. sorry ;-)
                          And I use the main signal inside a BarsInProgress == 0 block, for the 30-minute candles.
                          The trailing works inside the BarsInProgress == 1 block, for the 1-minute TF
                          100% correct.

                          The Backtest inside the StrategyAnalyzer is set to 30-minute timeframe. Is the Strategy generally able to use 1-minute candles, if the backtest is set to 30-min. tf?
                          Yes, it is. However to test that sort of things I always put some print statement in the code that triggers at specific events.To test if your startegy is using the 1min bars, you could put the print code below inside the BarsInProgress == 1 block, open the Output Window, and check if there is was print every minute after running the backtest.
                          Code:
                           
                          PrintWithTimeStamp("1-min bar update");

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                          0 responses
                          661 views
                          0 likes
                          Last Post Geovanny Suaza  
                          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                          0 responses
                          375 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by Mindset, 02-09-2026, 11:44 AM
                          0 responses
                          110 views
                          0 likes
                          Last Post Mindset
                          by Mindset
                           
                          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                          0 responses
                          574 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by RFrosty, 01-28-2026, 06:49 PM
                          0 responses
                          580 views
                          1 like
                          Last Post RFrosty
                          by RFrosty
                           
                          Working...
                          X