Add()
Previous Topic  Next Topic 

Definition
This is method provides two functions:


1. Adds a Bars object to a strategy for developing a multi-time frame or multi-instrument strategy. When running a strategy, the NinjaTrader UI will prompt you to select the primary instrument and bar interval to run on. This primary Bars object will carry a BarsInProgress index of 0. In a multi-time frame and multi-instrument strategy, supplementary Bars objects are added via this method in the Initialize() method and given an incremented BarsInProgress index value. See additional information on running multi-bars strategies.


2. Adds an indicator to the strategy only for the purpose of displaying it on a chart.


Note: The Add() method should only be called within the strategy Initialize() method.


Syntax
Add(Indicator indicator)


The following syntax will add another Bars object to the strategy for the primary instrument of the strategy.
Add(PeriodType periodType, int period)


The following syntax allows you to add another Bars object for a different instrument to the strategy.
Add(string instrument, PeriodType periodType, int period)


NOTE: You can optionally add the exchange name as a suffix to the symbol name. This is only advised if the instrument has multiple possible exchanges that it can trade on and it is configured within the Instrument Manager. For example: Add("MSFT Arca", PeriodType.Minute, 5).



Parameters

indicator

An indicator object

instrument

An instrument name such as "MSFT"

periodType

The period type of the bar such as:

PeriodType.Day

PeriodType.Minute

PeriodType.Tick

PeriodType.Volume

period

The period interval such as "3" for 3 minute bars


Examples

protected override void Initialize()
{
    // Add a 5 minute Bars object - BarsInProgress index = 1
    Add(PeriodType.Minute, 5);

    // Add a 100 tick Bars object for the ES 12-06 contract - BarsInProgress index = 2
    Add("ES 12-06", PeriodType.Tick, 100);
                                    
    // Charts a 20 period simple moving average to the chart
    Add(SMA(20)); 
}

protected override void OnBarUpdate()
{
    // Ignore bar update events for the supplementary Bars object added above
    if (BarsInProgress == 1 || BarsInProgress == 2)
        return;

    // Go long if we have three up bars on all bars objects
    if (Close[0] > Open[0] && Closes[1][0] > Opens[1][0] && Closes[2][0] > Opens[2][0])
        EnterLong();
}