Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Avg.Price ??

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

    Avg.Price ??

    Hello,
    I have to move the StopLoss to EntryPrice, when the position moves into profit.
    For example, Buy $ 10.
    StopLoss, 4%.
    If the price go to $ 11 The StopLoss move to Entryprice (10 $).
    Code:
    #region Positions
    private void GoLong()
    {

    SetStopLoss(CalculationMode.Percent,0.04);
    EnterLong(100, "");// 10$
    }
    #endregion

    #region OrderRouting
    private void ManagerOrder ()
    {
    if (Position.MarketPosition == MarketPosition.Long )
    {if( High[0] > Position.AvgPrice *(1+0.03)))
    SetStopLoss("°", CalculationMode.Price,Position.AvgPrice, true);
    Print(Time[0]+Position.AvgPrice);// the value of the Output Window is ( 0 ), instead of 10$.
    Why?
    Ciao.
    Roberto

    #2
    Hello,

    The AvgPrice does not update until the next OnBarUpdate. Does it stay at 0 or does it only output 0 for the first OnBarUpdate()?

    Also what are you calling this method from?

    Are you using managed or unmanaged orders?

    I look forward to assisting you further.
    BrettNinjaTrader Product Management

    Comment


      #3
      OK Brett,
      I checked better and Position.Avgprice is correctly updated.
      But I have another problem!
      Do not update the value of a variable is always the default (1).
      Code:
      #region OrderRouting
      private void ManagerOrder ()

      if (Position.MarketPosition == MarketPosition.Long && callOnce )
      {
      double myRangeLong=(MAX(High, 5)[0]-MIN(Low, 10)[5]);
      callOnce=false;
      Print(Time[0]+"Value myRangeLong"+myRangeLong);//remains 1
      }

      #endregion


      protected override void OnBarUpdate()

      if(Position.MarketPosition==MarketPosition.Flat)
      callOnce=true;

      ManagerOrder ();

      if(Position.MarketPosition!= MarketPosition.Flat) return;

      Etc......

      Comment


        #4
        Italy, which variable does not get updated? Can you please clarify?
        AustinNinjaTrader Customer Service

        Comment


          #5
          myRangeLong remains as a default. He should have as its value:
          double myRangeLong = (MAX (HIGH, 5) [0]-MIN (Low, 10) [5]);

          Comment


            #6
            Hello,

            Please further clarify, what variable does it have stored, which variable do you expect?

            You need to continue to add Print() statement until you find where it is getting reset when you do not expect or not reset when you expect.

            Let me know if I can be of further assistance.
            BrettNinjaTrader Product Management

            Comment


              #7
              Sorry but I do not know what to do.
              The variable name is: myRangeLong
              The variable is in the region OrderRouting. With the print command I see that the variable is the default value (1), is not updated.
              The calculation of the variable that has to do is:
              myRangeLong = (MAX (HIGH, 5) [0]-MIN (Low, 10) [5]);// result = 2,20
              Code:

              Code:
              # Region Variables
              private double myRangeLong = 1;
              # Endregion

              # Region OrderRouting
              private void ManagerOrder ()

              if (Position.MarketPosition == MarketPosition.Long callOnce & &)
              {
              double myRangeLong = (MAX (HIGH, 5) [0]-MIN (Low, 10) [5]);// result = 2,20
              callOnce = false;
              Print (Time [0] + "Value myRangeLong" myRangeLong +) / / Why, the result is always 1 of myRangeLong? Instead of being 2.20?
              }
              # Endregion


              protected override void OnBarUpdate ()

              if (Position.MarketPosition == MarketPosition.Flat)
              callOnce = true;

              ManagerOrder ();

              if (Position.MarketPosition! = MarketPosition.Flat) return;

              What should I do?
              Thanks
              Ciao.
              Roberto

              Comment


                #8
                Hello,

                Is there a reason you have double in from of it to instantiate a new variable each time, tr taking double out here.

                Also, please try taking the Min, and Max out as I believe this is where your issue is.

                I look forward to assisting you further.
                BrettNinjaTrader Product Management

                Comment


                  #9
                  Sorry, but to write and read using Google translation. I did not understand the meaning of your answer. I can tell you with the print command I see that the price of the calculation is correct. Then call the variable to set the price of the output value is 1.
                  code:
                  # Region OrderRouting
                  private void ManagerOrder ()
                  {
                  if (Position.MarketPosition == MarketPosition.Long callOnce & &)
                  {
                  double myRangeLong = (MAX (HIGH, 5) [0]-MIN (Low, 10) [5]);// Value myRangeLong 2,20
                  callOnce = false;
                  Print (Time [0] + "Initial value myRangeLong"+ myRangeLong );//The value is read in the output of Window is 2,20.
                  }

                  {
                  if (High [0]> Position.AvgPrice + (myRangeLong * 0.50))// Value myRangeLong remains 1, (1 * 0.50) . It should be (2,20 * 0.50)

                  So I can not define the Exit !!!
                  Ciao.
                  Roberto

                  Comment


                    #10
                    Hello,

                    Try this:

                    if (Position.MarketPosition == MarketPosition.Long callOnce & &)
                    {
                    double myRangeLong = (MAX (HIGH, 5) [0]-MIN (Low, 10) [5]);//

                    Too:

                    if (Position.MarketPosition == MarketPosition.Long callOnce & &)
                    {
                    double myRangeLong = (HIGH [0]-Low[5]);//


                    I believe the MAX and MIN is not working as you expect it too.

                    I look forward to assisting you further.
                    BrettNinjaTrader Product Management

                    Comment


                      #11
                      For the calculation there are problems: (MAX (HIGH, 5) [0]-MIN (Low, 10) [5]), it works.
                      I'll put the code:

                      # Region Variables
                      private bool be1 = true; //Break eaven 1
                      private bool be2 = true; // Break eaven 2
                      private double myRangeLong = 1;/ / Variable for the calculation of myRangeLong
                      # Endregion

                      # Region Positions
                      private void GoLong ()
                      {SetStopLoss ("Stop LongBuy" CalculationMode.Percent, 0.03, false);
                      EnterLong (100, "LongBuy");
                      SetProfitTarget(CalculationMode.Price,Close[0] + myRangeLong);
                      }
                      # Endregion

                      # Region OrderRouting
                      private void ManagerOrder ()
                      {
                      if (Position.MarketPosition == MarketPosition.Long && callOnce )

                      {
                      double myRangeLong = (MAX (HIGH, 5) [0]-MIN (Low, 10) [5]);
                      callOnce = false;
                      Print (Time [0] + "Value myRangeLong" +myRangeLong );
                      }


                      {
                      if (BE1 & & High [0]> Position.AvgPrice + (myRangeLong * 0.50))// This point needs to update the variable
                      SetStopLoss ("°", CalculationMode.Price, Position.AvgPrice, false);
                      if (BE2 & & High [0]> (Position.AvgPrice+ myRangeLong * 0764))//Also here
                      SetStopLoss ("", CalculationMode.Price, Position.AvgPrice + (myRangeLong * 0.50), false);//Also here

                      }

                      protected override void OnBarUpdate()
                      {
                      if(Position.MarketPosition==MarketPosition.Flat)
                      callOnce=true;

                      ManagerOrder ();

                      if(Position.MarketPosition!= MarketPosition.Flat) return;

                      if( Low[0] < MAX(High, 5)[0])
                      {
                      GoLong();
                      }

                      Ciao.
                      Roberto

                      Comment


                        #12
                        For the calculation there are NOT problems: (MAX (HIGH, 5) [0]-MIN (Low, 10) [5]), it works.
                        I'll put the code:

                        # Region Variables
                        private bool be1 = true; //Break eaven 1
                        private bool be2 = true; // Break eaven 2
                        private double myRangeLong = 1;/ / Variable for the calculation of myRangeLong
                        # Endregion

                        # Region Positions
                        private void GoLong ()
                        {SetStopLoss ("Stop LongBuy" CalculationMode.Percent, 0.03, false);
                        EnterLong (100, "LongBuy");
                        SetProfitTarget(CalculationMode.Price,Close[0] + myRangeLong);
                        }
                        # Endregion

                        # Region OrderRouting
                        private void ManagerOrder ()
                        {
                        if (Position.MarketPosition == MarketPosition.Long && callOnce )

                        {
                        double myRangeLong = (MAX (HIGH, 5) [0]-MIN (Low, 10) [5]);
                        callOnce = false;
                        Print (Time [0] + "Value myRangeLong" +myRangeLong );
                        }


                        {
                        if (BE1 & & High [0]> Position.AvgPrice + (myRangeLong * 0.50))// This point needs to update the variable
                        SetStopLoss ("°", CalculationMode.Price, Position.AvgPrice, false);
                        if (BE2 & & High [0]> (Position.AvgPrice+ myRangeLong * 0764))//Also here
                        SetStopLoss ("", CalculationMode.Price, Position.AvgPrice + (myRangeLong * 0.50), false);//Also here

                        }

                        protected override void OnBarUpdate()
                        {
                        if(Position.MarketPosition==MarketPosition.Flat)
                        callOnce=true;

                        ManagerOrder ();

                        if(Position.MarketPosition!= MarketPosition.Flat) return;

                        if( Low[0] < MAX(High, 5)[0])
                        {
                        GoLong();
                        }

                        Ciao.
                        Roberto

                        Comment


                          #13
                          Hello Roberto,

                          Can you please clarify the latest issues you're having and share the strategy file you're working from. Simplify wherever possible - remove custom methods and focus on one element at a time until you've verified all behavior works as you expect.

                          Strategy is a .cs file in My Documents\NinjaTrader\bin\custom\strategy
                          Ryan M.NinjaTrader Customer Service

                          Comment


                            #14
                            Already done this work!!
                            It does not matter! Thank you for your availability.

                            Comment


                              #15
                              Hello,

                              In this case without giving the code a quick run on my side to help you debug there is nothing else I can see ofhand at first glance.

                              Therefor I will need you to send me your full strategy for me to run on my side to support at ninjatrader dot com and reference this forum post and put ATTN: Brett in the email.

                              I look forward to assisting you further.
                              BrettNinjaTrader Product Management

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              576 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              334 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              101 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              553 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              551 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X