| Overall Statistics |
|
Total Trades 24 Average Win 1.45% Average Loss -0.55% Compounding Annual Return -26.507% Drawdown 23.600% Expectancy -0.155 Net Profit -9.906% Sharpe Ratio -0.305 Loss Rate 77% Win Rate 23% Profit-Loss Ratio 2.66 Alpha 0.341 Beta -26.954 Annual Standard Deviation 0.511 Annual Variance 0.261 Information Ratio -0.341 Tracking Error 0.511 Treynor Ratio 0.006 Total Fees $6.50 |
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
// Long Strangle - buy 1 ATM Call and 1 ATM Put with the same expiration date
// and on the same distance from the underlying price
namespace QuantConnect
{
public partial class LongStrangle : QCAlgorithm
{
string iSymbol = "MSFT";
DateTime iTime;
public override void Initialize()
{
SetCash(10000);
SetStartDate(2018, 1, 1);
SetEndDate(DateTime.Now.Date);
SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage);
AddEquity(iSymbol, Resolution.Minute);
}
public void OnData(TradeBars data)
{
if (IsMarketOpen(iSymbol) == false)
{
return;
}
if (IsNewBar(TimeSpan.FromHours(1)) == false)
{
return;
}
var price = Securities[iSymbol].Price;
// If options were exercised and we were assigned to buy shares, sell them immediately
if (Portfolio[iSymbol].Invested)
{
MarketOrder(iSymbol, -100);
}
if (Portfolio.Invested == false)
{
var contracts = OptionChainProvider.GetOptionContractList(iSymbol, Time);
// Choose all contracts within a month and strike price $1 to $5 from current underlying price
var atmCalls =
from c in contracts
where c.ID.OptionRight == OptionRight.Call
where c.ID.StrikePrice - price < 5 && c.ID.StrikePrice - price > 1
where (c.ID.Date - Time).TotalDays < 35 && (c.ID.Date - Time).TotalDays > 0
select c;
var atmPuts =
from c in contracts
where c.ID.OptionRight == OptionRight.Put
where price - c.ID.StrikePrice < 5 && price - c.ID.StrikePrice > 1
where (c.ID.Date - Time).TotalDays < 35 && (c.ID.Date - Time).TotalDays > 0
select c;
// Take options with the MIN expiration date and MAX distance from underlying price
var contractCall = atmCalls
.OrderBy(o => o.ID.Date)
.ThenBy(o => o.ID.StrikePrice - price)
.FirstOrDefault();
var contractPut = atmPuts
.OrderBy(o => o.ID.Date)
.ThenBy(o => price - o.ID.StrikePrice)
.FirstOrDefault();
// If we found such options - buy 1 ATM Call and 1 ATM Put
if (contractCall != null && contractPut != null)
{
AddOptionContract(contractCall, Resolution.Minute);
AddOptionContract(contractPut, Resolution.Minute);
MarketOrder(contractPut, 1);
MarketOrder(contractCall, 1);
}
}
}
public bool IsNewBar(TimeSpan interval, int points = 1)
{
var date = Securities[iSymbol].LocalTime;
if ((date - iTime).TotalSeconds > interval.TotalSeconds * points)
{
iTime = new DateTime(date.Ticks - date.Ticks % interval.Ticks, date.Kind);
return true;
}
return false;
}
}
}