using QuantConnect.Data.Consolidators;
using QuantConnect.Indicators;
using QuantConnect.Data.Market;
using Accord.MachineLearning.VectorMachines;
namespace QuantConnect.Algorithm
{
/*
* QuantConnect University: FOREX - Using Currency Data
*
* QuantConnect allows you to use currency data for your backtest with a
* simple line of code. See the SecurityType.Forex below.
*/
public class TestStrategy : QCAlgorithm
{
AverageTrueRange _atr;
decimal _price;
string _symbol = "EURUSD";
public override void Initialize()
{
SetStartDate(2015, 1, 4);
SetEndDate(DateTime.Now.Date.AddDays(-1));
SetCash(10000);
AddSecurity(SecurityType.Forex, _symbol, Resolution.Minute);
_atr = ATR(_symbol, 14, MovingAverageType.Simple, Resolution.Daily);
// Create a linear binary machine with 2 inputs
var svm = new SupportVectorMachine(inputs: 2);
// Construct Hour & Day Consolidators
var dayConsolidator = new TradeBarConsolidator(TimeSpan.FromDays(1));
var hourConsolidator = new TradeBarConsolidator(TimeSpan.FromHours (1));
dayConsolidator.DataConsolidated += OnDataDay;
dayConsolidator.DataConsolidated += OnDataHour;
SubscriptionManager.AddConsolidator(_symbol,dayConsolidator);
SubscriptionManager.AddConsolidator(_symbol,hourConsolidator);
}
public void OnData(TradeBars data)
{
if (!Portfolio.HoldStock)
{
Order("EURUSD", 1000);
Debug("Purchased EURUSD on " + Time.ToShortDateString());
}
}
private void OnDataDay(object sender,TradeBar consolidated)
{
_price = consolidated.Close;
if (!_atr.IsReady) return;
}
private void OnDataHour(object sender,TradeBar consolidated)
{
_price = consolidated.Close;
//Log (_hourwindow[0].Close.ToString());
//Log (_hourwindow [8].Close.ToString());
}
// Fire plotting events once per day:
public override void OnEndOfDay()
{
Plot("ATR", _atr);
Plot("ATR", _price);
}
}
}