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

combined P&L, multi instrument strategy

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

    combined P&L, multi instrument strategy

    Hi,

    Code:
            if (Positions[0].GetProfitLoss(Close[0], PerformanceUnit.Currency) + Positions[1].GetProfitLoss(Close[0], PerformanceUnit.Currency) > 0.00 )          
                {
                ExitLong("","");
                ExitLong(1,"","");
                   }
    the above should be calculating the combined unrealized P&L of the two instruments in the strategy. Correct? It seems to be not doing that. it doesn't exit the positions. neither of the positions are exited.

    #2
    calhawk01, in which BarsInProgress do you call that portion? You pass in Close[0] as reference price for both instruments in, that's likely where it breaks. I would first add in a print to see exactly when it triggers and what you calculate value wise.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Bertrand View Post
      calhawk01, in which BarsInProgress do you call that portion? You pass in Close[0] as reference price for both instruments in, that's likely where it breaks. I would first add in a print to see exactly when it triggers and what you calculate value wise.

      Code:
              if (BarsInProgress == 0 && BarsInProgress ==1 && Positions[1].MarketPosition == MarketPosition.Long && Positions[0].MarketPosition == MarketPosition.Long
                  && Positions[0].GetProfitLoss(Close[0], PerformanceUnit.Currency) + Positions[1].GetProfitLoss(Close[0], PerformanceUnit.Currency) > 0.00)
                  {
                  ExitLong("PrimaryBuy","PrimarySell");
                  ExitLong(1,"HedgeBuy","HedgeSell");
                  }
      i tried adding both barsinprogress. still doesnt work. please help!

      Comment


        #4
        I would not execute from both, as you would need to have the situtation then where both actually are called for the same timestamp, primary should be fine.

        Also would pass in then Closes[1] for the second call to calculate the pnl based on the secondary instruments close price and not the primary as in your call.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Bertrand View Post
          I would not execute from both, as you would need to have the situtation then where both actually are called for the same timestamp, primary should be fine.

          Also would pass in then Closes[1] for the second call to calculate the pnl based on the secondary instruments close price and not the primary as in your call.

          I'm having a hard time following your reply.

          And i tried changing the secondary "close" to close[1].. the second hard position still did not exit. only the primary did.

          can you please give me an example? ive been working on the for past 24 hours would really appreciate your help..

          Code:
                  if (BarsInProgress == 0 && BarsInProgress ==1 && Positions[1].MarketPosition == MarketPosition.Long && Positions[0].MarketPosition == MarketPosition.Long
                      && Positions[0].GetProfitLoss(Close[0], PerformanceUnit.Currency) + Positions[1].GetProfitLoss(Close[1], PerformanceUnit.Currency) > 0.00)
                      {
                      ExitLong("PrimaryBuy","PrimarySell");
                      ExitLong(1,"HedgeBuy","HedgeSell");
                      }
          Did not work..

          Comment


            #6
            Originally posted by calhawk01 View Post
            I'm having a hard time following your reply.

            And i tried changing the secondary "close" to close[1].. the second hard position still did not exit. only the primary did.

            can you please give me an example? ive been working on the for past 24 hours would really appreciate your help..

            Code:
                    if (BarsInProgress == 0 && BarsInProgress ==1 && Positions[1].MarketPosition == MarketPosition.Long && Positions[0].MarketPosition == MarketPosition.Long
                        && Positions[0].GetProfitLoss(Close[0], PerformanceUnit.Currency) + Positions[1].GetProfitLoss(Close[1], PerformanceUnit.Currency) > 0.00)
                        {
                        ExitLong("PrimaryBuy","PrimarySell");
                        ExitLong(1,"HedgeBuy","HedgeSell");
                        }
            Did not work..
            • "BarsInProgress == 0 && BarsInProgress ==1" is an impossible condition, and means that your filter will always be identically false.
            • Close based on BarsArray[1] is Closes[1], not Close[1].

            You can try this:
            Code:
            if (BarsInProgress == 0 
            && Positions[1].MarketPosition == MarketPosition.Long 
            && Positions[0].MarketPosition == MarketPosition.Long 
            && Positions[0].GetProfitLoss(Close[0], PerformanceUnit.Currency) + Positions[1].GetProfitLoss([B][COLOR=Red]Closes[1][0][/COLOR][/B], PerformanceUnit.Currency) > 0.00)

            Comment


              #7
              Originally posted by koganam View Post
              • "BarsInProgress == 0 && BarsInProgress ==1" is an impossible condition, and means that your filter will always be identically false.
              • Close based on BarsArray[1] is Closes[1], not Close[1].

              You can try this:
              Code:
              if (BarsInProgress == 0 
              && Positions[1].MarketPosition == MarketPosition.Long 
              && Positions[0].MarketPosition == MarketPosition.Long 
              && Positions[0].GetProfitLoss(Close[0], PerformanceUnit.Currency) + Positions[1].GetProfitLoss([B][COLOR=Red]Closes[1][0][/COLOR][/B], PerformanceUnit.Currency) > 0.00)

              Still doesnt work i feel like pulling my hair out... below is the entire strat

              Code:
                      protected override void OnBarUpdate()
              {
                  
                [B]          if (BarsInProgress == 0 
                          && Positions[1].MarketPosition == MarketPosition.Long 
                          && Positions[0].MarketPosition == MarketPosition.Long 
                          && Positions[0].GetProfitLoss(Close[0], PerformanceUnit.Currency) + Positions[1].GetProfitLoss(Closes[1][0], PerformanceUnit.Currency) > 0.00)        
                          
                          {
                          ExitLong("","");
                          ExitLong(1,"","");
                          }[/B]
                  
                          
                       if (Positions[0].MarketPosition == MarketPosition.Flat && Positions[1].MarketPosition == MarketPosition.Flat && Close[0] > 0)
                          {
                          EnterLong(0,PrimaryQuantity, "PrimaryBuy");
                          }
                      
                      if (Positions[1].MarketPosition == MarketPosition.Flat && Close[0] < (Positions[0].AvgPrice-hedge))
                          {
                          EnterLong(1,HedgeQuantity, "HedgeBuy");
                          }
                          
              
              }
              i've tried moving the bolded at the end and at the beginning..

              Comment


                #8
                Originally posted by calhawk01 View Post
                Still doesnt work i feel like pulling my hair out... below is the entire strat

                Code:
                        protected override void OnBarUpdate()
                {
                    
                  [B]          if (BarsInProgress == 0 
                            && Positions[1].MarketPosition == MarketPosition.Long 
                            && Positions[0].MarketPosition == MarketPosition.Long 
                            && Positions[0].GetProfitLoss(Close[0], PerformanceUnit.Currency) + Positions[1].GetProfitLoss(Closes[1][0], PerformanceUnit.Currency) > 0.00)        
                            
                            {
                            ExitLong("","");
                            ExitLong(1,"","");
                            }[/B]
                    
                            
                         if (Positions[0].MarketPosition == MarketPosition.Flat && Positions[1].MarketPosition == MarketPosition.Flat && Close[0] > 0)
                            {
                            EnterLong(0,PrimaryQuantity, "PrimaryBuy");
                            }
                        
                        if (Positions[1].MarketPosition == MarketPosition.Flat && Close[0] < (Positions[0].AvgPrice-hedge))
                            {
                            EnterLong(1,HedgeQuantity, "HedgeBuy");
                            }
                            
                
                }
                i've tried moving the bolded at the end and at the beginning..
                Is that the entirety of your OBU handler? If so, you are missing a CurrentBars check to be sure that you have valid data. There should be an entry in your log about the error. Look up "CurrentBars" in the NT Help.

                ref: http://www.ninjatrader.com/support/h...urrentbars.htm

                Comment


                  #9
                  Originally posted by koganam View Post
                  Is that the entirety of your OBU handler? If so, you are missing a CurrentBars check to be sure that you have valid data. There should be an entry in your log about the error. Look up "CurrentBars" in the NT Help.

                  ref: http://www.ninjatrader.com/support/h...urrentbars.htm

                  i added
                  Code:
                          if (CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired )
                          return;
                  but it did not make any difference. it's still not executing the exit order for the secondary data series! it enters the trade but doesnt want to exit it.

                  NT support can you please advise?

                  Comment


                    #10
                    Originally posted by calhawk01 View Post
                    i added
                    Code:
                            if (CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired )
                            return;
                    but it did not make any difference. it's still not executing the exit order for the secondary data series! it enters the trade but doesnt want to exit it.

                    NT support can you please advise?

                    the only thing that does work is ''flatten all position'' feature. but that shuts the strategy down. any way to keep the strategy going?

                    Comment


                      #11
                      Output:

                      5/2/2014 10:00:00 AM Ignored PlaceOrder() method at 5/2/2014 10:00:00 AM: Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='HedgeSell' FromEntrySignal='HedgeBuy' Reason='SignalName does not have a matching FromEntrySignal to exit'
                      But the signalname does match...
                      Code:
                       if (Positions[1].MarketPosition == MarketPosition.Flat && Close[0] < (Positions[0].AvgPrice-hedge))
                                  {
                                  EnterLong(1,HedgeQuantity, "HedgeBuy");
                                  }
                      is my entry

                      Comment


                        #12
                        finally got it working.. the issue was the Exit command

                        the correct one is
                        Code:
                         ExitLong(PrimaryQuantity,"PrimaryCombined","PrimaryBuy");
                                    ExitLong(1,HedgeQuantity,"HedgeCombined", "HedgeBuy");
                        question:

                        Code:
                        Positions[1].GetProfitLoss(GetCurrentBid(1), PerformanceUnit.Currency)
                        gives the unrealized P&L based on current bid on secondary instrument. correct?

                        Comment


                          #13
                          Glad you got it working calhawk01, that is correct for GetCurrentBid(1).
                          BertrandNinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by wzgy0920, 04-20-2024, 06:09 PM
                          2 responses
                          27 views
                          0 likes
                          Last Post wzgy0920  
                          Started by wzgy0920, 02-22-2024, 01:11 AM
                          5 responses
                          32 views
                          0 likes
                          Last Post wzgy0920  
                          Started by wzgy0920, 04-23-2024, 09:53 PM
                          2 responses
                          49 views
                          0 likes
                          Last Post wzgy0920  
                          Started by Kensonprib, 04-28-2021, 10:11 AM
                          5 responses
                          193 views
                          0 likes
                          Last Post Hasadafa  
                          Started by GussJ, 03-04-2020, 03:11 PM
                          11 responses
                          3,235 views
                          0 likes
                          Last Post xiinteractive  
                          Working...
                          X