Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trading based on EMA of 2nd Instrument

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

    Trading based on EMA of 2nd Instrument

    I would like to execute a long or short trade filtered by whether price of a second instrument is above or below an exponential moving average. I looked at the sample code but it is basing the decision on whether the value of the average on the first instrument is greater than the average of the second instrument and when I try to construct a statement I get all kinds of indexing errors. Here is the code I am using giving me errors. How should it look if I want to simply go long or short Instrument 1 if price is greater or lesser than the EMA of instrument 2?
    Code:
     protected override void Initialize()
            {
    			Add("DX 06-12", PeriodType.Minute, 1);
                CalculateOnBarClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                // Condition set 1
                if (Close([1][0] > EMADN(21)[1][0]))
                {
                    EnterLong(DefaultQuantity, "");
                }
    I also tried this form, and it gives me fewer errors but still doesn't compile. All the errors refer to the line right after Conditions set 1, so I expect I'm just not getting the syntax for the second instrument reference correct.
    [CODE protected override void Initialize()
    {
    Add("DX 06-12", PeriodType.Minute, 1);
    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (Close(BarsArray[1][0] > EMADN(BarsArray[1],21)[0]))
    {
    EnterLong(DefaultQuantity, "");
    }

    // Condition set 2
    if (Close[0] < EMADN(21)[0])
    {
    EnterShort(DefaultQuantity, "");
    }
    }][/CODE]

    I'm sure this is a simple formatting problem but I can't seem to figure it out.
    Thanks for your help.
    DavN

    #2
    Daven,

    Thanks for your note, I am happy to assist you.

    Close(BarsArray[1][0] > EMADN(BarsArray[1],21)[0]

    You should use Closes[1][0]. The indicator may also be an issue if EMADN has custom plots inside of it. Could you post this indicator?
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Referencing second instrument continued

      I'll save you the hassle of responding. I found the answer a little deeper in the User Guide. the key is to use Closes[1][0], (or Highs, Lows, etc.) to reference the second insrument price value. This is the format that works, (at least compiles) for the second instrument reference):
      Code:
              protected override void Initialize()
              {
      			Add("DX 06-12", PeriodType.Minute, 1);
                  CalculateOnBarClose = true;
              }
      
              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
                  // Condition set 1
                  if (Closes[1][0] > EMADN(BarsArray[1],21)[0])
                  {
                      EnterLong(DefaultQuantity, "");
                  }
      
                  // Condition set 2
                  if (Closes[1][0] < EMADN(BarsArray[1],21)[0])
                  {
                      EnterShort(DefaultQuantity, "");
                  }

      Comment


        #4
        Daven,

        Please let me know if you require additional assistance.
        Adam P.NinjaTrader Customer Service

        Comment


          #5
          Referencing Second Instrument continued.

          It appears that I do need some further assistance. I'm still working on this strategy, filtering trades on the primary instrument, with the value of a second instrument. When I use the following code to enter a trade on the primary instrument, it appears that I am also entering a trade on the secondary instrument. I am running real-time on a sim account, and the primary instrument is trading as it should but I also seem to be getting trades on the secondary instrument. When I have two instruments on a chart do I have to designate which instrument should be traded with the order entry, or does it automatically default to the primary instrument? Here's the code: (Sorry about the commented out code but I am still experimenting with the system).

          Code:
                      {
                          EnterLong(Cont1, "T1");
          //                EnterLongLimit(Cont2, WSTideBand8Env(2.5, 14, 1, 11, 0.66, 3).UpperBand[0] + Offset * TickSize, "T2");
                      	SetStopLoss("T1", CalculationMode.Ticks, Stop, false);
          //            	SetTrailStop("T2", CalculationMode.Ticks, Trl_Stop, false);
          				Variable0 = Position.AvgPrice;
                      }

          Comment


            #6
            Hi daven,

            For this, you can use BarsInProgress to filter updates to only one series.

            If you never want strategy to process updates except for the primary series, add this line to top of OnBarUpdate():

            if (BarsInProgress != 0) return;
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              So the code I submitted would actually process trades on both instruments? I want to update the data but I do not want to trade both instruments. Is that what the code you suggested will do?
              Thanks
              DaveN

              Comment


                #8
                Yes, if you don't filter OnBarUpdate() with BarsInProgress, then the code you have is run for every series in your strategy. BarsInProgress is what's used to identify and filter which series is the one raising OnBarUpdate() event.

                The code I suggested will only allow your code to be ran during updates to primary series. Since you use Closes[1][0] you are still able to access your secondary instrument values.
                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  Put it in the code and reloaded it. Seems to be working. Thanks. I really appreciate your help.
                  DaveN

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by PaulMohn, Today, 12:36 PM
                  0 responses
                  2 views
                  0 likes
                  Last Post PaulMohn  
                  Started by love2code2trade, 04-17-2024, 01:45 PM
                  4 responses
                  38 views
                  0 likes
                  Last Post love2code2trade  
                  Started by alifarahani, Today, 09:40 AM
                  2 responses
                  14 views
                  0 likes
                  Last Post alifarahani  
                  Started by junkone, Today, 11:37 AM
                  3 responses
                  20 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by frankthearm, Yesterday, 09:08 AM
                  12 responses
                  44 views
                  0 likes
                  Last Post NinjaTrader_Clayton  
                  Working...
                  X