Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Saving a reference point

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

    Saving a reference point

    Hello - working on building a trailing stop. How would I code the following, if unrealized P&L > 5* ATR for 10 periods then take the highest point of that bar and sell if unrealized P&L dips 2 ATRs

    Comments from Ninja and other traders welcome

    Thank you

    #2
    Hi, there are some custom trailing stop codes posted to our sharing that you may find useful in your project - http://www.ninjatrader-support2.com/...ng+stop&desc=1

    To store a value, just check for your condition and assign a variable value then -

    Code:
     
    if (myCondition)
    myVariable = High[0];
    Then you can reference this stored value later for comparisons to execute your stops.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Thank you - can I build this into my strategy or do i need to create an indicator? Based on your suggestion here is the code im writing

      // Setting the High Point
      if(Position.GetProfitLoss(Close[0], PerformanceUnit.Points)>(5*ATR(10)[0]))

      {
      myVariable = High[
      0];
      }

      "myVariable" is flagged as an error, errors says that the name does not exist in the current context

      Comment


        #4
        This means you did not define the variable, you can either do this locally with -

        Code:
        if(Position.GetProfitLoss(Close[0], PerformanceUnit.Points)>(5*ATR(10)[0]))
        
        {
        double myVariable = High[0];
        }


        Or define it under the Variables section of the code -

        Code:
         
        private double myVariable;
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Thank you I think this will help - I was able to properly assign the variable, however how do I use it in further code. Example below

          // Moving the trailing stop
          if(Close[0] ==(myVariable + (3*ATR(10)[0])))


          {
          ExitLong(
          "trail","");
          ExitShort(
          "trail","");

          }
          Last edited by titleistbb22; 11-20-2009, 05:14 PM.

          Comment


            #6
            Hello - any thoughts on my response?

            Comment


              #7
              Originally posted by titleistbb22 View Post
              Thank you I think this will help - I was able to properly assign the variable, however how do I use it in further code. Example below

              // Moving the trailing stop
              if(Close[0] ==(myVariable + (3*ATR(10)[0])))
              {
              ExitLong(
              "trail","");
              ExitShort(
              "trail","");
              }
              Not sure what the question is, but this is a correct usage of your new variable myVariable. The if condition translated says this: if the close of the current bar is equal to the value generated by adding myVariable and 3 times the current 10 period ATR value, then exit the entry with a blank signal name and call the exit "trail".

              A quick note: depending on how you set myVariable and the ATR value, it could very rarely equal the close of the current bar, thus not triggering the exits.
              AustinNinjaTrader Customer Service

              Comment


                #8
                thank you for the response. I get an error when using this code, here is exactly what I have. The error is 'myVariable' does not exist in the current context

                // Setting the High Point
                if(Position.GetProfitLoss(Close[0], PerformanceUnit.Points)>(5*ATR(10)[0]))
                {
                double myVariable = High[0];
                }

                // Moving trailing stop
                if (Close[0] == (myVariable + (3*ATR(10)[0])))

                {
                ExitLong (
                "trail","");
                ExitShort(
                "trail","");
                }

                Comment


                  #9
                  Please try this -

                  Code:
                   
                  // Setting the High Point
                   
                  double myVariable;
                   
                  if(Position.GetProfitLoss(Close[0], PerformanceUnit.Points)>(5*ATR(10)[0]))
                  {
                  myVariable = High[0];
                  }
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    getting a new error message - 'use of unassigned local variable 'myVariable' Here is the exact code Im using

                    // Setting the High Point

                    double myVariable;

                    if(Position.GetProfitLoss(Close[0], PerformanceUnit.Points)>(5*ATR(10)[0]))
                    {
                    myVariable = High[
                    0];
                    }

                    // Moving trailing stop
                    if (Close[0] == (myVariable + (3*ATR(10)[0])))
                    {
                    ExitLong (
                    "trail","");
                    ExitShort(
                    "trail","");
                    }

                    The error message identifies 'myVariable' in the //Moving trailing stop section as the error.

                    Im writing this code all in the OnBarUpdate area. Does it belong somewhere else? thx

                    Comment


                      #11
                      Then please remove the local definition (double myVariable) and add this to the 'Variables' section of the code -

                      Code:
                       
                      private double myVariable;
                      Then check if you can compile without the local assignment error then.
                      BertrandNinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by algospoke, Yesterday, 06:40 PM
                      2 responses
                      22 views
                      0 likes
                      Last Post algospoke  
                      Started by ghoul, Today, 06:02 PM
                      3 responses
                      14 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Started by jeronymite, 04-12-2024, 04:26 PM
                      3 responses
                      45 views
                      0 likes
                      Last Post jeronymite  
                      Started by Barry Milan, Yesterday, 10:35 PM
                      7 responses
                      21 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Started by AttiM, 02-14-2024, 05:20 PM
                      10 responses
                      181 views
                      0 likes
                      Last Post jeronymite  
                      Working...
                      X