Quantower
Quantower Website
  • Welcome to Quantower Help
  • 🚀Getting Started
    • What's new
    • Installation
    • First start
    • Platform update
    • Quantower Beta
    • Quantower Account
    • Quantower Licenses
    • ⚖️License Comparison
    • Backup & restore manager
    • Reset settings to default
    • Payment FAQ
    • “Coinpayments” payment
    • Binance fresh account
    • Referral Program
    • You have found a bug. What’s next?
  • ⚙️General Settings
    • Quantower Main Toolbar
    • Workspaces
    • Single Panel
    • Link panels
    • Binds
    • Group of panels
    • Templates
    • Set as Default
    • Symbols lookup
    • Table management
    • Alerts
    • General settings
    • Custom hotkeys
    • Setup Actions & Advanced filters
    • 📢Notifications center
  • 🔗Connections
    • Connections manager
    • Connection to Binance Futures
      • Errors with Binance connection
    • Connection to CQG (AMP Futures)
      • Errors with CQG
    • Connection to OANDA
    • Connection to FXCM
    • Connection to cTrader
      • How to connect to FxPro via Quantower
      • How to connect to Pepperstone via Quantower
      • How to connect to IC Markets via Quantower
    • Connection to Rithmic
      • Rithmic Issues
    • Connection to Topstep
    • Connection to Interactive Brokers
      • Errors with Interactive Brokers
    • Connection to Bybit
    • Connection to OKEx
    • Connection to MetaStock
    • Connection to IQFeed
  • 💡Analytics Panels
    • Chart
      • Chart Overview
      • Chart Types
        • Tick Bars
        • Time aggregation
        • Renko
        • Heiken Ashi
        • Kagi
        • Points & Figures
        • Range bars
        • Line break
        • Volume Bars
        • Reversal Bars
      • Chart Settings
        • View settings
        • Data Style
        • Time Scale
        • Price Scale
        • Visual Trading
          • Positions
        • Order Entry
        • Quick Ruler
        • Info Window
        • Volume Bars
        • Hotkeys
      • Chart overlays
      • Technical indicators
        • Channels
          • Range Marker
          • Donchian Channel
          • High Low Indicator
          • Round Numbers
          • Highest High
          • Lowest Low
          • Bollinger Bands
          • Bollinger Bands Flat
          • Price Channel
          • Fair Value Gap (FVG)
          • Keltner Channel
          • Moving Average Envelope
        • Moving averages
          • Demand Index
          • Exponential Moving Average
          • FYL Indicator
          • Linearly Weighted Moving Average
          • McGinley Dynamic Indicator
          • Modified Moving Average Indicator
          • Pivot Point Moving Average Indicator
          • Regression Line Indicator
          • Simple Moving Average Indicator
          • Smoothed Moving Average Indicator
          • Guppy Multiple Moving Average Indicator
          • Trend Breakout System Indicator
          • Triple Exponential Moving Average Indicator
        • Oscillators
          • Delta Divergence Reversal
          • Aroon Indicator
          • Moving Average Convergence/Divergence
          • Awesome Oscillator
          • Accelerator Oscillator
          • %R Larry Williams
          • Momentum
          • Rate of Change
          • Relative Strength Index (RSI) Indicator
          • Relative Spread Strength (RSS)
          • Balance of Power (BOP)
          • Commodity Channel Index
        • Trend
          • Bionic Candle
          • Average Directional Movement Index (ADX) Indicator
          • Ichimoku Cloud Indicator
          • Directional Movement Index (DMI) Indicator
          • ZigZag
        • Volatility
          • Average True Range
          • Standard deviation
        • Volume
          • COT High/Low
          • Depth of Bid / Ask
          • Delta Flow
          • Delta Rotation
          • Level2 indicator
          • Abnormal Volume
          • Abnormal Trades
          • Volume Impulse
      • Drawing tools
      • Volume Analysis Tools | Volume Profiles | Footprint chart | VWAP
        • Cluster chart
        • Volume profiles
        • Time statistics
        • Time histogram
        • Historical Time & Sales
      • Power Trades
      • VWAP | Volume Weighted Average Price
      • Anchored VWAP
    • Watchlist
    • Time & Sales
    • Price Statistic
    • DOM Surface
    • Option Analytics
    • TPO Profile Chart
  • 💵Trading Panels
    • Chart Trading
    • Crypto Order Entry
    • Multiple Order Entry
    • Order Entry
      • Order entry for Bybit
      • Order Entry for CQG
      • Order Types
      • Order placing strategies
        • Local SL/TP
    • DOM Trader
      • DOM Trader Settings
        • View Settings
        • DOM Trader Columns
        • Order Entry
        • VWAP Settings
        • Positions Bar
        • Hotkeys
      • DOM Trader Columns
      • How to set up Dom for scalping
    • Copy Trading
    • Market depth
    • Trading simulator
    • Market Replay
    • FX Cell
    • Backtest & Optimize
    • Strategies manager
  • 💼Portfolio Panels
    • Positions
    • Working Orders
    • Trades
    • Orders History
    • Synthetic Symbols
    • Historical Symbols
  • 📊Information Panels
    • Account performance
    • Account info
    • Crypto balances
    • Symbol Info
    • Currencies Exposure
    • Event Log
    • RSS (News panel)
    • Reports
  • 📌Miscellaneous
    • Futures Rollover
    • History Exporter
    • Symbol Mapping Manager
    • Sessions manager
    • Live Support
    • Market Heat map
    • Stat matrix
    • Exchange times
    • Quote Board
    • Browser
    • Excel and RTD function
      • Changing RTD Throttle Interval in Excel
    • Quantower Telegram Bot
    • 🎨Themes editor
  • 🤖Quantower Algo
    • Introduction
    • Install for Visual Studio 2022
    • Strategies manager
    • Backtest & Optimize
    • Debugging in VS 2022
    • Simple Indicator
    • Simple strategy
    • Input Parameters
    • Built-In indicators access
    • Custom indicators access
    • Level2 data
    • Access Volume analysis data from indicators
    • Indicator with custom painting (GDI)
    • Access Chart from indicator
    • Using markers with indicators
    • Using Clouds in Indicator
    • Adding a custom indicator to Watchlist
    • Downloading history
    • Access to trading portfolio
    • Trading operations
    • Example: Simple Moving Average
    • Access to crypto account and balances
    • Access to advanced aggregations
    • Access to symbol/account additional fields
    • Strategy runner (deprecated)
  • 💫Customization
    • Localization
  • ⁉️FAQ
    • General Errors
Powered by GitBook
On this page
  1. Quantower Algo

Downloading history

Using historical data for indicator or strategy calculations

Most of the indicators require history for their calculation only from the main symbol or symbol from the chart where they were added. But sometimes it may need history data from another time frame or another symbol. For example, if you need to calculate a correlation between symbols, or if you want to compare the results of Moving Average calculations on different time frames. In this topic, we will show you how to do it using Quantower API and its possibilities.

The start point for receiving history is symbol object and method GetHistory. The most simple way is to specify the required Period, history type and date range:

// Download 15 min bars for august 2018
IHistoricalData historicalData = Symbol.GetHistory(Period.MIN15, HistoryType.Bid, new DateTime(2018, 8, 1, 0, 0, 0, DateTimeKind.Utc), new DateTime(2018, 8, 31, 0, 0, 0, DateTimeKind.Utc));

If you specify exactly left and right border — you will receive the required history for this range. But if you want download history from some time in the past till now and receive real-time data, you can skip right border parameter definition:

// Download 15 min bars for last day with real time data
IHistoricalData historicalData = Symbol.GetHistory(Period.MIN15, HistoryType.Bid, DateTime.UtcNow.AddDays(-1));

The main object that represents history is IHistoricalData interface. It contains a collection of bars or ticks and main information about downloaded history: symbol, history type, a total count of items, period, etc. You can access particular item from the collection and receive IHistoryItem object, which provides you information about bar or tick: prices, time, volume and other. We use different classes for bars, ticks and lasts history presentation, so to get access to full data you need to convert your object to appropriate class:

// Downloading bars history
IHistoricalData historicalData = Symbol.GetHistory(Period.MIN15, HistoryType.Bid, DateTime.UtcNow.AddDays(-1));

// An example of access data from the bar:
double openPrice = ((HistoryItemBar)historicalData[0]).Open;
double closePrice = ((HistoryItemBar)historicalData[0]).Close;
double volume = ((HistoryItemBar)historicalData[0]).Volume;
DateTime leftBorderOfBar = ((HistoryItemBar)historicalData[0]).TimeLeft;

In case of tick history:

// Downloading ticks history
IHistoricalData historicalData = Symbol.GetHistory(Period.TICK1, HistoryType.Bid, DateTime.UtcNow.AddDays(-1));

// An example of acces  data from the tick:
double bidPrice = ((HistoryItemTick)historicalData[0]).Bid;
double askPrice = ((HistoryItemTick)historicalData[0]).Ask;

If you need to download history from a current symbol of indicator, you can use its property Symbol. If you need history from another symbol, that should be specified in settings - you can use InputParameter attribute and our article describing this.

It is a time to create some practical example using knowledge from the current topic. Let's assume we need an indicator that downloads 5min,15 min and 30 min history for the main symbol and for an additional one, that can be specified by the user. Then calculates simple moving average on this history and displays results on the chart.

At first, we need to specify variables for required indicators and Symbol variable and mark it with InputParameter:

/// <summary>
/// SMA indicators for different timeframes and symbols
/// </summary>
Indicator sma5MinCurrent;
Indicator sma15MinMainCurrent;
Indicator sma30MinMainCurrent;
Indicator sma5MinAdditional;
Indicator sma15MinAdditional;
Indicator sma30MinAdditional;

[InputParameter()]
Symbol AdditionalSymbol;

In OnInit of the indicator method we will download all required history and add indicators:

protected override void OnInit()
{   
    // Download history for current symbol for different timeframes and apply SMA indicator

    // 5 min
    IHistoricalData data5MinCurrent = this.Symbol.GetHistory(Period.MIN5, HistoryType.Bid, DateTime.UtcNow.AddDays(-1));            
    data5MinCurrent.AddIndicator(sma5MinCurrent = Core.Indicators.BuiltIn.SMA(10, PriceType.Close));

    // 15 min
    IHistoricalData data15MinCurrent = this.Symbol.GetHistory(Period.MIN15, HistoryType.Bid, DateTime.UtcNow.AddDays(-1));            
    data15MinCurrent.AddIndicator(sma15MinMainCurrent = Core.Indicators.BuiltIn.SMA(10, PriceType.Close));

    // 30 min
    IHistoricalData data30MinCurrent = this.Symbol.GetHistory(Period.MIN30, HistoryType.Bid, DateTime.UtcNow.AddDays(-1));            
    data30MinCurrent.AddIndicator(sma30MinMainCurrent = Core.Indicators.BuiltIn.SMA(10, PriceType.Close));
            
    // Download history for additional symbol (if specified) for different timeframes and apply SMA indicator

    if (AdditionalSymbol != null)
    {
        // 5 min
        IHistoricalData data5MinAdditional = this.AdditionalSymbol.GetHistory(Period.MIN5, HistoryType.Bid, DateTime.UtcNow.AddDays(-1));
        data5MinAdditional.AddIndicator(sma5MinAdditional = Core.Indicators.BuiltIn.SMA(10, PriceType.Close));

        // 15 min
        IHistoricalData data15MinAdditional = this.AdditionalSymbol.GetHistory(Period.MIN15, HistoryType.Bid, DateTime.UtcNow.AddDays(-1));
        data15MinAdditional.AddIndicator(sma15MinAdditional = Core.Indicators.BuiltIn.SMA(10, PriceType.Close));

        // 30 min
        IHistoricalData data30MinAdditional = this.AdditionalSymbol.GetHistory(Period.MIN30, HistoryType.Bid, DateTime.UtcNow.AddDays(-1));
        data30MinAdditional.AddIndicator(sma30MinAdditional = Core.Indicators.BuiltIn.SMA(10, PriceType.Close));
    }
}

In OnPaint method we simply display result using GDI+:

public override void OnPaintChart(PaintChartEventArgs args)
{
    Graphics gr = Graphics.FromHdc(args.Hdc);
    Font f = new Font("Arial", 10);

    // Draw results for main symbol            
    gr.DrawString(this.Symbol.Name, new Font(f, FontStyle.Underline), Brushes.LightCoral, 10, 35);
    gr.DrawString("SMA  5Min: " + this.Symbol.FormatPrice(sma5MinCurrent.GetValue()), f, Brushes.LightCoral, 10, 55);
    gr.DrawString("SMA 15Min: " + this.Symbol.FormatPrice(sma15MinMainCurrent.GetValue()), f, Brushes.LightCoral, 10, 75);
    gr.DrawString("SMA 30Min: " + this.Symbol.FormatPrice(sma30MinMainCurrent.GetValue()), f, Brushes.LightCoral, 10, 95);

    // Draw results for additional symbol (if specified)
    if (this.AdditionalSymbol != null)
    {
        gr.DrawString(this.AdditionalSymbol.Name, new Font(f, FontStyle.Underline), Brushes.LightGreen, 160, 35);
        gr.DrawString("SMA  5Min: " + this.AdditionalSymbol.FormatPrice(sma5MinAdditional.GetValue()), f, Brushes.LightGreen, 160, 55);
        gr.DrawString("SMA 15Min: " + this.AdditionalSymbol.FormatPrice(sma15MinAdditional.GetValue()), f, Brushes.LightGreen, 160, 75);
        gr.DrawString("SMA 30Min: " + this.AdditionalSymbol.FormatPrice(sma30MinAdditional.GetValue()), f, Brushes.LightGreen, 160, 95);
    }
}

And the results on the chart:

In this topic, we showed you the trivial example of downloading and using history. But if needed, you can use it any ways: as a source for indicators, for comparison, for displaying on the chart as overlays, for exporting data.

Last updated 6 years ago

🤖
Result of SMA indicators calculations for 2 symbol and different time frames