Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset

# Your New Python File
class CustomSlippageModel:
    def __init__(self, algorithm):
        self.algorithm = algorithm
        #self.sl = sl
        
    def GetSlippageApproximation(self, asset):
        slippage = asset.Price * d.Decimal(np.random.uniform(0.0,0.0002))
        #self.algorithm.Log("CustomSlippageModel: " + str(slippage))
        return slippage
        # custom slippage math
        #if self.sl == 1:
        #    slippage = asset.Price * d.Decimal(np.random.uniform(0.0,0.0002))
        #    self.algorithm.Log("CustomSlippageModel: " + str(slippage))
        #    return slippage
        #if self.sl == 2:
        #    slippage = asset.Price * d.Decimal(np.random.uniform(0.0002,0.0005))
        #    self.algorithm.Log("CustomSlippageModel: " + str(slippage))
        #    return slippage
        #if self.sl == 3:
        #    slippage = asset.Price * d.Decimal(np.random.uniform(0.0005,0.0008))
        #    self.algorithm.Log("CustomSlippageModel: " + str(slippage))
        #    return slippage
        #if self.sl == 4:
        #    slippage = asset.Price * d.Decimal(np.random.uniform(0.0008,0.001))
        #    self.algorithm.Log("CustomSlippageModel: " + str(slippage))
        #    return slippage
from System.Drawing import Color

class SMAStrategy(QCAlgorithm):

    def Initialize(self):
        
        self.SetStartDate(2000, 1, 1)  # Set Start Date
        self.SetEndDate(2021, 8, 31)  # Set End Date
        self.SetCash(100000)  # Set Strategy Cash
        
        self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin)
        
        self.spy = self.AddEquity("SPY", Resolution.Daily).Symbol
        
        lenght_fast = self.GetParameter("sma_lenght_fast")
        lenght_fast = 11 if lenght_fast is None else int(lenght_fast)
        lenght_slow = self.GetParameter("sma_lenght_slow")
        lenght_slow = 70 if lenght_slow is None else int(lenght_slow)
        
        self.max_leverage = 2.0       # Set total leverage you want
        self.qt_factor = self.GetParameter("qt")   
        self.qt_factor = 1.2 if self.qt_factor is None else float(self.qt_factor)# Set total leverage level you want to order each time
        
        self.sma_fast = self.SMA(self.spy, lenght_fast, Resolution.Daily)
        self.sma_slow = self.SMA(self.spy, lenght_slow, Resolution.Daily)
        
        self.tolerance = self.GetParameter("crossover_tolerance")
        self.tolerance = 0.0009 if self.tolerance is None else float(self.tolerance)
        #Set slippage model
        self.Securities[self.spy].SetSlippageModel(ConstantSlippageModel(0.0002))
        
        #Set Leverage interest model
        self.Securities[self.spy].MarginModel = BuyingPowerModel(requiredFreeBuyingPowerPercent = 0.05)
        
        self.SetWarmUp(timedelta(lenght_slow))

        self.Schedule.On(self.DateRules.On(2021,8, 31), self.TimeRules.At(15,45),
                        self.ExitPositions)
        
        stockPlot = Chart("Trade Plot")
        
        stockPlot.AddSeries(Series("Buy", SeriesType.Scatter, "$", Color.Green, ScatterMarkerSymbol.Triangle))
        stockPlot.AddSeries(Series("Liquidate", SeriesType.Scatter, "$", Color.Red, ScatterMarkerSymbol.TriangleDown))
        
        self.AddChart(stockPlot)

  
    def OnData(self, data):
        
        if not self.sma_slow.IsReady:
            return
        
        price = self.Securities[self.spy].Price
        
        self.Plot("Trade Plot", "Price", price)
        self.Plot("Trade Plot", "SMA", self.sma_slow.Current.Value)
        self.Plot("Trade Plot", "SMA", self.sma_fast.Current.Value)
        
        total_leverage = float(self.Portfolio.TotalPortfolioValue * 0.95) * self.max_leverage 
        qt = (total_leverage * (float(self.qt_factor)/self.max_leverage)) / price 
        
        
        if not self.Portfolio.Invested:
            if self.sma_fast.Current.Value > self.sma_slow.Current.Value * (1 + float(self.tolerance)):
                self.MarketOrder(self.spy, qt)
                self.Plot("Trade Plot", "Buy", price)
        else:
            if self.sma_fast.Current.Value < self.sma_slow.Current.Value * (1 - float(self.tolerance)):
                self.Liquidate()
                self.Plot("Trade Plot", "Liquidate", price)
        
        
    def ExitPositions(self):
        self.Liquidate()