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

Draw four squares with multiple timeframes

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

    Draw four squares with multiple timeframes

    Hello,

    I am new to development with NT.
    I am trying to create an indicator that draws four squares (one bellow each other) in a different panel.
    Each square takes its color depending on condition based on different timeframes.
    For example:
    When in 1 minute graph:
    If close of 5 minute graph>open 5 minute graph then draws a green square, else draws a red one.
    Bellow that square it will have to draw another one based on the same logic but calculated on 15 minute graph for example.

    I have made the timeframes input parameters so I can define wich timeframes will be used.

    Then I add timeframes like this:

    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Minute, Period_1);
    AddDataSeries(Data.BarsPeriodType.Minute, Period_2);
    AddDataSeries(Data.BarsPeriodType.Minute, Period_3);
    AddDataSeries(Data.BarsPeriodType.Minute, Period_4);
    }

    After that on the OnBarUpdate() do all calculations.
    I am using a FOR sentence and a SWITCH / CASE like this:

    for( tf=1; tf<=4; tf++)
    {

    switch(tf)
    {
    case 1:
    if (Closes[tf][0] > Opens[tf][0])
    {
    Draw.Square(this, "Up Square"+CurrentBar, true, 0, 0+Gap*3, Brushes.Green);
    }
    else if (Closes[tf][0] < Opens[tf][0])
    {
    Draw.Square(this, "Down Square"+CurrentBar, true, 0, 0+Gap*3, Brushes.Red);
    }
    break;
    //it continues for the 4 iterations because I have 4 timeseries


    I am supposed to have 4 squares in a row on each bar but it's not plotting correctly
    Also, I think it is using a lot of memory since pc performance decreases.

    Can you help me giving me some lights on having this working properly??

    Thanks

    .​​​​​​

    #2
    Hello Xeferil,

    Thanks for your post and welcome to the NinjaTrader forums!

    Using the draw methods with many objects will have a performance impact. In this regard, you may be better off using a plot for each time frame.

    It is recommended that you do not use variables when adding data series based on this warning note in the help guide: Arguments supplied to AddDataSeries() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided. Trying to load bars dynamically may result in an error similar to: Unable to load bars series. Your NinjaScript may be trying to use an additional data series dynamically in an unsupported manner. Reference: https://ninjatrader.com/support/help...dataseries.htm

    To assist I've created an educational example of how to plot 4 additional time frames using the Plotstyle.Square. Please import the example and apply to a 1 minute chart.

    [edit] When drawing objects/plots, they should be drawn relative to the chart bars as this is what the chart display is based on. In the example code this is accomplished by only plotting when BarsInProgress = 0 (0 = chart bars) Reference: https://ninjatrader.com/support/help...inprogress.htm

    Your conditions only addressed an up or down condition, if using candlestick bars you may want to identify a doji bar (close = open) and if so you could change the example code to account for this 3rd condition, for example:

    PlotBrushes[0][0] = Closes[1][0] > Opens[1][0] ? Brushes.Green : Closes[1][0] < Opens[1][0] ? Brushes.Red : Brushes.Gold; // if green bar paint square green, if red bar paint square red, otherwise must be doji so paint gold.
    [end edit]


    Here is a screenshot of what this looks like:

    Click image for larger version  Name:	Example4Square.PNG Views:	1 Size:	31.6 KB ID:	1047403


    Here is the example indicator:

    Example4Square.zip
    Last edited by NinjaTrader_PaulH; 02-07-2019, 10:41 AM. Reason: Added further information on BarsInProgress & plotbrushes
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thank you very much Paul!!!
      That is basically what I was trying to do.
      Now, can you please help me understand (or point me to where I can learn) all the logic in the exampleso I can have a clear picture of how all the plot works and begin o understand better NinjaScript basics.

      On TFx arrays you set a fixed level for each plot but I don-t know when you pass that parameter. Even further I don-t really understand where you declared those TFx arrays. I see on Properties Region that they are being defined and returned with Values[].
      What is that Values[] array? where does it get the elements? how or when do you pass the level parameter to each plot?

      If I understand correctly for what I have read. When AddPlot is called it creates an array of plots equal to number of bars on the chart. This plots has default color and values.
      All the plots information is held in the Values collection.
      Then when you return the Values[0] to TF3 you are giving all the data of the first plot in Values[0] to TF3.
      How is that when you do TF3[0]=0.4 you are referencing or setting the level foe that first plot?


      Regards.

      Comment


        #4
        Hello Xeferil,

        Thanks for your reply.

        I used the indicator wizard to create the code struucture that you see and I defined in the wizard the four plots and provided them with their plot names. Then it is a simple matter of assigning a value to the plots and using PlotBrushes to set the plot color based on conditions. In this case, each time the chart bars call OnBarUpdate, the plots are set at the same value and really it could be any value but to keep the order of low time frame to high I sequenced in that manner.

        You understand correctly concerning the plots and their relationship to Values. In the region properties, you can see TF3 is tied to the Values[0] data series and TF5 is tied to the next Values[1] data series, etc.

        PlotBrushes are used to provide a dynamic color change to the plot on per bar basis

        Instead of TF15[0] = 0.2; I could have also used Values[2][0] = 0.2; in the OnBarUpdate but I prefer to use the plot name as it is more meaningful.

        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_PaulH View Post
          Hello Xeferil,

          Thanks for your reply.

          I used the indicator wizard to create the code struucture that you see and I defined in the wizard the four plots and provided them with their plot names. Then it is a simple matter of assigning a value to the plots and using PlotBrushes to set the plot color based on conditions. In this case, each time the chart bars call OnBarUpdate, the plots are set at the same value and really it could be any value but to keep the order of low time frame to high I sequenced in that manner.

          You understand correctly concerning the plots and their relationship to Values. In the region properties, you can see TF3 is tied to the Values[0] data series and TF5 is tied to the next Values[1] data series, etc.

          PlotBrushes are used to provide a dynamic color change to the plot on per bar basis

          Instead of TF15[0] = 0.2; I could have also used Values[2][0] = 0.2; in the OnBarUpdate but I prefer to use the plot name as it is more meaningful.
          Thanks. Now I understand a little bit more about Values.
          So, first index of Values[2] references the plot number according to which was first created. Second index of Values[2][0] references the actual bar right?

          If so, what I don't understand yet is how do the script know that when you assign the value TF15[0]=0.2; (wich you say will be the same as Values[2][0]), that 0.2 value references to the level and knows where to plot it?

          Comment


            #6
            By the way.
            I have detected that indicator in the example is not working as I would like it to work and don't know how to fix it.
            In the attached image you can see a one minute graph and the indicator.
            What the indicator should do is paint the box according to price open/close state at that moment.
            When we look at the actual bar, when the 1 minute bar is changing, the boxes should be also changing color according to the action on its respective timeframe. And, as soon the 1 minute bar closes, the boxes should be painted to the state the bar on other timeframes where at the moment of the 1 minute close and never change the color. But as you can see, painted color in 60 minutes boxes doesn't correspond to price open/close at the moment 1 minute bar closed. From the point marked, it should be all green. (I changed data series to 5, 15, 30 and 60 minutes)

            Can you please help me solve this?

            Thanks again
            Last edited by Xeferil; 02-07-2019, 03:56 PM.

            Comment


              #7
              Hello Xeferil,

              Thanks for your post.

              In each OnBarUpdate of the chart bars (BarsInProgress = 0) we are assigning a Y value (vertical position) to locate the plot vertically and the bars ago index [0] is identifying the X value (horizontal position), For example, TF15 is the name of the plot and is associated with Values[2] in the region properties, so in the OnBarUpdate section we can assign the Y value of 0.2. For each plot location (related to the chart bars) we would need to assign a y value( when we want to see the plot).

              Correct the Values[A][B] where [A] is the data series listed in order as the plots are listed in State.SetDefaults and [B] would be the bars ago index of that plot data series. Reference: https://ninjatrader.com/support/help...us/?values.htm

              Regarding the indicator coloring, when used with Calculate.OnBarClose and/or when looking at the indicator with historical data, the bar that will looked at will be the last closed bar, not the currently forming bar. If you use Calculate.OnEachTick for the indicator, on live data or market replay, it will then refer to the currently forming bar. In that sense, you might consider this a "real time" type indicator. Please see " How Bars Data is Referenced" here: https://ninjatrader.com/support/help...nstruments.htm
              Paul H.NinjaTrader Customer Service

              Comment


                #8
                Hello Paul,

                Sorry for my late reply.
                To get it clear. When you AddPlot, the ONLY parameter you must give to it when you make Values[A][B] = x is the Y value (vertical position) ??

                Regarding coloring, I have seen now that when in "real time" the colors are right. Is there any way to get the indicator to also refer to forming bar when in historical data? Maybe plotting on BarsInProgress = 1 or 2 or 3 ??

                Finally, If I would like to add HeikenAshi data series. How can I do it without passing an specific instrument name? I mean, that dataseries change when I change instrument on graph.

                Thanks again for your help!

                Comment


                  #9
                  Hello Xeferil,

                  Thanks for your reply.

                  Correct, the plots X element is related to the chart bar slots so need only provide the Y value as the X will be the current bar.

                  For the historical option, you can turn on "Tick Replay" but this requires that you do have historical tick data.

                  To add HeikenAshi bars, you can use, for example: AddHeikenAshi(null, BarsPeriodType.Minute, 1, MarketDataType.Last); where the instrument is null which then will then use the charts instrument.
                  Paul H.NinjaTrader Customer Service

                  Comment


                    #10
                    Thank you very much Paul!!
                    You have been really helpfull. I will continue learning NS

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Perr0Grande, Today, 08:16 PM
                    0 responses
                    2 views
                    0 likes
                    Last Post Perr0Grande  
                    Started by elderan, Today, 08:03 PM
                    0 responses
                    5 views
                    0 likes
                    Last Post elderan
                    by elderan
                     
                    Started by algospoke, Today, 06:40 PM
                    0 responses
                    10 views
                    0 likes
                    Last Post algospoke  
                    Started by maybeimnotrader, Today, 05:46 PM
                    0 responses
                    12 views
                    0 likes
                    Last Post maybeimnotrader  
                    Started by quantismo, Today, 05:13 PM
                    0 responses
                    7 views
                    0 likes
                    Last Post quantismo  
                    Working...
                    X