Overall Statistics
Total Trades
47
Average Win
54.94%
Average Loss
-6.37%
Compounding Annual Return
25.777%
Drawdown
66.700%
Expectancy
1.280
Net Profit
124.178%
Sharpe Ratio
0.647
Probabilistic Sharpe Ratio
15.503%
Loss Rate
76%
Win Rate
24%
Profit-Loss Ratio
8.63
Alpha
0.353
Beta
-0.298
Annual Standard Deviation
0.501
Annual Variance
0.251
Information Ratio
0.409
Tracking Error
0.559
Treynor Ratio
-1.088
Total Fees
$0.00
Estimated Strategy Capacity
$12000000.00
Lowest Capacity Asset
BTCUSD XJ
Portfolio Turnover
4.18%
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.rsi = self.RSI("BTCUSD", 14, MovingAverageType.Simple, Resolution.Daily)

        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 or not self.rsi.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 and self.rsi.Current.Value < 70:
            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 and self.rsi.Current.Value > 30:
            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