Overall Statistics
Total Trades
3
Average Win
0%
Average Loss
0%
Compounding Annual Return
1.068%
Drawdown
1.900%
Expectancy
0
Net Profit
0.388%
Sharpe Ratio
0.304
Probabilistic Sharpe Ratio
30.633%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0.008
Beta
-0.001
Annual Standard Deviation
0.025
Annual Variance
0.001
Information Ratio
-2.464
Tracking Error
0.082
Treynor Ratio
-8.806
Total Fees
$1.00
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Data import SubscriptionDataSource
from QuantConnect.Python import PythonData

from datetime import date, timedelta, datetime
import numpy as np
import decimal
import json



"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
# ETHUSD (current use CoinMarketCap data)   2015-2020 end
    
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
class ETHUSD(PythonData):
    
    def __init__(self):
        self.cmcData = "https://www.dropbox.com/s/gk0g9mdskgfl9lq/ETHUSD.csv?dl=1"

    def GetSource(self, config, date, isLiveMode):
        return SubscriptionDataSource(self.cmcData, SubscriptionTransportMedium.RemoteFile)

    def Reader(self, config, line, date, isLiveMode):
        coin = ETHUSD()
        coin.Symbol = config.Symbol

        # # Example Line Format:  [Date      Open   High    Low     Close  9/13/2011 5.8    6.0     5.65    5.97]

        try:
            data = line.split(',')
            value =  float(data[4])
            if value == 0: return None

            coin.Time = datetime.strptime(data[0], "%d/%m/%Y")
            coin.Value = value
            coin["Open"]  =  float(data[1])
            coin["High"]  =  float(data[2])
            coin["Low"]   =  float(data[3])
            coin["Close"]  =  value

            return coin;

        except ValueError:
            return None



"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
# BTCUSD (current use CoinMarketCap data)   2015-2020 end
    
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
class BTCUSD(PythonData):

    def __init__(self):
        self.cmcData = "https://www.dropbox.com/s/zv4dwlw2hojxg82/BTCUSD.csv?dl=1"
    
    def GetSource(self, config, date, isLiveMode):
        return SubscriptionDataSource(self.cmcData, SubscriptionTransportMedium.RemoteFile)

    def Reader(self, config, line, date, isLiveMode):
        coin = BTCUSD()
        coin.Symbol = config.Symbol

        # # Example Line Format:  [Date      Open   High    Low     Close  9/13/2011 5.8    6.0     5.65    5.97]

        try:
            data = line.split(',')
            value =  float(data[4])
            if value == 0: return None

            coin.Time = datetime.strptime(data[0], "%d/%m/%Y")
            coin.Value = value
            coin["Open"]  =  float(data[1])
            coin["High"]  =  float(data[2])
            coin["Low"]   =  float(data[3])
            coin["Close"]  =  value

            return coin;

        except ValueError:
            return None
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Symbol Data

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
class SymbolData(object):
    
    def __init__(self, Symbol):
        
        self.Symbol = Symbol
        
        #-- meoEmaCrossAlpha
        self.EMA_fast = None
        self.EMA_slow = None
        self.FastIsOverSlow = False # This is used to prevent emitting the same signal repeatedly
        
    #-- meoEmaCrossAlpha
    @property
    def SlowIsOverFast(self):
        return not self.FastIsOverSlow
from datetime import timedelta, time

from meoCustomDatas import ETHUSD, BTCUSD

from Alphas.ConstantAlphaModel import ConstantAlphaModel
from Execution.ImmediateExecutionModel import ImmediateExecutionModel


class TestCustomSymbolWithFramework(QCAlgorithm):


    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    # Initialize
        
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    def Initialize(self):
        
        #-- Date Range
        self.SetStartDate(2019, 9, 20)
        self.SetEndDate(2020, 1, 30)

        self.SetCash(1000000)

        #-- Universe Selection ----------------------------------------------------------------------
        self.useCustomCryptoData = True    # if false, only allow long only position for crypto
        if self.useCustomCryptoData:
            self.BTCUSD_Symbol = self.AddData(BTCUSD, "BTCUSD", Resolution.Daily).Symbol
            self.ETHUSD_Symbol = self.AddData(ETHUSD, "ETHUSD", Resolution.Daily).Symbol
        else:
            self.BTCUSD_Symbol = Symbol.Create("BTCUSD", SecurityType.Crypto, Market.Bitfinex)
            self.ETHUSD_Symbol = Symbol.Create("ETHUSD", SecurityType.Crypto, Market.Bitfinex)
            
        self.symbols = [ 
                    Symbol.Create("SPY", SecurityType.Equity, Market.USA),
                    self.BTCUSD_Symbol,
                    self.ETHUSD_Symbol,
                    ]
        
        self.UniverseSettings.Resolution = Resolution.Daily
        self.SetUniverseSelection(ManualUniverseSelectionModel(self.symbols))
    
        
        #-- Alpha Creation ----------------------------------------------------------------------
        self.SetAlpha(ConstantAlphaModel(InsightType.Price, InsightDirection.Up, timedelta(1), None, None)) # type, direction, period, magnitude, confidence
        
        #-- Portfolio Construction ----------------------------------------------------------------------
        self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
        
        #-- Execution Model ----------------------------------------------------------------------
        self.SetExecution(ImmediateExecutionModel())


        #-- benchmarkMode for benchmark (do not use algorithm framework)
        self.benchmarkMode = True
        if self.benchmarkMode:
            self.SetAlpha(NullAlphaModel())
            self.SetPortfolioConstruction(NullPortfolioConstructionModel())
            self.SetSecurityInitializer(lambda x: x.SetMarketPrice(self.GetLastKnownPrice(x)))


        #---------------------------------------------------------------------------------------------------------------------------
        #-- Variables for program
        #---------------------------------------------------------------------------------------------------------------------------

        self.buyAndHoldDone = False # flag for benchmarkMode
        self.numSymbols = len(self.symbols)   



    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    # OnData 
        
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    def OnData(self, data):

        # benchmarkMode for benchmarking
        if(self.benchmarkMode):
            if not self.buyAndHoldDone:
                self.Debug("Benchmark Mode - not using Framework")
                target = 0 if self.numSymbols == 0 else 1.0 / self.numSymbols

                for symbol in self.symbols:
                    if data.ContainsKey(symbol):
                        if not self.Portfolio[symbol].Invested:
                            self.Debug(str(self.Time) + " trade for " + str(symbol))
                            self.SetHoldings(symbol, -0.1*target)
                            
                invested = [x.Symbol.Value for x in self.Portfolio.Values if x.Invested]
                if(len(invested) == self.numSymbols):
                    self.Debug(str(self.Time) + " fully invested")
                    self.buyAndHoldDone = True            
                    
            return