Overall Statistics
Total Trades
2
Average Win
0%
Average Loss
-0.22%
Compounding Annual Return
144.972%
Drawdown
42.600%
Expectancy
-1
Net Profit
164.125%
Sharpe Ratio
2.088
Probabilistic Sharpe Ratio
68.970%
Loss Rate
100%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
1.24
Beta
0.945
Annual Standard Deviation
0.558
Annual Variance
0.311
Information Ratio
2.348
Tracking Error
0.53
Treynor Ratio
1.233
Total Fees
$2.41
Estimated Strategy Capacity
$49000.00
Lowest Capacity Asset
MPO WEZMX4KUKGV9
import numpy as np

import clr

clr.AddReference("System")
clr.AddReference("QuantConnect.Algorithm")
clr.AddReference("QuantConnect.Indicators")
clr.AddReference("QuantConnect.Common")

from AlgorithmImports import *

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
import decimal as d


class QEMA(QCAlgorithm):
    openingBar = None
    currentBar = None

    def Initialize(self):
        self.SetTimeZone(TimeZones.NewYork)
        self.SetStartDate(2021, 9, 20)
        self.SetEndDate(2022, 10, 20)
        self.SetCash(1000)  # Set Strategy Cash
        self.symbol = self.AddEquity("AMPY", Resolution.Minute,
                                     dataNormalizationMode=DataNormalizationMode.Raw).Symbol
        self.timedelta_consolidator = self.Consolidate(self.symbol, \
                                                       timedelta(minutes=(int(self.GetParameter("timeframe")))),
                                                       self.OnDataConsolidated)

        self.entryTicket = None
        self.stopMarketTicket = None

        self.lookback = 1.0010
        self.ceiling, self.floor = 1.0015, 1.0075

        self.initialStopRisk = 0.95
        self.trailingStopRisk = 0.99

        self.fast = self.EMA(self.symbol, 9)
        self.RegisterIndicator(self.symbol, self.fast)


        self.mid = self.EMA(self.symbol, 21)
        self.RegisterIndicator(self.symbol, self.mid)
     

        self.slow = self.EMA(self.symbol, 55)
        self.RegisterIndicator(self.symbol, self.slow)
      

        self.vfast = self.EMA(self.symbol, 5)
        self.RegisterIndicator(self.symbol, self.vfast)

        self.SetWarmup(55)

        self.previous = None

    def OnData(self, data: Slice):

        if not self.slow.IsReady:
            return

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

        close = self.History(self.symbol, 201, Resolution.Daily)["close"]
        todayvol = np.std(close[1:201])
        yesterdayvol = np.std(close[0:200])
        deltavol = (todayvol - yesterdayvol) / todayvol
        self.lookback = round(self.lookback * (1 + deltavol))

        # Account for upper/lower limit of lockback length
        if self.lookback > self.ceiling:
            self.lookback = self.ceiling
        elif self.lookback < self.floor:
            self.lookback = self.floor

        # hist = self.History(self.symbol, int(self.GetParameter("HighestPrice.delta")), Resolution.Minute)
        # self.breakoutlvl = max(hist["high"])
        # self.highestPrice = self.breakoutlvl

        # define a small tolerance on our checks to avoid bouncing
        tolerance = float(1.0010)

        holdings = self.Portfolio[self.symbol].Quantity

        if holdings <= 0:
            # if the fast is greater than the slow, we'll go long
            if self.Securities[self.symbol].Price > self.fast.Current.Value and \
                    self.fast.Current.Value > self.mid.Current.Value * self.lookback and \
                    self.mid.Current.Value > self.slow.Current.Value * self.lookback:
                self.Log("BUY  >> {0}".format(self.Securities[self.symbol].Close))
                self.SetHoldings(self.symbol, 1)
                self.stopMarketTicket = self.StopMarketOrder(self.symbol, -1, self.initialStopRisk * self.Securities[self.symbol].Close,
                                                             "Stop Loss")

                self.Plot("Data Chart", "Stop Price", self.stopMarketTicket)
                self.Plot("Benchmark", "Stock Price", self.Securities[self.symbol].Close)

        # we only want to liquidate if we're currently long
        # if the fast is less than the slow we'll liquidate our long
        if holdings > 0 and self.vfast.Current.Value == self.fast.Current.Value:  # or \
            # self.Portfolio.TotalUnrealisedProfit >= self.Portfolio.TotalPortfolioValue * 1.05:
            self.Log("SELL >> {0}".format(self.Securities[self.symbol].Price))
            self.Liquidate(self.symbol, "Exit order")

        self.previous = self.Time

    def OnDataConsolidated(self, bar):
        self.currentBar = bar

    def OnOrderEvent(self, orderEvent):
        if orderEvent == OrderStatus.Filled:
            self.lastOrderEvent = orderEvent
            self.Debug(str(OrderId))