| Overall Statistics |
|
Total Trades 18 Average Win 0.26% Average Loss -0.22% Compounding Annual Return 5.564% Drawdown 1.600% Expectancy 0.249 Net Profit 5.564% Sharpe Ratio 1.547 Loss Rate 43% Win Rate 57% Profit-Loss Ratio 1.19 Alpha -0.009 Beta 0.232 Annual Standard Deviation 0.029 Annual Variance 0.001 Information Ratio -2.372 Tracking Error 0.079 Treynor Ratio 0.191 Total Fees $18.00 |
using System.Net;
namespace QuantConnect
{
public class ScheduledUniverseAlgorithm : QCAlgorithm
{
private int _lookbackPeriod = 3;
private Dictionary<Symbol, IEnumerable<TradeBar>> _history;
private Dictionary<DateTime, IEnumerable<Symbol>> _symbols;
public override void Initialize()
{
SetStartDate(2013, 1, 1); //Set Start Date
SetEndDate(2014, 1, 1); //Set End Date
SetCash(100000); //Set Strategy Cash
AddEquity("SPY", Resolution.Daily);
_history = new Dictionary<Symbol, IEnumerable<TradeBar>>();
_symbols = new Dictionary<DateTime, IEnumerable<Symbol>>();
Schedule.On(DateRules.EveryDay(Securities["SPY"].Symbol), TimeRules.At(9, 20), UniverseUpdate);
}
public void OnData(TradeBars data)
{
foreach (var item in data)
{
if (!Portfolio[item.Key].Invested)
{
Log("Buy 100 shares of " + item.Key);
Order(item.Key, 100);
}
}
}
public void UniverseUpdate()
{
const string liveUrl = @"https://www.dropbox.com/s/2az14r5xbx4w5j6/daily-stock-picker-live.csv?dl=1";
const string backtestUrl = @"https://www.dropbox.com/s/rmiiktz0ntpff3a/daily-stock-picker-backtest.csv?dl=1";
using (var client = new WebClient())
{
// handle live mode file format
if (LiveMode)
{
// fetch the file from dropbox
var file = client.DownloadString(liveUrl);
// if we have a file for today, break apart by commas and return symbols
_symbols.Add(DateTime.Today, file.ToCsv()
.Select(x => QuantConnect.Symbol.Create(x, SecurityType.Equity, Market.USA)));
}
else
{
if (_symbols.Count == 0)
{
// fetch the file from dropbox
var file = client.DownloadString(backtestUrl);
// split the file into lines and add to our cache
foreach (var line in file.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries))
{
var csv = line.ToCsv();
var date = DateTime.ParseExact(csv[0], "yyyyMMdd", null);
_symbols.Add(date, csv.Skip(1)
.Select(x => QuantConnect.Symbol.Create(x, SecurityType.Equity, Market.USA)));
}
}
}
}
IEnumerable<Symbol> fetchedSymbols;
if (!_symbols.TryGetValue(Time.Date, out fetchedSymbols)) return;
// Current securities
var currentSecurities = Securities.Keys;
// Add securities to the universe
var addedSecurities = fetchedSymbols.Except(currentSecurities);
foreach (var addedSecurity in addedSecurities)
{
AddEquity(addedSecurity, Resolution.Daily);
}
// Remove securities from the universe
// It will liquidate positions
// They will be part of the IAlgorithm.Securities, but not part of subscribed securities
var removedSecurities = currentSecurities.Except(fetchedSymbols);
foreach (var removedSecurity in removedSecurities)
{
RemoveSecurity(removedSecurity);
}
// Get History and save it to cache
foreach (var symbol in fetchedSymbols)
{
if (!_history.ContainsKey(symbol))
{
_history.Add(symbol, History(symbol, TimeSpan.FromDays(_lookbackPeriod), Resolution.Daily));
}
else
{
_history[symbol] = History(symbol, TimeSpan.FromDays(_lookbackPeriod), Resolution.Daily);
}
}
Log("Universe updated at " + Time);
}
}
}