using System;
using System.Collections;
using System.Collections.Generic;
using QuantConnect.Securities;
using QuantConnect.Models;
using QuantConnect.Indicators.CandlestickPatterns;
namespace QuantConnect
{
/*
* QuantConnect University: Importing Custom Yahoo / Google Data:
*
* With the LEAN Engine you can import any data type. We attempt to make this
* easier by providing importer code for Yahoo and Google.
*
* Quandl.com is a library and API wrapper for many data sources which makes
* sorting and reading the data easier.
*/
public class CustomDataYahooQuandl : QCAlgorithm
{
private string _symbol = "BKN";
private HangingMan _pattern = new HangingMan();
//Initialize the data and resolution you require for your strategy:
/// <summary>
/// Setup the algorithm data, cash, job start end date etc:
/// </summary>
public override void Initialize()
{
SetStartDate(2009, 2, 2);
SetEndDate(DateTime.Now);
SetCash(25000);
AddData<Yahoo>(_symbol);
//Quandl Indexes
//AddEquity(_symbol, Resolution.Hour);
var security = AddSecurity(SecurityType.Equity, _symbol, Resolution.Daily);
//AddSecurity(SecurityType.Equity, _symbol, Resolution.Daily);
_pattern = CandlestickPatterns.HangingMan(security.Symbol);
}
/// <summary>
/// Yahoo Daily Bars Event Handler: Daily bars arrive here for processing.
/// </summary>
public void OnData(Yahoo data)
{
if (_pattern == 1)
{
IEnumerable<Slice> slices = History(TimeSpan.FromDays(4), Resolution.Daily);
IEnumerable<TradeBar> bars = slices.Get(_symbol); // Bars
var decimals = slices.Get(_symbol, Field.Close).ToList();
var ordered = (from d in decimals orderby d descending select d);
if (Enumerable.SequenceEqual(decimals, ordered)) {
// Bullish Harami, go long
SetHoldings(_symbol, 20);
}
}
else if (_pattern == -1)
{
IEnumerable<Slice> slices = History(TimeSpan.FromDays(4), Resolution.Daily);
IEnumerable<TradeBar> bars = slices.Get(_symbol); // Bars
var decimals = slices.Get(_symbol, Field.Close).ToList();
var ordered = (from d in decimals orderby d ascending select d);
if (Enumerable.SequenceEqual(decimals, ordered)) {
// Sell out
SetHoldings(_symbol, -20);
}
}
}
/// <summary>
/// QC-TradeBars Data Event Handler: Not used in this strategy:
/// </summary>
public void OnData(TradeBars data)
{
}
}
}
using System.Globalization;
namespace QuantConnect {
/*
* QuantConnect University: Generic Yahoo Data Importer:
*
* Import Yahoo data using only a symbol. Be sure to confirm yahoo has the
* data you're requesting.
*/
public class Yahoo : BaseData
{
public decimal Open = 0;
public decimal High = 0;
public decimal Low = 0;
public decimal Close = 0;
public decimal AdjustedClose = 0;
public decimal Volume = 0;
public Yahoo()
{
this.Symbol = "";
}
/// <summary>
/// Return the URL external source for the data: QuantConnect will download it an read it line by line automatically:
/// </summary>
public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLive)
{
var startDate = new DateTime(2009, 2, 2).ToString("yyyy-MM-dd");
var endDate = DateTime.Now.ToString("yyyy-MM-dd");
//QUANDL WRAPPER ON YAHOO FINANCE API TO SORT DATA:
//https://www.quandl.com/api/v1/datasets/YAHOO/INDEX_SPY.csv?trim_start=2000-01-01&trim_end=2014-12-03&sort_order=asc
return new SubscriptionDataSource("###############", SubscriptionTransportMedium.RemoteFile);
}
/// <summary>
/// Convert each line of the file above into an object.
/// </summary>
public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLive)
{
Yahoo yBar = new Yahoo();
try
{
string[] data = line.Split(',');
//Required.
yBar.Symbol = config.Symbol;
yBar.Time = DateTime.ParseExact(data[0], "yyyy-MM-dd", CultureInfo.InvariantCulture);
//User configured / optional data on each bar:
yBar.Open = Convert.ToDecimal(data[1]);
yBar.High = Convert.ToDecimal(data[2]);
yBar.Low = Convert.ToDecimal(data[3]);
yBar.Close = Convert.ToDecimal(data[4]);
yBar.Volume = Convert.ToDecimal(data[5]);
yBar.AdjustedClose = Convert.ToDecimal(data[6]);
//This is the value the engine uses for portfolio calculations
yBar.Value = yBar.AdjustedClose;
}
catch {
}
return yBar;
}
}
}