Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

strategy comparing two instruments

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

    strategy comparing two instruments

    how would I go about creating a strategy where... say for example


    if ES is up from 9:30am to 10:00am, enterlong YM at 11am and
    if ES is down from 9:30am to 10:00am, entershort YM at 11am

    I'm assuming I would run a backtest on YM, but how do I call upon the instrument ES.

    Thanks in advance

    #2
    Here is a great start for Multi-Instrument strategies.

    RayNinjaTrader Customer Service

    Comment


      #3
      I looked at the below code and was not sure...
      if I want to know if ES is up between 9:30am and 10:00am...
      if so, Enterlong in YM at 11am

      do I have to first place the below code in Initialize()

      Add("ES 12-07", PeriodType.Minute, 1);


      and then how do I call upon it? Sorry if I mangled it up. Still new at programming

      if ("ES 12-07".ToTime(93000)<=ToTime(100000))
      Enterlong(1).ToTime(110000);






      /// This method is used to configure the strategy and is called once before any strategy method is called.
      ///</summary>
      protectedoverridevoid Initialize()
      {
      // Add an ER2 09-07 1 minute Bars object to the strategy
      Add("ER2 09-07", PeriodType.Minute, 1);

      // 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 RSI and ADX indicators to the chart for display
      // This only displays the indicators for the pimary Bars object (main instrument) on the chart
      Add(RSI(14, 0));
      Add(ADX(
      14));

      // Sets a 20 tick trailing stop for an open position
      SetTrailStop(CalculationMode.Ticks, 20);

      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;

      // Checks if the 14 period ADX on both instruments are trending (above a value of 30)
      if (ADX(14)[0] > 30 && ADX(BarsArray[1], 14)[0] > 30)
      {
      // If RSI crosses above a value of 30 then enter a long position via a limit order
      if (CrossAbove(RSI(14, 0), 30, 1))
      {
      // Draws a square 1 tick above the high of the bar identifying when a limit order is issued
      DrawSquare("My square" + CurrentBar, 0, High[0] + 1 * TickSize, Color.DodgerBlue);

      // Enter a long position via a limit order at the current ask price
      EnterLongLimit(GetCurrentAsk(), "RSI");
      }
      }

      // Any open long position will exit if RSI crosses below a value of 75
      // This is in addition to the trail stop set in the Initialize() method
      if (CrossBelow(RSI(14, 0), 75, 1))
      ExitLong();
      }

      Comment


        #4
        Right z32000. First place Add("ES 12-07", PeriodType.Minute, 1); into the Initialize() method. Then in the OnBarUpdate() you want to run your checks on the BarsInProgress = 1 and place your trades in the context of BarsInProgress = 0 (when you load your strategy run it on the YM not the ES).

        Code:
        if (BarsInProgress == 1)
        {
             if (ToTime(Time[0]) == 93000)
                  esOpen = Open[0];
             else if (ToTime(Time[0]) == 100000)
                  esClose = Close[0];
             if (esClose > esOpen)
                  tradeYM = true;
        }
        
        if (BarsInProgress == 0)
        {
             if (tradeYM == true)
                  EnterLong();
        }
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Thanks Josh! You're a genius
          I'm not sure if Ninjatrader takes this into account but some instruments...
          such as hong kong futures where the market opens the night before...

          So say if I wanted to know if the hong kong market was up between 10pm and 2am and if so, go long on ES the next day...

          does this make things much more tricker? I would think NT only compares two instruments within the same time period.


          Originally posted by Josh View Post
          Right z32000. First place Add("ES 12-07", PeriodType.Minute, 1); into the Initialize() method. Then in the OnBarUpdate() you want to run your checks on the BarsInProgress = 1 and place your trades in the context of BarsInProgress = 0 (when you load your strategy run it on the YM not the ES).

          Code:
          if (BarsInProgress == 1)
          {
               if (ToTime(Time[0]) == 93000)
                    esOpen = Open[0];
               else if (ToTime(Time[0]) == 100000)
                    esClose = Close[0];
               if (esClose > esOpen)
                    tradeYM = true;
          }
           
          if (BarsInProgress == 0)
          {
               if (tradeYM == true)
                    EnterLong();
          }

          Comment


            #6
            Add the HK instrument like you did the ES instrument.

            Code:
            if (BarsInProgress == 1)
            {
                 if (ToTime(Time[0]) == 93000 && ToDay(Time[0]) == (ToDay(DateTime.Now) - 1))
                      hkOpen = Open[0];
                 else if (ToTime(Time[0]) == 100000 && ToDay(Time[0]) == (ToDay(DateTime.Now) - 1))
                      hkClose = Close[0];
                 if (hkClose > hkOpen)
                      tradeHK = true;
            }
            
            if (BarsInProgress == 0)
            {
                 if (tradeHK == true && ToDay(Time[0]) == ToDay(DateTime.Now))
                      EnterLong();
            }
            DateTime.Now will only work in real-time situations because it is checking your system clock. If you want this to work in backtest you will need to go and save some time variables in your code for the comparison.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              I'm not quite sure what do you mean by save some time variables...
              I am doing this for backtesting first and foremost, so it's rather tough not being able to use the DateTime.Now...



              Originally posted by Josh View Post
              Add the HK instrument like you did the ES instrument.

              Code:
              if (BarsInProgress == 1)
              {
                   if (ToTime(Time[0]) == 93000 && ToDay(Time[0]) == (ToDay(DateTime.Now) - 1))
                        hkOpen = Open[0];
                   else if (ToTime(Time[0]) == 100000 && ToDay(Time[0]) == (ToDay(DateTime.Now) - 1))
                        hkClose = Close[0];
                   if (hkClose > hkOpen)
                        tradeHK = true;
              }
               
              if (BarsInProgress == 0)
              {
                   if (tradeHK == true && ToDay(Time[0]) == ToDay(DateTime.Now))
                        EnterLong();
              }
              DateTime.Now will only work in real-time situations because it is checking your system clock. If you want this to work in backtest you will need to go and save some time variables in your code for the comparison.

              Comment


                #8
                Code:
                if (BarsInProgress == 1)
                {
                     if (ToTime(Time[0]) == 93000)
                          hkOpen = Open[0];
                     else if (ToTime(Time[0]) == 100000)
                     {
                          hkClose = Close[0];
                          if (hkClose > hkOpen)
                         {
                               tradeHK = true;
                               hkDay = ToDay(Time[0]);
                         }
                     }
                }
                
                if (BarsInProgress == 0)
                {
                     if (tradeHK == true && ToDay(Time[0]) == (hkDay + 1))
                     {
                          EnterLong();
                          tradeHK = false;
                          hkDay = 0;
                          hkOpen = 0;
                          hkClose = 0;
                     }
                }
                Something like this. I haven't tested this code so you'll need to play with it yourself.
                Last edited by NinjaTrader_JoshP; 11-20-2007, 12:22 AM.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  thanks Josh again I see you used the time from 9:30am to 10:00am..
                  but instead if it was 9:30am to 1:00am (the next day)...
                  is this possible?


                  Originally posted by Josh View Post
                  Code:
                  if (BarsInProgress == 1)
                  {
                       if (ToTime(Time[0]) == 93000)
                            hkOpen = Open[0];
                       else if (ToTime(Time[0]) == 100000)
                       {
                            hkClose = Close[0];
                            if (hkClose > hkOpen)
                           {
                                 tradeHK = true;
                                 hkDay = ToDay(Time[0]);
                           }
                       }
                  }
                   
                  if (BarsInProgress == 0)
                  {
                       if (tradeHK == true && ToDay(Time[0]) == (hkDay + 1))
                       {
                            EnterLong();
                            tradeHK = false;
                            hkDay = 0;
                            hkOpen = 0;
                            hkClose = 0;
                       }
                  }
                  Something like this. I haven't tested this code so you'll need to play with it yourself.

                  Comment


                    #10
                    All time comparisons are possible. You will just need to work out various techniques through the use of Time[0], ToTime(), and ToDay().
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      would I still need to define the variables hkDay, tradeHK, hkOpen and hkClose in the Variable and Properties section?

                      thanks

                      Originally posted by Josh View Post
                      All time comparisons are possible. You will just need to work out various techniques through the use of Time[0], ToTime(), and ToDay().

                      Comment


                        #12
                        Right. I provided you with different techniques you can use. You don't necessarily need to use the exact technique I've outlined under all circumstances.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          hi Josh, I just gave the code a try, but I don't think it works...

                          {
                          if (tradeHK == true && ToDay(Time[0]) == (hkDay + 1))
                          {
                          EnterLong();
                          tradeHK = false;
                          hkDay = 0;
                          hkOpen = 0;
                          hkClose = 0;
                          }
                          }


                          You wrote the above... but I don't see how this long position can ever be executed since your condition states... Enter long "if current date is equal to current date plus 1"...

                          I gave it a try and unfortunately it did not work...any ideas?
                          Thanks


                          Originally posted by Josh View Post
                          Right. I provided you with different techniques you can use. You don't necessarily need to use the exact technique I've outlined under all circumstances.

                          Comment


                            #14
                            The technique shown there is suppose to say "if the current date is the previous date + 1 (aka if current date is current date)". The hkDay variable is set in the BarsInProgress = 1. When your strategy goes through the historical data of the previous date and it evalutes to true it will set the variable to that date. I see what you mean it will never evalute to true. You will need to add a condition to prevent it from updating on the latest date.

                            The code I posted are just pseudocodes and have never been tested. They are to demonstrate theoretical techniques, but you will need to develop it to your exact needs on your own.

                            The idea behind what I posted is this. On timeframe 1 store the previous day's up/down trend in the opening hour. On timeframe 0 check to see if timeframe1 was up or down on the previous day and do something accordingly.
                            Josh P.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                            0 responses
                            566 views
                            0 likes
                            Last Post Geovanny Suaza  
                            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                            0 responses
                            330 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by Mindset, 02-09-2026, 11:44 AM
                            0 responses
                            101 views
                            0 likes
                            Last Post Mindset
                            by Mindset
                             
                            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                            0 responses
                            547 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by RFrosty, 01-28-2026, 06:49 PM
                            0 responses
                            548 views
                            1 like
                            Last Post RFrosty
                            by RFrosty
                             
                            Working...
                            X