Overall Statistics
using System;
using System.Collections;
using System.Collections.Generic;
using QuantConnect.Securities;
using QuantConnect.Models;
using System.Linq;
using MathNet.Numerics.Statistics;

namespace QuantConnect {

    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>();
        }

    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using QuantConnect.Securities;
using QuantConnect.Models;
using System.Globalization;

namespace QuantConnect {

    public class Currency : BaseData
    {
        public decimal Open = 0;
        public decimal High = 0;
        public decimal Low = 0;
        public decimal Close = 0;

        public Currency()
        {
            this.Symbol = "USDCLP";
        }
        
        public override string GetSource(SubscriptionDataConfig config, DateTime date, DataFeedEndpoint datafeed)
        {
            return "https://www.dropbox.com/s/it25fk3fe3s10de/USDCLP.csv?dl=1";
        }
        
        public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, DataFeedEndpoint datafeed)
        {
            Currency currency = new Currency();

            try
            {
                string[] data = line.Split(',');
                currency.Time = DateTime.ParseExact(data[0], "dd-MM-yyyy", CultureInfo.InvariantCulture);
                currency.Close = Convert.ToDecimal(data[3]);
                currency.Open = Convert.ToDecimal(data[3]);
                currency.Symbol = "USDCLP";
                currency.Value = currency.Close;
            }
            catch {
                
            }

            return currency;
        }
    }
}
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;
using System.Globalization;

namespace QuantConnect {    

    public class Index : BaseData
    {
        public decimal Close = 0;

        public Index()
        {
            this.Symbol = "IPSA";
        }
        
        public override string GetSource(SubscriptionDataConfig config, DateTime date, DataFeedEndpoint datafeed)
        {
            return "https://www.dropbox.com/s/k2yixqaw7cmt57b/IPSA.csv?dl=1";
        }
        
        public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, DataFeedEndpoint datafeed)
        {
            Index index = new Index();
            try
            {
                string[] data = line.Split(',');
                index.Time = DateTime.ParseExact(data[0], "dd-MM-yyyy", CultureInfo.InvariantCulture);
                index.Close = Convert.ToDecimal(data[1]);
                index.Symbol = "IPSA";
                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
{
    public partial class CustomDataSourceAlgorithm : QCAlgorithm
    {
        CorrelationPair today = new CorrelationPair();
        List<CorrelationPair> prices = new List<CorrelationPair>();
        int minimumCorrelationHistory = 3;
        
        public override void Initialize()
        {
            SetStartDate(1999, 1, 4);
            SetEndDate(2014, 7, 25);

            //Set the cash for the strategy:
            SetCash(100000);

            //Define the symbol and "type" of our generic data:
            AddData<Currency>("USDCLP");
            AddData<Index>("IPSA");
        }
        

        public void OnData(Currency data)
        {
            today = new CorrelationPair(data.Time);
            today.Add("USDCLP", data.Open);
        }

        
        public void OnData(Index data)
        {
            try
            {
                today.Add("IPSA", 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 Index
        public override void OnEndOfDay()
        {
            if (today.Prices.ContainsKey("IPSA")) 
            {
                if(today.Prices["IPSA"] != 0 && today.Date.DayOfWeek == DayOfWeek.Wednesday)
                {
                    Plot("IPSA", today.Prices["IPSA"]);
                }
                if(today.Prices["USDCLP"] != 0 && today.Date.DayOfWeek == DayOfWeek.Wednesday)
                {
                    Plot("USDCLP", today.Prices["USDCLP"]);
                }
            }
        }
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using QuantConnect.Securities;
using QuantConnect.Models;
using System.Globalization;

namespace QuantConnect {

    public class DollarRupee : BaseData
    {
        public decimal Open = 0;
        public decimal High = 0;
        public decimal Low = 0;
        public decimal Close = 0;

        public DollarRupee()
        {
            this.Symbol = "USDCLP";
        }
        
        public override string GetSource(SubscriptionDataConfig config, DateTime date, DataFeedEndpoint datafeed)
        {
            return "https://www.dropbox.com/s/it25fk3fe3s10de/USDCLP.csv?dl=1";
        }
        
        public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, DataFeedEndpoint datafeed)
        {
            DollarRupee currency = new DollarRupee();

            try
            {
                string[] data = line.Split(',');
                currency.Time = DateTime.ParseExact(data[0], "dd-MM-yyyy", CultureInfo.InvariantCulture);
                currency.Close = Convert.ToDecimal(data[3]);
                currency.Open = Convert.ToDecimal(data[3]);
                currency.Symbol = "USDCLP";
                currency.Value = currency.Close;
            }
            catch {
                
            }

            return currency;
        }
    }
}