Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

position.AvgPrice causing problems within strategy

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

    position.AvgPrice causing problems within strategy

    Hi,

    I'm having some intermittent trouble with the following;

    I am using this line of code to save the prior trade as a private double in the variables section.

    Code:
        
        protected override void OnPositionUpdate(IPosition position)
            {
                if (position.MarketPosition == MarketPosition.Long) priorTrade = position.AvgPrice;
                Print(Instrument.FullName + " " + position.AvgPrice);
            }
    Then I am using the priorTrade within my logic to prevent subsequent trades at the same price as the previous trade.

    I loaded the strategy today which should have placed a long limit to buy, when it didn't I removed this line of code below referencing the prior trade and reloaded the strategy which placed the order.

    Code:
    priorTrade != gNumb().Line4[0]
    Could anyone offer any help with this issue?

    Regards,
    suprsnipes

    #2
    suprsnipes, when do you reset the price level to not take a trade, daily weekly?

    Not sure I fully follow, but to me it seems your logic is working as this trade as perhaps at the same price level as the last one saved to the corresponding variable?

    Comment


      #3
      Hi Bertrand,

      The variable priorTrade is only stored for the session as far as I know...does the variable reset when you stop the strategy?

      My logic is as you have mentioned. I'm using the priorTrade to ensure if all other conditions are met AFTER the strategy has already entered a long position at the same price level as the priorTrade then my strategy will ignore the signal.

      I am testing another strategy with the same priorTrade code where I am NOT running into the same problem.

      The main issue is the strategy won't place any trades whatsoever, but as soon as I remove the line of code regarding the priorTrade the strategy runs as expected...can you offer any further suggestions?

      Comment


        #4
        suprsnipes, well if you do not reset it explicitly at Session break it may still be filled from a prior session as the strategy cycles through the historical OnBarUpdate() part as you start it up. So you would need to debug the values being set by the strategy for this variable as you already determined it works as expected without this involved in your conditions.

        Comment


          #5
          Thanks for the quick reply (as always),

          Bertrand could you please let me know where you would suggest I place the code to reset the priorTrade variable?

          Regards,
          suprsnipes

          Comment


            #6
            You're welcome, you could place it at the session break then -

            Comment


              #7
              Hi Bertand,

              After pointing me in the right direction which I really appreciate by the way, to reset the priorTrade variable at the start of each session this is what I come up with.

              Code:
               protected override void OnBarUpdate()
                      {
                          // At the start of a new session
                          if (Bars.SessionBreak)
                          {
                          // Reset the priorTrade variable
                              priorTrade = 0;
                          }  
                       }
              I'll be testing this over the next few days but it appears as though the above code has solved the problem.

              Learning heaps by the way...and it's great the you guys at NT offer support to inexperienced programmers willing to have a go.

              Best regards,
              suprsnipes

              Comment


                #8
                You're welcome, great this helped you made it work.

                Comment

                Latest Posts

                Collapse

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