Overall Statistics
Total Orders
2
Average Win
0%
Average Loss
0%
Compounding Annual Return
0.797%
Drawdown
2.000%
Expectancy
0
Start Equity
100000
End Equity
102665.19
Net Profit
2.665%
Sharpe Ratio
-2.118
Sortino Ratio
-2.477
Probabilistic Sharpe Ratio
15.908%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0.01
Annual Variance
0
Information Ratio
0.543
Tracking Error
0.01
Treynor Ratio
0
Total Fees
$2.00
Estimated Strategy Capacity
$2300000.00
Lowest Capacity Asset
SPY R735QTJ8XC9X
Portfolio Turnover
0.01%
# region imports
from AlgorithmImports import *
# endregion

class InteractiveBrokersBrokerageExampleAlgorithm(QCAlgorithm):

    def initialize(self):
        self.set_start_date(2021, 1, 1)
        self.set_cash(100000)
        
        self.set_brokerage_model(BrokerageName.INTERACTIVE_BROKERS_BROKERAGE, AccountType.MARGIN)
        
        self._symbol = self.add_equity("SPY", Resolution.MINUTE, extended_market_hours = True).symbol
        
        # Set default order properites
        self.default_order_properties = InteractiveBrokersOrderProperties()
        self.default_order_properties.account = "TestAccount1"   # Only for FA accounts
        self.default_order_properties.fa_profile = "TestProfile1" # Only for FA accounts
        self.default_order_properties.fa_group = "TestGroup1"     # Only for FA accounts
        self.default_order_properties.fa_method = "EqualQuantity" # Only for FA accounts
        self.default_order_properties.time_in_force = TimeInForce.GOOD_TIL_CANCELED
        self.default_order_properties.outside_regular_trading_hours = False


    def on_data(self, data):
        if self.portfolio.invested:
            return
        
        # Place an order with the default order properties
        self.limit_order(self._symbol, 10, data[self._symbol].price + 10)
        
        # Place an order and with new order properties
        order_properties = InteractiveBrokersOrderProperties()
        order_properties.account = "TestAccount2"   # Only for FA accounts
        order_properties.fa_profile = "TestProfile2" # Only for FA accounts
        order_properties.fa_group = "TestGroup2"     # Only for FA accounts
        order_properties.fa_method = "NetLiq"        # Only for FA accounts
        order_properties.outside_regular_trading_hours = True
        ticket = self.limit_order(self._symbol, 10, round(data[self._symbol].price * .9, 2), order_properties = order_properties)
        
        # Update the order
        update_fields = UpdateOrderFields()
        update_fields.quantity = 8
        update_fields.limit_price = round(data[self._symbol].price + 10, 2)
        update_fields.tag = "Informative order tag"
        response = ticket.update(update_fields)
        if not self.live_mode and response.is_success:
            self.debug("Order updated successfully")