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

Coding in Displacement for Bollinger Visual Plot?

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

    Coding in Displacement for Bollinger Visual Plot?

    Hi all,

    I'm fairly new to coding and have been grateful for all the available resources and threads that are available on this site to help with learning. That said, I'm at a roadblock in terms of creating this strategy where I'd like to plot an editted Bollinger Band with a modified displacement value.

    Basically, I'd like to use Bollinger Bands with a displacement that pushes it to the left (negative values in the displacement box of the indicator seems to move it to the left, whereas positive moves it to the right) in my strategy in which when and if a moving average crosses over the middle band, it initiates a trade. I'm having trouble hard-coding the visual part into my strategy, where I use Displacement = -2; (since it appears that negative values shifts it to the left) followed by AddPlot(Brushes.Seashell, "ModifiedBollinger"); I've also used the "BarsAgo" parameter in the Strategy Builder, in which it seems to actually take effect in terms of outputting the correct data for computation. However, both parameters, BarsAgo and hard-coding in Displacement, does not seem to change the visual plot, no matter what value is inserted (negative or positive).

    From past insight, it seems that hard-coding the visual shift would be the way to correctly do this, but as a person with no experience of coding, I do not have the slightest idea on how to do it correctly in NinjaScript. Any help is appreciated, and I thank you for your time!
    Last edited by ComfyCouch; 12-04-2020, 11:26 PM.

    #2
    Update: Per a suggestion from another thread (https://ninjatrader.com/support/foru...gy#post1050480), I treated both indicators as new plots and had it plot from new values based off using the code: Values[0][0] = moving average indicator... etc, etc. However, when changing the value in "Values" to say "Values[0][2]" along with [2] at the end for bars ago (theoretically, as a way to show displacement but in the form of bars ago) for the other indicator (Bollinger), it appears to move it ahead (right) rather than left. I realize my point or ultimate goal may not be explained very well, but ultimately, I'm basically trying to make the Bollinger indicator more lagging, if that makes sense, by adding a displacement value. Hope this may clear up some misunderstandings, if present. As always, thank you for your help and time!

    Comment


      #3
      Hello ComfyCouch,

      With or without displacement the plot values are still calculated for every bar in order, then the displacement just visually adjusts which bar that plot value appears over. It's not changing the values of the plot.

      If displacement is a positive value and the plot values are shifted right, it's showing plot values from however many bars ago over the current bar.
      If you want a value from a few bars in the past, ignoring the values calculated on the current bar, this can be retrieved by using a bars ago value to a plot value from some bars ago.

      When a plot is visually shifted to the left, the value calculated for the current bar is displayed visually over a previous bar.

      However, the current bar has no value on it. It's blank. There are no future calculations from data that doesn't exist yet. It's just blank space.
      OnBarUpdate is going to run for every bar in order.

      Do you want blank space values? If so, just use 0's.

      Do you want to do wait a few bars after some calculated value from a previous bar causes your condition to be true? If so, when the condition is true, use a counter to wait however many bars you would like before triggering the action.


      If neither, can you further clarify what you would like to accomplish?
      Last edited by NinjaTrader_ChelseaB; 12-06-2020, 07:26 PM.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Hi Chelsea,

        Thanks for your response. I understand that the displacement of a plot only provides a visual shift and nothing to the actual value. I also understand that when displaced (at least visually to the left), the current bar has no visual value on it. Please let me know if I'm understanding this correctly.

        What I'm basically trying to accomplish, in my strategy, however, is to create a signal from when a displaced Bollinger (visually shifted to the left, by 2 bars, thus having no visual plot on the current and previous bar), with its current value (shown on the 2nd previous bar), crosses the moving average (with no displacement) at the value of it's 2nd previous bar (and the value being that 2nd previous bar, not the current nor it's first previous). Now that I write this out plainly, would this simply be accomplished by adding a value of 2 to the BarsAgo of the moving average and leaving the BarsAgo value of the Bollinger at 0? I understand I'm all over the place so please excuse me if all this sounds convoluted.

        Again, thank you for your reply and help and I look forward to your response.

        Comment


          #5
          Hello ComfyCouch,

          I'm still not quite understanding.

          Imagine that the values are not displaced. Which bars do the plot values appear over that you want to check the cross from?

          It sounds like you want to check if a cross occurs on the current bar, and then wait two bars before triggering an action.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Thanks for the reply, Chelsea.

            With no displacement, the cross I'd like to check would be when the Bollinger at the current bar plot value crosses the Moving Average at the plot value of 2 bars ago. Please let me know if this clarifies my asking.

            Comment


              #7
              Hello ComfyCouch,

              The bollinger of the previous bar is less than the moving average of 3 bars ago and the bollinger of the current bar is greater than the moving average of 2 bars ago?
              Code:
              if (Bollinger(2, 14).Middle[1] < SMA(14)[3] && Bollinger(2, 14).Middle[0] < SMA(14)[2])
              Or the bollinger of the previous bar is less than the moving average of 2 bars ago and the bollinger of the current bar is greater than the moving average of 2 bars ago?
              Code:
              if (CrossAbove(Bollinger(2, 14).Middle, SMA(14)[2]))
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Chelsea, I appreciate all your help thus far and I thank you for your response.

                I believe the first part is closer to what applies to my previous example. In order to clarify any misunderstandings, I've outlined a rather artistic drawing/example of what I'm trying to accomplish. The Bollinger, in this drawing, is displaced by 2 (so it still plots the value at the current bar, but visually shifted back 2 bars).

                As a heads-up, the black vertical line at the signal (signal is in yellow) is just to help visualize where the signal occurs, which is at the bar after the vertical line (I apologize if my drawing does not accurately depict that).

                Click image for larger version

Name:	SignalExample.png
Views:	565
Size:	40.8 KB
ID:	1131159
                Last edited by ComfyCouch; 12-07-2020, 02:06 PM.

                Comment


                  #9
                  Hello ComfyCouch,

                  Unfortunately, I'm not understanding the specific rules in this picture.

                  If you are able to describe which bars you want values from, I can assist with how that would look in C#.

                  You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our NinjaTrader Ecosystem team follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Hey Chelsea, I apologize for the picture being unclear.

                    The bars I'd want values from is the current bar (for the Bollinger) and 2 bars ago (for the Moving Average).

                    While I appreciate the outstanding help and commitment to my question/problem, and agree that referring to a professional would probably be best for me, I think I'd like to try to create/modify this script on my own for as long as I can. The community and its wonderful helpers (such as yourself) and support threads have been guiding me along with this script and I'm sure something as small as this (since I'm definitely blowing it out of proportion due to my flawed explanations) can be solved with all this support.

                    Comment


                      #11
                      Hello ComfyCouch,

                      So you want if the Bollinger of 0 bars ago is greater than the MA from 2 bars ago?

                      Cross above uses a series where it is comparing a previous bar from a first series, to be less than either a previous bar from a second series or a static double, and then the next bar of the first series becoming greater than the next bar from a second series or static double.

                      At least three values are used, and optionally 4 values. A previous and next value from the first series and the previous value and next value from a second series (CrossAbove(Series<double> Series1, Series<double> Series 2, lookbackperiod), or a previous and next value from the first series and a static double (CrossAbove(Series<double> Series1, double staticDouble, lookback period).

                      When you mention:
                      "The bars I'd want values from is the current bar (for the Bollinger) and 2 bars ago (for the Moving Average)."
                      This seems to only involve two values, the bollinger with 0 bars ago, and the moving average from 2 bars ago. So no cross would be involved.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by NinjaTrader_ChelseaB View Post

                        So you want if the Bollinger of 0 bars ago is greater than the MA from 2 bars ago?
                        That sums up the intrinsic part of what my strategy entails, so yes, that is what I basically want.

                        Originally posted by NinjaTrader_ChelseaB View Post

                        This seems to only involve two values, the bollinger with 0 bars ago, and the moving average from 2 bars ago. So no cross would be involved.
                        From my understanding of a previous support thread that I read into, I believe that is the reason why I replaced "CrossAbove" and "CrossBelow" with "greater than" and "less than", respectively, however, I'm not sure if that makes a difference.

                        Due to a lot of my unclear explanations, I'd like to confirm that we're on the same understanding in terms of what I'm trying to accomplish.

                        From another charting platform, in which I first discovered this part of the strategy, I was able to use a displacement value to visually shift an indicator's plot (Bollinger's, in this case) to the left by 2 bars. Along with the Bollinger, I plotted a moving average indicator with no changes (all default periods/values). In this part of the strategy, the main attempt of what I'm trying to accomplish is to get a signal that is generated when the Bollinger (that is displaced) crosses the moving average at 2 bars ago. In intrinsic form, this would mean that when the current bar value of the Bollinger, in its displaced form (of being visually shifted to the left by 2 bars), crosses the bar value of the moving average (that is default) at two bars ago, it would create a signal that I would use as information for the next part of my strategy (which is not discussed here, although it most likely has problems as well).

                        Please let me know if we're on the same page here; I feel awful for taking your time and not explaining myself well.

                        As always, thank you for current help and I look forward to your response!

                        Comment


                          #13
                          Hello ComfyCouch,

                          I wouldn't be able to tell how another platform would code what you are seeing visually.

                          You mentioned a cross, which would involve at least three values.
                          If you know which bars ago values and which bar numbers you want, I am happy to assist.

                          I recommend that you use prints to further understand what the values are.
                          Print the time of the bar, and print the series using the bars ago value to see if this is the one you want.

                          Below is a link to a forum post that demonstrates how to use prints to understand behavior.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Hi Chelsea,

                            I apologize for not answering back as I've been away. Thank you for previous and current help to this dilemma, I really appreciate it.

                            Unfortunately, I still haven't quite solved the issue with my strategy. I'd like to revisit this discussion by clarifying what I'm trying to do.

                            Firstly, I want to displace my Bollinger by a value of -2 (shifting it to the LEFT). On the visual side, I've done this by coding in a Displacement value in State.Historical (under OnStateChange). That shows a visual plot of a displaced Bollinger in my strategy.

                            From there, I'd like to use the plot values of the displaced Bollinger in my strategy. That's where I encounter some trouble - I'm not sure how to do this. I've seen a couple different ideas (including yours) in order to accomplish this, but am not sure if they would work and if they would, how to incorporate it.

                            a) I could create a new indicator and use those plot values of the displaced Bollinger as an input (somehow?).
                            b) Wait a few bars until my condition with my Displaced Bollinger becomes true, thus triggering the action.


                            Would these options be somewhat viable in NinjaScript?

                            Again, I really appreciate the ongoing help and look forward to your response (I understand if you cannot reply being this close to the holidays - which is completely fine. In that case, I hope you have a great holiday weekend! )

                            Comment


                              #15
                              Hello ComfyCouch,

                              Displacment is only visual. For something displaced to the left, the current bars plot value is visually displayed on a bar to the left and there is no value displayed on the current bar.

                              If there is no value on the current bar, do you want condition to be true and then wait a number of bars before triggering the action?

                              Don't think in displacement. Think about what specific bar numbers you want to process information from.

                              You can use bools and an int counter to wait a number of bars before triggering an action.

                              Code:
                              private bool conditionTriggered;
                              private int barCount;
                              
                              conditionTriggered = false;
                              
                              if (conditionTriggered == false /* && your condition here */)
                              {
                              // the condition has been triggered, change bool and wait 2 bars before triggering action
                              conditionTriggered = true;
                              barCount = 0;
                              }
                              
                              if (conditionTriggered && barCount > 2)
                              {
                              conditionTriggered = false;
                              // we have waited two bars, trigger the action here
                              } else
                              {
                              // it has not been two bars, increment the counter and continue waiting
                              barCount++;
                              }
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by sidlercom80, 10-28-2023, 08:49 AM
                              166 responses
                              2,234 views
                              0 likes
                              Last Post sidlercom80  
                              Started by thread, Yesterday, 11:58 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post thread
                              by thread
                               
                              Started by jclose, Yesterday, 09:37 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post jclose
                              by jclose
                               
                              Started by WeyldFalcon, 08-07-2020, 06:13 AM
                              10 responses
                              1,414 views
                              0 likes
                              Last Post Traderontheroad  
                              Started by firefoxforum12, Yesterday, 08:53 PM
                              0 responses
                              11 views
                              0 likes
                              Last Post firefoxforum12  
                              Working...
                              X