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

Using OnPositionUpdate to send mail

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

    Using OnPositionUpdate to send mail

    Hi Ninjas,

    I like to know when my Algo has changed its position and how much profit has been done until that moment; in order to get this, I have this little code:

    Code:
    protected override void OnPositionUpdate(IPosition position)
    		{
    			if (position.MarketPosition != MarketPosition.Flat)
    			{
    				SendMail ("[email protected]", "[email protected]", "Algo Summary ", Position.MarketPosition.ToString() + ".  P/L " + GetAccountValue(AccountItem.RealizedProfitLoss).ToString());
    			}
    		}
    It seems to work, but there still are 2 issues that I'd like to address:

    1. I received 2 mails immediately, with a report of the old position/accumlated profit and the new position, and actually I need just one mail with the last and actual position only

    2. The accumulated profit reported is not the actual amount, instead is the right early one, the immediate before to the actual or real one, how to address this?

    Thanks
    Last edited by pstrusi; 01-02-2015, 09:48 AM.

    #2
    Hi pstrusi,

    Thank you for your note.

    OnPositionUpdate() should only trigger once for each position change.

    Can you make a working reduced sample to show that the position is updating twice on each position update?

    Use a print instead of an email and print the position.MarketPosition.ToString().

    (Are you asking about historical position updates?)


    Regarding your section question, I'm not quite sure what you are asking. When you mention "The accumulated profit reported is not the actual amount, instead is the right early one, the immediate before to the actual or real one, how to address this?", are you asking if the reported pnl is the realized profit and loss not including the this latest order?

    When called in OnPositionUpdate, GetAccountValue(AccountItem.RealizedProfitLoss) should include the current closed trade. (But will not include any open trades)
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,

      Thanks for your quick response.

      1 Yes, I know that OnPositionUpdate should trigger the signal just when a position actually changed, but it seems that if a position has been changed cause two executions, it could send 2 notifications, because I don't see any other explanation. Analyzing the 2 executions, I can see that with the first one the position had not changed yet, so it should not have sent the email. What do you think?

      2 I'm looking to know how much realized real profit has been made with the last trade close, but instead I don't get the actual realized profit but the earlier one. How can I get the real realized profit that includes the last trade CLOSED. It's obvious that a trade not closed is an open position with a variable unrealized profit

      Looking forward for any tip
      Last edited by pstrusi; 01-02-2015, 10:04 AM.

      Comment


        #4
        Hi pstrusi,

        OnExecution should also only trigger once for each order that fills.

        Are you getting a part filled order? Are you sure there are not two orders that are filling?

        What does the execution.ToString() show in the output?


        With the Realized Profit and Loss I will need to see a sample for this as you are not describing the behavior NinjaTrader should be taking when the position updates.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Yes Chelsea, it's just one order filled partially in two parts, two executions. The Output in the 2 notification mails:

          First one reading: "Short. P/L 25.8500000000261"
          Second one reading: "Long. P/L 25.8500000000261"

          With the Realized Profit and Loss I will need to see a sample for this as you are not describing the behavior NinjaTrader should be taking when the position updates.
          It's pretty easy Chelsea, this is the easy example; as you can see in the Output above the Profit/Loss realized reported is supposedly 25.80 but actually in NT account realized profit is shown as 271.80; now do you understand? With the last trade closed the real and correct realized profit is 271.80 that INCLUDES actually the last trade; but the info that I'm getting from the e-mail with the last trade is 25.85. It seems not including the very last trade closed. I hope that with this clear example you could spot the issue and help me to address it

          Thanks
          Last edited by pstrusi; 01-02-2015, 11:40 AM.

          Comment


            #6
            Hello pstrusi,

            A part fill will cause two executions and will cause the position to be updated twice. There is no way to prevent this as it just what happens when there is a part fill.

            You can prevent the first email by checking that the order is fully filled. I would suggest doing this in OnExecution after the execution is triggered. This could also be done in OnOrderUpdate.

            myEntryOrder = EnterLong(10);

            if (Position.MarketPosition != MarketPosition.Flat && myEntryOrder.OrderState == OrderState.Filled)


            Currently you are printing the Account value.
            GetAccountValue(AccountItem.RealizedProfitLoss).To String()
            This should match what is shown in the Account tab of the Control Center but will not match the PnL for the strategy. Also, this is not updated by the time OnPositionUpdate is run.

            Because of this, it would be best to print this on the first tick after an execution.

            To find the PnL of the Strategy use:
            Performance.AllTrades.TradesPerformance.Currency.C umProfit.ToString()


            Attached is a demonstration of how to print the account value after an execution and have the performance show once when there is an execution.
            Attached Files
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Chelsea, thanks a lot for your time, diligence and great help. Your answer and suggestions, definitively will help me to address the issues.

              Best regards,
              PS

              Comment


                #8
                Hi Chelsea,

                I've tried your suggestions in my code, using:
                - Performance.RealtimeTrades.TradesPerformance.Curre ncy.CumProfit
                - Performance.AllTrades.TradesPerformance.Currency.C umProfit;

                but unfortunately it doesn't match with the realized PnL ( I've done a test starting with sim101 in zero ) and what happens is those variables keep a historical PnL ( starting one day backwards ).

                So, how can I reset these variables to zero in the code before going live once again ?

                If not, Do you have other suggestions to be able to have the real realized PnL updated as soon as a trade is closed?

                Thanks
                Last edited by pstrusi; 01-04-2015, 04:55 PM.

                Comment


                  #9
                  Please refer to my post on the following thread: http://www.ninjatrader.com/support/f...ad.php?t=71122

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Perr0Grande, Today, 08:16 PM
                  0 responses
                  2 views
                  0 likes
                  Last Post Perr0Grande  
                  Started by elderan, Today, 08:03 PM
                  0 responses
                  5 views
                  0 likes
                  Last Post elderan
                  by elderan
                   
                  Started by algospoke, Today, 06:40 PM
                  0 responses
                  10 views
                  0 likes
                  Last Post algospoke  
                  Started by maybeimnotrader, Today, 05:46 PM
                  0 responses
                  11 views
                  0 likes
                  Last Post maybeimnotrader  
                  Started by quantismo, Today, 05:13 PM
                  0 responses
                  7 views
                  0 likes
                  Last Post quantismo  
                  Working...
                  X