| Overall Statistics |
|
Total Trades 40 Average Win 0.8% Average Loss -1.01% Compounding Annual Return 7296476873.353% Drawdown 4.800% Expectancy 0.207 Net Profit 8.6% Sharpe Ratio 9.165 Loss Rate 32% Win Rate 68% Profit-Loss Ratio 0.79 Alpha 7.424 Beta -1.195 Annual Standard Deviation 0.788 Annual Variance 0.621 Information Ratio 8.81 Tracking Error 0.801 Treynor Ratio -6.044 |
using System;
using System.Collections;
using System.Collections.Generic;
using QuantConnect.Securities;
using QuantConnect.Models;
using QuantConnect.Indicators;
namespace QuantConnect
{
/*
* QuantConnect University: Binary Options are a different way of trading than traditional equities.
*
* Although QuantConnect does not support live binary option trading yet, we do support backtesting your binary option strategies!
* This framework algorithm will get you started on how to setup a Binary Option Backtest.
*/
public class BinaryOptionsFrameworkAlgorithm : QCAlgorithm
{
//Setup your variables for the strategy:
//1. Starting cash and the internal cash counter.
private decimal _cash = 2000;
//2. Record keeping of the entry price for the strategy
private decimal _price = 0;
//3. Bet size
private decimal _stakeSize = 20;
//4. Win fraction of the stake size
private decimal _winFraction = 0.8m;
//5. Timeframe for the Holdings:
//private DateTime _entry = new DateTime();
private TimeSpan _period = TimeSpan.FromSeconds(1800);
//private ArrayList symbols = new ArrayList();
private string _symbol = "EURUSD";
// BinaryBet _binaryBet;
//Other working variables:
//private TradeBars _data;
public static QCAlgorithm Reference;
private List<BinaryBet> _bets = new List<BinaryBet>();
private RelativeStrengthIndex _rsi;
private BollingerBands _bb;
private MovingAverageConvergenceDivergence _macd;
//Initialize the algorithm parameters such as symbols and dates:
public override void Initialize()
{
AddSecurity(SecurityType.Forex, _symbol, Resolution.Minute);
_bb = BB(_symbol, 20, 1m, MovingAverageType.Exponential, Resolution.Minute);
_macd = MACD(_symbol, 13 , 26, 9 , MovingAverageType.Exponential, Resolution.Minute);
_rsi = RSI(_symbol, 14, MovingAverageType.Exponential, Resolution.Hour);
SetStartDate(2014, 3, 20);
SetEndDate(2014, 3, 25);
SetCash(_cash);
Reference = this;
}
//Data Event Handler: Process new financial data.
public void OnData(TradeBars data)
{
//Scan for Entry:
//Rules for entry.
_price = data[_symbol].Price;
if (!_bb.IsReady) return;
//if(!_macd.IsReady) return;
//if(!_aroon.IsReady) return;
//if(!_rsi.IsReady) return;
decimal signalDeltaPercent = (_macd - _macd.Signal)/(_macd.Fast);
var tolerance = 0.0025m;
var percentUp = _bb.UpperBand/20000m;
var percentDown = _bb.LowerBand/20000m;
if ( _price >= (_bb.UpperBand +percentUp)
&& signalDeltaPercent < tolerance && _rsi >= 70 )
{
_bets.Add(new BinaryBet(_symbol, Direction.Short, _stakeSize, _period, _winFraction));
}
else if ( _price <= (_bb.LowerBand - percentDown)
&& signalDeltaPercent > tolerance && _rsi <= 30)
{
_bets.Add(new BinaryBet(_symbol, Direction.Long, _stakeSize, _period, _winFraction));
}
//Scan Bets:
foreach (var bet in _bets) {
if (!bet.Paid && bet.Expired) {
if (bet.Scan()) {
var lastProfit = bet.ProfitLoss;
var security = Securities[bet.Symbol];
//Update cash:
_cash += lastProfit;
bet.Paid = true;
//Record profit loss for statistics
security.Holdings.AddNewProfit(lastProfit);
Transactions.TransactionRecord.Add(Time, lastProfit);
}
}
}
//Update the cash:
Portfolio.SetCash(_cash);
}
} // End of Algorithm
} // End of Namespaceusing System;
using System.Collections;
using System.Collections.Generic;
using QuantConnect.Securities;
using QuantConnect.Models;
using QuantConnect.Indicators;
namespace QuantConnect {
//Container for holding bet information.
public class BinaryBet
{
private QCAlgorithm _algorithm;
private Direction _direction;
private string _symbol;
private DateTime _entryTime;
private decimal _entryPrice;
private decimal _exitPrice;
private TimeSpan _period;
private decimal _stake;
private bool _paid;
private decimal _winFraction;
/*private RelativeStrengthIndex _rsi;
private BollingerBands _bb;
private MovingAverageConvergenceDivergence _macd;*/
public BinaryBet(string symbol, Direction direction, decimal stake, TimeSpan period, decimal winFraction = 0.8m)
{
this._algorithm = BinaryOptionsFrameworkAlgorithm.Reference;
this._symbol = symbol;
this._period = period;
this._stake = stake;
this._winFraction = winFraction;
this._entryTime = _algorithm.Time;
this._entryPrice = _algorithm.Securities[_symbol].Price;
this._direction = direction;
this._paid = false;
/* this._bb = BB(_symbol, 20, 1m, MovingAverageType.Exponential, Resolution.Minute);
this._macd = MACD(_symbol, 13 , 26, 9 , MovingAverageType.Exponential, Resolution.Minute);
this._rsi = RSI(_symbol, 14, MovingAverageType.Exponential, Resolution.Hour);*/
}
//Option has passed its expiration date.
public bool Expired
{
get
{
return (_algorithm.Time > (_entryTime + _period));
}
}
/*public RelativeStrengthIndex Rsi
{
get
{
return _rsi;
}
}
public MovingAverageConvergenceDivergence Macd
{
get
{
return _macd;
}
}
public BollingerBands Bb
{
get
{
return _bb;
}
} */
//Flag indicating the bet has been paid.
public bool Paid
{
get { return _paid; }
set { _paid = value; }
}
public string Symbol {
get { return _symbol; }
}
public decimal EntryPrice{
get {return _entryPrice;}
}
public Direction BetDirection
{
get { return _direction;}
set { _direction = value;}
}
public decimal ProfitLoss
{
get {
if (!Expired) return 0;
switch (_direction)
{
default:
case Direction.Long:
return (_algorithm.Securities[_symbol].Price > _entryPrice) ? (_stake * _winFraction) : (-1*_stake);
case Direction.Short:
return (_algorithm.Securities[_symbol].Price < _entryPrice) ? (_stake * _winFraction) : (-1*_stake);
}
}
}
// Scan the algorithm time and price
public bool Scan()
{
if (_exitPrice == Decimal.Zero && Expired)
{
_exitPrice = _algorithm.Securities[_symbol].Price;
return true;
}
return false;
}
}
//Long or Short Bet
public enum Direction {
Long,
Short
}
}