| Overall Statistics |
|
Total Trades 4 Average Win 0.56% Average Loss -1.51% Compounding Annual Return -23.510% Drawdown 1.100% Expectancy -0.312 Net Profit -0.950% Sharpe Ratio -8.472 Loss Rate 50% Win Rate 50% Profit-Loss Ratio 0.38 Alpha -0.123 Beta -0.132 Annual Standard Deviation 0.024 Annual Variance 0.001 Information Ratio -8.083 Tracking Error 0.097 Treynor Ratio 1.519 Total Fees $1.00 |
using QuantConnect.Securities.Option;
using System.Net;
namespace QuantConnect
{
public class StraddleFundamentalAlgorithm : QCAlgorithm
{
public class ListTuple: List<Tuple<DateTime, string>>
{
public DateTime date {get;set;}
public string ticker {get; set;}
public ListTuple(DateTime _date, string _ticker)
{
date = _date;
ticker = _ticker;
}
}
public List<ListTuple> ReadEarningData(){
Debug("lala");
WebClient webClient = new WebClient();
string theText = webClient.DownloadString("http://tradeitforweed.ca/earnings_sql3.csv");
string[] lines = theText.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
List<ListTuple> splitted = new List<ListTuple>();
foreach (var line in lines){
if(line.Split(',')[0].Length != 0){
ListTuple example = new ListTuple(DateTime.ParseExact(line.Split(',')[0], "M/d/yyyy", CultureInfo.InvariantCulture), line.Split(',')[1]);
splitted.Add(example);
}
}
return splitted;
}
private List<Symbol> _symbols = new List<Symbol>();
private SecurityChanges _changes = SecurityChanges.None;
private decimal TP = 0.125M;
private List<ListTuple> earnings = new List<ListTuple>();
private double Held = 0;
private decimal SL = -0.095M;
public override void Initialize()
{
earnings = ReadEarningData();
UniverseSettings.Resolution = Resolution.Daily;
SetStartDate(2012, 01, 01);
SetEndDate(2012, 1, 15); //2012,5,5
SetCash(10000);
//AddUniverse(CoarseSelectionFunction, FineSelectionFunction);
}
public IEnumerable<Symbol> CoarseSelectionFunction(IEnumerable<CoarseFundamental> coarse) {
return coarse
.Where(x => x.HasFundamentalData)
.OrderByDescending(x => x.DollarVolume)
.Select(x => x.Symbol).Take(10);
}
public IEnumerable<Symbol> FineSelectionFunction(IEnumerable<FineFundamental> fine) {
List<ListTuple> list = new List<ListTuple>();
IEnumerable<Symbol> second = list.Cast<Symbol>();
return second;
}
public override void OnData(Slice data) {
// Check the next earnings.
var nextEarnings = earnings.Where(x => x.date == Time.Date.AddDays(-14)).ToArray();
foreach (var nextEarning in nextEarnings)
{
Log(nextEarning.ticker);
if (Held <= 2){
// Just an example, implement your logic here.
AddSecurity(SecurityType.Equity, nextEarning.ticker, Resolution.Daily);
}
}
var todayEarnings = earnings.Where(x => x.date == Time.Date.AddDays(1)).ToArray();
foreach (var nextEarning in todayEarnings)
{
Log(nextEarning.ticker);
// Just an example, implement your logic here.
RemoveSecurity(nextEarning.ticker);
}
List<Symbol> hSymbols = new List<Symbol>();
List<SecurityHolding> hHoldings = new List<SecurityHolding>();
int i = 0;
foreach (var kvp in Portfolio)
{
var holdingSymbol = kvp.Key;
var holdings = kvp.Value;
if (holdings.AbsoluteQuantity > 0)
{
hSymbols.Add(holdingSymbol);
hHoldings.Add(holdings);
}
}
foreach (var kvp in data.OptionChains) {
var chain = kvp.Value;
var symbol = kvp.Key;
if (_symbols.Contains(symbol.Underlying)){
return;
}
var atmStraddle = chain
.OrderBy(x => Math.Abs(chain.Underlying.Price - x.Strike))
.ThenByDescending(x => x.Expiry)
.FirstOrDefault();
if (atmStraddle != null && Held <= 2)
{
Debug("Bought: " + symbol + " &Held: " + (Held.ToString()));
Held++;
_symbols.Add(symbol.Underlying);
Buy(OptionStrategies.Straddle(symbol, atmStraddle.Strike, atmStraddle.Expiry), 1);
Debug(string.Format("{0} straddle orders submitted", symbol.Underlying));
}
}
_changes = SecurityChanges.None;
}
public override void OnSecuritiesChanged(SecurityChanges changes)
{
_changes = changes;
if (Held <= 2){
if (changes.AddedSecurities.Count > 0)
{
Log("Securities added: " + string.Join(",", changes.AddedSecurities.Select(x => x.Symbol.Value)));
}
if (changes.RemovedSecurities.Count > 0)
{
Debug("Securities removed: " + string.Join(",", changes.RemovedSecurities.Select(x => x.Symbol.Value)));
}
}
foreach (var security in changes.RemovedSecurities)
{
if (security.Invested)
{
Held = Held - 0.5;
Liquidate(security.Symbol);
Debug("Sold back: " + security.Symbol + " &Held: " + Held.ToString());
}
}
// Add option for every added security
foreach (var security in changes.AddedSecurities)
{
if (security is Option) continue;
//if (security.Symbol.Equals("SPY")) continue;
if (security.Symbol.Value.IndexOf(' ') != 0){
var option = AddOption(security.Symbol.Value);
option.SetFilter(-2, 2, TimeSpan.FromDays(30), TimeSpan.FromDays(45));
}
else {
var theSymbol = security.Symbol.Value.Split(' ')[0];
var option = AddOption(theSymbol);
option.SetFilter(-2, 2, TimeSpan.FromDays(30), TimeSpan.FromDays(45));
}
}
}
}
}