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

Determining Median of Bid / Ask Volume over multiple bars

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

    Determining Median of Bid / Ask Volume over multiple bars

    Hi there,

    Working with code referring to Volumetric bars, I'd like to determine the median bid(!) volume over the last 3 bars.
    Is there an easy way to do this?

    ...I was thinking somehow via concatenating barsType.Volumes[CurrentBar] + barsType.Volumes[priorBar] +... into a new ArrayList (?) and then running GetMedian over the ArrayList?

    Any pointers would be appreciated.

    Kind regards
    Frank

    #2
    Hello Frank,

    The issue with using GetMedian() would be that this requires a Series<double>, and Volumes is unfortunately an array of VolumetricData objects.



    I think you would have to custom calculate this with custom logic.
    The formula is (High + Low) / 2.

    So a loop from current bar minus three to the current bar to determine the highest bid volume high in last 3 bars and lowest bid volume in the last 3 bars. Then the saved highest value plus the saved lowest value then divided by two.

    Below is a public link to a 3rd party educational site on for loops.
    Iterate over numbers with for. Increment or decrement an index int from a start to an end value.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,

      Thanks, just briefly - your comment describes the average, I am actually after the median.
      I solved this as follows in the end - for others reference:

      Code:
      List<double> AskVols = new List<double>();
      List<double> BidVols = new List<double>();
      
      private double medianVolAsk;
      private double medianVolBid;
      
      protected override void OnBarUpdate()
      {
      for (int myBar = CurrentBar - 1; myBar >= CurrentBar - MedianLookbackBars; myBar--){[INDENT]for(double Lvl = Low[CurrentBar - myBar]; Lvl <= High[CurrentBar - myBar]; Lvl += TickSize){[/INDENT][INDENT=2]AskVols.Add(barsType.Volumes[myBar].GetAskVolumeForPrice(Lvl));[/INDENT][INDENT=2]BidVols.Add(barsType.Volumes[myBar].GetBidVolumeForPrice(Lvl));[/INDENT][INDENT]}[/INDENT]
       }
      
      medianVolAsk = Median(AskVols);
      medianVolBid = Median(BidVols);
      }
      double Median(List<double> xs) {[INDENT]var ys = xs.OrderBy(x => x).ToList();
      double mid = (ys.Count - 1) / 2.0;
      return (ys[(int)(mid)] + ys[(int)(mid + 0.5)]) / 2;[/INDENT]
       }

      Comment


        #4
        Hello Frank,

        From the help guide:

        1. This method should NOT be confused with Median prices defined as (High + Low) / 2. This method returns the statistical median of a series.

        The median is average of the highest high and lowest low. In other words its the middle point between the high and the low.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by junkone, Today, 11:37 AM
        1 response
        9 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by frankthearm, Yesterday, 09:08 AM
        11 responses
        41 views
        0 likes
        Last Post frankthearm  
        Started by quantismo, 04-17-2024, 05:13 PM
        5 responses
        35 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by proptrade13, Today, 11:06 AM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_Clayton  
        Started by love2code2trade, 04-17-2024, 01:45 PM
        4 responses
        35 views
        0 likes
        Last Post love2code2trade  
        Working...
        X