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

Mutiple Position exit question

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

    Mutiple Position exit question

    If I am using a strategy that opens 2 different positions, 1 in the eur/usd and 1 in the gbp/usd using BarsInProgress, and I get to a profit of 5 pips, what is the easiest way to simply exit the positions (prior to the close of the bar)???

    #2
    edgeliner, just set a profit target of 5 ticks / pips for each signal -

    BertrandNinjaTrader Customer Service

    Comment


      #3
      Bertrand,

      I don't want to have a profit target of 5 pips for each one....what I want to do is, let's say that EUR is up 17 and GBP/USD is down 12, or if GBP/USD is up 44 and EUR/USD is down 39, or if EUR/USD is up 6 and GBP is down 1, etc., there is an overall profit of 5. Does that make sense? In other words, if my strategy opens up both positions using BarsArray, I want to exit the positions when there is an overall profit a 5. How can I achieve this? I tried if (Postion.AvgPrice * TickSize == 5), then close the positions, but it doesn't close them????

      Comment


        #4
        You would then need to calculate the combined PnL of your positions and then check if this is exceeding your target to get flat -

        Code:
         
        double myPnl = Positions[0].GetProfitLoss(Closes[0][0], PerformanceUnit.Points) + Positions[1].GetProfitLoss(Closes[1][0], PerformanceUnit.Points);
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Thanks Bertrand...I'll give it a try.........

          Comment


            #6
            Bertrand.....Could you go a little deeper into that?? I'm not quite sure what that means????? Thanks!

            Comment


              #7
              This would give you your combined PnL for both positions you have open from BarsArray 0 and BarsArray 1 - Positions is used in this multiseries context.
              BertrandNinjaTrader Customer Service

              Comment


                #8
                Bertrand.... Please be more specific! I just want to figure out how to program if I have a total profit of 5 with my 2 positions ...... close out. I tried what you said and have had no luck. Can you please be more specific? Help me out with the "double" thing please......

                Comment


                  #9
                  edgeliner, please post the exact code you attempted using - my snippet was just a sample how to get the total profit summed for both of your positions. You would need to construct a conditions out of this to exit your open positions once your threshhold is reached or exceeded.
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    Here it is Bertrand..... I just want to exit when the profit is 5 pips, not neccessarily at the end of the bar......

                    protected override void Initialize()
                    {
                    Add("$GBPUSD", PeriodType.Minute, 1);
                    TraceOrders = true;
                    CalculateOnBarClose = true;
                    }

                    /// <summary>
                    /// Called on each bar update event (incoming tick)
                    /// </summary>
                    protected override void OnBarUpdate()
                    {

                    {
                    if (Historical)
                    return;
                    }

                    // Checks if OnBarUpdate() is called from an update on EUR/USD minute Bars
                    if (BarsInProgress == 0)

                    {
                    EnterLong(DefaultQuantity, "EUR/USD Long");
                    }

                    // Checks if OnBarUpdate() is called from an update on GBP/USD minute Bars
                    if (BarsInProgress == 1)
                    {
                    EnterShort(DefaultQuantity, "GBP/USD Short");
                    }

                    // Positions[0] is the position of the EUR/USD and Positions[1] is that of the GBP/USD
                    myPnl = Positions[0].GetProfitLoss(Closes[0][0], PerformanceUnit.Points) + Positions[1].GetProfitLoss(Closes[1][0], PerformanceUnit.Points);

                    if (myPnl == 5)
                    {
                    ExitLong();
                    ExitShort();
                    }

                    Comment


                      #11
                      edgeliner,

                      Please change CalculateOnBarClose from true to false.
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #12
                        Josh.....

                        If I compile this as is, it says "The name 'myPnl' does not exist in the current context

                        If I add:

                        public double myPnl

                        {
                        get { return myPnl; }
                        set { myPnl = Math.Max(1, value); }
                        }

                        to the #region Properties, it compiles fine, but when I try to load it to a chart, Ninjatrader crashes! Does that make sense?

                        Comment


                          #13
                          Here is what my #region Properties looks like:

                          #region Properties
                          [Description("")]
                          [Category("Parameters")]
                          public int MyInput0

                          {
                          get { return myInput0; }
                          set { myInput0 = Math.Max(1, value); }

                          }

                          public double myPnl

                          {
                          get { return myPnl; }
                          set { myPnl = Math.Max(1, value); }
                          }

                          #endregion

                          Comment


                            #14
                            myPnl should not be a public property, but a double variable you defined under the Variables section, then remove the one under Properties you created and it should compile well and don't crash upon being added to the chart. Thanks
                            BertrandNinjaTrader Customer Service

                            Comment


                              #15
                              Thanks Bertrand....... Can you lay out the way it should state the double variable you defined under the Variables section???

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by DawnTreader, 05-08-2024, 05:58 PM
                              17 responses
                              62 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by ttrader23, 05-08-2024, 09:04 AM
                              10 responses
                              48 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by agclub, 04-21-2024, 08:57 PM
                              6 responses
                              41 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by thumper57, 05-11-2024, 04:30 PM
                              9 responses
                              28 views
                              0 likes
                              Last Post thumper57  
                              Started by Seneca, 08-25-2020, 08:31 AM
                              2 responses
                              5,945 views
                              0 likes
                              Last Post Tin34
                              by Tin34
                               
                              Working...
                              X