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

Multiple OnRender drawings within one bar

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

    Multiple OnRender drawings within one bar

    Hello,

    I would like to draw a mark (rectangle) on chart every time MarketDataType.Last e.Volume > [predefined value].

    Right now I am able to draw just one mark per bar (see attached screenshot and the code).

    How can I store (and plot) multiple values for each bar? Am I supposed to use something like 2-dimensional array or Series? Would you give me a hint or code example, please?

    Thanks
    Attached Files

    #2
    Hello tester11x,

    If you want to store multiple values per bar but also retain the series you are using, you are likely going to need a list or dictionary for that purpose.

    It looks like you are storing both the volume and price so a dictionary may work for this use case, here is one example of defining a dictionary:

    Code:
    private Series<Dictionary<double, long>> myValues;
    
    
    myValues = new Series<Dictionary<double, long>> (this);            
    
    
    
    myValues[0][e.Price] = e.Volume;
    And to use it:

    Code:
    foreach(KeyValuePair<double, long> pair in myValues[0])
    {
           double price = pair.Key;
           long volume = pair.Value;
    }


    For each bar you will have a collection of prices and the volumes at those prices by using a dictionary. You could also use a list with an object but that would be more complex. A list could be used if wanted to mark all instances which may include duplicate prices. An object which has a double and long variable could be created for each time your condition becomes true and added to the list. A dictionary does not allow for duplicate keys, so that would work best if you wanted to accumulate or do something to the existing value if the same price/key were to come up again in the same bar.


    I look forward to being of further assistance.
    Last edited by NinjaTrader_Jesse; 08-29-2019, 03:36 PM.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks for help - Dictionary is exactly what I was looking for and the first part of code works very well. But when I try to use the foreach cycle for reading following error message appers:

      Foreach command cannot be used for variable type NinjaTrader.NinjaScript.Series<System.Collections. Generic.Dictionary<double,long>>, because NinjaTrader.NinjaScript.Series<System.Collections. Generic.Dictionary<double,long>> doesn't contain public definition of GetEnumerator.

      (It is just translation to EN as my system uses different language)

      Something is missing somewhere I guess....?

      Comment


        #4
        Hello tester11x,

        Thank you for the reply.

        It looks like I missed the BarsAgo on that sample, you just need to add the [0] to the series:


        Code:
        foreach(KeyValuePair<double, long> pair in myValues[B][0])[/B]
        I went ahead and updated the original post as well in case anyone copies that.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hello,

          the "foreach" issue is solved. Thank you.

          Now, when I add the indicator to the chart I get this error: Error on calling 'OnMarketData' method on bar 63: Object reference not set to an instance of an object.

          It is connected to line 91 in my code: singleVolumes[0][e.Price] = e.Volume;

          Would you please have a look to attached script and check the error?

          Thanks
          Attached Files

          Comment


            #6
            Hello tester11x,

            You would need to create a new dictionary for each bar where you wanted to use it, this was not shown in the prior sample as this will relate to your script design.

            The Series<T> is just a container so you can fill it with whatever objects you wanted, but you need to create the objects if you want to use them. I had not shown this in the sample specifically, but to create a new dictionary and assign it to the series would look like the following:

            Code:
            myValues[0] = new Dictionary<double, long>();

            The same case would happen for any object you supply to a series, it would need to hold a value for the bar in question and then you can modify that object or in this case insert dictionary entries.

            If you want to have a dictionary created for every bar, you can just put this in OnBarUpdate so the series gets a new value per bar. However because you are using tick data from OnMarketData if you plan to use the script OnEachTick or OnPriceChange you will need to surround creating the object with a condition:

            Code:
            if(IsFirstTickOfBar)
            {
                 myValues[0] = new Dictionary<double, long>();
            }
            That way the series is only created at the start of the bar, and not for every tick.

            If you want to only set a value on bars where your condition was true, you can check if the object is null and if so create the dictionary for the bar. This type of logic would happen before setting the value in OnMarketData instead of OnBarUpdate. Part of this would be to decrease the amount of memory used but can also be used logically. Series which have no value set for a bar consider that bar an invalid data point which you can check for to use with logic.



            In any case it is good to check if the series has a value before trying to assign or modify the value in any case where an object is used.

            Code:
            if(myValues[0] == null) // haven't set a value yet
            I attached a quick sample of what was described in the first post which shows a couple of ways to create the dictionary.


            I look forward to being of further assistance.
            Attached Files
            JesseNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by aussugardefender, Today, 01:07 AM
            0 responses
            1 view
            0 likes
            Last Post aussugardefender  
            Started by pvincent, 06-23-2022, 12:53 PM
            14 responses
            238 views
            0 likes
            Last Post Nyman
            by Nyman
             
            Started by TraderG23, 12-08-2023, 07:56 AM
            9 responses
            383 views
            1 like
            Last Post Gavini
            by Gavini
             
            Started by oviejo, Today, 12:28 AM
            0 responses
            1 view
            0 likes
            Last Post oviejo
            by oviejo
             
            Started by pechtri, 06-22-2023, 02:31 AM
            10 responses
            125 views
            0 likes
            Last Post Leeroy_Jenkins  
            Working...
            X