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

Buy/Sell Volume

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

    Buy/Sell Volume

    I don't know exactly why but the sum of buy & sell volumes is never equal to the entire volume of a bar

    if someone knows what's going wrong with this code....


    #region Variables
    privateint activeBar = -1;
    private System.Collections.ArrayList alBuys = new System.Collections.ArrayList();
    private System.Collections.ArrayList alSells = new System.Collections.ArrayList();
    privatedouble lastPrice = 0.0;
    privateint buys = 0;
    privateint sells = 0;
    #endregion
    protectedoverridevoid Initialize()
    {
    Add(new Plot(new Pen(Color.Lime, 6), PlotStyle.Bar, "Buys"));
    Add(new Plot(new Pen(Color.Red, 6), PlotStyle.Bar, "Sells"));
    Add(new Line(Color.Gold, 0, "Zero Line"));

    CalculateOnBarClose = false;
    DisplayInDataBox = true;
    Overlay = false;
    PriceTypeSupported = false;
    PlotsConfigurable = true;
    }

    protectedoverridevoid OnBarUpdate()
    {
    if (CurrentBar < activeBar)
    {
    Values[0].Set((double)alBuys[CurrentBar]);
    Values[1].Set(-(double)alSells[CurrentBar]);
    return;
    }
    elseif (CurrentBar != activeBar)
    {
    alBuys.Insert(Math.Max(activeBar, 0), Historical ? 0 : buys);
    alSells.Insert(Math.Max(activeBar, 0), Historical ? 0 : sells);
    buys = 0;
    sells = 0;
    activeBar = CurrentBar;
    }
    else
    {
    Values[0].Set(buys);
    Values[1].Set(-sells);
    }
    }

    protectedoverridevoid OnMarketData(MarketDataEventArgs e)
    {
    if (e.MarketDataType == MarketDataType.Last)
    {
    lastPrice = e.Price;

    // Buys : >= Ask
    if (lastPrice >= e.MarketData.Ask.Price)
    {buys += e.Volume;}
    // Sells : <= Bid
    elseif (lastPrice <= e.MarketData.Bid.Price)
    {sells += e.Volume;}
    }
    }
    }
    }
    Last edited by jerome; 12-29-2009, 08:26 AM.

    #2
    Jerome, when faced with a situation in which something is not working as you expect it to, it is best to start with a clean slate where everything works fine, then adding complexity (and functionality) one layer at a time.

    You can also use Print() statements to print out variable states, which helps verify how everything works.
    AustinNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by elirion, Today, 01:36 AM
    0 responses
    0 views
    0 likes
    Last Post elirion
    by elirion
     
    Started by gentlebenthebear, Today, 01:30 AM
    0 responses
    2 views
    0 likes
    Last Post gentlebenthebear  
    Started by samish18, Yesterday, 08:31 AM
    2 responses
    9 views
    0 likes
    Last Post elirion
    by elirion
     
    Started by Mestor, 03-10-2023, 01:50 AM
    16 responses
    389 views
    0 likes
    Last Post z.franck  
    Started by rtwave, 04-12-2024, 09:30 AM
    4 responses
    31 views
    0 likes
    Last Post rtwave
    by rtwave
     
    Working...
    X