Overall Statistics
Total Trades
63
Average Win
0.10%
Average Loss
-0.07%
Compounding Annual Return
-2.223%
Drawdown
1.800%
Expectancy
0.000
Net Profit
-0.189%
Sharpe Ratio
-0.463
Probabilistic Sharpe Ratio
33.686%
Loss Rate
60%
Win Rate
40%
Profit-Loss Ratio
1.48
Alpha
-0.088
Beta
0.126
Annual Standard Deviation
0.044
Annual Variance
0.002
Information Ratio
-4.768
Tracking Error
0.117
Treynor Ratio
-0.162
Total Fees
$63.27
import decimal as d
import numpy as np

class OptimizedCalibratedFlange(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2013,10,1)   # Set Start Date
        self.SetEndDate(2013,10,31)    # Set End Date
        self.SetSecurityInitializer(self.CustomSecurityInitializer)
        self.security = self.AddEquity("SPY", Resolution.Hour)
        self.spy = self.security.Symbol

    def OnData(self, data):
        open_orders = self.Transactions.GetOpenOrders(self.spy)
        if len(open_orders) != 0: return

        if self.Time.day > 10 and self.security.Holdings.Quantity <= 0:
            quantity = self.CalculateOrderQuantity(self.spy, .5)
            self.Log("MarketOrder: " + str(quantity))
            self.MarketOrder(self.spy, quantity, True)   # async needed for partial fill market orders
        
        elif self.Time.day > 20 and self.security.Holdings.Quantity >= 0:
            quantity = self.CalculateOrderQuantity(self.spy, -.5)
            self.Log("MarketOrder: " + str(quantity))
            self.MarketOrder(self.spy, quantity, True)   # async needed for partial fill market orders

    def CustomSecurityInitializer(self, security):
        self.Debug("CustomSecurityInitializer called")
        '''Initialize the security with raw prices'''
        security.SetSlippageModel(CustomSlippageModel(self))
        security.SetFeeModel(CustomFeeModel(self))
        
class CustomFeeModel:
    def __init__(self, algorithm):
        self.algorithm = algorithm

    def GetOrderFee(self, security, order):
        self.algorithm.Debug("GetOrderFee called")
        fee = max(1, security.Price * order.AbsoluteQuantity * d.Decimal(0.00001))
        self.algorithm.Log("CustomFeeModel: " + str(fee))
        return fee

        
class CustomSlippageModel:
    def __init__(self, algorithm):
        self.algorithm = algorithm
        
    def GetSlippageApproximation(self, asset, order):
        slippage = asset.Price * d.Decimal(0.0001 * np.log10(2*float(order.AbsoluteQuantity)))
        self.algorithm.Log("CustomSlippageModel: " + str(slippage))
        return slippage