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 GussJ, 03-04-2020, 03:11 PM
    16 responses
    3,281 views
    0 likes
    Last Post Leafcutter  
    Started by WHICKED, Today, 12:45 PM
    2 responses
    19 views
    0 likes
    Last Post WHICKED
    by WHICKED
     
    Started by Tim-c, Today, 02:10 PM
    1 response
    9 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by Taddypole, Today, 02:47 PM
    0 responses
    5 views
    0 likes
    Last Post Taddypole  
    Started by chbruno, 04-24-2024, 04:10 PM
    4 responses
    53 views
    0 likes
    Last Post chbruno
    by chbruno
     
    Working...
    X