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

How to move through stop levels using Incremental Operators ++ or --

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

    How to move through stop levels using Incremental Operators ++ or --

    My brain has popped trying to figure this out so hoping one of the many kind souls here can help to point the way...

    I use fib retracement tool levels in my strategy to move stops via SetStop() like so:

    Code:
     if (CrossAbove(Close, FiftyLevel, 1))
                                        {
                                        
                                        SetStopLoss("Long1", CalculationMode.Price, (ThirtyEightLevel - 2 * TickSize), false);
                                        SetStopLoss("Long2", CalculationMode.Price, (ThirtyEightLevel - 2 * TickSize), false);
                                        }​
    however I don't go level by level (i.e. the next movement might be once 100 level is crossed, move stop to 61.8 level ) but would like to incorporate the option moving stops by iterating thru each level into my script. I'm guessing using the ++ operator would do this (and -- operator for shorts?) but am stuck how to apply?

    Any ideas or direction anyone could point?

    Many thanks in advance!

    #2
    Hello swjake,

    You are wanting to loop through the levels in a fibonacci retracement?

    Are you modeling your code from the sample code in the help guide?

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi ChelseaB,

      I loop thru the levels of my fib retracement towards the beginning of OBU to get the price levels to use for entries, targets and stops and that works well for me. Here's the loop:
      Code:
       DrawingTools.FibonacciRetracements myFib = draw as DrawingTools.FibonacciRetracements;
                              if (draw.Tag == "myFib")
                              {
                                  foreach (PriceLevel p in myFib.PriceLevels)
                                  {
                                      
                                  if (p.Value == 261.8)
                                  TwoSixtyOneLevel = p.GetPrice(myFib.StartAnchor.Price, myFib.EndAnchor.Price - myFib.StartAnchor.Price, true);
                                  if (p.Value == 230.90)
                                  TwoThirtyLevel = p.GetPrice(myFib.StartAnchor.Price, myFib.EndAnchor.Price - myFib.StartAnchor.Price, true);    
                                  if (p.Value == 200)
                                  TwoHundredLevel = p.GetPrice(myFib.StartAnchor.Price, myFib.EndAnchor.Price - myFib.StartAnchor.Price, true);
                                  if (p.Value == 180.9)
                                  OneEightyLevel = p.GetPrice(myFib.StartAnchor.Price, myFib.EndAnchor.Price - myFib.StartAnchor.Price, true);
                                  if (p.Value == 161.8)
                                  OneSixtyOneLevel = p.GetPrice(myFib.StartAnchor.Price, myFib.EndAnchor.Price - myFib.StartAnchor.Price, true);    
                                  if (p.Value == 138.2)
                                  OneThirtyEightLevel = p.GetPrice(myFib.StartAnchor.Price, myFib.EndAnchor.Price - myFib.StartAnchor.Price, true);
                                  if (p.Value == 119.1)
                                  OneNineteenLevel = p.GetPrice(myFib.StartAnchor.Price, myFib.EndAnchor.Price - myFib.StartAnchor.Price, true);
                                  if (p.Value == 128.65)
                                  OneTwentyEightLevel = p.GetPrice(myFib.StartAnchor.Price, myFib.EndAnchor.Price - myFib.StartAnchor.Price, true);
                                  if (p.Value == 100)
                                  OneHundredLevel = p.GetPrice(myFib.StartAnchor.Price, myFib.EndAnchor.Price - myFib.StartAnchor.Price, true);
                                  if (p.Value == 19.1)
                                  NineteenLevel = p.GetPrice(myFib.StartAnchor.Price, myFib.EndAnchor.Price - myFib.StartAnchor.Price, true);
                                  if (p.Value == 80.9)
                                  EightyLevel = p.GetPrice(myFib.StartAnchor.Price, myFib.EndAnchor.Price - myFib.StartAnchor.Price, true);
                                  if (p.Value == 71.35)
                                  SeventyOneLevel = p.GetPrice(myFib.StartAnchor.Price, myFib.EndAnchor.Price - myFib.StartAnchor.Price, true);
                                  if (p.Value == 38.2)
                                  ThirtyEightLevel = p.GetPrice(myFib.StartAnchor.Price, myFib.EndAnchor.Price - myFib.StartAnchor.Price, true);
                                  if (p.Value == 61.8)
                                  SixtyOneLevel = p.GetPrice(myFib.StartAnchor.Price, myFib.EndAnchor.Price - myFib.StartAnchor.Price, true);
                                  if (p.Value == 28.65)
                                  TwentyEightLevel = p.GetPrice(myFib.StartAnchor.Price, myFib.EndAnchor.Price - myFib.StartAnchor.Price, true);
                                  if (p.Value == 23.60)
                                  TwentyThreeLevel = p.GetPrice(myFib.StartAnchor.Price, myFib.EndAnchor.Price - myFib.StartAnchor.Price, true);
                                  if (p.Value == 0)
                                  ZeroLevel = p.GetPrice(myFib.StartAnchor.Price, myFib.EndAnchor.Price - myFib.StartAnchor.Price, true);
                                  if (p.Value == 50)
                                  FiftyLevel = p.GetPrice(myFib.StartAnchor.Price, myFib.EndAnchor.Price - myFib.StartAnchor.Price, true);
                                   
                                   }
                                 }
      ​

      my issue is I'm not sure how to implement incremental operators ++/-- to move my stops thru the levels one by one, so if price crosses one level, the stop is automatically trailed up to the level below/above it, if that makes sense. I'm not sure of the syntax. Instead of writing out each level in code, to shorten it using ++ or -- operators.

      Thanks Chelsea!





      Comment


        #4
        Hello swjake,

        I'm not quite sure what you are trying to increment..

        Are you trying to increment the index of the accessed element in the collection?

        For example if you were using myFib.PriceLevels[5] (and myFib.PriceLevels.Count is greater than 6), and you want to increment to index [6]?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi Chelsea,

          Thanks for the quick reply. What I'm trying to do is give myself (or user) the option to choose how to implement stops when the strategy is enabled, which I had planned to use with a bool.

          A) use current SetStop logic, or B) strictly moving SetStop level by level (not skipping some levels as in option A), keeping SetStop one Fib level behind price

          So if long entry is 38.2 and initial SetStop is 23.6, once price crosses 50, move stop up one level (38.2); once price crosses 61.8, move stop to 50, etc. Keeping the stop trailing one Fib level behind price.

          I thought I could set a variable like MoveStop++ to increment level by level but am lost....I don't know the proper syntax to do so, only know how write out a if statement for each stop level as in post #1 above.

          Hope you can make sense of that

          Comment


            #6
            Hello swjake,

            "I thought I could set a variable like MoveStop++ to increment level by level but am lost"

            myIndex = 0; // myIndex is 0 and when used as the index for the PriceLevels collection will get the first price level info

            Print(myFib.PriceLevels[myIndex].GetPrice(myFib.StartAnchor.Price, myFib.EndAnchor.Price - myFib.StartAnchor.Price, false)));

            myIndex++; // myIndex has been incremented to 1 and when used as the index for the PriceLevels collection will get the second price level info
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Uregon, Today, 03:44 AM
            0 responses
            2 views
            0 likes
            Last Post Uregon
            by Uregon
             
            Started by CyberNate, 11-17-2020, 08:15 PM
            2 responses
            426 views
            0 likes
            Last Post nurpalgo  
            Started by sdauteuil, 09-23-2021, 10:16 AM
            7 responses
            1,247 views
            0 likes
            Last Post marcus2300  
            Started by sofortune, 05-18-2024, 11:48 AM
            2 responses
            34 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Started by Zach55, 02-19-2024, 07:22 PM
            2 responses
            67 views
            0 likes
            Last Post lbadisa1  
            Working...
            X