Exclusive 22% OFF All Quantra by QuantInsti Courses for IBridgePy Users Master algorithmic trading from beginner to advanced — Python for trading, machine learning strategies, options trading, and more.
HUI22 Use HUI22 for 22% off
HUI7 Stack HUI7 for an additional 7% off
Browse Courses →
← Back to Blog

multiple strategies on multiple accounts

October 17, 2018

Running multiple strategies accounts is a critical capability for professional traders and fund managers. This advanced feature allows you to implement different multiple strategies accounts simultaneously across various portfolios with Interactive Brokers and Python.

multiple strategies accounts

Implementing Different Strategies Per Account

This is a sample code to demonstrate how IBridgePy for Multi Account implement different trading strategies on multiple accounts. The sample code assumes that two IB accounts (accountCode1 and accountCode2) are linked to one login credential.

For the accountCode1, the strategy is to maintain a long position of 100 shares of Forex: CASH,EUR,USD when the current ask price is higher than moving average of 50 and hold no position when the current ask price is lower than moving average of 50.

For the accountCode2, the strategy is to maintain a long position of 500 shares of Forex: CASH,USD,JPY when the current ask price is higher than moving average of 100 and hold no position when the current ask price is lower than moving average of 100.

 
def initialize(context):
    context.security1 = symbol('EUR')
    context.security2 = symbol('JPY')
    
def handle_data(context, data):
    # Strategy for Account 1
    hist1 = data.history(context.security1, 'close', 50, '1d')
    ma50 = hist1.mean()
    current_price1 = data.current(context.security1, 'ask')
    
    if current_price1 > ma50:
        order_target('accountCode1', context.security1, 100)
    else:
        order_target('accountCode1', context.security1, 0)
    
    # Strategy for Account 2
    hist2 = data.history(context.security2, 'close', 100, '1d')
    ma100 = hist2.mean()
    current_price2 = data.current(context.security2, 'ask')
    
    if current_price2 > ma100:
        order_target('accountCode2', context.security2, 500)
    else:
        order_target('accountCode2', context.security2, 0)

For more information on multi-account management, visit our tutorials or check our documentation.