| Overall Statistics |
|
Total Trades 0 Average Win 0% Average Loss 0% Compounding Annual Return 781.96% Drawdown 4.500% Expectancy 0 Net Profit 0% Sharpe Ratio 11.225 Loss Rate 0% Win Rate 0% Profit-Loss Ratio 0 Alpha 3.024 Beta -1.86 Annual Standard Deviation 0.135 Annual Variance 0.018 Information Ratio 3.376 Tracking Error 0.207 Treynor Ratio -0.813 |
namespace QuantConnect {
/// <summary>
/// Custom Data Type: Bitcoin data from Quandl.
/// http://www.quandl.com/help/api-for-bitcoin-data
/// </summary>
public class Bitcoin : BaseData
{
//Set the defaults:
public decimal Open = 0;
public decimal High = 0;
public decimal Low = 0;
public decimal Close = 0;
public decimal VolumeBTC = 0;
public decimal VolumeUSD = 0;
public decimal WeightedPrice = 0;
/// <summary>
/// 1. DEFAULT CONSTRUCTOR: Custom data types need a default constructor.
/// We search for a default constructor so please provide one here. It won't be used for data, just to generate the "Factory".
/// </summary>
public Bitcoin()
{
this.Symbol = "BTC";
}
/// <summary>
/// 2. RETURN THE STRING URL SOURCE LOCATION FOR YOUR DATA:
/// This is a powerful and dynamic select source file method. If you have a large dataset, 10+mb we recommend you break it into smaller files. E.g. One zip per year.
/// We can accept raw text or ZIP files. We read the file extension to determine if it is a zip file.
/// </summary>
/// <param name="config">Subscription data, symbol name, data type</param>
/// <param name="date">Current date we're requesting. This allows you to break up the data source into daily files.</param>
/// <param name="datafeed">Datafeed type: Backtesting or the Live data broker who will provide live data. You can specify a different source for live trading! </param>
/// <returns>string URL end point.</returns>
public override string GetSource(SubscriptionDataConfig config, DateTime date, DataFeedEndpoint datafeed)
{
switch (datafeed)
{
//Backtesting Data Source: Example of a data source which varies by day (commented out)
default:
case DataFeedEndpoint.Backtesting:
//return "http://my-ftp-server.com/futures-data-" + date.ToString("Ymd") + ".zip";
// OR simply return a fixed small data file. Large files will slow down your backtest
return "http://www.quandl.com/api/v1/datasets/BITCOIN/BITSTAMPUSD.csv?sort_order=asc";
case DataFeedEndpoint.LiveTrading:
//Alternative live socket data source for live trading
return "....";
}
}
/// <summary>
/// 3. READER METHOD: Read 1 line from data source and convert it into Object.
/// Each line of the CSV File is presented in here. The backend downloads your file, loads it into memory and then line by line
/// feeds it into your algorithm
/// </summary>
/// <param name="line">string line from the data source file submitted above</param>
/// <param name="config">Subscription data, symbol name, data type</param>
/// <param name="date">Current date we're requesting. This allows you to break up the data source into daily files.</param>
/// <param name="datafeed">Datafeed type - Backtesting or LiveTrading</param>
/// <returns>New Bitcoin Object which extends BaseData.</returns>
public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, DataFeedEndpoint datafeed)
{
//New Bitcoin object
Bitcoin coin = new Bitcoin();
try
{
//Example File Format:
//Date, Open High Low Close Volume (BTC) Volume (Currency) Weighted Price
//2011-09-13 5.8 6.0 5.65 5.97 58.37138238, 346.0973893944 5.929230648356
string[] data = line.Split(',');
coin.Time = DateTime.Parse(data[0]);
coin.Open = Convert.ToDecimal(data[1]);
coin.High = Convert.ToDecimal(data[2]);
coin.Low = Convert.ToDecimal(data[3]);
coin.Close = Convert.ToDecimal(data[4]);
coin.VolumeBTC = Convert.ToDecimal(data[5]);
coin.VolumeUSD = Convert.ToDecimal(data[6]);
coin.WeightedPrice = Convert.ToDecimal(data[7]);
coin.Symbol = "BTC";
coin.Value = coin.Close;
}
catch { /* Do nothing, skip first title row */ }
return coin;
}
}
}using System;
using System.Windows;
namespace QuantConnect
{
public class BinaryAlgorithm : QCAlgorithm
{
private decimal _cash = 2000;
private decimal _entryPrice = 0;
private decimal _stake = 20;
bool shrt = false;
bool lng = false;
BollingerBands _bb;
MovingAverageConvergenceDivergence _macd;
private decimal wins;
private decimal losses;
RelativeStrengthIndex _rsi = new RelativeStrengthIndex(14);
//SimpleMovingAverage _sma50, _sma20;
// private decimal totalProfit;
//private decimal loss;
// private decimal finalPrice;
ArrayList symbols = new ArrayList();
private Dictionary <string, MovingAverageConvergenceDivergence> macdDic = new Dictionary<string, MovingAverageConvergenceDivergence>();
private Dictionary <string, RelativeStrengthIndex> rsiDic = new Dictionary<string,RelativeStrengthIndex>();
private Dictionary <string, BollingerBands> bbDic = new Dictionary <string, BollingerBands>();
private Dictionary<string, DateTime> lastTimeDic = new Dictionary<string, DateTime>();
private Dictionary<string, decimal> entryPriceDic = new Dictionary<string, decimal> ();
private Dictionary<string, decimal> winDic = new Dictionary<string, decimal> ();
private Dictionary<string, decimal> lossDic = new Dictionary<string, decimal> ();
private Dictionary<string, bool> shrtDic = new Dictionary<string, bool> ();
private Dictionary<string, bool> lngDic = new Dictionary<string, bool> ();
decimal _price;
// decimal _bet;
//private AroonOscillator _aroon;
private DateTime lastTradeTime;
public override void Initialize()
{
SetStartDate(2014,12,1);
SetEndDate(2015,1,1);
symbols.Add("EURUSD");
//symbols.Add("EURJPY");
SetCash(_cash);
foreach( string _symbol in symbols)
{
AddSecurity(SecurityType.Forex, _symbol, Resolution.Second);
_bb = BB(_symbol, 20, 1m, MovingAverageType.Exponential, Resolution.Minute);
bbDic.Add(_symbol, _bb);
_macd = MACD(_symbol, 13 , 26, 9 , MovingAverageType.Exponential, Resolution.Minute);
macdDic.Add(_symbol,_macd);
_rsi = RSI(_symbol, 14, MovingAverageType.Exponential, Resolution.Hour);
rsiDic.Add(_symbol, _rsi);
shrtDic.Add(_symbol, false);
lngDic.Add(_symbol, false);
entryPriceDic.Add(_symbol, 0);
winDic.Add(_symbol, 0);
lossDic.Add(_symbol, 0);
}
}
public void OnData(TradeBars data)
{
foreach(string _symbol in symbols)
{
EnterTrade(data, _symbol);
shrt = shrtDic[_symbol];
lng = lngDic[_symbol];
_entryPrice = entryPriceDic[_symbol];
if ((shrt || lng) && (data[_symbol].Time - lastTimeDic[_symbol]).TotalSeconds >= 900)
{
if ( (_entryPrice > data[_symbol].Price && shrt) ||
(_entryPrice < data[_symbol].Price && lng)) {
_cash += (_stake+_stake * .8m);
winDic[_symbol] += 1;
Log(_symbol + " I win: " + "Long = true, Short = false: "+ lng+ " Positive means Short win, Negative Means Long win: "+
(_entryPrice - data[_symbol].Price)+ " Total Cash (Running Tally): "+ _cash);
} else {
lossDic[_symbol] +=1 ;
Log(_symbol + " I lose: " + "Long = true, Short = false: "+ lng+ " Positive means short win , Negative means long win: "+
(_entryPrice - data[_symbol].Price)+ " Total Cash (Running Tally): "+ _cash);
}
Portfolio.SetCash(_cash);
shrtDic[_symbol] = false;
lngDic[_symbol] = false;
}
}
}
public void EnterTrade(TradeBars data, string _symbol)
{
//Rules for entry.
if (!_bb.IsReady) return;
//if(!_macd.IsReady) return;
//if(!_aroon.IsReady) return;
//if(!_rsi.IsReady) return;
_price = data[_symbol].Close;
decimal signalDeltaPercent = (macdDic[_symbol] - macdDic[_symbol].Signal)/(macdDic[_symbol].Fast);
var tolerance = 0.0025m;
//var percentUp = _bb.UpperBand/20000m;
//var percentDown = _bb.LowerBand/20000m;
if ( _price >= (_bb.UpperBand ) && !(lng || shrt)
&& signalDeltaPercent < tolerance && _rsi >= 70 )
{
lastTimeDic[_symbol] = data[_symbol].Time;
entryPriceDic[_symbol] = Securities[_symbol].Price;
_cash -= _stake;
shrtDic[_symbol] = true;
}
else if ( _price <= (_bb.LowerBand ) && !(lng || shrt)
&& signalDeltaPercent > tolerance && _rsi <= 30)
{
lastTimeDic[_symbol] = data[_symbol].Time;
entryPriceDic[_symbol] = Securities[_symbol].Price;
lngDic[_symbol] = true;
}
}
public override void OnEndOfDay()
{
foreach( string _symbol in symbols)
{
wins = winDic[_symbol];
losses = lossDic[_symbol];
try{
Debug(_symbol+" Number of Wins: " + wins +" Number of Losses: "+ losses + " Percent Win: "+ Math.Round(100*(wins/ (losses + wins)), 2) +
" Time/Date: "+ lastTimeDic[_symbol]);
Debug( "Total Cash: "+ _cash);
winDic[_symbol] = 0;
lossDic[_symbol] = 0;
}
catch (Exception err){
Error(err.Message);
}
}
}
}
}