| Overall Statistics |
|
Total Trades 24 Average Win 0.02% Average Loss 0% Compounding Annual Return 0.532% Drawdown 3.500% Expectancy 0 Net Profit 0.180% Sharpe Ratio 0.1 Loss Rate 0% Win Rate 100% Profit-Loss Ratio 0 Alpha 0.017 Beta -0.451 Annual Standard Deviation 0.084 Annual Variance 0.007 Information Ratio -0.119 Tracking Error 0.084 Treynor Ratio -0.018 Total Fees $3.00 |
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
// Short Straddle - sell 1 ATM Call and 1 Put at the price close to underlying
// and the same expiration date
namespace QuantConnect
{
public partial class ShortStraddle : 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
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 closest expiration date and min distance to underlying price
var contractCall = atmCalls
.OrderBy(o => o.ID.Date)
.ThenByDescending(o => o.ID.StrikePrice - price)
.FirstOrDefault();
var contractPut = atmPuts
.OrderBy(o => o.ID.Date)
.ThenByDescending(o => price - o.ID.StrikePrice)
.FirstOrDefault();
// If we found such options - sell 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;
}
}
}