Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How do I make trade enter 1 chart bar past higher timeframe?

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

    How do I make trade enter 1 chart bar past higher timeframe?

    Decided to make fresh post

    I have a condition that checks the MACD Histogram on a higher timeframe whether that be 30 minutes, 1 hour, etc...

    And then if that condition is met, to either enter into a long or short 1 chart bar past that timeframe.

    So, for example it would enter into a trade at 14:05, 15:05
    or
    14:35, 15:35

    I spoke with another representative that suggested creating a variable that added minutes to a Times function of the higher timeframe and only activating the trade if that exact time is met but this stopped any trades from occurring.

    #2
    Hello unpronounceable1700,

    Add the 30 minute or 1 hour series with AddDataSeries().





    Use the BarsArray[1], or Closes[1] series (where [1] is the index of the added series) as the input series for the MACD.

    In the script, reference the MACD value from the primary chart series BarsInProgress 0.

    In the scope of the class:
    Code:
    private MACD myMACDHigherTF;
    private bool sendOrder;
    In OnStateChange():
    Code:
    else if (State == State.Configure)
    {
    AddDataSeries(null, BarsPeriodType.Minute, 30);
    }
    else if (State == State.DataLoaded)
    {
    myMACDHigherTF = MACD(BarsArray[1], 12, 26, 9);
    }
    In OnBarUpdate():
    Code:
    if (CurrentBars[0] < 1 || CurrentBars[1] < 1)
    return;
    
    if (BarsInProgress == 0)
    {
    if (sendOrder == true)
    {
    EnterLong();
    sendOrder = false;
    }
    }
    else if (BarsInProgress == 1)
    {
    if (myMACDHigherTF.Default[0] > myMACDHigherTF.Avg[0]) // your condition checked during the 30 tf here
    {
    Print(string.Format("{0} | myMACDHigherTF.Default[0]: {2}", Time[0], myMACDHigherTF.Default[0]));
    sendOrder = true;
    }
    As long as the script is running with Calculate.OnBarClose, the value of myMACDHigherTF.Default[0] would change after the 30 minute bar closes, and then the bool would be set for the next update of the primary series during BarsInProgress 0.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello unpronounceable1700,

      Add the 30 minute or 1 hour series with AddDataSeries().





      Use the BarsArray[1], or Closes[1] series (where [1] is the index of the added series) as the input series for the MACD.

      In the script, reference the MACD value from the primary chart series BarsInProgress 0.

      In the scope of the class:
      Code:
      private MACD myMACDHigherTF;
      private bool sendOrder;
      In OnStateChange():
      Code:
      else if (State == State.Configure)
      {
      AddDataSeries(null, BarsPeriodType.Minute, 30);
      }
      else if (State == State.DataLoaded)
      {
      myMACDHigherTF = MACD(BarsArray[1], 12, 26, 9);
      }
      In OnBarUpdate():
      Code:
      if (CurrentBars[0] < 1 || CurrentBars[1] < 1)
      return;
      
      if (BarsInProgress == 0)
      {
      if (sendOrder == true)
      {
      EnterLong();
      sendOrder = false;
      }
      }
      else if (BarsInProgress == 1)
      {
      if (myMACDHigherTF.Default[0] > myMACDHigherTF.Avg[0]) // your condition checked during the 30 tf here
      {
      Print(string.Format("{0} | myMACDHigherTF.Default[0]: {2}", Time[0], myMACDHigherTF.Default[0]));
      sendOrder = true;
      }
      As long as the script is running with Calculate.OnBarClose, the value of myMACDHigherTF.Default[0] would change after the 30 minute bar closes, and then the bool would be set for the next update of the primary series during BarsInProgress 0.
      Testing that code out, I get this error
      Strategy 'test': Error on calling 'OnBarUpdate' method on bar 29: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

      Comment


        #4
        Hello unpronounceable1700,

        Apologies, that was an incorrect index in the print.

        Corrected this would appear:

        Print(string.Format("{0} | myMACDHigherTF.Default[0]: {1}", Time[0], myMACDHigherTF.Default[0]));
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hello unpronounceable1700,

          Apologies, that was an incorrect index in the print.

          Corrected this would appear:

          Print(string.Format("{0} | myMACDHigherTF.Default[0]: {1}", Time[0], myMACDHigherTF.Default[0]));
          Ah wow, I didn't even know a print command could disrupt the code like that. It's always the smallest little thing too.

          The trades go through now, but in the output it still is only showing them trading on the exact half hour, which would imply it's trading one half hour after the condition was met which skips a lot of time

          I wonder if this is possible with ninjascript, it should be. If not I may have to manually trade it.

          ---

          Also looking back on the list of trades on Tradingview, sometimes it's not always one bar after the half hour, in fact scrolling through all the trades in my most recent backtest is 2 bars after the half hour.

          But the main thing is, it trades only once per condition. And that condition is only checked every half hour

          Comment


            #6
            Hello unpronounceable1700,

            I am not able to reproduce. I've added the code to a test script and enabled TraceOrders and I'm seeing the order is submitted after the 1 minute primary series bar immediately proceeding the 30 minute bar has closed. I also added a dot to show when the condition is evaluating as true so you can see this before the execution marker on the chart.

            Attached is the output text file and the exported test script.
            NinjaScript Output 9_11_2024 6_45 AM.txt
            MACDMTFTest_NT8.zip

            You could choose to trigger the action after two primary bar closes after the secondary bar close if you want to code the logic that way. You would need an integer variable as a counter and when the condition is true set the bool to true, increment the counter once. In the entry condition check if the counter is 2, if its not increment it once (and do nothing else), if it is submit the order and set the counter back to 0 and the bool back to false.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_ChelseaB View Post
              Hello unpronounceable1700,

              I am not able to reproduce. I've added the code to a test script and enabled TraceOrders and I'm seeing the order is submitted after the 1 minute primary series bar immediately proceeding the 30 minute bar has closed. I also added a dot to show when the condition is evaluating as true so you can see this before the execution marker on the chart.

              Attached is the output text file and the exported test script.
              [ATTACH]n1317739[/ATTACH]
              [ATTACH]n1317740[/ATTACH]

              You could choose to trigger the action after two primary bar closes after the secondary bar close if you want to code the logic that way. You would need an integer variable as a counter and when the condition is true set the bool to true, increment the counter once. In the entry condition check if the counter is 2, if its not increment it once (and do nothing else), if it is submit the order and set the counter back to 0 and the bool back to false.
              Ah okay. 999 entries per direction and trace orders = true seemed to be the difference not having it work.

              But I just have one more issue, and that's only changing the MACD average to a macdhistogram

              All I did was change

              private Series <double> signal;
              private Series <double> macdhistogram;​

              and

              else if (State == State.DataLoaded)
              {
              signal[0] = EMA(MACD(BarsArray[1], 12, 26, 9),9)[0];
              macdhistogram[0] = MACD(BarsArray[1], 12, 26, 9)[0] - signal[0];
              }​

              and

              signal[0] = EMA(MACD(BarsArray[1], 12, 26, 9),9)[0];
              macdhistogram[0] = MACD(BarsArray[1], 12, 26, 9)[0] - signal[0];​

              if (macdhistogram[0] - macdhistogram[1] > 0)
              entry stuff here

              I get a Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.

              Not sure exactly what the correct way to set that up would be.


              Everything else about your code stayed the same, just changed the MACD part
              Last edited by unpronounceable1700; 09-11-2024, 05:05 PM.

              Comment


                #8
                Hello unpronounceable1700,

                Indicator plot series values cannot be used until bars start processing in OnBarUpdate.

                private EMA signal;
                private MACD macdhistogram;

                macdhistogram = MACD(BarsArray[1], 12, 26, 9);​
                signal = EMA(macdhistogram, 9);

                Print(macdhistogram.Diff[0] - signal[0]);

                if (CurrentBar > 1)
                Print(macdhistogram.Diff[0] - signal[0] - macdhistogram.Diff[1]);
                Chelsea B.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Today, 05:17 AM
                0 responses
                34 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                124 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                64 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                41 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                46 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X