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

Monitor Instrument Profit / OpenPnL

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

    Monitor Instrument Profit / OpenPnL

    Hi,
    i have build a strategy for ninja8 which is monitoring netpos, realized and unrealized profit for each traded instrument. Now iam trying to implement the functions in a ninja7 script.

    OnStartUp() is called when the strategy is activated by the user, then the current account is selected: Account MonitorAccount = Account;

    MonitorAccount.AccountUpdate += OnAccountUpdate;
    MonitorAccount.PositionUpdate += OnPositionUpdate;

    The events are not triggered when the price is changing for the instrument,
    how to track the profit in realtime?

    Iam using Sim101 Account with "Simulated datafeed" connection. Which event i should use in ninja7 to track the openPnL for the instrument in realtime?

    Also all these values return 0:

    double PnL = MonitorAccount.GetAccountValue(AccountItem.Realize dProfitLoss, xCurrency).Value;
    double CumProfit = Performance.AllTrades.TradesPerformance.Currency.C umProfit;
    double GrossProfit = Performance.AllTrades.TradesPerformance.GrossProfi t;
    double GrossLoss = Performance.AllTrades.TradesPerformance.GrossLoss;

    am i missing something?

    i have attached the sample strategy, thx for help
    Attached Files
    Aylin
    NinjaTrader Ecosystem Vendor - RealTime Trading Global

    #2
    Hello Aylin,

    Thank you for your note.

    To track the PL in real time you could use GetProfitLoss method, see the following link,



    Regarding your GetAccountValue call returning a zero, this would be expected on historical trades, see the following note.



    If you run the strategy with CalculateOnBarClose=false, are the events to track the profit in real time triggered?

    I look forward to your reply.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Hi, thank you for the information,

      i have set CalculateOnBarClose = false;

      and the DataSeries Property of my strategy set to 1 Tick .

      Now i can see OnBarUpdate is called each tick, that event i can use to track the realized and unrealized profit in realtime.

      On the weekend i got erros accessing Close[0] but now its working.



      My ninja account has some Executions and 2 Open Positions (FDAX , ES)

      private double GetInstrumentOpenPnl()
      {
      PerformanceUnit myPerformanceUnit = PerformanceUnit.Currency;
      Account my_account = MonitorAccount;

      double openPnl = 0;

      foreach(Position position in my_account.Positions)
      {
      string instr_name = position.Instrument.FullName;
      Print(position.Quantity+" qty found for instrument: " + instr_name);

      if (position.MarketPosition != MarketPosition.Flat)
      {

      double oPnl = position.GetProfitLoss(Close[0], PerformanceUnit.Currency);
      openPnl+=oPnl;

      Print("Open PnL: " + oPnl);
      //RTGNinjaCloud.UpdatePL("ACC",instr_name,"USD",posi tion.Quantity,rpl,opl);
      }
      }
      return openPnl;
      }

      When i loop the positions, how do i know the index of the matching Close[X] ?

      Because only the first instrument found has a valid open pnl..
      the other value is like "499512.5" - that is not correct

      best regards
      Last edited by Aylin; 03-26-2018, 12:40 PM.
      Aylin
      NinjaTrader Ecosystem Vendor - RealTime Trading Global

      Comment


        #4
        Hello Aylin,

        You could create a counter to track each loop, for example,
        Code:
        private counterX =0;
        
        foreach(Position position in my_account.Positions)
        {
        counterX++;
        Print(counterX.ToString());
        }
        Please let us know if you need further assistance.
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          i have found this line of code to get the price ,which i can use to get the open PnL for each instrument:

          double price = position.Account.Connection.MarketDataStreams[position.Instrument].Bid.Price;

          this seems to work for me
          Aylin
          NinjaTrader Ecosystem Vendor - RealTime Trading Global

          Comment


            #6
            ok my ninja7 strategy is almost ready , the last bit of code iam missing is for monitoring the REALIZED profit of each instrument.

            I have code for total profit:

            double realized = MonitorAccount.GetAccountValue(AccountItem.Realize dProfitLoss,Currency.Unknown).Value;


            In ninja8 i have used this code to get the realized profit of each instrument:
            double r = Cbi.SystemPerformance.Calculate(Instrument).AllTra des.TradesPerformance.Currency.CumProfit;

            but its not compiling in nt7 *__*

            thx
            Last edited by Aylin; 03-27-2018, 11:22 AM.
            Aylin
            NinjaTrader Ecosystem Vendor - RealTime Trading Global

            Comment


              #7
              Hello Aylin,

              In the NT8 helpguide under code breaking changes, you can search a NT7 or NT8 Property or Method and see its equivalent in each version.

              The link for the code breaking table can be found at the following link,


              Please let us know if you are unable to find what you're looking for.
              Attached Files
              Alan P.NinjaTrader Customer Service

              Comment


                #8
                Iam sorry but i cant find the equivalent of that function in ninja7 ..

                i hav tried GetTrades() to select the Trades for one Instrument but it asks for an entry signal name and index parameter which i have no idea of i just want all trades for the same instrument.

                double r = Performance.AllTrades.GetTrades(execution.Instrume nt.FullName,null,0).TradesPerformance.Currency.Cum Profit;


                Can i use this method somehow or is there another logic i need to make use of?
                Aylin
                NinjaTrader Ecosystem Vendor - RealTime Trading Global

                Comment


                  #9
                  Hello Aylin,

                  There would be no exact equivalent however without passing the entry signal to the method you mentioned, you could use the following to loop through previous trades,



                  As far as narrowing it down to the instrument, without an entry signal, its possible you could keep a list of previous trades and the instrument associated with it. Then you'd use the loop in the above sample to match N trades ago, with N trades ago in your custom list logging the instrument associated with a trade.

                  That would be the work around I could suggest.

                  Please let us know if you need further assistance.
                  Alan P.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by geddyisodin, Today, 05:20 AM
                  3 responses
                  20 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by lorem, Today, 09:18 AM
                  1 response
                  4 views
                  0 likes
                  Last Post lorem
                  by lorem
                   
                  Started by bmartz, Today, 09:30 AM
                  0 responses
                  3 views
                  0 likes
                  Last Post bmartz
                  by bmartz
                   
                  Started by GussJ, 03-04-2020, 03:11 PM
                  14 responses
                  3,245 views
                  0 likes
                  Last Post GussJ
                  by GussJ
                   
                  Started by ArkansasClint, Today, 09:28 AM
                  0 responses
                  2 views
                  0 likes
                  Last Post ArkansasClint  
                  Working...
                  X