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

Positions Property available within OnPositionUpdate?

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

    Positions Property available within OnPositionUpdate?

    I'm trying to use OnPositionUpdate to notify me when a position has been closed out. I'd like to be emailed the instrument and price it was closed out at, and then an update on the latest performance stats for the strategy.

    I'm running a strategy with 5 instruments. Since the position is closed out by the time OnPositionUpdate runs, how do you access the last trade information (price, quantity, instrument) from OnPositionUpdate? Is there a BarsInProgress type of functionality like there is in OnBarUpdate to filter the code in OnPositionUpdate so that I only get sent an email for the instruments that just went flat, not all the instruments that are in the strategy but already flat? Or, to do this best, should I be using OnOrderUpdate?

    Code:
    [SIZE=2][FONT=Courier New][COLOR=#0000ff]protected[/COLOR][/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]override[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] OnPositionUpdate(IPosition position) [/SIZE][/FONT]
    [SIZE=2][FONT=Courier New]{ [/FONT][/SIZE]
    [FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] (position.MarketPosition == MarketPosition.Flat) [/SIZE][/FONT]
    [SIZE=2][FONT=Courier New]{ [/FONT][/SIZE]
    [SIZE=2][FONT=Courier New]SendMail([/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#800000]"email"[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2],[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000]"other email"[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2], Instrument.ToString() + [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000]" Position Closed"[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2], "[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000]Performance update: "[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] + Performance.RealtimeTrades.TradesPerformance.Currency.CumProfit.ToString());[/SIZE][/FONT]
    [FONT=Courier New][SIZE=2]}[/SIZE][/FONT]
    [SIZE=2][FONT=Courier New]} [/FONT][/SIZE]

    #2
    Hi kcsystemtrader, I suggest using Positions to check the market position of your individual added instrumnets - http://www.ninjatrader-support.com/H...Positions.html
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Like this? If Positions[1] is closed out and triggers the OnPositionUpdate call, won't the code below still send an email for Positions[2] and Positions[3] if they both happen to be flat.

      I still don't understand what to use to get the PnL for the position that just closed out. So in the example above I would want some print statement in the " ... " that collects and prints the PnL for the last trade that just closed out against Positions[1].

      Code:
      [SIZE=2][FONT=Courier New][COLOR=#0000ff]if[/COLOR][/FONT][/SIZE][FONT=Courier New][SIZE=2] (position.MarketPosition == MarketPosition.Flat) [/SIZE][/FONT]
      [SIZE=2][FONT=Courier New]{ [/FONT][/SIZE]
      [FONT=Courier New][SIZE=2][/SIZE][/FONT] 
      [FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2](Positions[[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]].MarketPosition == MarketPosition.Flat)[/SIZE][/FONT]
      [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
      [SIZE=2][FONT=Courier New]SendMail([/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#800000]"email1"[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2],[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000]"email2"[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2],[FONT=Courier New][SIZE=2][COLOR=#800000]"..."[/COLOR] , [COLOR=#800000]"..."[/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2]);[/SIZE][/FONT]
      [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
      [FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2](Positions[2[/SIZE][/FONT][FONT=Courier New][SIZE=2]].MarketPosition == MarketPosition.Flat)[/SIZE][/FONT]
      [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
      [SIZE=2][FONT=Courier New]SendMail([/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#800000]"email1"[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2],[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000]"email2"[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2], [COLOR=#800000]"..."[/COLOR] , [COLOR=#800000]"..."[/COLOR])[/SIZE][/FONT][FONT=Courier New][SIZE=2];[/SIZE][/FONT]
      [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
      [SIZE=2][FONT=Courier New][FONT=Courier New][COLOR=#0000ff]if[/COLOR][SIZE=2](Positions[3[/SIZE][SIZE=2]].... etc[/SIZE][/FONT][/FONT][/SIZE]
       
      }
      [/SIZE][/FONT]

      Comment


        #4
        For PnL you would want to be using TradesPerformance. http://www.ninjatrader-support.com/H...anceClass.html
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Is there a way to check what position is calling the OnPositionUpdate method?

          Or, is the method internally programmed to only run against the position that called OnPositionUpdate (i.e. this would be like if OnBarUpdate inherently knew what the BarsInProgress index value was).

          It is not clear from the help files how OnPositionUpdate interacts with a multi-instrument strategy.

          The following is copied from the help files. I'm using 'lastProfit' in my SendMail code and after the first trade, it does not update on the 2nd trade of the day. It keeps printing the 'lastProfit' value for the first trade.

          Code:
          [FONT=Courier New][COLOR=#0000ff]if[/COLOR] (Performance.RealtimeTrades.Count > 0)[/FONT] 
          [FONT=Courier New]{[/FONT] 
          [FONT=Courier New]    [COLOR=#008000]// Get the last completed real-time trade (at index 0)[/COLOR][/FONT] 
          [FONT=Courier New]    [COLOR=#0000ff]Trade[/COLOR] lastTrade = Performance.RealtimeTrades[0];[/FONT] 
           
          [FONT=Courier New]    [COLOR=#008000]// Calculate the PnL for the last completed real-time trade[/COLOR][/FONT] 
          [FONT=Courier New][COLOR=#0000ff]    double[/COLOR] lastProfit = lastTrade.ProfitCurrency * lastTrade.Quantity;[/FONT] 
           
          [FONT=Courier New]    [COLOR=#008000]// Pring the PnL to the Output window[/COLOR][/FONT] 
          [FONT=Courier New]    Print("The last trade's profit is " + lastProfit);[/FONT] 
          [FONT=Courier New]}[/FONT]

          Comment


            #6
            The OnPositionUpdate() is called anytime a position from your strategy changes its state.

            You could use the normal Position.MarketPosition with the BarsInProgress check in the OnBarUpdate(), the alternative approach from this link - http://www.ninjatrader-support.com/H...Positions.html

            To access the last completed realtime trade please try -
            Code:
            [SIZE=2][SIZE=2]Trade lastTrade = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - [/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2]]; 
            
            [/SIZE][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] (lastTrade != [/SIZE][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2]) 
            Print([/SIZE][/SIZE][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]"The last trade profit is "[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] + lastTrade.ProfitPoints); [/SIZE][/SIZE]
            [SIZE=2][SIZE=2][/SIZE][/SIZE]

            BertrandNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by casabella, Today, 04:12 AM
            0 responses
            10 views
            0 likes
            Last Post casabella  
            Started by Ludwik, Today, 03:52 AM
            0 responses
            7 views
            0 likes
            Last Post Ludwik
            by Ludwik
             
            Started by wuannetraam, Today, 02:40 AM
            0 responses
            10 views
            0 likes
            Last Post wuannetraam  
            Started by cyberpete76, 03-27-2023, 12:29 AM
            7 responses
            272 views
            1 like
            Last Post slightly  
            Started by renewsaltwater, Today, 01:15 AM
            0 responses
            4 views
            0 likes
            Last Post renewsaltwater  
            Working...
            X