Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
2.345%
Drawdown
0.000%
Expectancy
0
Net Profit
0.049%
Sharpe Ratio
14.603
Probabilistic Sharpe Ratio
100.000%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.014
Beta
0.019
Annual Standard Deviation
0.002
Annual Variance
0
Information Ratio
-23.19
Tracking Error
0.104
Treynor Ratio
1.677
Total Fees
$1.00
Estimated Strategy Capacity
$6300000000.00
class LimitOrderTest(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2021, 4, 1)  # Set Start Date
        self.SetEndDate(2021, 4, 8)  # Set End Date
        self.SetCash(100000)  # Set Strategy Cash
        self.AddEquity("SPY", Resolution.Second) # Finer resolution for more realistic fills
        self._pending_order = None
        self.limitPrice = 405
        self.orderStatus = None
        self.DefaultOrderProperties.TimeInForce = TimeInForce.GoodTilCanceled

        self.Consolidate("SPY", timedelta(days=1), self.OnDataConsolidated)

    def OnData(self, data): 
        
        if self.orderStatus == "Filled":
            self.orderStatus = None
            self.Log("Order filled. BAR DATA: {}: Open:{}, Close:{}, High:{}, Low:{}".format(data['SPY'].EndTime, data['SPY'].Open, data['SPY'].Close, data['SPY'].High, data['SPY'].Low))
        
        if self.Portfolio.Invested:
            return
        
        if self._pending_order is not None and data['SPY'].Low < self.limitPrice:
                self.Log("Limit order should fill. {}: Open:{}, Close:{}, High:{}, Low:{}, Limit price: {}".format(data['SPY'].EndTime, data['SPY'].Open, data['SPY'].Close, data['SPY'].High, data['SPY'].Low, self.limitPrice))
            
    def OnDataConsolidated(self, bar):
        
        self.Log("DAILY DATA: {}: Open:{}, Close:{}, High:{}, Low:{}".format(bar.EndTime, bar.Open, bar.Close, bar.High, bar.Low))
        
        if not self.Portfolio.Invested and self._pending_order is None:
            self._pending_order = self.LimitOrder("SPY", 10, self.limitPrice) 
    
    def OnOrderEvent(self, orderEvent):
        
        self.Log(str("OnOrderEvent has been called: ") + str(orderEvent))
        self._pending_order = None
        
        if orderEvent.Status == OrderStatus.Filled:
            self.orderStatus = "Filled"