1. Home
  2. Docs
  3. Knowledge Base
  4. MT University
  5. Initializing Algorithms

Initializing Algorithms

The Initialize method is called to set up your strategy. Here you can request data, define internal indicators and their warm up periods, as well as, prepare trade bar history for external indicators. The Initialize method is called just once at the start of your algorithm.

Launcher Options

The menu selections you made in the Launcher panel are exposed to your algorithm during initialization:

  • Exchange
  • Base cryptocurrency
  • Trading pair
  • Start and end dates of candle data
  • Starting cash (for paper trade)

This data is stored in an internal Engine object called the MachinaTrader Object (MTO) that is used to initialize your backtest. You can access its properties and values as follows:


Selecting Asset Data

Your algorithm will run against market data for the trading pair you selected in the panel’s Quote drop-down list. This trading pair (or symbol) is stored in a list in MTO.Symbols.

In the future, MachinaTrader will allow traders to specify several trading pairs (symbols) to be backtested by a single strategy algorithm. Each symbol will be included in the list MTO.Symbols which can be iterated, allowing each symbol to be operated on or handled in turn. Then, and now, whether your algorithm trades one or several symbols, you should access your symbols via a loop:

					public override void Initialize()
    foreach (var symbol in MTO.Symbols)
        Log($"This symbol is {symbol}");

MachinaTrader supports international trading across multiple timezones, exchanges and markets. The symbol.MarketSymbol property is used to distinguish between the same currency pairs on different exchanges (e.g. Binance and BitPanda both offer BTCUSDT).

Setting Timeframe

The candle timeframe you select from the Launcher panel is stored in MTO.PeriodSeconds. As the name implies this is the duration of the candle timeframe in seconds.

If there is a gap in the candle data (e.g. because there are no trades), by default the data is still pumped into your strategy on each time step. This behavior is called "fillForward" and defaults to true. You can disable this by setting fillForward to false.

The MTO.PeriodSeconds value can be passed to many helper methods, as well as, TA-lib indicators. For example, if you intend to use TA-lib indicators then you must use the SetUpHistory() method to prepare their lookback period (with 20 bars in this example):

					public override void Initialize()
    foreach (var symbol in MTO.Symbols)
        Log($"This symbol is {symbol}");
        SetupHistory(symbol, MTO.PeriodSeconds, 20);

[mkb-info] Some Engine methods (including internal indicators) require the value stored in MTO.Resolution. For Crypto assets MachinaTrader provides Minute, Hourly, and Daily resolution. This topic is discussed in the page "Internal Indicators".[/mkb-info]

Setting Dates

Backtesting uses your Period selection (from the panel calendar) to set MTO.StartDate and MTO.EndDate for the backtest time range. These dates (and times) are in UTC.

There are some reasonable restrictions on the amount of candle data that you can request. The maximum period for each timeframe is listed below:

[mkb-info]Trade bars, indicators and any other plot points you might create should not exceed 10,000 data points. If your algorithm's data points exceed this value then the Engine will stop plotting and visually inform you that the data maximum has been exceeded.[/mkb-info]

Initial Capital

In backtests your starting capital is stored in MTO.InitialCapital. In live strategy trading this value is ignored and your brokerage cash balance is used instead. In paper trading it is recommended to set the cash to a fictional 1,000 USD or 1 BTC (or other cryptocurrency) depending on which Base currency you selected.

Setting Indicators

Internal indicators should be created and warmed up in the Initialize method for most applications.

A basic implementation of RSI with a lookback period of 14 bars looks like this in the Initialize method:

					public override void Initialize()
    foreach (var symbol in MTO.Symbols)
        _rsi = RSI(symbol, 14,  MovingAverageType.Simple, MTO.Resolution);
    SetWarmUp(14, MTO.Resolution);

For more details, please see the Indicators section.

Setting Warm Up Period

Often algorithms need some historical data to prime technical indicators, or populate historical data arrays. Using the SetWarmUp(TimeSpan period) or SetWarmUp(int barCount) methods you can specify a warm-up period for your algorithm which pumps in data from before your backtest's start date. During the warm-up period you cannot place a trade.

Refer to the code example in the previous section "Setting Indicators" and substitute the methods below to set your algorithm's warm-up period:

					SetWarmUp(200); //Warm up 200 bars for all subscribed data.
SetWarmUp(TimeSpan.FromDays(7)); //Warm up 7 days of data.

Algorithms can use the IsWarmingUp property to determine if the warm-up period has completed.

Was this article helpful to you? Yes No

How can we help?

Close Menu