Announcement

Collapse
No announcement yet.

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 -

    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);

        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.

              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.

                  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

                            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 Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              669 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              378 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              111 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              575 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              581 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X