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

Setting Stop Loss to Previous Swing Low

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

    Setting Stop Loss to Previous Swing Low

    Hi,

    Can anyone help me with setting up my stop losses and targets? I would like for my stop loss to be equal to the previous swing low for for longs and vice versa for shorts. Also, how can I code my target to be equal to the same distance from entry price to stop loss (1:1)? I'm still trying to figure out the programming aspect...

    Thank you in advance!

    #2
    Hello dakid4getz,

    Thank you for your post.

    Are you going to be using the Strategy Wizard for this or custom programming?
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Hi Cal,

      It's for a custom strategy.

      Comment


        #4
        Hello dakid4getz,

        Thank you for your response.

        Below is an example for a long position:
        Code:
                    if(Position.MarketPosition == MarketPosition.Long)
        			{
        				SetStopLoss(CalculationMode.Price, Swing(5).SwingLow[0]);
        				SetProfitTarget(CalculationMode.Price, (Position.AvgPrice + (Position.AvgPrice - Swing(5).SwingLow[0])));
        			}
        Please let me know if you have any questions.

        Comment


          #5
          Does 5 in swing(5) indicate the strength of the swing?

          Comment


            #6
            Would the swing stop loss be written like this? And would it go where the stop loss commands would usually go?

            {

            if(Position.MarketPosition == MarketPosition.Long)
            {
            SetStopLoss(CalculationMode.Price, Swing(5).SwingLow[0]);
            }
            if(Position.MarketPosition == MarketPosition.Short)
            {
            SetStopLoss(CalculationMode.Price, Swing(5).SwingHigh[0]);
            }
            CalculateOnBarClose = true;
            }

            Comment


              #7
              Hello brucelevy,

              You are correct on both counts.

              Comment


                #8
                How would you call the atm to modify stops to swings?

                Comment


                  #9
                  Hello brucelevy,

                  Thank you for your post.

                  You would use the ATM Strategy methods and functions. Specifically AtmStrategyChangeStopTarget(): http://ninjatrader.com/support/helpG...stoptarget.htm

                  Comment


                    #10
                    Where is the AtmIdValue so I can differentiate between the long and short etc? Trying to piece this together...

                    {
                    if (orderId.Length == 0 && atmStrategyId.Length == 0)

                    atmStrategyId = GetAtmStrategyUniqueId();
                    orderId = GetAtmStrategyUniqueId();
                    AtmStrategyCreate(Cbi.OrderAction.Sell, OrderType.Market, 0, 0,
                    TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId);
                    }

                    {
                    AtmStrategyChangeStopTargetCalculationMode.Price, Swing(5).SwingLow[0]), "", "AtmIdValue");
                    }

                    Comment


                      #11
                      Hello brucelevy,

                      The "AtmIdValue" is the unique identifier for the ATM strategy.

                      In the case of the code you have provided, you would want to use the atmStrategyId variable as this is assigned to the atmStrategyId in your created ATM Strategy in your code.

                      If you are using multiple ATM Strategies in your strategy, I would suggest keeping track of the strategy ID for each ATM Strategy so you'll know what ATM Strategy is assigned to what strategy ID.
                      Zachary G.NinjaTrader Customer Service

                      Comment


                        #12
                        How can I specify which swing high or low I am referring to? What if I want the close of current price to be above the prior swing high?

                        Close[0] > Swing(5).SwingHigh[1]
                        Does SwingHigh[1] mean 1 bar back, or 1 SwingHigh back? If 1 bar back then how can I know how many bars back to look for to encompass that prior swing high.

                        Comment


                          #13
                          if you do it like this. then the [1] would be 1 bar back

                          but u can use
                          Swing(IDataSeries input, int strength).SwingHighBar(int barsAgo, int instance, int lookBackPeriod)


                          instance in this case would be how many swings to look back
                          so
                          SwingHighBar(0, 1, 10)

                          would be look from candle 0 to candle 10 and search the first morst recent swing.

                          while
                          SwingHighBar(0, 2, 10)

                          would look for the second most recent swing.

                          check out this for more detail

                          Comment


                            #14
                            Hello brucelevy,

                            SwingHigh[1] would return the Swing indicator value of the referenced bar (this being the indicator value from one bar ago).

                            What BigRo has mentioned would be the syntax you would want to use to return the bars ago value of the most recent swing high.

                            Please, let us know if we may be of further assistance.
                            Zachary G.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Christopher_R, Today, 12:29 AM
                            0 responses
                            6 views
                            0 likes
                            Last Post Christopher_R  
                            Started by sidlercom80, 10-28-2023, 08:49 AM
                            166 responses
                            2,235 views
                            0 likes
                            Last Post sidlercom80  
                            Started by thread, Yesterday, 11:58 PM
                            0 responses
                            3 views
                            0 likes
                            Last Post thread
                            by thread
                             
                            Started by jclose, Yesterday, 09:37 PM
                            0 responses
                            7 views
                            0 likes
                            Last Post jclose
                            by jclose
                             
                            Started by WeyldFalcon, 08-07-2020, 06:13 AM
                            10 responses
                            1,415 views
                            0 likes
                            Last Post Traderontheroad  
                            Working...
                            X