• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Calculating cumulative Realized PNL for secondary instrument

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

    Calculating cumulative Realized PNL for secondary instrument

    Hello,

    Is there a way to calculate cumulative Realized PnL of a secondary instrument?

    My goal is to Exit primary instrument if cumulative Realized PnL of secondary instrument is > than e.g. $500.

    Cumulative Realized PnL of a secondary instrument should start calculating when primary instrument is opened.

    When primary instrument is closed, Cumulative Realized PnL of a secondary instrument should reset.

    Thanks in advance.

    #2
    Hello markkm,

    Thanks for your post.

    Trade performance can be calculated off of any TradeCollection. If you would like to specifically look at the strategy's performance for trades made with the secondary instrument, you could create your own TradeCollection and add trades from SystemPerformance.AllTrades or another existing TradeCollection.

    For example:

    Code:
    TradeCollection Trades = new TradeCollection(true, true);
    
    foreach(Trade trade in SystemPerformance.AllTrades)
    	if (trade.Entry.Instrument == BarsArray[1].Instrument)
    		Trades.Add(trade);
    
    foreach(Trade trade in Trades)							
    	Print(trade.Entry.Instrument);
    
    Print(Trades.TradesPerformance.Currency.CumProfit);
    Documentation items that can be referenced for further reading are linked below.

    TradesPerformance - https://ninjatrader.com/support/help...erformance.htm
    TradeCollection - https://ninjatrader.com/support/help...collection.htm
    SystemPerformance - https://ninjatrader.com/support/help...erformance.htm

    Please let us know if we can be of further assistance.
    JimNinjaTrader Customer Service

    Comment


      #3
      Thanks Jim for your help.
      The documentation examples show trade collection for 1st / last trades or the last 20 trades.
      Can I set a bool variable to start trade collection for a secondary instrument ? If, yes how can I implement that?

      Thanks again!

      Comment


        #4
        Hello markkm,

        If you make the TradeCollection a private class level variable, you can use any logic you would like to control when you collect trades from SystemPerformance.AllTrades and calculate the TradesPerformance. This would include creating a bool and using that bool in an if statement to control your code.

        For example:
        Code:
        if(myBool == true)
        {
            // Collect trades here
        }
        If you are looking for advise on writing the syntax for creating the bool and class level variables, I'd recommend using the Strategy Builder and clicking View Code to observe how it generates its syntax. You may also refer to our NinjaTrader 7 help guide for some basic programming concepts. If you are looking for more robust programming education, I'd recommend looking for programming resources external to NinjaTrader.

        Strategy Builder 301 - https://www.youtube.com/watch?v=HCyt90GAs9k

        Programming Concepts - https://ninjatrader.com/support/help...g_concepts.htm

        Please let us know if you have any additional questions.
        JimNinjaTrader Customer Service

        Comment


          #5
          Thank you, I'll let you know if I have any questions.

          Comment


            #6
            I tried to implement the sample code into my strategy but I can't figure it out.
            Sorry I went through lots of forums on how to start collecting trades, and there is not many. I also went through all the documentations for TradeCollection, but it doesn't specify what should be done for TradeCollection OnState / PublicClass / OnBarUpdate etc ...

            For the sample code you wrote below, should it be written OnBarUpdate?

            Comment


              #7
              Okay, I am actually answering my own questions. I was able to print secondary instrument PnL.
              Now I have another problem. It keeps accumulating. How can I reset TradeCollection? e.g. If primary instrument == flat, and start recounting again?
              I tried with bool variable but no luck. It stops counting when bool = false, but then continues from where it was before when bool was true.

              Thanks Jim again, I really appreciate your help!

              Comment


                #8
                Hello markkm,

                You may use the Clear method which is used with other Collections, or you may reassign Trades to a new TradeCollection like in my previous example.

                More information on using Collections can be referenced in Microsoft's documentation and with C# examples in other projects. These external resources can be useful resources for learning a C# concept that you would like to apply with NinjaScript.

                Another tip would be to use IntelliPrompt (the Intellisense-like functionality that we see in the NinjaScript Editor) to see methods and properties that are available in a certain object. This can provide clues to what you could use to write your code. You will see this information after typing the object name and adding a period.

                Collections (Microsoft Docs) - https://docs.microsoft.com/en-us/dot...ramework-4.5.2

                Intelliprompt - https://ninjatrader.com/support/help...elliprompt.htm
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Ok,thank you very much! I'll try it.

                  Comment


                    #10
                    I went through the Microsoft example, and tried many iterations using Trades.Clear() and still not clearing the Collection.
                    I tried to clear the "Trades" collection when Position.Market == Position.Market.Flat , and it's not clearing it. It still prints Cum profit from before when Position.Market was not flat.

                    Here's my code:

                    PHP Code:
                    TradeCollection Trades = new TradeCollection(truetrue);

                                foreach(
                    Trade trade in SystemPerformance.AllTrades)
                                    if (
                    trade.Entry.Instrument == BarsArray[1].Instrument)
                                        
                    Trades.Add(trade);
                                        
                                        
                                foreach(
                    Trade trade in Trades)                            
                                Print(
                    trade.Entry.Instrument);
                                Print(
                    Trades.TradesPerformance.Currency.CumProfit);
                            
                                
                                if (
                    Position.MarketPosition == MarketPosition.Flat)
                                
                    Trades.Clear();
                                Print(
                    Trades.TradesPerformance.Currency.CumProfit); 
                    I am sorry but I keep coming back to the same question, what am I missing?

                    Comment


                      #11
                      After testing this further it looks like you will need to call Remove() for all of the Trades you would like cleared, and clearing/creating a new TradeCollection alone will not create the expected result.

                      Please refer to the example below for some further direction. I should also note that I saw best results when creating a new TradeCollection whenever I needed to collect/recollect trades.

                      Code:
                      protected override void OnBarUpdate()
                      {	 
                              if(Position.MarketPosition == MarketPosition.Flat)
                      		EnterLong();
                      	else
                      		ExitLong();
                       
                      	TradeCollection Trades = new TradeCollection(true, true);
                      	
                      	if(Position.MarketPosition != MarketPosition.Flat)
                      	{
                              foreach(Trade trade in SystemPerformance.AllTrades) 
                                  if (trade.Entry.Instrument == BarsArray[1].Instrument) 
                                      Trades.Add(trade);
                      	}
                      	else
                      	{
                      		foreach(Trade trade in Trades)
                      			Trades.Remove(trade);
                      	}
                      		
                              Print(String.Format("Position: {0}, Performance: {1}", Position.MarketPosition, Trades.TradesPerformance.Currency.CumProfit)); 
                      }
                      Attached Files
                      JimNinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by [email protected], Yesterday, 07:45 AM
                      1 response
                      7 views
                      0 likes
                      Last Post koganam
                      by koganam
                       
                      Started by tgn55, Today, 07:22 AM
                      0 responses
                      2 views
                      0 likes
                      Last Post tgn55
                      by tgn55
                       
                      Started by U0000999, Today, 05:17 AM
                      0 responses
                      4 views
                      0 likes
                      Last Post U0000999  
                      Started by Prataturry89, Today, 03:40 AM
                      0 responses
                      5 views
                      0 likes
                      Last Post Prataturry89  
                      Started by UltraFastKetoBoost28, Today, 02:43 AM
                      0 responses
                      6 views
                      0 likes
                      Last Post UltraFastKetoBoost28  
                      Working...
                      X