Overall Statistics |
Total Trades 441 Average Win 2.83% Average Loss -1.21% Compounding Annual Return 30.252% Drawdown 10.500% Expectancy 0.889 Net Profit 7885.113% Sharpe Ratio 0.246 Loss Rate 44% Win Rate 56% Profit-Loss Ratio 2.34 Alpha 4.772 Beta -0.036 Annual Standard Deviation 19.391 Annual Variance 376.004 Information Ratio 0.242 Tracking Error 19.392 Treynor Ratio -130.649 |
using System; using System.Collections; using System.Collections.Generic; using QuantConnect.Securities; using QuantConnect.Models; namespace QuantConnect { /// <summary> /// vvvvvv UPDATE TO YOUR CORE ALGORITHM CLASS NAME HERE: vvvv "QCUMartingalePositionSizing" ==> "MyAlgorithm" /// </summary> public partial class CustomDataSourceAlgorithm : QCAlgorithm { public void CustomSetHoldings(string symbol, decimal percentage, bool liquidateExistingHoldings = false) { decimal cash = Portfolio.Cash; decimal currentHoldingQuantity = Portfolio[symbol].Quantity; //Range check values: if (percentage > 1) percentage = 1; if (percentage < -1) percentage = -1; //If they triggered a liquidate if (liquidateExistingHoldings) { foreach (string holdingSymbol in Portfolio.Keys) { if (holdingSymbol != symbol) { //Go through all existing holdings, market order the inverse quantity Order(holdingSymbol, -Portfolio[holdingSymbol].Quantity); } } } //Now rebalance the symbol requested: decimal targetHoldingQuantity = Math.Floor((percentage * Portfolio.TotalPortfolioValue) / Securities[symbol].Price); decimal netHoldingQuantity = targetHoldingQuantity - currentHoldingQuantity; if (Math.Abs(netHoldingQuantity) > 0) { Order(symbol, (int)netHoldingQuantity); } return; } } }
using System; using System.Collections; using System.Collections.Generic; using QuantConnect.Securities; using QuantConnect.Models; namespace QuantConnect { /// <summary> /// Custom Data Type: USD/INR data from Quandl /// http://www.quandl.com/BNP/USDINR-Currency-Exchange-Rate-USD-vs-INR /// </summary> public class DollarRupee : BaseData { public decimal Open = 0; public decimal High = 0; public decimal Low = 0; public decimal Close = 0; public DollarRupee() { this.Symbol = "USDINR"; } public override string GetSource(SubscriptionDataConfig config, DateTime date, DataFeedEndpoint datafeed) { return "https://www.dropbox.com/s/w71phighrsh8uu0/USDINR.csv?dl=1"; } public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, DataFeedEndpoint datafeed) { //New USDINR object DollarRupee currency = new DollarRupee(); try { string[] data = line.Split(','); //Dates must be in the format YYYY-MM-DD. If your data source does not have this format, you must use //DateTime.ParseExact() and explicit declare the format your data source has. currency.Time = DateTime.Parse(data[0]); currency.Close = Convert.ToDecimal(data[1]); currency.Symbol = "USDINR"; currency.Value = currency.Close; } catch { } return currency; } } }
using System; using System.Collections; using System.Collections.Generic; using QuantConnect.Securities; using QuantConnect.Models; using System.Linq; using MathNet.Numerics.Statistics; namespace QuantConnect { /// <summary> /// 3.0 CUSTOM DATA SOURCE: USE YOUR OWN MARKET DATA (OPTIONS, FOREX, FUTURES, DERIVATIVES etc). /// /// The new QuantConnect Lean Backtesting Engine is incredibly flexible and allows you to define your own data source. /// /// This includes any data source which has a TIME and VALUE. These are the *only* requirements. To demonstrate this we're loading /// in "Nifty" data. This by itself isn't special, the cool part is next: /// /// We load the "Nifty" data as a tradable security we're calling "NIFTY". /// /// </summary> public partial class CustomDataSourceAlgorithm : QCAlgorithm { //Create variables for analyzing Nifty CorrelationPair today = new CorrelationPair(); List<CorrelationPair> prices = new List<CorrelationPair>(); int minimumCorrelationHistory = 11; public override void Initialize() { SetStartDate(1998, 1, 1); SetEndDate(2014, 7, 25); //Set the cash for the strategy: SetCash(100000); //Define the symbol and "type" of our generic data: AddData<DollarRupee>("USDINR"); AddData<Nifty>("NIFTY"); } public void OnData(DollarRupee data) { today = new CorrelationPair(data.Time); today.Add("USDINR", data.Close); } public void OnData(Nifty data) { try { today.Add("NIFTY", data.Close); if (today.Date == data.Time) { prices.Add(today); if(prices.Count > minimumCorrelationHistory) { prices.RemoveAt(0); } } if (prices.Count < 2) { return; } string maxAsset = ""; double maxGain = -9999; foreach(string i in today.Prices.Keys) { double last = (from pair in prices select pair.Prices[i]).Last(); double first = (from pair in prices select pair.Prices[i]).First(); double gain = (last - first) / first; if (gain > maxGain) { maxAsset = i; maxGain = gain; } } //Strategy if (maxAsset != "") { CustomSetHoldings(maxAsset, 1, true); } } catch(Exception err) { Debug( "Error: " + err.Message ); } } //Plot Nifty public override void OnEndOfDay(string symbol) { if (symbol == "NIFTY") { if (today.Prices.ContainsKey("NIFTY")) { if(today.Prices["NIFTY"] != 0 && today.Date.DayOfWeek == DayOfWeek.Wednesday) { Plot("NIFTY", today.Prices["NIFTY"]); } if(today.Prices["USDINR"] != 0 && today.Date.DayOfWeek == DayOfWeek.Wednesday) { Plot("USDINR", today.Prices["USDINR"]); } } } } } }
using System; using System.Collections; using System.Collections.Generic; using QuantConnect.Securities; using QuantConnect.Models; namespace QuantConnect { /// <summary> /// Custom Data Type: CNX Nifty data from NSE /// http://www.nseindia.com/ /// </summary> public class Nifty : BaseData { public decimal Open = 0; public decimal High = 0; public decimal Low = 0; public decimal Close = 0; public Nifty() { this.Symbol = "NIFTY"; } public override string GetSource(SubscriptionDataConfig config, DateTime date, DataFeedEndpoint datafeed) { return "https://www.dropbox.com/s/9dzb6spi38rweit/CNXNIFTY.csv?dl=1"; } public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, DataFeedEndpoint datafeed) { //New Nifty object Nifty index = new Nifty(); try { //Example File Format: //Date, Open High Low Close Volume Turnover //2011-09-13 7792.9 7799.9 7722.65 7748.7 116534670 6107.78 string[] data = line.Split(','); //Dates must be in the format YYYY-MM-DD. If your data source does not have this format, you must use //DateTime.ParseExact() and explicit declare the format your data source has. index.Time = DateTime.Parse(data[0]); index.Open = Convert.ToDecimal(data[1]); index.High = Convert.ToDecimal(data[2]); index.Low = Convert.ToDecimal(data[3]); index.Close = Convert.ToDecimal(data[4]); index.Symbol = "NIFTY"; //We need to de index.Value = index.Close; } catch { } return index; } } }
using System; using System.Collections; using System.Collections.Generic; using QuantConnect.Securities; using QuantConnect.Models; using System.Linq; using MathNet.Numerics.Statistics; namespace QuantConnect { /// <summary> /// Make a list of common dates to hold price data /// </summary> public class CorrelationPair { public DateTime Date = new DateTime(); public Dictionary<string, double> Prices = new Dictionary<string, double>(); public CorrelationPair() { Prices = new Dictionary<string, double>(); Date = new DateTime(); } public void Add(string symbol, decimal price) { Prices.Add(symbol, Convert.ToDouble(price)); } public CorrelationPair(DateTime date) { Date = date.Date; Prices = new Dictionary<string, double>(); } } }