Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

ATR Profit Target

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

    ATR Profit Target

    Just trying to setup a simple profit target and trailing stop. Profit target is 2 ATR's and then trail by 1 ATR(until i think of a better trail). Im a beginner at coding, im getting multiple errors. What do I need to change or add below?......




    protected override void OnBarUpdate()
    {
    {

    SetStopLoss(CalculationMode.Price, ATR(14)[0]);
    SetProfitTarget(CalculationMode.Price, 2*ATR(14)[0]);
    }

    // Condition set 1
    Then all my entry conditions are below....
    Last edited by zachj; 01-20-2012, 11:38 AM.

    #2
    What errors would you be exactly getting? Could you perhaps post a screenshot?

    For a basic example of working code, please see this reference sample -

    Comment


      #3
      I've edited the code below and it compiles but how do i set the profit target to be my entry + 2x's the ATR? and trailing stop to be 1 ATR? As I have it now it is not adding the profit target to the entry price, its just adding it to 0.
      Last edited by zachj; 01-20-2012, 11:37 AM.

      Comment


        #4
        Originally posted by zachj View Post
        I've edited the code below and it compiles but how do i set the profit target to be my entry + 2x's the ATR? and trailing stop to be 1 ATR? As I have it now it is not adding the profit target to the entry price, its just adding it to 0.
        What code?

        Comment


          #5
          The code in my first post. I had updated it. Either way ive posted the latest code below. Figured out the issue though. I was using .Price instead of .Ticks. But now have a new issue. It hits the target and sells instead of trailing the position. It then puts in a new buy order. And it also trails the initial buy order, I only want it to trail when it hits the profit target. Im assuming its the placement of my SetStopLoss and SetProfitTarget maybe?


          protected override void OnBarUpdate()
          {
          {

          SetStopLoss(CalculationMode.Ticks, ATR(14)[0]);
          SetProfitTarget(CalculationMode.Ticks, (ATR(14)[0])*2);
          }

          // Condition set 1
          Then all my entry conditions are below....
          Last edited by zachj; 01-20-2012, 12:19 PM.

          Comment


            #6
            Originally posted by zachj View Post
            The code in my first post. I had updated it. Either way ive posted the latest code below. Figured out the issue though. I was using .Price instead of .Ticks. But now have a new issue. It hits the target and sells instead of trailing the position. It then puts in a new buy order. And it also trails the initial buy order, I only want it to trail when it hits the profit target. Im assuming its the placement of my SetStopLoss and SetProfitTarget maybe?


            protected override void OnBarUpdate()
            {
            {

            SetStopLoss(CalculationMode.Ticks, ATR(14)[0]);
            SetProfitTarget(CalculationMode.Ticks, (ATR(14)[0])*2);
            }

            // Condition set 1
            Then all my entry conditions are below....
            A SetProfitTarget() will exit the market when hit. That is its purpose.

            If you do not want to exit, then do not use SetProfitTarget(). Instead set a target price at entry, then start adjusting the SetStopLoss() after that target price is hit.

            Comment


              #7
              Ok so how do i set a target price after entry or where can i find info on how to do this please? thank you

              Comment


                #8
                Originally posted by zachj View Post
                Ok so how do i set a target price after entry or where can i find info on how to do this please? thank you
                As your posted code is just a snippet, I can only show you in pseudocode snippets.

                Code:
                if (entryConditionsMetForGoingLong)
                {
                targetPrice = Close[0] + ATR(14)[0];
                enterLongAtEntryPrice;  //your entry
                }
                Once you are in a position, you need to check before you do anything, so:

                Code:
                if (Position.MarketPosition != MarketPosition.Flat)
                {
                if (Close[0] >= targetPrice)
                {
                SetStopLoss(AdjustedPrice) // your adjustment
                }
                }
                Remember to reset the SetStopLoss() when you go flat from trade exit.

                Comment


                  #9
                  I tried to incorporate your. See attached. Part is under condition set 1 and the rest of ur code is at the end. Getting targetPrice does not exist. And not sure if your code was put in correct places?...

                  Attached Files

                  Comment


                    #10
                    Originally posted by zachj View Post
                    I tried to incorporate your. See attached. Part is under condition set 1 and the rest of ur code is at the end. Getting targetPrice does not exist. And not sure if your code was put in correct places?...
                    If that is the only error you are getting, then it is because you have not declared the variable. Declare it as a class variable of type "double" in the "Variables" region.

                    Comment


                      #11
                      I had already tried putting double in front of targetPrice, still get the error. Is there anything wrong with the below snippet?

                      double targetPrice = Close[0] +(ATR(14) [0])*2;

                      Comment


                        #12
                        Originally posted by zachj View Post
                        I had already tried putting double in front of targetPrice, still get the error. Is there anything wrong with the below snippet?

                        double targetPrice = Close[0] +(ATR(14) [0])*2;
                        There is nothing wrong with it as a code statement; it is wrong in the context of what it is supposed to do. Because of its nature, targetPrice is required it to retain its value beyond the occurrence of all events.

                        If you declare it that way, you have declared it with a local scope, so it will not be visible outside of its block. Declare it the way that I specified: as a class variable in the "Variables" section.

                        Comment


                          #13
                          I apologize, my coding skills are beginning level. class variable in the "Variables" section, where is variables section and how would that code line look?

                          I had moved double targetPrice = Close[0] +(ATR(14) [0])*2; to be at the start of onbarupdate section and it compiled. Was not setting any targets though when i look at the chart.
                          Last edited by zachj; 01-20-2012, 04:42 PM.

                          Comment


                            #14
                            Originally posted by zachj View Post
                            I apologize, my coding skills are beginning level. class variable in the "Variables" section, where is variables section and how would that code line look?

                            I had moved double targetPrice = Close[0] +(ATR(14) [0])*2; to be at the start of onbarupdate section and it compiled. Was not setting any targets though when i look at the chart.
                            Just as I would expect, as the targetPrice variable is still being declared as local in scope.

                            I would suggest that you look at your source file from the top. You should be able to find a section/region labelled as "Variables". I really do not know any other way to explain how to find a code section in a file. No need to apologize; we all started somewhere, from a state where we were beginners.

                            Comment


                              #15
                              Ok so I have declared it as a class variable of type "double" in the "Variables" region by the following..
                              private double targetPrice = 20;

                              Im not sure if just setting a arbitrary number of 20 is ok. But it does compile and targetPrice should no longer be "local in scope".

                              Ive attached my whole code. Ive put a "--------------------->" next to the lines were I have inserted the code you had given me in one of your earlier posts.

                              Im still not getting any targets showing up on the Strategy Chart.

                              Have I inputted your code in the incorrect spots in the OnBarUpdate() section? Or entered it incorrectly somehow?
                              Attached Files
                              Last edited by zachj; 01-20-2012, 06:43 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              670 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              379 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              111 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              575 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              582 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X