Overall Statistics
Total Trades
47
Average Win
45.22%
Average Loss
-6.00%
Compounding Annual Return
50.734%
Drawdown
50.800%
Expectancy
1.560
Net Profit
323.931%
Sharpe Ratio
1.005
Probabilistic Sharpe Ratio
33.604%
Loss Rate
70%
Win Rate
30%
Profit-Loss Ratio
7.53
Alpha
0.522
Beta
-0.245
Annual Standard Deviation
0.496
Annual Variance
0.246
Information Ratio
0.732
Tracking Error
0.552
Treynor Ratio
-2.038
Total Fees
$0.00
Estimated Strategy Capacity
$2900000.00
Lowest Capacity Asset
BTCUSD XJ
Portfolio Turnover
4.47%
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from QuantConnect.Orders import *

class MovingAverageCrossover(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 1, 1)  # set start date
        self.SetCash(10000)  # set strategy cash
        self.btc = self.AddCrypto("BTCUSD", Resolution.Daily)  # Bitcoin data

        self.fast = self.EMA("BTCUSD", 14, Resolution.Daily)
        self.slow = self.EMA("BTCUSD", 28, Resolution.Daily)
        self.long_term = self.EMA("BTCUSD", 50, Resolution.Daily)
        self.volume = self.SMA("BTCUSD", 14, Resolution.Daily, Field.Volume)

        self.previous = None
        self.stopMarketTicket = None

    def OnData(self, data):
        if not self.fast.IsReady or not self.slow.IsReady or not self.long_term.IsReady or not self.volume.IsReady:
            return

        if self.previous is not None and self.previous.date() == self.Time.date():
            return

        holdings = self.Portfolio["BTCUSD"].Quantity

        if self.fast.Current.Value > self.slow.Current.Value and self.Securities["BTCUSD"].Close > self.long_term.Current.Value:
            if holdings <= 0 and self.btc.Volume > self.volume.Current.Value:
                self.SetHoldings("BTCUSD", 1.0)
                self.stopMarketTicket = self.StopMarketOrder("BTCUSD", 
                                                            -self.Portfolio["BTCUSD"].Quantity, 
                                                            0.9 * self.Securities["BTCUSD"].Close)

        elif self.fast.Current.Value < self.slow.Current.Value and self.Securities["BTCUSD"].Close < self.long_term.Current.Value:
            if holdings >= 0 and self.btc.Volume > self.volume.Current.Value:
                self.SetHoldings("BTCUSD", -1.0)
                self.stopMarketTicket = self.StopMarketOrder("BTCUSD", 
                                                            self.Portfolio["BTCUSD"].Quantity, 
                                                            1.1 * self.Securities["BTCUSD"].Close)

        self.previous = self.Time

    def OnOrderEvent(self, orderEvent):
        order = self.Transactions.GetOrderById(orderEvent.OrderId)
        if order.Type == OrderType.StopMarket and orderEvent.Status == OrderStatus.Filled:
            self.stopMarketTicket = None