Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

How to know when a position has changed ?

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

    How to know when a position has changed ?

    Hi ninjas,

    This might look like a silly question but I'm struggling with this.

    I'd like to know when really a position is changed, so I'm wondering is there any method or function that could this job ? Something similar to "position.MarketPosition" but not only for Close[0] but also for Close[1] in order to be able to compare them and determined if any changes.

    My goal for this, is to instruct the code to send me a mail when really a new position was acquired in the strategy, and not a bunch of mails for any bar updated.

    Thanks

    #2
    pstrusi, for this task you can look into the OnPositionUpdate() event handler - http://www.ninjatrader.com/support/h...tionupdate.htm

    You can montior here for a position changed state and only then email youself the update desired.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Hi Bertrand, thanks for your response.

      I read the link suggested, but I still need some help here, so my questions:

      1. Is this necessary and where should it be in order not to affect the code ?

      "protected override void OnPositionUpdate(IPosition position)
      {

      }
      "

      2. Is this example what I need ? In my code you never will be flat. I don't get it and how to spot the change

      " protected override void OnPositionUpdate(IPosition position)
      {
      if (position.MarketPosition == MarketPosition.Flat)
      {
      // Do something like reset some variables here
      }
      }
      "

      Thanks

      Comment


        #4
        pstrusi, you would need to put this method handler outside of your OnBarUpdate() method, as it would be called independently of it, so only listening to the position update events your script would see. If it's called for a non flat position update > you would have a trade in the strategy then - consider for example this snippet for a test to start working with this handling :

        protected override void OnPositionUpdate(IPosition position)
        {
        if (position.MarketPosition != MarketPosition.Flat)
        Print(this.Name + " " + " is currently " + position.MarketPosition.ToString());

        }
        BertrandNinjaTrader Customer Service

        Comment


          #5
          So, 2 questions:

          1. Should I put this method, for example, before the OnBarUpdate() one ?
          2. If my script changes position from Long to Short and the reciprocal, will that snippet detected that change, when always the code will have a non-flat position ?

          Thanks

          Comment


            #6
            Correct, it would detect any non flat position update - just put it in your code and see what prints you get.

            It would not matter where it's put, it's an independent event of your OnBarUpdate().
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Thanks a lot for your help.

              Comment


                #8
                Bertrand, the snippet that must report whenever a position change occurs, doesn't work; it simply says nothing.

                The snippet:
                protected override void OnPositionUpdate(IPosition position)
                {
                if (position.MarketPosition != MarketPosition.Flat)
                Print(this.Name + " " + " is currently " + position.MarketPosition.ToString());

                }

                I've put it before OnBarUpdate(). I'm not expert but I think that if Algo changes from a Long position to a Short position, since both of them are != to flat positions, it will never detect a change. If not could you please what should I do to solve it?

                By the way, my platform is weird, I have to stop Algos, even de account synchronization didn't work properly, after stoping algos, I still had an open position. Well, I'll reset all platforms today and hoping not to see this again.

                Comment


                  #9
                  pstrusi, what are you expecting to see? Do you have the print / output window open where the messages would be seen? If the algo reverses you would be in a new marketposition which is not flat > so I would expect this event to trigger a print then.
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    Maybe I had something wrong, cause now it's working.

                    I was expecting to receive an email, the instruction is this:

                    SendMail ("mail1", "mail2", "Algo Summary ", Position.MarketPosition.ToString() + ". P/L " + GetAccountValue(AccountItem.RealizedProfitLoss).To String());

                    Thanks for your help, finally I got a decent flow of mail

                    Comment


                      #11
                      Glad to hear, yes the prints would show historically as well, whereas the emails will be realtime only. So you should now see one only on the position change event.
                      BertrandNinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by arvidvanstaey, Today, 02:19 PM
                      4 responses
                      11 views
                      0 likes
                      Last Post arvidvanstaey  
                      Started by samish18, 04-17-2024, 08:57 AM
                      16 responses
                      59 views
                      0 likes
                      Last Post samish18  
                      Started by jordanq2, Today, 03:10 PM
                      2 responses
                      9 views
                      0 likes
                      Last Post jordanq2  
                      Started by traderqz, Today, 12:06 AM
                      10 responses
                      18 views
                      0 likes
                      Last Post traderqz  
                      Started by algospoke, 04-17-2024, 06:40 PM
                      5 responses
                      47 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Working...
                      X