Contents
Indicator Reference
Mid Price
Create Manual Indicators
You can manually create a MidPrice
indicator, so it doesn’t automatically update. Manual indicators let you update their values with any data you choose. The following reference table describes the MidPrice
constructor.
MidPrice()1/2
MidPrice QuantConnect.Indicators.MidPrice (string
name,int
period )
Initializes a new instance of the MidPrice
class using the specified name and period.
MidPrice()2/2
MidPrice QuantConnect.Indicators.MidPrice (
int
period
)
Initializes a new instance of the MidPrice
class using the specified period.
Update Manual Indicators
You can update the indicator automatically or manually.
Automatic Update
To register a manual indicator for automatic updates with the security data, call the RegisterIndicator
method.
private MidPrice _midprice; // In Initialize() _midprice = new MidPrice(name, period); _midprice.Updated += IndicatorUpdateMethod; RegisterIndicator(symbol, _midprice, Resolution.Daily); // In IndicatorUpdateMethod() if (_midprice.IsReady) { var indicatorValue = _midprice.Current.Value; }
# In Initialize() self.midprice = MidPrice(name, period) self.midprice.Updated += self.IndicatorUpdateMethod self.RegisterIndicator(symbol, self.midprice, Resolution.Daily) # In IndicatorUpdateMethod() if self.midprice.IsReady: indicator_value = self.midprice.Current.Value
To customize the data that automatically updates the indicator, see Custom Indicator Periods and Custom Indicator Values.
Manual Update
Updating your indicator manually enables you to control when the indicator is updated and what data you use to update it. To manually update the indicator, call the Update
method with a TradeBar
, QuoteBar
, or an IndicatorDataPoint
. The indicator will only be ready after you prime it with enough data.
private MidPrice _midprice; private Symbol symbol; // In Initialize() _midprice = new MidPrice(period); symbol = AddEquity("SPY").Symbol; // In OnData() if (data.QuoteBars.ContainsKey(_symbol)) { _midprice.Update(data.QuoteBars[symbol]); } if (_midprice.IsReady) { var indicatorValue = _midprice.Current.Value; }
# In Initialize() self.midprice = MidPrice(period) self.symbol = self.AddEquity("SPY").Symbol # In OnData() if data.QuoteBars.ContainsKey(self.symbol): self.midprice.Update(data.QuoteBars[self.symbol]) if self.midprice.IsReady: indicator_value = self.midprice.Current.Value
Create Automatic Indicators
The MIDPRICE method creates an MidPrice indicator, sets up a consolidator to update the indicator, and then returns the indicator so you can use it in your algorithm.
The following reference table describes the MIDPRICE
method:
MIDPRICE()1/1
MidPrice QuantConnect.Algorithm.QCAlgorithm.MIDPRICE (Symbol
symbol,Int32
period,*Nullable<Resolution>
resolution,*Func<IBaseData, IBaseDataBar>
selector )
Creates a new MidPrice indicator.
If you don't provide a resolution, it defauls to the security resolution. If you provide a resolution, it must be greater than or equal to the resolution of the security. For instance, if you subscribe to hourly data for a security, you should update its indicator with data that spans 1 hour or longer.
For more information about the selector argument, see Alternative Price Fields.
Get Indicator Values
To get the value of the indicator, use its Current.Value
attribute.
private MidPrice _midprice; // In Initialize() var symbol = AddEquity("SPY").Symbol; _midprice = MIDPRICE(symbol, period); // In OnData() if (_midprice.IsReady) { var current = _midprice.Current.Value; }
# In Initialize() symbol = self.AddEquity("SPY").Symbol self.midprice = self.MIDPRICE(symbol, period) # In OnData() if self.midprice.IsReady: current = self.midprice.Current.Value
Visualization
To plot indicator values, in the OnData
event handler, call the Plot
method.
private MidPrice _midprice; // In Initialize() var symbol = AddEquity("SPY").Symbol; _midprice = MIDPRICE(symbol, period); // In OnData() if (_midprice.IsReady) { Plot("My Indicators", "midprice", _midprice.Current); }
# In Initialize() symbol = self.AddEquity("SPY").Symbol self.midprice = self.MIDPRICE(symbol, period) # In OnData() if self.midprice.IsReady: self.Plot("My Indicators", "midprice", self.midprice.Current)
For more information about plotting indicators, see Plotting Indicators.