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 a rectangle

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

    Draw a rectangle

    I'm trying to create an indicator that draws a Candle on 5 bars.

    unable to draw a rectangle in a window, so I wrote a strategy.

    It works when you draw a single candle.

    when a single candle drawing no problem, when I try to draw 5 candles it does not work. see the following strategy. What's wrong?

    How to do this with an indicator?
    Attached Files

    #2
    Hello,

    Thank you for the question.

    This would be caused because you are not checking if there are enough bars for your loop.

    Currently on bar 1 you have 1 bar so it works, when you count to 5 bars on bar 1 you get to a invalid index location because 5 bars have not happened yet by the end of your loop.

    I got this script working by adding this in the beginning of your OnBarUpdate method.

    Code:
    if(CurrentBar < NBarras) return;
    You need at least 5 bars for your loop to work, so do not calculate until at least 5 bars are available preventing an index error. I used the public variable NBarras as this is how you are getting the 5 bars it seems.

    Also you can see this error by opening the output window (Tools -> Output Window).

    For this to be in an indicator You can create a new indicator and name it what you want this to be.
    When you get to its code, remove the OnBarUpdate method and its braces.
    Next replace OnBarUpdate from what you have in the strategy into the indicator along with the public properties at the bottom, your private method, and the variables at the top.

    Please let me know if I may be of additional assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Please take a look now at the code. I put a for (.....) to draw the 5 candles, but now the system hangs. No answer.

      Now follows the code
      Attached Files

      Comment


        #4
        Hello,

        It looks like in this updated script the for loop has an error, the sign is =+ but needs to be += for this to run correctly and not freeze.

        Generally in NinjaScript files it is not recommended to use for loops as it locks the processing as you see with this script. This can be bad if you have a lot of historical data as it will lock the chart until it is finished loading all of the bars.

        I have changed the top of the script from

        Code:
        if(CurrentBar < (NBarras * NCandles)) return;
        to

        Code:
        if(CurrentBar < Count - 2) return;
        Now instead of just having enough bars to calculate, this waits until the end of the historical data to start. This prevents it from running through un needed bars because you have a for loop that you are telling specific bar locations with.

        There are various ways to use the current bar, another way to get only in realtime would be

        Code:
        if(Historical) return;
        This means that a tick needs to come in before the calculation would happen. The first example lets you see it on the last historical bar so you don't need to wait on a tick.

        All three ways are valid, this just depends on what you are trying to do. For the fastest speeds you would want to use one of the two I posted about, the current way will work also this is just something to keep in mind.

        Please let me know if I may be of additional assistance.
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by DayTradingDEMON, Today, 09:28 AM
        4 responses
        21 views
        0 likes
        Last Post DayTradingDEMON  
        Started by geddyisodin, Yesterday, 05:20 AM
        9 responses
        50 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by George21, Today, 10:07 AM
        1 response
        12 views
        0 likes
        Last Post NinjaTrader_ChristopherJ  
        Started by Stanfillirenfro, Today, 07:23 AM
        9 responses
        24 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by navyguy06, Today, 09:28 AM
        1 response
        9 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Working...
        X