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

Displacement = -1 VS Values[0][1]

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

    Displacement = -1 VS Values[0][1]

    Hey guys,​
    What is the difference between (1) and (2)?

    (1)
    Displacement = -1;
    '"+"'
    Values[0][0] = High[1];

    (2)
    Values[0][1] = High[1];​

    Are the two codes equivalent?
    Last edited by rafaelcoisa; 03-23-2023, 03:21 PM.

    #2
    Hello rafaelcoisa,

    Thank you for your note.

    You can certainly test this out programmatically and see the results between the two calculations. I created an indicator that has displacement set to -1 by default and sets the indicator plot of the current bar to the SMA value from 1 bar ago. I created a second indicator that keeps the default value of 0 for displacement and sets the plot value for the previous bar to the value of the SMA from 1 bar ago. You may plot both of these indicators, along with an SMA indicator with a period of 14 to compare the plots visually and they will display the same values. That said, if you try to print the values of the plots from the previous bar to the NinjaScript Output window they will not match. The values from the displaced script will be offset, by plot index, by one. Displacement is just a visual shift and it can be modified from the UI in the indicator parameters. It does not change the plot value indexes.

    The examples I created are attached. Please let me know if I may be of further assistance.
    Attached Files
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Hey NinjaTrader_Emily,
      Thanks for your answer.

      Just two more questions:
      - Is there no way to offset a plot via index (Values[0][1]) and still be able to use "PlotStyle.PriceBox" and/or "PriceMarker"?

      - If I combine "IsFirstBarOfSession" with "IsFirstTickOfBar" [if (Bars.IsFirstBarOfSession && IsFirstTickOfBar)], can something go wrong like miss the first bar of session or something else? If yes, does this happen with all types of charts, time chart, tick chart, etc?​

      Comment


        #4
        Hello rafaelcoisa,

        Thank you for your reply.

        I'm not sure I fully understand what you are trying to achieve with the offset. Please provide an example, i.e. a series that you have set and how you would like that displayed on the chart or a screenshot with an explanation of what you are looking for.

        As for IsFirstBarOfSession and IsFirstTickOfBar, there are a couple of items to keep in mind. I have included the help guide links and some notes below:
        • https://ninjatrader.com/support/help...rofsession.htm
          • "Note: This property always returns true on the very first bar processed (i.e., CurrentBar == 0). The represented time of the bar will NOT necessarily be equal to the trading hours start time (e.g., if you request 50 1-minute bars at 11:50:00 AM, the first bar processed of the session would be 11:00:00 AM). Loading a data series based on "dates" (Days or custom range) ensures that the first bar processed matches hours defined by the session template."
          • "Warning: This property will always return false on non-intraday bar periods (e.g., Day, Month, etc). For checking for new non-intraday bar updates, please see IsFirstTickOfBar​"
        • https://ninjatrader.com/support/help...ttickofbar.htm
          • "This property is only of value in scripts that run tick by tick which is when the Calculate property is set to Calculate.OnEachTick or Calculate.OnPriceChange.​"
          • "Note: If a bar type is set up to remove the last bar on a chart, IsFirstTickOfBar will automatically be set to True."​
            • For example, Renko bars remove the last bar
        With that in mind, if you have a non-intraday bar period then Bars.IsFirstBarOfSession will always be false. Additionally, if you are not loading the data series based on a date range, then it could return true even if it is not the first bar of a session. IsFirstTickOfBar would not be relevant if calculate is set to OnBarClose and it will always be True if the bar type has functionality that removes the last bar.

        I look forward to your reply.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Hey NinjaTrader_Emily,

          Thanks for your reply, I was not aware that the condition would not work on "non-intraday" and "renko chart".

          About "addplot", a simple example would be this:
          Code:
          if (State == State.SetDefaults)
          {
          AddPlot(Brushes.Blue, "PlotA");
          AddPlot(Brushes.Red, "PlotB");
          AddPlot(Brushes.Green, "PlotC");
          }
          protected override void OnBarUpdate()
          {
          if (CurrentBar < 2)
          return;
          
          Values[0][1] = Median[1];
          Values[1][1] = Low[1];
          Values[2][1] = High[1];
          }
          Neither "PlotStyle.PriceBox" nor "PriceMarker" work. Hence the question, is there any way to make them work offseting a plot via index ("Values[0][1]") ?

          And yet about the condition [if (Bars.IsFirstBarOfSession && IsFirstTickOfBar)]. My question is more about the order of things. For example, "IsFirstTickOfBar" becomes true, but "IsFirstBarOfSession" only becomes true on the second tick, which would imply that the condition [if (Bars.IsFirstBarOfSession && IsFirstTickOfBar)] is false and thus I would miss the first bar of the session. Another way to ask the same question I asked earlier is: Does "Bars.IsFirstBarOfSession" always become true before "IsFirstTickOfBar"?

          Comment


            #6
            Hello Rafaelcoisa,

            Thank you for your reply.

            I suspect the reason the plot is not displaying as PriceBox and you are not seeing a Price Marker is that the plots do not have a value assigned for the current bar. Both the price marker and price box will display values based on the right-most bar on the chart; if you scroll to previous values on the chart, you should see the price markers on the y-axis. Otherwise, you will need to assign a value to the current bar (for example, Values[0][0]) to see the PriceBox plot while a bar is forming on the right-most side of the chart. Here is a snippet to demonstrate:

            Code:
            if (State == State.SetDefaults)
            {
            AddPlot(new Stroke(Brushes.Orange, 2), PlotStyle.PriceBox, "PlotA");
            AddPlot(new Stroke(Brushes.DarkViolet, 2), PlotStyle.PriceBox, "PlotB");​
            }
            protected override void OnBarUpdate()
            {
                if (CurrentBar < 2)
                    return;
                // This plot for PlotA will not show unless you scroll back on the chart to a previous bar
                Values[0][1] = Median[1];
                // This plot for PlotB will show the PriceBox because it has a value for the currentBar
                Values[1][0] = Low[0];
            }​
            Further, I still do not understand the type of displacement you are trying to achieve. For example, if you assign the value of Median from 0 bars ago to the plot 0 bars ago, this also means that 1 bar ago the plot will have the value of Median from 1 bar ago. Are you instead wanting the current bar of the plot to have the value of Median from 1 bar ago or something along those lines?

            As for IsFirstTickOfBar and IsFirstBarOfSession, I suggest adding prints to a script to see them in action. IsFirstBarOfSession returns true on the first bar processed and the first bar of the session based on the trading hours selected. It returns true on all ticks of that bar; it does not wait until the second tick of the bar to become true. IsFirstTickOfBar and IsFirstBarOfSession would both become true on the first tick of the first bar of the session (as well as the very first bar processed as previously discussed). Here is an example of some prints that could demonstrate and potentially answer your question:

            Code:
            protected override void OnBarUpdate()
            {
                if (Bars.IsFirstBarOfSession)
                {
                    // this statement would print for all ticks of the first bar of the session in real-time.  when processing historical data, it will only process OnBarClose unless you enable TickReplay
                    Print("IsFirstBarOfSession: " + Bars.IsFirstBarOfSession + " CurrentBar: " + CurrentBar);
                }
                if (IsFirstTickOfBar && Bars.IsFirstBarOfSession)
                {
                    // this statement will print once when it is the first tick of the first bar of the session
                    Print("IsFirstTickOfBar: " + IsFirstTickOfBar + " IsFirstBarOfSession: " + Bars.IsFirstBarOfSession + " CurrentBar: " + CurrentBar);
                }
            }​
            Please let me know if I may be of further assistance.
            Emily C.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by bortz, 11-06-2023, 08:04 AM
            47 responses
            1,610 views
            0 likes
            Last Post aligator  
            Started by jaybedreamin, Today, 05:56 PM
            0 responses
            9 views
            0 likes
            Last Post jaybedreamin  
            Started by DJ888, 04-16-2024, 06:09 PM
            6 responses
            19 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by Jon17, Today, 04:33 PM
            0 responses
            6 views
            0 likes
            Last Post Jon17
            by Jon17
             
            Started by Javierw.ok, Today, 04:12 PM
            0 responses
            16 views
            0 likes
            Last Post Javierw.ok  
            Working...
            X