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 text after Stochastics test on multiple time frames

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

    Draw text after Stochastics test on multiple time frames

    Hello all,

    I am trying to develop a simple indicator which will test Stochastics value on multiple time frames and Draw something on my main chart (5 min.)

    I have gone through help and forum and I am still stuck. My code below compiles OK, but I see no DrawText written in my chart.

    Could you kindly point out what is wrong with my code? Thanks a lot.

    protected override void Initialize()
    {
    Add(PeriodType.Month, 1);
    Add(PeriodType.Week, 1);
    Add(PeriodType.Minute, 1440);
    Add(PeriodType.Minute, 240);
    Add(PeriodType.Minute, 60);
    Add(PeriodType.Minute, 30);

    Overlay = false;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    if (BarsInProgress == 0);

    {
    if (Stochastics(BarsArray[1], 7, 14, 3).D[0] < 20)
    {
    DrawText("My text" + CurrentBar, "M", 0, Low[0] + (TickSize*14), Color.Blue);
    }

    else if (Stochastics(BarsArray[2], 7, 14, 3).D[0] < 20)
    {
    DrawText("My text" + CurrentBar, "W", 0, Low[0] + (TickSize*12), Color.Blue);
    }

    else if (Stochastics(BarsArray[3], 7, 14, 3).D[0] < 20)
    {
    DrawText("My text" + CurrentBar, "D", 0, Low[0] + (TickSize*10), Color.Blue);
    }

    else if (Stochastics(BarsArray[4], 7, 14, 3).D[0] < 20)
    {
    DrawText("My text" + CurrentBar, "240", 0, Low[0] + (TickSize*8), Color.Blue);
    }

    else if (Stochastics(BarsArray[5], 7, 14, 3).D[0] < 20)
    {
    DrawText("My text" + CurrentBar, "60", 0, Low[0] + (TickSize*6), Color.Blue);
    }

    else if (Stochastics(BarsArray[6], 7, 14, 3).D[0] < 20)
    {
    DrawText("My text" + CurrentBar, "30", 0, Low[0] + (TickSize*4), Color.Blue);
    }
    else
    {
    }
    }
    }

    #2
    Hello kadan,

    Thanks for your post.

    If you haven't already, check the "log" tab of the NinjaTrader control center for any error messages related to the indicator.

    I suspect you may not have enough data loaded or are not checking for data being loaded with a check of the CurrentBars. Reference: http://ninjatrader.com/support/helpG...urrentbars.htm

    If the above does not resolve the issue, you might want to add print statements inside so you can see when/if the conditions are true.

    Lastly, even though you are using CurrentBar in the tag name I would make each tagname unique so that on the same bar if you have multiple true statements you would see multiple draw statements.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hello kadan,

      Take a look at the first line of OnBarUpdate:
      if (BarsInProgress == 0);

      You have semi-colon after an if statement. These don't work that way.

      Below is a publicly available link to a 3rd party education site on branching commands.
      Encode branching logic with if, else-if and else. Evaluate conditions to true or false.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by NinjaTrader_Paul View Post
        Hello kadan,

        Thanks for your post.

        If you haven't already, check the "log" tab of the NinjaTrader control center for any error messages related to the indicator.

        I suspect you may not have enough data loaded or are not checking for data being loaded with a check of the CurrentBars. Reference: http://ninjatrader.com/support/helpG...urrentbars.htm

        If the above does not resolve the issue, you might want to add print statements inside so you can see when/if the conditions are true.

        Lastly, even though you are using CurrentBar in the tag name I would make each tagname unique so that on the same bar if you have multiple true statements you would see multiple draw statements.
        Dear Paul,

        Thank you for your answer.

        I have very limited knowledge of C# and I live off NT wizard and NT forum and NT help file.

        First, I want to see multiple draw statements if conditions are met on more timeframes. So this is fine for me.

        I have changed the code to test for sufficient number of bars like this:

        if (CurrentBars[0] < BarsRequired || CurrentBars[1] < BarsRequired || CurrentBars[2] < BarsRequired || CurrentBars[3] < BarsRequired || CurrentBars[4] < BarsRequired || CurrentBars[5] < BarsRequired || CurrentBars[6] < BarsRequired) return;

        Then I have checked my NT log and there is no error.

        I have loaded lots of history into a chart but still nothing is drawn eventhough I know that conditions were met at least on some timeframes.

        At this moment I am really stuck and have no idea how to move forward on this. Anybody else has an idea??

        Comment


          #5
          Hello kadan,

          Thanks for your reply.

          As NinjaTrader_ChelseaB observed the semi-colon at the end of the line if (BarsInProgress == 0); would prevent your intended operation of only on the base bar updates and the code below that would then be run on each bars in progress update.

          If you are still not getting an output, please attach your current source code file for review.
          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_Paul View Post
            Hello kadan,

            Thanks for your reply.

            As NinjaTrader_ChelseaB observed the semi-colon at the end of the line if (BarsInProgress == 0); would prevent your intended operation of only on the base bar updates and the code below that would then be run on each bars in progress update.

            If you are still not getting an output, please attach your current source code file for review.
            Dear Paul,

            I have changed the code based on your suggestion to test "CurrentBars[0] < BarsRequired". The code compiles OK. I get no errors in NT log window.

            Still, when applied onto chart there are no DrawTexts at all. Attached I am sending the whole code. As I am not a programmer I have no clue what is wrong. I will appreciate any suggestions to make it work.
            Attached Files

            Comment


              #7
              Hello kadan,

              Thanks for your reply.

              Using your code as is and a base chart of 1 minute bars, you would need to have about 750 days of data loaded for the indicator to begin showing on the chart.

              Using the CurrentBars check using BarsRequired in the way it is written means that the code will not run until the worst case amount of days of data have been loaded. One of the data series is a monthly and this means that you would need 20 months of data loaded before you would see any output on the chart (BarsRequired defaults to 20). You can specify a different amount by setting the BarsRequired value in the initialize() method (see: http://ninjatrader.com/support/helpG...rsrequired.htm) however you would also have to consider the amount of bars required for the indicator (MACD) to produce a reasonable signal for the Weekly and monthly MACDs.

              Alternatively you might consider modifying your code in this way, example:

              if (CurrentBars[1] > BarsRequired && Stochastics(BarsArray[1], 7, 14, 3).D[0] < 20)

              The if statement will verify it has more than enough bars for its BarsArray object and then will check the value of the MACD on that BarsArray. Once the conditions are met the text will show.

              You could modify each line similarly making sure to match the correct BarsArray object with the CurrentBars check. Example for clarity:
              else if (CurrentBars[5] > BarsRequired && Stochastics(BarsArray[5], 7, 14, 3).D[0] < 20)

              If you choose to do this, be sure to remove the initial CurrentBars check line and the return;

              I would advise to add back in the line: if (BarsInProgress == 0) at the top (note: no semi colon should be used).

              Making these changes you will see text appear with less than 750 days of data loaded however keep in mind that you may not be getting the text print from the weekly or Monthly bars until enough data is loaded (20 weeks, 20 months).
              Paul H.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_Paul View Post
                Hello kadan,

                Thanks for your reply.

                Using your code as is and a base chart of 1 minute bars, you would need to have about 750 days of data loaded for the indicator to begin showing on the chart.

                Using the CurrentBars check using BarsRequired in the way it is written means that the code will not run until the worst case amount of days of data have been loaded. One of the data series is a monthly and this means that you would need 20 months of data loaded before you would see any output on the chart (BarsRequired defaults to 20). You can specify a different amount by setting the BarsRequired value in the initialize() method (see: http://ninjatrader.com/support/helpG...rsrequired.htm) however you would also have to consider the amount of bars required for the indicator (MACD) to produce a reasonable signal for the Weekly and monthly MACDs.

                Alternatively you might consider modifying your code in this way, example:

                if (CurrentBars[1] > BarsRequired && Stochastics(BarsArray[1], 7, 14, 3).D[0] < 20)

                The if statement will verify it has more than enough bars for its BarsArray object and then will check the value of the MACD on that BarsArray. Once the conditions are met the text will show.

                You could modify each line similarly making sure to match the correct BarsArray object with the CurrentBars check. Example for clarity:
                else if (CurrentBars[5] > BarsRequired && Stochastics(BarsArray[5], 7, 14, 3).D[0] < 20)

                If you choose to do this, be sure to remove the initial CurrentBars check line and the return;

                I would advise to add back in the line: if (BarsInProgress == 0) at the top (note: no semi colon should be used).

                Making these changes you will see text appear with less than 750 days of data loaded however keep in mind that you may not be getting the text print from the weekly or Monthly bars until enough data is loaded (20 weeks, 20 months).
                Dear Paul,

                Thank you for your suggestions. I have left out the test for Monthly time frame and I use 200 days of history which is sufficient for Weekly.

                I finally git the result I was aiming for. I am attaching the final code in case somebody would find it (or parts of it) useful.

                I very much appreciate your support.

                Have a nice weekend.
                Attached Files

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by FrancisMorro, Today, 03:24 AM
                0 responses
                1 view
                0 likes
                Last Post FrancisMorro  
                Started by Segwin, 05-07-2018, 02:15 PM
                10 responses
                1,770 views
                0 likes
                Last Post Leafcutter  
                Started by Rapine Heihei, 04-23-2024, 07:51 PM
                2 responses
                31 views
                0 likes
                Last Post Max238
                by Max238
                 
                Started by Shansen, 08-30-2019, 10:18 PM
                24 responses
                944 views
                0 likes
                Last Post spwizard  
                Started by Max238, Today, 01:28 AM
                0 responses
                11 views
                0 likes
                Last Post Max238
                by Max238
                 
                Working...
                X