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

stop limit (mkt) order 1 tick above previous bar highest bid+offer volume price

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

    #31
    Hello Jesse

    1) Again, what I want is highest bid or ask volume at a price​. I know the dictionary holds volume for each price. my question is how to obtain the the highest voler of the price for that bar from the data, e.g. set double _maxvolumeprice but how to obtain the highest volume price for the 1 barago from the dictionary coded.

    2) You mentioned I need to separate the accumulation code under OnMarketData because currently its under onBarupdate. And I want the update timing to be onMarketData
    You could use what you have posted and add OnMarketData to support accumulating volumes at prices to the dictionaries and then set that result to the series you made.
    Hope my question is clear.

    Comment


      #32
      Hello Marble,

      1) Again, what I want is highest bid or ask volume at a price​. I know the dictionary holds volume for each price. my question is how to obtain the the highest voler of the price for that bar from the data, e.g. set double _maxvolumeprice but how to obtain the highest volume price for the 1 barago from the dictionary coded.
      I understand, that is why I provided the samples that I had. You would need to loop over the volume and find the highest price.

      Code:
      foreach (KeyValuePair<double, double> kvp in MySeriesDictAsk[0])
      {
      Print("price: " + kvp.Key + " volume: " + kvp.value);
      }​​
      A simple way to find the highest price would be to use a variable.

      Code:
      long maxVol = 0; 
      double price = 0;
      foreach (KeyValuePair<double, double> kvp in MySeriesDictAsk[0])
      {
          if(kvp.Value > maxVol)
          {
              maxVol = kvp.Value; 
              price = kvp.Key;
          }
      }​


      You mentioned I need to separate the accumulation code under OnMarketData because currently its under onBarupdate. And I want the update timing to be onMarketData
      I said that you can alternatively do that similar to the first example that I provided using VolumeProfile. You provided a different example that uses OnBarUpdate and a 1 tick series. That is also a way to do the same task.
      JesseNinjaTrader Customer Service

      Comment


        #33
        Jesse

        1 I have added as below(at the end of calculation). but this max price is only for max of Ask. I need the highest vol (ask+bid) this looks awkward. How can I get the highest volme (ask +bid) price for the bar?
        Code:
                private void CalculateValues(bool forceCurrentBar)
                {
                    // This lets us know what processing mode we are in
                    // if indexOffset == 0 and State is Realtime then we are in 'realtime processing mode'
                    // if indexOffset is > 0 then we are in 'historical processing mode'
                    int     indexOffset     = BarsArray[1].Count - 1 - CurrentBars[1];
                    bool     inTransition     = State == State.Realtime && indexOffset > 1;
        
                    // For Calculate.OnBarClose in realtime processing we have to advance the index on the tick series to not be one tick behind
                    // The means, at the end of the 'transition' (where State is Realtime but we are still in historical processing mode) -> we have to calculate two ticks (CurrentBars[1] and CurrentBars[1] + 1)
                    if (!inTransition && lastInTransition && !forceCurrentBar && Calculate == Calculate.OnBarClose)
                        CalculateValues(true);
        
                    bool     useCurrentBar     = State == State.Historical || inTransition || Calculate != Calculate.OnBarClose || forceCurrentBar;
        
                    // This is where we decide what index to use
                    int     whatBar         = useCurrentBar ? CurrentBars[1] : Math.Min(CurrentBars[1] + 1, BarsArray[1].Count - 1);
        
                    // This is how we get the right tick values
                    double     volume             = BarsArray[1].GetVolume(whatBar);
                    double    price            = BarsArray[1].GetClose(whatBar);            
        
                    // Accumulate volume
                    if (price >= BarsArray[1].GetAsk(whatBar))
                    {
                        buys += volume;
                        if (TempAskDict.ContainsKey(price))
                            TempAskDict[price] += volume;
                        else
                            TempAskDict.Add(price, volume);
                    }
                    else if (price <= BarsArray[1].GetBid(whatBar))
                    {
                        sells += volume;
                        if (TempBidDict.ContainsKey(price))
                            TempBidDict[price] += volume;
                        else
                            TempBidDict.Add(price, volume);
                    }
        
                    lastInTransition     = inTransition;
                }
        
                private void SetValues(int barsAgo)
                {
                    double maxVol = 0;
                    double price = 0;
        
                    // Typical assignment for BuySellVolume
                    BuyVolume[barsAgo] = buys + sells;
                    SellVolume[barsAgo] = sells;
        
                    // When using Dictionaries, make sure we create a new empty dictionaries to store values from our TempDictionaries
                    MySeriesDictAsk[barsAgo] = new Dictionary<double, double>();
                    MySeriesDictBid[barsAgo] = new Dictionary<double, double>();
        
                    // Populate values from the TempAsk Dictionary to the bar's Ask Dictionary
                    foreach (KeyValuePair<double, double> kvp in TempAskDict)
                        MySeriesDictAsk[barsAgo].Add(kvp.Key, kvp.Value);
        
                    // Populate values from the TempBid Dictionary to the bar's Bid Dictionary
                    foreach (KeyValuePair<double, double> kvp in TempBidDict)
                        MySeriesDictBid[barsAgo].Add(kvp.Key, kvp.Value);
        
        
                    // Be sure to reset our accumulation of Ask Volume from Dictionary
                    double dictAskTotalVolume = 0;
                    foreach (KeyValuePair<double, double> kvp in MySeriesDictAsk[barsAgo])
                        dictAskTotalVolume += kvp.Value;
                ​​
        ​​
                    // Assign our accumulation of Ask Volume to the plot
                    Values[2][barsAgo] = dictAskTotalVolume;
        
                    // Be sure to reset our accumulation of Bid Volume from Dictionary
                    double dictBidTotalVolume = 0;
                    foreach (KeyValuePair<double, double> kvp in MySeriesDictBid[barsAgo])
                        dictBidTotalVolume += kvp.Value;
        
                    // Assign our accumulation of Bid Volume to the plot
                    Values[3][barsAgo] = dictBidTotalVolume;
        
                    // Correct the Buy Volume plot to hold Buy and Sell Volume
                    Values[2][barsAgo] = Values[2][barsAgo] + Values[3][barsAgo];    
        
        
                    //Set Highest Volumeprice
                    foreach (KeyValuePair<double, double> kvp in MySeriesDictAsk[0]){
                                if(kvp.Value > maxVol)
                            {
                                maxVol = kvp.Value;
                                price = kvp.Key;
        
                            }
        
                        }
        
                    //Print Highest Volumeprice**returns error as Key ValuePair not accessable, Statement expected...
                    foreach (KeyValuePair<double, double> kvp in MySeriesDictAsk[0]){
                    Print("price: " + kvp.Key + " volume: " + kvp.value);
                            }
        
        
        
                }​

        2. I have added Print code to output the highest price but is returns error KeyValuepair is not accessable. I have changed access level to public but error remains.. how can I set the access level>
        Code:
                    //Print Highest Volumeprice**returns error as Key ValuePair not accessable, Statement expected...
                    foreach (KeyValuePair<double, double> kvp in MySeriesDictAsk[0]){
                    Print("price: " + kvp.Key + " volume: " + kvp.value);
                            }
        ​

        3. You mentioned the attached is much better starting point than vol profile that you suggested. therefore I am workin on this file. You suggested to set accum under OnMarketData() because this is under OnBarUpdate. (And I want the update to be made on each tick), I am following our suggestion to set accum under OnMarketData() but not clear how to move the accum part you mentioned to to be moved to under OnMarketData.
        Now you are suggesting to use vol profile??? I am more confused

        Thanks,
        Last edited by Marble; 10-07-2022, 05:49 PM.

        Comment


          #34
          Hello Marble,

          1 I have added as below(at the end of calculation). but this max price is only for max of Ask.
          You don't need to make any changes to the CalculateValues method, that already works for the task that you requested. That already accumulates the price/volume information into dictionaries so you can use that data later. That works for both ask and bid data.

          I need the highest vol (ask+bid) this looks awkward. How can I get the highest volme (ask +bid) price for the bar?
          If you need to find ask + bid volume you would have to make logic to do that. The ask and bid volume are two separate volumes so you would have to find the max ask volume and price for that max volume and do the same task for bid volume. If you then wanted to add those two volumes together you could do that after finding the volumes.

          I provided a simple example of how you could loop over the data that the sample you provided already makes, that is in post 32. That type of logic could be placed in OnBarUpdate after the data is set using SetValues(0);


          2. I have added Print code to output the highest price but is returns error KeyValuepair is not accessable. I have changed access level to public but error remains.. how can I set the access level>
          Did you make other changes to the script such as its using statements? The script you uploaded already uses KeyValuePair so that should not have any problems. This is copied directly from the script and is how you loop over the dictionary data:

          Code:
          foreach (KeyValuePair<double, double> kvp in MySeriesDictBid[barsAgo])



          3. You mentioned the attached is much better starting point than vol profile that you suggested. therefore I am workin on this file. You suggested to set accum under OnMarketData() because this is under OnBarUpdate. (And I want the update to be made on each tick), I am following our suggestion to set accum under OnMarketData() but not clear how to move the accum part you mentioned to to be moved to under OnMarketData.
          Now you are suggesting to use vol profile??? I am more confused

          Early on in the thread I had suggested looking at the volume profile code to get an idea of how to accumulate volume into dictionaries. You then provided an alternative sample that you found on the forum. We dont need to continue looping back to the older parts of this thread because you provided a different sample to start from.

          I had mentioned that you can alternatively use OnMarketData similar to the volume profile if you wanted to or were more comfortable using that logic, That would only be if you had already explored the volume profile and felt that you understood its code well. The sample you provided does what you are asking using OnBarUpdate. No changes are needed to that sample, you don't have to modify that at all to accumulate the data into dictionaries because it already does that task. Its just an alternate way of doing the same task.

          If you are having confusion on the sample that you attached I would suggest to re import the sample so it has no modifications and then explore the code so that you are certain you understand what each part of the code is doing. That would greatly help when you want to make other changes to the code such as finding a max volume at a price.
          JesseNinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by shahabjet1, 08-23-2020, 01:32 AM
          3 responses
          405 views
          0 likes
          Last Post NinjaTrader_Emily  
          Started by tkaboris, Today, 10:46 AM
          9 responses
          57 views
          0 likes
          Last Post bltdavid  
          Started by trader3000a, Today, 02:48 PM
          1 response
          15 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by soulfx, 02-06-2023, 10:24 AM
          1 response
          36 views
          0 likes
          Last Post NinjaTrader_Erick  
          Started by sjmuk1, Today, 01:46 PM
          1 response
          30 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Working...
          X