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

Using Indicator as Stop Loss

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

    Using Indicator as Stop Loss

    In Strategy Builder can I use an indicator for a stop loss or profit target? Example Stop Loss will be lowest low of X bar or highest high of X bars.
    Thanks.

    #2
    Hello Trader17,

    Thanks for the post.

    In the Stops and Targets section of the Strategy Builder you can click the "set" button and get the value of any indicator in the platform for the price of the stop/target. The MIN or MAX indicator could be used to get the high or low values from X bars ago.

    Please let me know if I can assist further.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      For Stops how do I differentiate between stops for longs and shorts? Like for a short stop I would use the MAX of X bars and for the long stop use the MIN of X bars.
      Thank you.

      Comment


        #4
        Hello Trader17,

        Thanks for the reply.

        You would need to unlock your code and program this. The strategy builder will only allow for one stop/target option so switching back and forth for long/short orders will need to be done in a fashion similar to the following pseudocode:

        Code:
        if(long)
        {
            SetStopLoss(CalculationMode.Price, MAX(5));
        }
        else if(short)
        {
            SetStopLoss(CalculationMode.Price, MIN(5));
        }
        Please let me know if you have any questions.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Thank you. Once the code is unlocked it cannot be locked again, correct? But can it be compiled with errors highlighted?
          Thank you.

          Comment


            #6
            Hello Trader17,

            Thanks for the follow up.

            That is correct. Once you unlock it, the code will be in a compilable state. Any manual code additions can be carefully debugged.
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_ChrisL View Post
              Hello Trader17,

              Thanks for the reply.

              You would need to unlock your code and program this. The strategy builder will only allow for one stop/target option so switching back and forth for long/short orders will need to be done in a fashion similar to the following pseudocode:

              Code:
              if(long)
              {
              SetStopLoss(CalculationMode.Price, MAX(5));
              }
              else if(short)
              {
              SetStopLoss(CalculationMode.Price, MIN(5));
              }
              Please let me know if you have any questions.
              I tried this in a strategy and I am getting error message while compiling that "long" and "short" are invalid expressions. Why? Below is the code I inserted.
              Thank you.


              if(long)
              {
              SetStopLoss(CalculationMode.Price, MIN(5));
              }
              else if(short)
              {
              SetStopLoss(CalculationMode.Price, MAX(5));
              }

              Comment


                #8
                Hello Trader17,

                Thank you for the reply.

                My example was in "pseudocode". A complete C# implementation would look something like the attached script. I used OnPositionUpdate() to know the moment the strategy becomes long/short and save the boolean variables in case I needed them at a later time.

                Please let me know if I can assist further.

                Attached Files
                Chris L.NinjaTrader Customer Service

                Comment


                  #9
                  Got it. Thank you very much.

                  Comment


                    #10
                    Originally posted by NinjaTrader_ChrisL View Post
                    Hello Trader17,

                    Thank you for the reply.

                    My example was in "pseudocode". A complete C# implementation would look something like the attached script. I used OnPositionUpdate() to know the moment the strategy becomes long/short and save the boolean variables in case I needed them at a later time.

                    Please let me know if I can assist further.

                    Thank you very much. MIN or MAX returns the lowest or highest price. What if I want to subtract/add a tick or two to this value? Is what I did below correct or needs to be done differently?

                    Thanks.

                    LongPosition = true; //Use LongPosition elsewhere if needed
                    SetStopLoss(CalculationMode.Price, MIN(5)[0] -1,tick);

                    Comment


                      #11
                      Hello Trader17,

                      Thanks for the reply.

                      You can add or subtract ticks like so:

                      SetStopLoss(CalculationMode.Price, MIN(5)[0] -TickSize,tick);

                      To subtract two ticks:

                      SetStopLoss(CalculationMode.Price, MIN(5)[0] -(TickSize*2),tick);

                      Please let me know if you have any questions.
                      Chris L.NinjaTrader Customer Service

                      Comment


                        #12
                        Thank you very much. Also, will SetTrailStop work with MIN and MAX? So instead of using MIN and MAX to place the fixed stop at the start of the trade I want it to move dynamically on each bar close. Can this be done in Strategy Builder? If not, would be a nice feature to have.
                        Thank you very much.
                        Last edited by Trader17; 10-24-2018, 11:50 AM.

                        Comment


                          #13
                          Could I replace SetStopLoss with SetTrailStop? Will that keep moving the stop dynamically with the close of each bar?
                          Thanks.

                          Comment


                            #14
                            Hello Trader17,

                            Thanks for the reply.

                            Yes, you may use a trailing stop instead of a static one.

                            Please see the help guide on how to use a trailing stop in your strategy:


                            Please let me know if I can assist further.
                            Chris L.NinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by NinjaTrader_ChrisL View Post
                              Hello Trader17,

                              Thank you for the reply.

                              My example was in "pseudocode". A complete C# implementation would look something like the attached script. I used OnPositionUpdate() to know the moment the strategy becomes long/short and save the boolean variables in case I needed them at a later time.

                              Please let me know if I can assist further.
                              I did not use the bool but did it this way instead. Is it correct?

                              protected override void OnPositionUpdate(Cbi.Position position, double averagePrice, int quantity, Cbi.MarketPosition marketPosition)
                              {
                              if (position.MarketPosition == MarketPosition.Long)
                              {
                              LongPosition = true; //Use LongPosition elsewhere if needed
                              SetStopLoss(CalculationMode.Price, MIN(5)[0]);
                              }
                              else if (position.MarketPosition == MarketPosition.Short)
                              {
                              ShortPosition = true; //Use ShortPosition elsewhere if needed
                              SetStopLoss(CalculationMode.Price, MAX(5)[0]);
                              }
                              }

                              Thank you.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by AveryFlynn, Today, 04:57 AM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_Erick  
                              Started by Max238, Today, 01:28 AM
                              4 responses
                              37 views
                              0 likes
                              Last Post Max238
                              by Max238
                               
                              Started by r68cervera, Today, 05:29 AM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by geddyisodin, Today, 05:20 AM
                              1 response
                              11 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by timko, Today, 06:45 AM
                              2 responses
                              14 views
                              0 likes
                              Last Post NinjaTrader_ChristopherJ  
                              Working...
                              X