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

adding 2nd timeframe

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

    adding 2nd timeframe

    Hello,

    I'm developing a strategy that mainly focussed on a daily timeframe, however I would like 1 specific indicator that calculates on a weekly timeframe. How can I do this ?

    Greetings
    Sebastiaan

    #2
    Hello SebastiaanR,

    Thank you for your post.

    You could add a weekly data series, and have your indicator calculate using that series. This section of our help guide goes into detail on how to add a secondary time frame and calculate indicators using the secondary series:



    This section of the help guide on ensuring you have enough bars in each series to calculate may be helpful as well:



    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hi there,

      So I tried adding the 2nd time frame however I came up with some issues. When I want to add my HeikenAshi indicator with a weekly timeframe to my graph it gives me some weird stuff. I uploaded a picture of it.

      Comment


        #4
        Hello SebastiaanR,

        Thank you for your note.

        You've got some odd things going on in your State == State.DataLoaded there, but I can't really tell what's going on on the chart itself from your screenshot.

        There's not a reason to set up a primarySeries and secondarySeries DataSeries. Here's how I would set this up:

        Code:
                    else if (State == State.Configure)
                    {
                        AddDataSeries(BarsPeriodType.Week, 1);
                    }
                    else if (State == State.DataLoaded)
                    {
                        HeikenAshi81 = HeikenAshi8(Closes[0]);
                        HeikenAshi82 = HeikenAshi8(Closes[1]);
        
                        AddChartIndicator(HeikenAshi81);
                    }
        I'm using Closes[0] and Closes[1] to denote I want the indicators to use the close price series of the primary and secondary data series. Also, you might notice I'm not adding the second one to the chart - this is because AddChartIndicator can only use the primary data series.

        From the help guide: "An indicator being added via AddChartIndicator() cannot use any additional data series hosted by the calling strategy, but can only use the strategy's primary data series."



        Now, you can still access values from the secondary indicator and use those in your calculations - it just will not show up on the chart using the secondary series.

        Please let us know if we may be of further assistance to you.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Hi kate

          Thanks for the reply. I think I understand now. Didn't know the addchartindicator only works with primary data series. Now lets say I use the secondary data series as indicator for example :
          HeikenAshi8(Closes[1][0]) > HeikenAshi8(Closes[1][1]). Is there any way to validate if my strategy uses this data ? since there is no way to visually check these values ?

          Comment


            #6
            Hello SebastiaanR,

            Thank you for your note.

            You'd want to access the values for the heiken ashi instances you've created like this, for example:

            if(HeikenAshi82[0].HAClose > HeikenAshi82[1].HAClose) would tell you if the close price of the most recent secondary series HeikenAshi82 bar was greater than the close price of the previous secondary series heiken ashi bar.

            If you're needing to confirm what values are on a specific bar, I'd say that adding prints to your code to print out various values would be very helpful.

            This forum post goes into great detail on how to use prints to help figure out where issues may stem from — this should get you going in the correct direction. You can even add these using the Strategy Builder.



            If you run into issues like we saw here, the above information will allow you to print out all values used in the condition in question that may be evaluating differently. With the printout information you can assess what is different between the two.

            Please let us know if we may be of further assistance to you.
            Kate W.NinjaTrader Customer Service

            Comment


              #7
              Hi kate,

              Thanks for the reply. So I added the things u suggested and tried testing. Now I see that if I use a weekly timeframe and I want to compare if(HeikenAshi82[0].HAClose > HeikenAshi82[1].HAClose) it actually uses a bar previous to the ones asked. so it does if(HeikenAshi82[1].HAClose > HeikenAshi82[2].HAClose). I also added a momentum indicator with the secondary weekly series and I also printed that data to check and again here it uses 1 bar before the bar that I asked. I don't understand how this happens.

              Comment


                #8
                Hello SebastiaanR,

                Thank you for your reply.

                Would you be able to provide a sample of the code you are using showing your prints?

                Thanks in advance; I look forward to assisting further.
                Kate W.NinjaTrader Customer Service

                Comment


                  #9
                  Hi kate,

                  I added 4 pictures. The first 2 are from the code. next 2 are charts. In the first chart u can see that the strategy enters a position.This is on a daily timeframe On the last pic of the chart I drew and arrow showing the candle of the weekly chart. This is a red candle and this should be the one used in the code (if(HeikenAshi82[0].HAClose > HeikenAshi82[1].HAClose)). However it used data of the candle before that one. The green candle. Thats why the trade was entered. Normally it should not be entered.

                  Comment


                    #10
                    Hello SebastiaanR,

                    Thank you for your reply.

                    How are you backtesting your strategy? Are you just applying it to a regular chart and looking at the historical trades, running it using the Playback connection, or using the Strategy Analyzer?

                    You should expect that a strategy running real-time (live brokerage account, live market simulation, Market Replay etc...) will produce different results than the performance results generated during a backtest. This difference may be more easily seen on certain Bars types (e.g. Point and Figure) than others due to their inherent nature in bar formation.

                    During a backtest you can select conservative or liberal fill algorithms which will produce different results. Fills are determined based on 4 data points, OHLC of a bar since that is the only information that is known during a backtest and there will be no intra-bar data. This means actions cannot happen intra-bar, fills cannot happen intra-bar. All prices and actions come from and occur when the bar closes as this is all the information that is known.

                    Because of this, when using Historical data in a backtest, OnBarUpdate will only update 'On bar close' as it does not have the intra-bar information necessary for 'On price change' or 'On each tick'.

                    What this means for you is basically this. Let's imagine we're looking at a chart with 28 days of daily and weekly bars. That would look like this:

                    Click image for larger version

Name:	2019-12-31_0924.png
Views:	334
Size:	90.2 KB
ID:	1082380

                    To illustrate what values we're going to get for which bars, I've made a very simple indicator for this example:

                    Code:
                        public class MyCustomIndicator14 : Indicator
                        {
                            private HeikenAshi8 HeikenAshi82;
                    
                            protected override void OnStateChange()
                            {
                                if (State == State.SetDefaults)
                                {
                                    Description                                    = @"Enter the description for your new custom Indicator here.";
                                    Name                                        = "MyCustomIndicator14";
                                    Calculate                                    = Calculate.OnBarClose;
                                    IsOverlay                                    = true;
                                    DisplayInDataBox                            = true;
                                    DrawOnPricePanel                            = true;
                                    DrawHorizontalGridLines                        = true;
                                    DrawVerticalGridLines                        = true;
                                    PaintPriceMarkers                            = true;
                                    ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                                    //Disable this property if your indicator requires custom values that cumulate with each new market data event. 
                                    //See Help Guide for additional information.
                                    IsSuspendedWhileInactive                    = true;
                                }
                                else if (State == State.Configure)
                                {
                                    AddDataSeries(BarsPeriodType.Week, 1);
                                }
                                else if (State == State.DataLoaded)
                                {
                                    HeikenAshi82 = HeikenAshi8(Closes[1]);
                                }
                            }
                    
                            protected override void OnBarUpdate()
                            {
                                Print("OBU - BIP: " + BarsInProgress + " | " +Time[0]);
                                // check to ensure there's enough bars in the secondary series for calculations
                                if(CurrentBars[1] < 1)
                                    return;
                                Draw.Text(this, "myText"+CurrentBar, "HAOpen:\n" + HeikenAshi82.HAOpen[0]+"\n HAClose:\n"+HeikenAshi82.HAClose[0],0, Low[0] - (4*TickSize), Brushes.White);  
                            }
                        }
                    What this is going to do is simply print the value of the HeikenAshi82 instance that would be used on each bar:

                    Click image for larger version

Name:	2019-12-31_0940.png
Views:	365
Size:	186.1 KB
ID:	1082381
                    You might notice some bars have two prints - for those, the lower print is for the secondary weekly series.

                    When you apply an indicator to historical data as we're doing here, as we discussed before, all it knows is the OHLC, so no matter what you've chosen for the calculation mode, on historical data, it's only able to run OnBarClose. We can see what this means for us here - the first weekly bar's values are what are pulled for the second week of daily bars and it remains that way going forward.

                    However, in real time, this would function differently if you set the strategy to calculate on each tick or on bar close. Here's a look at the last few days of data with our example indicator running On Bar Close:

                    Click image for larger version

Name:	2019-12-31_0954.png
Views:	345
Size:	173.8 KB
ID:	1082382

                    Now here's the same chart with the same indicator running OnEachTick:

                    Click image for larger version

Name:	2019-12-31_0954_001.png
Views:	359
Size:	190.5 KB
ID:	1082383

                    Also, here is a link to the differences on real-time vs backtest (historical).


                    Adding intra-bar granularity can help with this.

                    Intra-bar granularity adds a more granular data series such as a 1 tick series so that the strategy has finer granularity in the historical data in between the OHLC of the primary series. This allows for more accurate trades by supplying the correct price at the correct time for the order to fill with.

                    In NinjaTrader 8, there have been two new enhancements so that programmers do not have to manually add this secondary series and code the script to for high accuracy fills (Order Fill Resolution) and for intra-bar actions (TickReplay).

                    Here is a link to our forums that goes into depth on using Order Fill Resolution and Tick Replay to ensure your backtests are as close to real time results as possible. Please note that High Order Fill Resolution can only be used on strategies that only use a single data series, so in this particular case, you'd need to add the more granular data series within the code itself:

                    Citizens of the NinjaTrader Community, A common question we hear from clients is 'why are results from backtest different from real-time or from market replay?'. Live orders are filled on an exchange with a trading partner on an agreed upon price based on market dynamics. Backtest orders are not using these market dynamics.


                    High Fill Order Resolution and TickReplay cannot be used together. If it is necessary to have both, you would again need to add intra-bar granularity to the script in the code itself for order fill accuracy and use TickReplay to update indicators with Calculate set to OnPriceChange or OnEachTick historically.

                    Please let us know if we may be of further assistance to you.
                    Attached Files
                    Kate W.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi kate,
                      thanks for the answer and I wish u a very happy newyears !
                      So I did some checking and I plotted a chart. Now on this chart in ninjatrader I see the actual dates are incorrect. I posted 2 pictures. Both are the same stock at the same time. However u can see that ninjatrader doesn't show the exact same chart. It is moved 1 day to the right. So on my broker the candle on 16/12 is not the same as in ninja. In ninja it's 17/12.

                      Comment


                        #12
                        Hello SebastiaanR,

                        Thank you for your reply.

                        First, let's make sure your historical data isn't incorrect.

                        Please delete your historical data for this instrument using the instructions provided at the following page of the NinjaTrader Help Guide:

                        Removing Historical Data
                        • Once the data has been deleted close NinjaTrader. Open the Documents\NinjaTrader 8\db\cache folder. Select all files then right mouse click and select “delete.”
                        • Open NinjaTrader and connect to your data provider. Open a new chart to force NinjaTrader to reload the historical data, and compare.
                        Please let me know if this resolves this item.
                        Kate W.NinjaTrader Customer Service

                        Comment


                          #13
                          Hi kate,

                          I did this. Now it seems that I have an error on requesting bar series.
                          2-1-2020 21:37:24 Default Error on requesting bars series: 'Historical Market Data Service error message:No market data permissions for AEB, BATEEN, CHIXEN, FWB, IBIS, TGATE, TRQXEN STK'
                          I do however have a subscription for the data. Very strange

                          Comment


                            #14
                            Hello SebastiaanR,

                            Thank you for your reply.

                            It looks like none of those are the one from your screenshot - did that one load data?

                            Please send me your log and trace files so that I may look into what occurred - we'll want you to send them this way rather than post them on the forums as they can contain identifying information.

                            You can do this by going to the Control Center-> Help-> Email Support

                            Ensuring 'Log and Trace Files' is checked will include these files. This is checked by default.

                            Please reference the following ticket number in the body of the email: 2374396 ATTN Kate W.

                            Thanks in advance; I look forward to assisting you further.
                            Kate W.NinjaTrader Customer Service

                            Comment


                              #15

                              Hello,

                              Thank you for your patience.

                              I'm responding here as well as via email so we can keep everything in one place.

                              I've taken a look at your log and trace files and you've been getting this error since well before today, so that's not what would be affecting this particular chart. Here's an example from before the new year:

                              2019-12-23 21:51:36:665|3|2|IB was unable to locate instrument. Please verify your symbol mapping is correct for instrument RELX Default
                              2019-12-23 21:51:36:894|0|4|Error on requesting bars series: 'Historical Market Data Service error message:No market data permissions for BATEEN, CHIXEN, FWB, SWB, TGATE, TRQXEN STK'
                              2019-12-23 21:51:38:371|0|4|Error on requesting bars series: 'IB was unable to locate instrument. Please verify your symbol mapping is correct for instrument RELX Default'

                              I would check with IB on that particular error.

                              What I'm not seeing, however, is it trying to load AF — the chart we're trying to match. Also, I note you're on a previous version of NinjaTrader, so you'll want to update to make sure we're not running into an issue that's been resolved in the latest release.

                              To update NinjaTrader, please follow the steps below:
                              • First, copy your license key from NinjaTrader under Help> License Key then exit NinjaTrader
                              • Click on the link: http://ninjatrader.com/PlatformDirect
                              • Enter your license key and press Submit
                              • Select 'NinjaTrader 8'
                              • Select 'Download'
                              • Critical: Before running the installer, ensure NinjaTrader is closed.

                              Also, you'll want to double check the version of either TWS or IB Gateway you're using. You can find the version number (also sometimes called the Build number) on the splash screen when the one you use starts up. If it's anything other than what's listed below, please uninstall the one you have and install the appropriate one below:Lastly, would you be able to post a screenshot of what you're seeing when you try to pull up an AF chart?

                              Thanks in advance; I look forward to assisting you further.



                              Kate W.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by bill2023, Yesterday, 08:51 AM
                              8 responses
                              43 views
                              0 likes
                              Last Post bill2023  
                              Started by yertle, Today, 08:38 AM
                              6 responses
                              25 views
                              0 likes
                              Last Post ryjoga
                              by ryjoga
                               
                              Started by algospoke, Yesterday, 06:40 PM
                              2 responses
                              24 views
                              0 likes
                              Last Post algospoke  
                              Started by ghoul, Today, 06:02 PM
                              3 responses
                              16 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by jeronymite, 04-12-2024, 04:26 PM
                              3 responses
                              46 views
                              0 likes
                              Last Post jeronymite  
                              Working...
                              X