Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Profit target of previous bar high/low

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

    Profit target of previous bar high/low

    Hi, i'm trying to set my stoploss and target to the sum of the high and low of the previous bar or any N bars ago. I think I have it right, but when I backtest it, it enters and exit at the same value.

    What am I missing?

    See attached picture.

    Code is below:

    SetProfitTarget("L1", CalculationMode.Ticks, Variable0);
    SetStopLoss("L1", CalculationMode.Ticks, Variable0, false);

    CalculateOnBarClose = true;
    }


    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (Close[0] > EMA(14)[0])
    {
    EnterLong(DefaultQuantity, "L1");
    Variable0 = High[1] + Low[1] * TickSize;
    }
    Attached Files

    #2
    John, what that be meant to be a price based or tick based value then you're trying to set the stop / target at? I guess I'm unsure why you want them at the identical values. Anyways for a dynamic calculated value you should place the calls in OnBarUpdate() before you actually enter your position and not in the Initialize() - that place would only be good if you for example always wanted a static 10 tick stop or target and updating it based on bar values / market conditions.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Bertrand, thanks for the reply. I think my logic was incorrect, but I was able to get my logic working for ProfitTarget, but StopLoss still not working when I run backtest.

      Example: Euro:
      High[1] = 1.400;
      Low[1] = 1.200;
      Close[1]= 1.300

      ProfitTarget: (High[1] - Low[1]) + Close[1] = 0.200 + 1.300 = 1.500
      StopLoss: (High[1] - Low[1]) - Close[1] = 0.200 - 1.300 = 1.100


      if (CrossAbove(Close, EMA(14), 1))
      {
      Variable0 = (High[1] - Low[1]) + (Close[1]);
      Variable1 = (High[1] - Low[1]) - (Close[1]);
      SetProfitTarget("L1", CalculationMode.Price, Variable0);
      SetStopLoss("L1", CalculationMode.Price, Variable1, false);
      EnterLong(DefaultQuantity, "L1");
      }

      Comment


        #4
        I've been using the Print() to see what is happening and I also tried using the ticksize:

        if (CrossAbove(Close, EMA(14), 1))
        {
        Variable0 = (High[1] - Low[1]);
        SetProfitTarget("L1", CalculationMode.Ticks, Variable0);
        SetStopLoss("L1", CalculationMode.Ticks, Variable0, false);
        EnterLong(DefaultQuantity, "L1");
        }

        Printing (High[1] - Low[1]) sometimes give me the value below.

        0.8866 - 0.868 = 0.0186000000000001


        Any help is appreciated.

        Comment


          #5
          .2 - 1.30 is -1.10... That's an invalid price?...

          try it backwards .. 1.30 - .2 ...
          or use an ABS function on the result...



          Originally posted by john_robertson00 View Post
          Bertrand, thanks for the reply. I think my logic was incorrect, but I was able to get my logic working for ProfitTarget, but StopLoss still not working when I run backtest.

          Example: Euro:
          High[1] = 1.400;
          Low[1] = 1.200;
          Close[1]= 1.300

          ProfitTarget: (High[1] - Low[1]) + Close[1] = 0.200 + 1.300 = 1.500
          StopLoss: (High[1] - Low[1]) - Close[1] = 0.200 - 1.300 = 1.100


          if (CrossAbove(Close, EMA(14), 1))
          {
          Variable0 = (High[1] - Low[1]) + (Close[1]);
          Variable1 = (High[1] - Low[1]) - (Close[1]);
          SetProfitTarget("L1", CalculationMode.Price, Variable0);
          SetStopLoss("L1", CalculationMode.Price, Variable1, false);
          EnterLong(DefaultQuantity, "L1");
          }

          Comment


            #6
            sledge, thank you for the reply. I used both your suggestions to make it work.

            Math.Abs(Variable0 = (High[1] - Low[1]) + (Close[1]));
            Math.Abs(Variable1 = (Close[1] - (High[1] - Low[1]));
            SetProfitTarget("L1", CalculationMode.Price, Variable0);
            SetStopLoss("L1", CalculationMode.Price, Variable1, false);
            EnterLong(DefaultQuantity, "L1");

            Now, How can I add 5 ticks to my StopLoss?
            (Close[1] - (High[1] - Low[1]) + 10 Ticks <---- I'm not sure how to do this.

            Any help is appreciated.

            Thanks.

            Comment


              #7
              Glad to hear John and thanks sledge for the assist. Ticks could be expressed via TickSize directly, so multiples could be added / subtracted -

              BertrandNinjaTrader Customer Service

              Comment


                #8
                Hi Bertrand. Thank you for the information. The TickSize info you provided worked good with the code below.

                Math.Abs(Variable0 = (High[1] - Low[1]) + (Close[1]));
                Math.Abs(Variable1 = (Close[1] - (High[1] - Low[1]) + (10 * TickSize));

                However, when I'm trying to backtest against Futures 6A or 6B and print the values to WIndow Output, I'm still getting some incorrect values as shown below.

                Is there a way to only keep 0.0000? I'm using NT 7.0.1000.22.

                0.0186000000000001

                0.0160999999999999

                Comment


                  #9
                  Hi John, that could be the case when working with the full precision range a double has to offer here. You could for example use Math.Round or String.Format options to get to your desired output.

                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    The link you provided provides all the good combinations to help me out.
                    Bertrand, like always, fantastic job.
                    Thank you.

                    Comment


                      #11
                      Just thought of posting here so others can benefit too.
                      The syntax I found to get only 4 decimal and some troubleshooting print statements to see on the output window:

                      protected override void OnBarUpdate()
                      {
                      // Condition set 1
                      if (Close[0] > EMA(EMA10)[0])

                      {
                      EnterLong(DefaultQuantity, "L1");

                      Variable2 = (High[0] - Low[0])/TickSize; --->can sometimes return: 160.0009999999
                      Variable2 = Math.Round(Variable2, 4); ---->this will keep: 160
                      SetTrailStop("L1", CalculationMode.Ticks, Variable2, false);

                      Print (Time[0]);
                      Print("Formatted to 4 decimal places: " + Variable2);
                      }
                      Last edited by john_robertson00; 05-09-2014, 05:42 PM.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by dbennett3, Yesterday, 10:49 AM
                      1 response
                      20 views
                      0 likes
                      Last Post marcus2300  
                      Started by Touch-Ups, Today, 08:42 AM
                      0 responses
                      9 views
                      0 likes
                      Last Post Touch-Ups  
                      Started by obiwan_cashobi, Today, 05:50 AM
                      0 responses
                      12 views
                      0 likes
                      Last Post obiwan_cashobi  
                      Started by janefoster, Yesterday, 11:50 PM
                      0 responses
                      8 views
                      0 likes
                      Last Post janefoster  
                      Started by kaywai, 07-13-2024, 09:05 PM
                      3 responses
                      34 views
                      0 likes
                      Last Post kaywai
                      by kaywai
                       
                      Working...
                      X