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

Calculate Buy volume and sell volume

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

    Calculate Buy volume and sell volume

    Hi, I'm building a cummulative delta indicator and I'm trying to get the delta value for each candle. In order to calculate the delta value. the idea is to make (buy volume - sell volume). I've tryied with BuySellVolume indicator but the problem is that it doen't supports tickreplay data.

    How can I make in order to calculate the buy and sell volume for each candle in order to get the delta value?

    Thanks in advance

    #2
    Hello speedytrade02,

    Thanks for your notes.

    The Order Flow Cumulative Delta indicator could be used in a custom NinjaScript to get the DeltaClose value of a bar.

    See this help guide page for more information about accessing Order Flow Cumulative Delta in a custom NinjaScript and sample code: https://ninjatrader.com/support/help...ive_delta2.htm

    Otherwise, you could use the BuySellVolume indicator to get the number of buys and sells in a custom NinjaScript. The BuySellVolume indicator does work when applied to a chart with Tick Replay enabled. Note that the Buys plot of the BuySellVolume indicator is the number of Buys + Sells that occur. To get only the Buys, you could subtract the Sells plot from the Buys plot.

    Then, you could subtract the calculated Buys from the Sells plot of the indicator to calculate that value.

    For example:

    //class level variable
    private BuySellVolume BuySellVol;

    //OnStateChange()
    else if (State == State.Configure)
    {
    BuySellVol = BuySellVolume();
    }​

    //OnBarUpdate()
    Print(string.Format("Buy volume is {0} and Sell volume is {1}. ", (BuySellVol.Buys[0] - BuySellVol.Sells[0]), BuySellVol.Sells[0]));



    See this help guide page for more information about BuySellVolume(): https://ninjatrader.com/support/help...sellvolume.htm

    You could view the BuySellVolume indicator in a New > NinjaScript Editor window to see how the indicator is programmed to calculate Buys and Sells using OnMarketData(). To view the script, open a New > NinjaScript Editor window, open the Indicators folder, and double-click on the BuySellVolume indicator.

    Note that OnMarketData() could be used with Tick Replay. From the OnMarketData() help guide page:

    By default, this method is not called on historical data (backtest), however it can be called historically by using TickReplay

    If used with TickReplay, please keep in mind Tick Replay ONLY replays the Last market data event, and only stores the best inside bid/ask price at the time of the last trade event. You can think of this as the equivalent of the bid/ask price at the time a trade was reported. As such, historical bid/ask market data events (i..e, bid/ask volume) DO NOT work with Tick Replay. To obtain those values, you need to use a historical bid/ask series separately from TickReplay through OnBarUpdate(). More information can be found under Developing for Tick Replay.


    OnMarketData(): https://ninjatrader.com/support/help...marketdata.htm
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hi Brandon and thanks for all the info.

      I've been trying the OrderFlowCumulativeDelta indicator as you said, and checked the link you've provided.

      I updated my code

      HTML Code:
       else if (State == State.Configure)  {  
       AddDataSeries(Data.BarsPeriodType.Tick, 1);  
      }​
      
              protected override void OnBarUpdate()
              {
                  if (Bars == null || CurrentBar == 0)
      
                      return;
      
                  if (Bars.IsLastBarOfSession)
                  {
                      cumulativeDelta = 0.0;
                      previousDelta = 0.0;
                  }
      
                  double deltaDifference = OrderFlowCumulativeDelta(BarsArray[0], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Session, 0).DeltaClose[0];
                  cumulativeDelta += deltaDifference;
      
                  Value[0] = cumulativeDelta;
              }​
      but I'm getting crazy results.

      Click image for larger version

Name:	image.png
Views:	358
Size:	21.1 KB
ID:	1267966

      How can I get the correct Cummulative Delta?

      Comment


        #4
        Hello speedytrade02,

        Thanks for your notes.

        You should make sure that you are doing a CurrentBars check for both the primary series the script is running on and the added secondary 1-Tick series in the script.

        CurrentBars: https://ninjatrader.com/support/help...urrentbars.htm
        Make sure you have enough bars: https://ninjatrader.com/support/help...nough_bars.htm

        In OnBarUpdate() you should be checking if BarsInProgress == 0 and then access the OrderFlowCumulativeDelta() values within that BarsInProgress condition.

        Also, in your script you will need to call .Update() on OrderFlowCumulativeDelta() in BarsInProgress 1 to update the secondary series of the hosted indicator to make sure the values you get in BarsInProgress == 0 are in sync.

        An example of this could be found in the sample code on the OrderFlowCumulativeDelta() help guide page.

        OrderFlowCumulativeDelta(): https://ninjatrader.com/support/help...ive_delta2.htm



        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Hi Brandon and Thanks for the infor, I think that I almost have it.

          Now I've updated the code this way:

          HTML Code:
          private double cumulativeDelta = 0.0;
          private double previousDelta = 0.0;
          private int previousSession = -1;
          private OrderFlowCumulativeDelta cumDelta;
          protected override void OnStateChange()
          {
              if (State == State.SetDefaults)
              {
                  Description                                 = @"Enter the description for your new custom Indicator here.";
                  Name                                        = "CummulativeDelta";
                  Calculate                                   = Calculate.OnBarClose;
                  BarsRequiredToPlot                          = 1;
                  IsOverlay                                   = false;
                  DisplayInDataBox                            = true;
                  DrawOnPricePanel                            = true;
                  DrawHorizontalGridLines                     = true;
                  DrawVerticalGridLines                       = true;
                  PaintPriceMarkers                           = true;
                  ScaleJustification                          = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                  IsSuspendedWhileInactive                    = true;        
              }
              else if (State == State.Configure)
              {          
                  AddDataSeries(Data.BarsPeriodType.Tick, 1);
              }
          
              else if (State == State.DataLoaded)
              {
                      // Instantiate the indicator
                      cumDelta = OrderFlowCumulativeDelta(CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Session, 0);
              }
          }
          protected override void OnBarUpdate()
          {
              if (CurrentBars[0] < BarsRequiredToPlot || CurrentBars[1] < BarsRequiredToPlot)
                  return;
              if (BarsInProgress == 1)
              {
                      // We have to update the secondary series of the hosted indicator to make sure the values we get in BarsInProgress == 0 are in sync
                      cumDelta.Update(cumDelta.BarsArray[1].Count - 1, 1);
              }
              double currentDelta = cumDelta.DeltaClose[0];
              double deltaDifference = currentDelta - previousDelta;
              cumulativeDelta += deltaDifference;        
              previousDelta = currentDelta;
          
              Value[0] = cumDelta.DeltaClose[0];
          }​

          The idea of the indicator is to get the CummulativeDeltaChange

          Delta Change = Current candle Delta - (previous candle Delta)
          Cumulative Delta Change = Sum of all Delta Change from the begining of the session​
          With the code that I currently have, I'm only getting the cummulative delta being printed on the indicator. The idea would be to print the CummulativeDeltaChange on the indicator.

          Note: on the code I'v thought on defining a variable where i save the current delta in order to substract it after as previous delta (this way I think that I'd have the delta change)

          What can be missing in the code or what may be wrong?

          Thanks again

          Comment


            #6
            Hello speedytrade02,

            Thanks for your notes.

            If you want to subtract the current bar's DeltaClose value from the previous bar's DeltaClose value you could certainly do that by passing in BarsAgo 0 to get the current bar's DeltaClose value and passing in BarsAgo 1 to get the previous bar's DeltaClose value.

            cumDelta.DeltaClose[0] would access the current bar's DeltaClose value from the OrderFlowCumulativeDelta indicator. cumDelta.DeltaClose[1] would access the previous bar's DeltaClose value from the OrderFlowCumulativeDelta indicator.

            For example, subtracting the previous bar DeltaClose from the current bar DeltaClose might look something like this:

            //OnBarUpdate()
            if (BarsInProgress == 0)
            {
            double deltaDifference = cumDelta.DeltaClose[0] - cumDelta.DeltaClose[1];
            }
            Brandon H.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by iceman2018, Today, 05:07 PM
            0 responses
            4 views
            0 likes
            Last Post iceman2018  
            Started by rhyminkevin, Today, 04:58 PM
            1 response
            27 views
            0 likes
            Last Post Anfedport  
            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  
            Working...
            X