using System;
using System.Collections.Generic;
using QuantConnect;
using QuantConnect.Algorithm;
using QuantConnect.Data;
using QuantConnect.Data.Consolidators;
using QuantConnect.Data.Market;
using QuantConnect.Indicators;
using QuantConnect.Orders;
using QuantConnect.Securities;
public class BasicTemplateAlgorithm : QCAlgorithm
{
private readonly List<string> Symbols = new List<string>{"USDJPY","AUDCHF"};
private readonly Dictionary<string, SymbolData> Data = new Dictionary<string, SymbolData>();
//Initialize the data and resolution you require for your strategy:
public override void Initialize()
{
//Start and End Date range for the backtest:
SetStartDate(2013, 1, 1);
SetEndDate(2015, 1, 1);
//Cash allocation
SetCash(25000);
SetBrokerageModel(BrokerageName.OandaBrokerage);
//Add as many securities as you like. All the data will be passed into the event handler:
foreach (var symbol in Symbols)
{
AddSecurity(SecurityType.Forex, symbol, Resolution.Daily);
// create symbol data for each symbol and initialize it
Data.Add(symbol, new SymbolData(symbol, this));
}
}
public override void OnData(Slice data)
{
foreach (var symbolData in Data.Values)
{
if(symbolData.EMA_Long.IsReady)
{
//do something with our symbol data
if (symbolData.Security.Close > symbolData.EMA_Long.Current.Value)
{
SetHoldings(symbolData.Symbol, .75m*symbolData.Security.Leverage/(decimal)Symbols.Count);
}
}
}
}
public class SymbolData
{
public readonly string Symbol;
public readonly Security Security;
public readonly ExponentialMovingAverage EMA_Short;
public readonly ExponentialMovingAverage EMA_Long;
public readonly HeikinAshi HA;
public SymbolData(string symbol, QCAlgorithm algorithm)
{
Symbol = symbol;
Security = algorithm.Securities[symbol];
var consolidator = new QuoteBarConsolidator(TimeSpan.FromDays(1));
EMA_Short = new ExponentialMovingAverage(21);
EMA_Long = new ExponentialMovingAverage(55);
HA = new HeikinAshi();
//algorithm.RegisterIndicator(symbol, SMA, consolidator, Field.Close);
algorithm.RegisterIndicator(symbol, EMA_Long, consolidator);
algorithm.RegisterIndicator(symbol, EMA_Short, consolidator);
algorithm.RegisterIndicator(symbol, HA, consolidator);
algorithm.SubscriptionManager.AddConsolidator(symbol, consolidator);
}
}
}