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

Question on Multi instrument forex

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

    Question on Multi instrument forex

    Im tyring to program a Strategy that enters a long position in two separate pairs and exits both pairs when a certian profit target (dollar amount of unrealized P&L) is achieved.
    I think I may be close but on back tests is only entering the primary pair and not the secondary (it should enter both at the same time and exit both at the same time). It does seem to be exiting when the profit target is achieved but im not sure if it will work when both pairs are entered properly.

    If anyone can look at the following and give some tips I would really apprieciat it.

    Please excuse my ignorance, this is my first try at this.

    Thank You,

    #region Variables
    // Wizard generated variables
    // User defined variables (add any user defined variables below)
    #endregion
    ///<summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    ///</summary>
    protectedoverridevoid Initialize()
    {
    // Add an $USDCHF 2 minute Bars object to the strategy
    Add("$USDCHF", PeriodType.Minute, 2);

    // Note: Bars are added to the BarsArray and can be accessed via an index value
    // E.G. BarsArray[1] ---> Accesses the 1 minute Bars added above
    // Add ZeroLagStochs indicator to the chart for display
    // This only displays the indicator for the pimary Bars object (main instrument) on the chart
    Add(ZeroLagStochs());
    CalculateOnBarClose =
    true;
    }
    ///<summary>
    /// Called on each bar update event (incoming tick)
    ///</summary>
    protectedoverridevoid OnBarUpdate()
    {
    // OnBarUpdate() will be called on incoming tick events on all Bars objects added to the strategy
    // We only want to process events on our primary Bars object (main instrument) (index = 0) which
    // is set when adding the strategy to a chart
    if (BarsInProgress != 0)
    return;

    // Condition set 1
    if (CrossAbove(ZeroLagStochs().Mov, 75, 1))
    {
    //Submits buy market order for EURUSD
    EnterLong(100000, "long");
    //Submits buy market order for USDCHF
    EnterLong(120000, "BUY $USDCHF");
    }
    // Condition set 2
    if (Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) >= 200)
    {
    //Exits EURUSD long position when total unrealized P&L reaches $200.00
    ExitLong("", "long");
    //Exit CHF
    ExitLong("", "Buy $USDCHF");
    }
    }
    #region Properties
    #endregion
    }
    }

    #2
    You would need to do something like:

    //Submits buy market order for EURUSD
    EnterLong(100000, "long"
    );
    //Submits buy market order for USDCHF
    EnterLong(1, 120000, "BUY $USDCHF");


    Notice the "1" I have added which represents the secondary series.

    Check out the following Help Guide section and scroll down to the "Working with a Multi-Instrument Strategy" section.

    RayNinjaTrader Customer Service

    Comment


      #3
      Multi instrument

      Thanks Ray, I tried making the change you pointed out in you last post but it just made it only enter into the secondary pair. I read the section you asked me to. I have made a few changes but I still cant seem to get the Stategy to enter into both instruments. Can you please review what I have changed? Maybe you can steer me in the right direction.

      Thank you,

      protectedoverridevoid Initialize()
      {
      // Add an $USDCHF 2 minute Bars object to the strategy
      Add("$USDCHF", PeriodType.Minute, 2);

      // Note: Bars are added to the BarsArray and can be accessed via an index value
      // E.G. BarsArray[1] ---> Accesses the 2 minute Bars added above

      // Add ZeroLagStochs indicator to the chart for display
      // This only displays the indicator for the pimary Bars object (main instrument) on the chart
      Add(ZeroLagStochs());
      CalculateOnBarClose = true;
      }
      ///<summary>
      /// Called on each bar update event (incoming tick)
      ///</summary>
      private IOrder entryOrder = null;

      protectedoverridevoid OnBarUpdate()
      {
      // OnBarUpdate() will be called on incoming tick events on all Bars objects added to the strategy
      // We only want to process events on our primary Bars object (main instrument) (index = 0) which
      // is set when adding the strategy to a chart
      if (BarsInProgress != 0)
      return;

      // Condition set 1
      if (CrossAbove(ZeroLagStochs().Mov, 75, 1))
      {
      // Submit an order for EURUSD in the context of EURUSD bar update event
      if (entryOrder == null)
      entryOrder = EnterLongLimit(0, true, 100000,Close[1], "long");



      // Submit an order for USDCHF in the context of EURUSD bar update event
      if (entryOrder == null)
      entryOrder = EnterLongLimit(1, true, 120000,Close[1], "BUY $USDCHF");
      }
      // Condition set 3
      if (Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) >= 200)
      {
      //Exits EURUSD long position when total unrealized P&L reaches $200.00
      ExitLong("", "long");
      //Exit CHF
      ExitLong(1,"", "Buy $USDCHF");
      }
      }
      #region Properties
      #endregion
      }
      }

      Comment


        #4
        Don't see anything popping out at me.

        You will need to debug your strategy. Here is a tip we put out on that in case you have never seen it.

        RayNinjaTrader Customer Service

        Comment


          #5
          Multi instrument

          Thanks again Ray, You have been a big help. I would like to use the "Print()" command to try to find some of the bugs but I am having trouble getting it to work. Would you mind showing me an example of how I would attach it on the codes below?

          Thank you,

          // Condition set 2
          if ( Position.GetProfitLoss( Close [0],PerformanceUnit.Currency) >= 200)
          {
          {
          ExitLong(
          "", "long");

          ExitLong(
          1,"", "Buy $USDCHF");
          }

          TraceOrders =
          false;
          }

          Comment


            #6
            First, put TraceOrders in Initialize() method:

            Then something like:

            if (yourCondition == true)
            {
            Print("Whatever information you want to see here");
            }
            RayNinjaTrader Customer Service

            Comment


              #7
              Multi instrument

              I think I almost have a working strategy. The only problem I am having touble getting the strategy to close all positions when a certain overall unrealized profit is met. For example: If the EURUSD = $1,000 and the USDCHF = $800 for a net Unrealized PnL of $200 then I want the strategy to close both positions. Then continue to search for new positions. I cant seem to find anything specific to this. If anyone could help me that would great.

              Thank you for all your help so far.

              This is what I have currently, but it is only closing the primary instrument when when it reaches a profit of <= $200 and not closing the secondary ever.

              ///<summary>
              /// Called on each bar update event (incoming tick)
              ///</summary>
              protectedoverridevoid OnBarUpdate()

              {

              if (BarsInProgress != 0)
              return;

              // Condition set 1
              if (ZeroLagStochs().Mov[0] > ZeroLagStochs().Smoothed[0]
              && CrossAbove(Stochastics(
              3, 5, 3).D, Stochastics(3, 5, 3).K, 1))
              {
              EnterLong(
              100000, "BUY EURUSD");
              }

              {
              // Condition set 2
              if (Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) <= -200)
              {
              EnterLong(
              1,130000, "BUY USDCHF");
              }
              }

              {
              // Condition set 3
              if (Position.GetProfitLoss(Close[0],PerformanceUnit.Currency) >= 200)

              {
              ExitLong();
              }
              Print(
              "Open PnL: " + Position.GetProfitLoss(Close[0], PerformanceUnit.Points));
              }
              }

              Comment


                #8
                Make sure you call ExitLong() for both instruments.

                ExitLong(int barsInProgressIndex, int quantity,string signalName,string fromEntrySignal)
                RayNinjaTrader Customer Service

                Comment


                  #9
                  Multi instrument

                  Thanks for responding so quickly Ray. I did try that however it only will close each trade when each individual instrument gets to a Unrealized PnL of >= $200. I want it to close all trades at a total unrealized PnL >= $200. So basically when the stategy takes profit there should be no trades left open. Then it should start the process over.

                  I believe this is what you were asking me to do in the last post.


                  if (Position.GetProfitLoss(Close[0],PerformanceUnit.Currency) >= 200)

                  {
                  ExitLong(
                  "exit","BUY EURUSD");
                  ExitLong(
                  1,"exit","BUY USDCHF");
                  }

                  Comment


                    #10
                    Callling ExitLong() for both bar series will exit both positions at any time. Must be a logic flaw with your code, you can enable TraceOrders to see what may be happening.

                    Maybe try something simple like:

                    ExitLong(0, Positions[0].Quantity, "","");
                    ExitLong(
                    1, Positions[1].Quantity, "","");
                    RayNinjaTrader Customer Service

                    Comment


                      #11
                      Multi instrument

                      ok Ray, I did what you suggested and it seems to be exiting at the same time like it should be.

                      I found another problem that you may no the answer to right off hand. The stategy does not seem to be looking at the TOTAL unrealized PNL but only the primary instrument PnL. It should only close all of the positions when the TOTAL unrealized PnL is >= $200. This is what I am using currently, can you suggest an alternative?

                      Thank you,

                      if (Position.GetProfitLoss(Close[0],PerformanceUnit.Currency) >= 200)
                      {
                      ExitLong(
                      0, Positions[0].Quantity, "exit","BUY EURUSD");
                      ExitLong(
                      1, Positions[1].Quantity, "exit","BUY USDCHF");
                      }

                      Comment


                        #12
                        Try something like:

                        if (Positions[0].GetProfitLoss(Closes[0][0],PerformanceUnit.Currency) + Positions[1].GetProfitLoss(Closes[1][0],PerformanceUnit.Currency) >= 200)
                        {
                        ExitLong(
                        0, Positions[0].Quantity, "exit","BUY EURUSD");
                        ExitLong(
                        1, Positions[1].Quantity, "exit","BUY USDCHF");
                        }
                        RayNinjaTrader Customer Service

                        Comment


                          #13
                          Multi instrument

                          In the backtests the strategy appears to be working properly. However, when I try to test it on my MBT Demo acct. the strategy appears to think it already has a position open when I start it. Therefore wont open a new position due to entries per direction. Is there something I have to do clear any old data?

                          Attached is a snipet of my Output screen. But neither my demo or sim 101 are showing open position.

                          Thank you,
                          Attached Files

                          Comment


                            #14
                            See if this post clears things up - http://www.ninjatrader-support.com/v...ead.php?t=4033
                            RayNinjaTrader Customer Service

                            Comment


                              #15
                              Multi instrument

                              I read through all I could find on 'Strategy Position vs. Account Position"

                              I have manually entered trades into my MB Demo that I have stategy set to run on as was suggested. It still is ignoring orders based on "Entriesperdirection properties" New orders are based on unrealized PnL which the strategy position and the acct. positions never match.

                              I also tried starting the strategy before the new session but it just continues through it. I tried cheking the box "Flatten all" under the "strategy" tab but that cancels the strategy also.

                              The first time I ran the Strategy in real time it seemed to run fine.

                              Am I missing somthing?
                              Thank you,

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by algospoke, Yesterday, 06:40 PM
                              2 responses
                              20 views
                              0 likes
                              Last Post algospoke  
                              Started by ghoul, Today, 06:02 PM
                              3 responses
                              14 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by jeronymite, 04-12-2024, 04:26 PM
                              3 responses
                              45 views
                              0 likes
                              Last Post jeronymite  
                              Started by Barry Milan, Yesterday, 10:35 PM
                              7 responses
                              21 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by AttiM, 02-14-2024, 05:20 PM
                              10 responses
                              181 views
                              0 likes
                              Last Post jeronymite  
                              Working...
                              X