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 get current unrealised PnL of 2 or more instruments

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

    How to get current unrealised PnL of 2 or more instruments

    Hello,
    I want to know how one can get the current open unrealized PnL of 2 different instruments. Suppose a strategy opens 2 orders for 2 instruments in long and short positions, then is there a method to get current total unrelized pnL? Thanks.

    #2
    Hello woodies,

    The Positions collection will hold a Position object for all instruments added to the Strategy.


    With this you can use the GetUnrealizedProfitLoss().
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello,

      Thanks for the link. Will try this method.

      Comment


        #4
        Code:
        else if (State == State.Configure)
        {
        //Add 2 data series of 2 instruments
        AddDataSeries(PrimaryInstrument, BarsPeriodType.Second,5); //index1
        AddDataSeries(HedgeInstrument, BarsPeriodType.Second,5); //index2
        
        }
        
        private void GoLongSpread()
        {
        EnterLong(1,Qty1,"");
        EnterShort(2,Qty2,"");
        }
        
        private void GoShortSpread()
        {
        EnterShort(1,Qty1,"");
        EnterLong(2,Qty2,"");
        }
        
        private void CloseSpread()
        {
        if(//BarsInProgress==1 &&
        Positions[1].MarketPosition==MarketPosition.Long)
        {
        ExitLong();
        }
        else if(//BarsInProgress==2 &&
        Positions[2].MarketPosition==MarketPosition.Short)
        {
        ExitShort();
        }
        if(//BarsInProgress==1 &&
        Positions[1].MarketPosition==MarketPosition.Short)
        {
        ExitShort();
        }
        else if(//BarsInProgress==2 &&
        Positions[2].MarketPosition==MarketPosition.Long)
        {
        ExitLong();
        }
        
        return;
        }
        Hello,
        I have been working with the previous guidance for opening and closing the spread.
        When opening the spread, the method works smoothly but when closing, it closes only the primary instrument and not the hedge or secondary instrument.
        I have tried the Positions[index] and also BarsInProgress == index conditions , but both ways it does not close the spread fully.

        I have also tried to code the individual order handling using Order objects and tracking the orders in OnOrderUpdate and OnExecution methods, but the EnterLong() or short methods are working equally well. Also for spread trading, it is necessary to scalein and scale out, so keeping track of orders with Order object becomes complicated.
        The Flatten() method works for closing the spread order but disables the strategy.
        Can you offer a solution for properly closing a spread order?
        Thanks.

        Comment


          #5
          Hello woodies,

          If you are placing orders to multiple instruments, use the overload that specifies the barsInProgressIndex.

          ExitLong(int barsInProgressIndex, int quantity, string signalName, string fromEntrySignal)


          Place orders to all of the specific bars in progress you would like orders placed to.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Thank you, that worked. I tried that overload earlier with indexing but leaving other parameters empty.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by usazencort, Today, 01:16 AM
            0 responses
            1 view
            0 likes
            Last Post usazencort  
            Started by kaywai, 09-01-2023, 08:44 PM
            5 responses
            602 views
            0 likes
            Last Post NinjaTrader_Jason  
            Started by xiinteractive, 04-09-2024, 08:08 AM
            6 responses
            22 views
            0 likes
            Last Post xiinteractive  
            Started by Pattontje, Yesterday, 02:10 PM
            2 responses
            20 views
            0 likes
            Last Post Pattontje  
            Started by flybuzz, 04-21-2024, 04:07 PM
            17 responses
            230 views
            0 likes
            Last Post TradingLoss  
            Working...
            X