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

Double value as key in arrayList

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

    Double value as key in arrayList

    Hi,

    I created an array with price and volume of the candle. After that when I try to get the volume for a given price not able to get it even though the key got added to the array.
    Its working for /NQ but failing for /CL. can you please let me know if there any limitations with double. I am not able to get the POCPrice from the list eventhough the key present when I lopped through it to find the existing values in the array.


    Sample code snippet below,

    if(barsType.Volumes[CurrentBars[0]].GetMaximumVolume(null, out price) > 0 ){
    POCPrice = price;
    }


    double ticksAggregation= 4 * TickSize;

    Print(Time[0]+ " POCPrice: "+POCPrice +" ticksAggregation: "+ticksAggregation);


    //create data dictionary
    Dictionary<double, long> kvpArray = new Dictionary<double, long>();

    for (double i = Lows[0][0]; i <= Highs[0][0]; i=i+ticksAggregation) // cycle through each tick level
    {

    long bidVol = barsType.Volumes[CurrentBars[0]].GetBidVolumeForPrice(i);
    long askVol = barsType.Volumes[CurrentBars[0]].GetAskVolumeForPrice(i);

    Print(Time[0]+ " Adding price and values: "+i+ " bidVol: "+ bidVol+" askVol: "+askVol );
    kvpArray.Add(i, bidVol +askVol);

    }


    foreach (KeyValuePair<double, long> pair in kvpArray)
    {
    Print(Time[0]+ " Key values : "+ pair.Key +" Value : "+pair.Value);

    }


    long pocVolume=0;

    if(kvpArray.ContainsKey(POCPrice)){
    pocVolume= kvpArray[(double)POCPrice];
    }else{
    Print(Time[0]+ " POCPrice key not present : "+ POCPrice);
    }




    #2
    Hello greenhollow803,

    Thank you for your post.

    It seems as though the following condition is looking for a key that returns a value of POCPrice:
    if(kvpArray.ContainsKey(POCPrice))

    What you are actually adding as the key is the value of i from this loop:
    for (double i = Lows[0][0]; i <= Highs[0][0]; i=i+ticksAggregation) // cycle through each tick level
    {

    long bidVol = barsType.Volumes[CurrentBars[0]].GetBidVolumeForPrice(i);
    long askVol = barsType.Volumes[CurrentBars[0]].GetAskVolumeForPrice(i);

    Print(Time[0]+ " Adding price and values: "+i+ " bidVol: "+ bidVol+" askVol: "+askVol );
    kvpArray.Add(i, bidVol +askVol);
    }

    If you print the value of POCPrice and compare that to the values for i, what are the results? I see you already have prints that include POCPrice and i:
    Code:
    Print(Time[0]+ " POCPrice: "+POCPrice +" ticksAggregation: "+ticksAggregation);
    Code:
    Print(Time[0]+ " Adding price and values: "+i+ " bidVol: "+ bidVol+" askVol: "+askVol );
    After viewing the output, do you see a discrepancy between these values?

    There is a thread that discusses getting the POC of a bar and looping through the price levels here:


    Please let me know if I may be of further assistance.​
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      I see same value. for i 76.08 and POCPrice also 76.08. But not able to fetch the value from the list.

      /CL 30min candle at 9am EST today for reference

      Comment


        #4
        Originally posted by greenhollow803 View Post
        I see same value. for i 76.08 and POCPrice also 76.08. But not able to fetch the value from the list.

        /CL 30min candle at 9am EST today for reference
        Hello greenhollow803,

        Thanks for your reply.

        It seems as though the NinjaScript methods and properties are behaving as expected. Loops and dictionaries are both general C# concepts and our support team does not offer C# educational services or hands-on debugging assistance. You will ultimately need to use debugging tools, such as additional prints, to better understand why this script is not behaving as expected. I also suggest checking the Log tab of the Control Center for any error messages.

        What are the results of this loop?
        Code:
        foreach (KeyValuePair<double, long> pair in kvpArray)
        {
        Print(Time[0]+ " Key values : "+ pair.Key +" Value : "+pair.Value);
        
        }
        Do you see the keys and values in the output? What if you try to print the dictionary count before and after your condition looking for POCPrice? More information may be found at the following publicly available link:


        ​Thanks for your time and patience.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Thank you for the response.

          The for each loop printed all the price levels and its volume values correct. But when I try to get POC Price value it says not found. Key value added as double.
          Same code is working fine for /ES and /NQ. Only not working for /CL and /NG.

          I printed before and after also counts etc. As I said code is working for /ES and /NQ. is there is an issue with code it should throw error for /NQ also right?

          is there anything to do double values of /CL or /NG ?

          Comment


            #6
            Hello greenhollow803,

            Thanks for your reply.

            I am not aware of any limitations for doubles on CL or NG. What expiry months are you using for these instruments? Are you getting real-time data for CL and NG? (you may check this by viewing the bid/ask prices in chart trader to see if they are updating)

            Do you see any output at all when running this script on CL and NG compared to the output you see when running it on ES and NQ? Please export the output from running it on CL or NG by right-clicking the NinjaScript Output window and selecting Save As to create a text file with the output that may be attached to your reply.

            I look forward to your response.
            Emily C.NinjaTrader Customer Service

            Comment


              #7
              I get latest market data and there are no issues with the real-time data.
              I printed the log details and attached here.

              I am looping through the candle and adding the price as key and total bid and ask volumes as Value.
              After that I printed the all the key and values.

              When I try to access the POC Price which is 76.24 its not fetching even though it exists in the list.
              Attached Files

              Comment


                #8
                It printed "key equals POCPrice". So approxCompare is working.
                How can I access a key value with ApproxCompare ? Do you got any example

                Comment


                  #9
                  Originally posted by greenhollow803 View Post
                  It printed "key equals POCPrice". So approxCompare is working.
                  How can I access a key value with ApproxCompare ? Do you got any example
                  I suggest reviewing the following page of the help guide and the external resources linked at the bottom of the page for more information:


                  We also have a page for ApproxCompare() here:


                  Please let us know if we may be of further assistance.
                  Emily C.NinjaTrader Customer Service

                  Comment


                    #10
                    Thanks for the help. Only catch is need t find a way to access data by key instead of looping through the values

                    Comment


                      #11
                      Hello greenhollow803,

                      Thanks for your reply.

                      You could consider looping through the dictionary to find keys and use ApproxCompare() to compare with POCPrice as the other forum user previously mentioned (it seems they have removed their post). Here is the loop they provided:


                      Code:
                      foreach (KeyValuePair<double, long> pair in kvpArray)
                      {
                      Print(Time[0]+ " Key values : "+ pair.Key +" Value : "+pair.Value);
                      
                      if ( (pair.Key).ApproxCompare(POCPrice) == 0 ) // if same
                      {
                      Print("key equals POCPrice");
                      }
                      else
                      {
                      Print("key NOT equals POCPrice");
                      }
                      
                      }​


                      I see in your original snippet, you were attempting to do the following assignment:
                      Code:
                      pocVolume= kvpArray[(double)POCPrice]
                      You could modify the previous snippet to do something similar:
                      Code:
                      foreach (KeyValuePair<double, long> pair in kvpArray)
                      {
                      Print(Time[0]+ " Key values : "+ pair.Key +" Value : "+pair.Value);
                      
                      if ( (pair.Key).ApproxCompare(POCPrice) == 0 ) // if same
                      {
                      Print("key equals POCPrice");
                      // assign the value of the key that equals POCPrice to the pocVolume variable
                      pocVolume = pair.Key;
                      Print("pocVolume: " + pocVolume);
                      }
                      else
                      {
                      Print("key NOT equals POCPrice");
                      }
                      
                      }​
                      I am not sure if this is exactly what you are looking to achieve, though you could test it out and review the print of the value of pocVolume to see if the output matches the expected value or not. Then, this snippet could be modified as needed to suit your needs.

                      Thanks for your time and patience.​
                      Emily C.NinjaTrader Customer Service

                      Comment


                        #12
                        yeah. I added that code and its working. But looping for every price is bad a coding practice IMO. Hence Looking for options to access directly using key instead of looping through. May be with some lamda expressions might be possible right. Amy idea.

                        Thank you very much for all your help

                        Comment


                          #13
                          Originally posted by greenhollow803 View Post
                          yeah. I added that code and its working. But looping for every price is bad a coding practice IMO. Hence Looking for options to access directly using key instead of looping through. May be with some lamda expressions might be possible right. Amy idea.

                          Thank you very much for all your help
                          You could consider calling ApproxCompare() when you are already looping the the bar's prices and adding keys/values to your dictionar. Then, add POCPrice as the key rather than i when i is compared to POCPrice and deemed to be the same:
                          Code:
                          for (double i = Lows[0][0]; i <= Highs[0][0]; i=i+ticksAggregation) // cycle through each tick level
                          {
                          
                          long bidVol = barsType.Volumes[CurrentBars[0]].GetBidVolumeForPrice(i);
                          long askVol = barsType.Volumes[CurrentBars[0]].GetAskVolumeForPrice(i);
                          
                          Print(Time[0]+ " Adding price and values: "+i+ " bidVol: "+ bidVol+" askVol: "+askVol );
                          if (i.ApproxCompare(POCPrice) == 0)
                          kvpArray.Add(POCPrice, bidVol + askVol);
                          else
                          kvpArray.Add(i, bidVol +askVol);
                          
                          }

                          Then, the following should evaluate to be true because there would be a key that was assigned the value of POCPrice directly rather than the value of i:
                          Code:
                          if(kvpArray.ContainsKey(POCPrice))
                          Please let us know if we may be of further assistance.
                          Emily C.NinjaTrader Customer Service

                          Comment


                            #14
                            Thank you very much Emily. I am working on calculate the 68% of the volume surrounded near POC to get the VAL and VAH. hence I am collecting the price and volume. Here I need to loop price values near POC until I get the 68% of total volume of the candle. Hence I need to extend my solution to get values from the array for the remaining price levels as well. For now its working for me and able to get the VAL and VAH. But need to simplify the looping logic. Again thanks a lot for your help. Much appreciated

                            Comment


                              #15
                              Originally posted by greenhollow803 View Post
                              Thank you very much Emily. I am working on calculate the 68% of the volume surrounded near POC to get the VAL and VAH. hence I am collecting the price and volume. Here I need to loop price values near POC until I get the 68% of total volume of the candle. Hence I need to extend my solution to get values from the array for the remaining price levels as well. For now its working for me and able to get the VAL and VAH. But need to simplify the looping logic. Again thanks a lot for your help. Much appreciated
                              Simplifying your looping logic falls under the category of general C# support which goes beyond the support offered by our team. You can utilize outside educational resources to help you with reducing and simplifying your logic. Unfortunately, in the support department at NinjaTrader it is against our policy to create, debug, or modify, code or logic for our clients. Further, we do not provide C# programming education services or one-on-one educational support in our NinjaScript Support department. This is so that we can maintain a high level of service for all of our clients as well as our associates.

                              That said, through email or on the forum we are happy to answer any questions you may have about NinjaScript if you decide to code this yourself. We are also happy to assist with finding resources in our help guide as well as simple examples, and we are happy to assist with guiding you through the debugging process to assist you with understanding unexpected behavior.

                              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 to 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. Please let me know if this option interests you so we may follow up accordingly.

                              Thank you for using NinjaTrader.​
                              Emily C.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by rhyminkevin, Today, 04:58 PM
                              0 responses
                              16 views
                              0 likes
                              Last Post rhyminkevin  
                              Started by lightsun47, Today, 03:51 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post lightsun47  
                              Started by 00nevest, Today, 02:27 PM
                              1 response
                              14 views
                              0 likes
                              Last Post 00nevest  
                              Started by futtrader, 04-21-2024, 01:50 AM
                              4 responses
                              49 views
                              0 likes
                              Last Post futtrader  
                              Started by Option Whisperer, Today, 09:55 AM
                              1 response
                              15 views
                              0 likes
                              Last Post bltdavid  
                              Working...
                              X