Overall Statistics
Total Orders
41
Average Win
20.16%
Average Loss
-32.00%
Compounding Annual Return
36.688%
Drawdown
69.300%
Expectancy
0.379
Start Equity
100000
End Equity
281487.5
Net Profit
181.488%
Sharpe Ratio
0.755
Sortino Ratio
0.909
Probabilistic Sharpe Ratio
23.094%
Loss Rate
15%
Win Rate
85%
Profit-Loss Ratio
0.63
Alpha
0
Beta
0
Annual Standard Deviation
0.551
Annual Variance
0.304
Information Ratio
0.804
Tracking Error
0.551
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$6300000000.00
Lowest Capacity Asset
ES YJHOAMPYKQGX
Portfolio Turnover
8.27%
# region imports
from AlgorithmImports import *
# endregion

class TradingTechnologiesBrokerageExampleAlgorithm(QCAlgorithm):

    def initialize(self):
        self.set_start_date(2021, 1, 1)
        self.set_cash(100000)

        self.set_brokerage_model(BrokerageName.TRADING_TECHNOLOGIES, AccountType.MARGIN)
        
        
        self.continuous_contract = self.add_future(Futures.Indices.SP_500_E_MINI, Resolution.MINUTE, 
                                                  data_normalization_mode = DataNormalizationMode.BACKWARDS_RATIO,
                                                  data_mapping_mode = DataMappingMode.LAST_TRADING_DAY,
                                                  contract_depth_offset = 0)
        self.current_contract = None

        # Set default order properties
        self.default_order_properties.time_in_force = TimeInForce.DAY
        
    def get_target_price(self, contract, factor):
        target_price = contract.price * factor
        inverse_price_variation = 1 / contract.symbol_properties.minimum_price_variation
        return round(target_price * inverse_price_variation)/inverse_price_variation

    def on_data(self, data):
        if not self.portfolio.invested:
            self.current_contract = self.securities[self.continuous_contract.mapped]
            
            # Place an order with the default order properties  
            self.market_order(self.current_contract.symbol, 1)
            
            # Place an order with new order properties
            order_properties = OrderProperties()
            order_properties.time_in_force = TimeInForce.GOOD_TIL_CANCELED
            limit_price = self.get_target_price(self.current_contract, 0.9)
            ticket = self.limit_order(self.current_contract.symbol, 1, limit_price, order_properties = order_properties)
        
            # Update the order
            update_fields = UpdateOrderFields()
            update_fields.quantity = 2
            update_fields.limit_price = self.get_target_price(self.current_contract, 1.05)
            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")
        
        # Rollover to the next contract
        elif self.current_contract is not None and self.current_contract.symbol != self.continuous_contract.mapped:
            self.log(f"{self.time} - rolling position from {self.current_contract.symbol} to {self.continuous_contract.mapped}")
        
            current_position_size = self.current_contract.holdings.quantity
            self.liquidate(self.current_contract.symbol)
            self.market_order(self.continuous_contract.mapped, current_position_size)
            
            self.current_contract = self.securities[self.continuous_contract.mapped]