View Full Version : Bid/Ask/Last Pressure Indicator
TradingDreamer
09-14-2009, 12:14 PM
I am trying to write a indicator to calculate if the trade (last) happened at the Bid price or the ask price. I am new to Ninja Trader, but I have successfully created some new indicators.
1. There is a BuySellPressure indicator, but I can't get it to work even when connected to a live data feed (IQ Feed) so I am programming my own. I currently am setting up 2 indicators. There is code for one below.
2. I can see my indicator on the chart, but it is incorrect. When the bar competes, my bid equals total volume and my ask volume equals total volume. Total volume should = ask vol + bid vol for the bar.
3. I am using the simulator account
Here is the code for just the bid volume indicator. I am using calculatonbarclose = false;
private DataSeries dsBar;
dsBarBuyPres = new DataSeries(this);
if (GetCurrentAsk() == Close[0])
{
dsBarBuyPres[0] = VOL()[0] - dsBarBuyPres[0];
BarBuy.Set(dsBarBuyPres[0]);
}
(a) Why does it seem the ask price is always = to the Close?
(b) Can I get the bid and ask price when running using the simulator?
(c) Can I get the bid and ask price when running using an external data feed connection (a program that feeds in the ticks)?
(d) Can I get the bid and ask price using historical data?
I would really like a BuySellPressure indicator that works in some soft of testing mode. Any help will be appreciated.
NinjaTrader_Josh
09-14-2009, 12:20 PM
TradingDreamer,
You should work from within OnMarketData() and not OnBarUpdate() with the GetCurrentBid/Ask methods.
TradingDreamer,
Try something like this....
---------------------------------------------------------------------
protected override void OnMarketData(MarketDataEventArgs e)
{
double theAsk = 0;
double theBid = 0;
if (e.MarketDataType != MarketDataType.Last) return;
if (e.MarketData.Ask != null) theAsk = e.MarketData.Ask.Price;
if (e.MarketData.Bid != null) theBid = e.MarketData.Bid.Price;
if(theBid <= 0 || theAsk <= 0) return;
if (e.Price == theAsk) { DO SOMETHING }
else if (e.Price == theBid) { DO SOMETHING }
}
}
--------------------------------------------------------------------
RJay
TradingDreamer
09-14-2009, 02:59 PM
Thanks. I was wondering about OnMarketData().
nkhoi
10-25-2009, 06:36 PM
TradingDreamer,
You should work from within OnMarketData() and not OnBarUpdate() with the GetCurrentBid/Ask methods.
are you saying the buysellstrength indicator is wrong because it uses onbarupdate method?
#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Gui.Chart;
#endregion
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
[Description("Enter the description of your new custom indicator here")]
public class BuySellStrength : Indicator
{
#region Variables
// Wizard generated variables
private int myInput0 = 1; // Default setting for MyInput0
// User defined variables (add any user defined variables below)
#endregion
/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Bar, "Plot0"));
Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Bar, "Plot1"));
Add(new Line(Color.FromKnownColor(KnownColor.Black), 0, "Zero"));
CalculateOnBarClose = true;
Overlay = false;
PriceTypeSupported = false;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.
if (CurrentBar < 1)
return;
// bid is buyers
// ask is sellers
int bidvol = GetCurrentBidVolume();
int askvol = GetCurrentAskVolume();
int buysellstrength = askvol - bidvol;
//Print ("buysellstrength is "+buysellstrength+" bidvol = "+bidvol+" askvol = "+askvol);
if( askvol > bidvol )
{
Plot1.Set(buysellstrength); // green
}
else
{
Plot0.Set(buysellstrength); // red
}
//Plot0.Set(Close[0]); //red
//Plot1.Set(Close[0]); //green
}
#region Properties
[Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
[XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
public DataSeries Plot0
{
get { return Values[0]; }
}
[Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
[XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
public DataSeries Plot1
{
get { return Values[1]; }
}
[Description("")]
[Category("Parameters")]
public int MyInput0
{
get { return myInput0; }
set { myInput0 = Math.Max(1, value); }
}
#endregion
}
}
#region NinjaScript generated code. Neither change nor remove.
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
public partial class Indicator : IndicatorBase
{
private BuySellStrength[] cacheBuySellStrength = null;
private static BuySellStrength checkBuySellStrength = new BuySellStrength();
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public BuySellStrength BuySellStrength(int myInput0)
{
return BuySellStrength(Input, myInput0);
}
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public BuySellStrength BuySellStrength(Data.IDataSeries input, int myInput0)
{
checkBuySellStrength.MyInput0 = myInput0;
myInput0 = checkBuySellStrength.MyInput0;
if (cacheBuySellStrength != null)
for (int idx = 0; idx < cacheBuySellStrength.Length; idx++)
if (cacheBuySellStrength[idx].MyInput0 == myInput0 && cacheBuySellStrength[idx].EqualsInput(input))
return cacheBuySellStrength[idx];
BuySellStrength indicator = new BuySellStrength();
indicator.BarsRequired = BarsRequired;
indicator.CalculateOnBarClose = CalculateOnBarClose;
indicator.Input = input;
indicator.MyInput0 = myInput0;
indicator.SetUp();
BuySellStrength[] tmp = new BuySellStrength[cacheBuySellStrength == null ? 1 : cacheBuySellStrength.Length + 1];
if (cacheBuySellStrength != null)
cacheBuySellStrength.CopyTo(tmp, 0);
tmp[tmp.Length - 1] = indicator;
cacheBuySellStrength = tmp;
Indicators.Add(indicator);
return indicator;
}
}
}
// This namespace holds all market analyzer column definitions and is required. Do not change it.
namespace NinjaTrader.MarketAnalyzer
{
public partial class Column : ColumnBase
{
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.BuySellStrength BuySellStrength(int myInput0)
{
return _indicator.BuySellStrength(Input, myInput0);
}
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public Indicator.BuySellStrength BuySellStrength(Data.IDataSeries input, int myInput0)
{
return _indicator.BuySellStrength(input, myInput0);
}
}
}
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
public partial class Strategy : StrategyBase
{
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.BuySellStrength BuySellStrength(int myInput0)
{
return _indicator.BuySellStrength(Input, myInput0);
}
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public Indicator.BuySellStrength BuySellStrength(Data.IDataSeries input, int myInput0)
{
if (InInitialize && input == null)
throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");
return _indicator.BuySellStrength(input, myInput0);
}
}
}
#endregion
NinjaTrader_Josh
10-26-2009, 07:15 AM
nkhoi,
It is not "wrong" per se. You would just have to understand the caveats of GetCurrentBid/Ask like how it is always equal to the close when processing historically.