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

Want strategy to use higher timeframe, but trade on chart

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

    Want strategy to use higher timeframe, but trade on chart

    I used
    if (BarsInProgress == 1)
    {​

    But that only traded on that timeframe

    Then I tried
    if (BarsInProgress == 0)
    {​​

    but that just traded every bar on the main timeframe.

    On tradingview where I originally build these strategies. It would use the higher timeframe to get an entry condition, then enter the first bar after on the lower time frame.

    So say if there's an hour timeframe to check for condition, and the chart is 5 minutes. It would active at 14:05 then the next at 15:05

    #2
    Hello unpronounceable1700,

    Thank you for your post.

    You can specify the barsInProgressIndex of the Bars object the order is to be submitted against.

    For example, using EnterLong():

    EnterLong(int barsInProgressIndex, int quantity, string signalName)​

    Code:
    //check Close prices on the secondary Bars object
    if (Closes[1][0] > Closes[1][1])
    {​
    //submit order to primary Bars series
    EnterLong(0, 1, "longEntry");
    }


    Please let us know if you have any further questions.
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Gaby View Post
      Hello unpronounceable1700,

      Thank you for your post.

      You can specify the barsInProgressIndex of the Bars object the order is to be submitted against.

      For example, using EnterLong():

      EnterLong(int barsInProgressIndex, int quantity, string signalName)​

      Code:
      //check Close prices on the secondary Bars object
      if (Closes[1][0] > Closes[1][1])
      {​
      //submit order to primary Bars series
      EnterLong(0, 1, "longEntry");
      }


      Please let us know if you have any further questions.
      Hmm, I tried multiple variations of that and none seem to work, although they have varying results. But it's always nearly every chart bar or close to it.

      Like at most it should be every half hour, with the trade happening 5 minutes after the exact half hour

      Maybe I would have to add a time constraint, like it has to wait a half hour before a new trade?

      Comment


        #4
        Hello,

        You could save CurrentBars[1] to a variable at the time you enter a trade, then for each subsequent trade check that CurrentBars[1] minus your saved variable is equal to 1. If the 1 hour series is your secondary series, this would ensure at least 1 bar has elapsed in that series before entering a new trade.
        Gaby V.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Gaby View Post
          Hello,

          You could save CurrentBars[1] to a variable at the time you enter a trade, then for each subsequent trade check that CurrentBars[1] minus your saved variable is equal to 1. If the 1 hour series is your secondary series, this would ensure at least 1 bar has elapsed in that series before entering a new trade.
          How would I go about creating a variable for that?

          Comment


            #6
            Hello,

            You can just save it to an int variable.

            For example,

            Code:
            //at the class level
            private int mySavedBar;
            
            //in OBU
            mySavedBar = CurrentBar;
            Gaby V.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Gaby View Post
              Hello,

              You can just save it to an int variable.

              For example,

              Code:
              //at the class level
              private int mySavedBar;
              
              //in OBU
              mySavedBar = CurrentBar;
              Ah okay thank you, I had in my head a variable was something else

              I've got this code

              if (BarsInProgress == 1)
              {

              mySavedBar = CurrentBar;​

              if ((macdhistogram[0] - macdhistogram[1]) > 0 && CurrentBars[1] - mySavedBar == 1)
              {
              EnterLong(0, 1, "Longafter");
              }​

              Still trying to wrap my head around how to code this lol

              This just trades only on the higher timeframe
              Last edited by unpronounceable1700; 09-05-2024, 12:27 PM.

              Comment


                #8
                Hello,

                This just trades only on the higher timeframe
                If the 5 minute series is your primary series, this would make sense because you are using 0 as the barsInProgressIndex when calling EnterLong(). 0 would be the primary data series. 1 is your added data series (secondary series).

                The CurrentBar math is to make sure 1 bar has elapsed in the secondary series.

                To clarify, your primary series is the 5 minute and the secondary series is the 1 hour? And you want to wait for 1 bar to elapse on the secondary 1 hour series before placing a trade on the primary 5 minute series?
                Gaby V.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_Gaby View Post
                  Hello,



                  If the 5 minute series is your primary series, this would make sense because you are using 0 as the barsInProgressIndex when calling EnterLong(). 0 would be the primary data series. 1 is your added data series (secondary series).

                  The CurrentBar math is to make sure 1 bar has elapsed in the secondary series.

                  To clarify, your primary series is the 5 minute and the secondary series is the 1 hour? And you want to wait for 1 bar to elapse on the secondary 1 hour series before placing a trade on the primary 5 minute series?
                  Yeah, but I may switch the exact timeframes. But the chart timeframe will use the higher one, and the entry should be one chart bar after the higher timeframe

                  From this code, the log is showing it still only trading on exactly the higher timeframe

                  if (BarsInProgress == 1)
                  {

                  mySavedBar = CurrentBar;​


                  if ((macdhistogram[0] - macdhistogram[1]) > 0 && CurrentBars[1] - mySavedBar == 1)
                  {
                  EnterLong(0, 1, "Longafter");
                  }


                  //Shortafter
                  if ((macdhistogram[0] - macdhistogram[1]) < 0 && CurrentBars[1] - mySavedBar == 1)
                  {
                  EnterShort(0, 1, "Shortafter");
                  }

                  }​

                  I didn't paste the code for the print out and MACD Histogram just to not clutter the main part

                  Comment


                    #10
                    If you want it to trade on the lower timeframe (1 hour), you would need to use 1 as the barsInProgressIndex when calling EnterLong(). 0 is the higher timeframe (5 minutes).
                    Gaby V.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_Gaby View Post
                      If you want it to trade on the lower timeframe (1 hour), you would need to use 1 as the barsInProgressIndex when calling EnterLong(). 0 is the higher timeframe (5 minutes).
                      By higher I mean like the larger timescale. So 1 hour.

                      On Tradingview the trades show as 1 chart bar after the large timeframe
                      So for example 12:05 - 13:05 - 14:05

                      I've been trying to get it to work using time commands, but then there's errors where you can't use summation with them

                      So basically the formula is

                      Oh MACD went up from 12:00 to 13:00? Well activate a long trade just 5 minutes after 13:00
                      Last edited by unpronounceable1700; 09-06-2024, 11:57 AM.

                      Comment


                        #12
                        Your code is updating mySavedBar everytime BIP is 1. mySavedBar should only be updated with the current CurrentBars[1] value when you actually enter a trade. Otherwise, mySavedBar and CurrentBars[1] are always going to be the same value.

                        I recommend debugging the script using prints and TraceOrders.



                        I've been trying to get it to work using time commands, but then there's errors where you can't use summation with them
                        What errors are you getting exactly, how are you trying to do summation with time commands? Please clarify if possible.
                        Last edited by NinjaTrader_Gaby; 09-06-2024, 11:44 AM.
                        Gaby V.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_Gaby View Post
                          Your code is updating mySavedBar everytime BIP is 1. mySavedBar should only be updated with the current CurrentBars[1] value when you actually enter a trade. Otherwise, mySavedBar and CurrentBars[1] are always going to be the same value.

                          I recommend debugging the script using prints and TraceOrders.





                          What errors are you getting exactly, how are you trying to do summation with time commands? Please clarify if possible.
                          Well my thought process was if it's one bar after, that's just 5 minutes in time. So if I could have the function activate only when Bararray 0 was 5 minutes after the last bar of bararray 1 then it should work as intended.

                          This was my most recent attempt: Times[0][0]-Times[1][0] == 5

                          Comment


                            #14
                            Hello,

                            If you want to wait 5 minutes after something occurs on the 1 hour bars, you can create a timer by using .AddMinutes.

                            Please see the attached sample script. Make sure to run it on a 5 minute chart to fit your example.
                            Attached Files
                            Gaby V.NinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by NinjaTrader_Gaby View Post
                              Hello,

                              If you want to wait 5 minutes after something occurs on the 1 hour bars, you can create a timer by using .AddMinutes.

                              Please see the attached sample script. Make sure to run it on a 5 minute chart to fit your example.
                              ----

                              From that all that happens in the output is showing "Start time:" and the hour timeframe
                              And the timerhit does not activate
                              Last edited by unpronounceable1700; 09-06-2024, 05:41 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by dallasstarsfan, Today, 06:04 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post dallasstarsfan  
                              Started by FBraun, Today, 01:42 PM
                              2 responses
                              9 views
                              0 likes
                              Last Post FBraun
                              by FBraun
                               
                              Started by john_44573, Today, 12:55 PM
                              4 responses
                              29 views
                              0 likes
                              Last Post john_44573  
                              Started by IDumpedCinderella, Today, 07:39 AM
                              4 responses
                              19 views
                              0 likes
                              Last Post IDumpedCinderella  
                              Started by defa0009, Today, 09:33 AM
                              10 responses
                              24 views
                              0 likes
                              Last Post defa0009  
                              Working...
                              X