multiple strategies on multiple accounts

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.sec1 = symbol('CASH, EUR, USD')
    context.sec2 = symbol('CASH, USD, JPY')

def handle_data(context, data):
    handle_data_account1(context, data)
    handle_data_account2(context, data)

def handle_data_account1(context, data):
    hist = data.history(context.sec1, 50, '1d')
    ask_price = show_real_time_price(context.sec1, 'ask_price)
    if ask_price >= hist['close'].rolling(50).mean():
        orderId = order_target(context.sec1, 100, accountCode1)
        order_status_monitor(orderId, 'Filled')
    else:
        orderId = order_target(context.sec1, 0, accountCode1)
        order_status_monitor(orderId, 'Filled')

def handle_data_account2(context, data):
    hist = data.history(context.sec2, 100, '1d')
    ask_price = show_real_time_price(context.sec1, 'ask_price)
    if ask_price >= hist['close'].rolling(100).mean():
        orderId = order_target(context.sec1, 500, accountCode2)
        order_status_monitor(orderId, 'Filled')
    else:
        orderId = order_target(context.sec1, 0, accountCode2)
        order_status_monitor(orderId, 'Filled')

10 thoughts on “multiple strategies on multiple accounts”

  1. Hi,
    Is there a way to run multiple strategies in parallel with one account?
    If yes, how? If no, how to create new acount for the same user?

    Thanks

    1. Your trading decisions should be made in the function of `handle_data` or other functions that are scheduled by `schedule_function`. It means that you may put multiple strategies into different functions and schedule them using `schedule_function`. However, multiple strategies will be executed in one account and they need to access one account portfolio, which works like parallel programming and need some coding skills.

      1. multiple strategies in same file will be a chaos is there any way we can call multiple files from run_me.py and different frequency or bar size for each strategy

      2. Gold evening: is there a way to run more than one trading system at a time using a single account? Maybe is the same question of Amit. Starting from the fact that it’s impossible to code different trading systems into handle_data (the only way should be to use multiple RUN_ME.py files), if it’s not possible to trade multiple trading systems, iBridgePy cannot be a solid alternative to platform like Tradestation, Multicharts or Amibroker. Do you think this will be possible in a short time? Thanks!

  2. i want to use repBarFreq different for different strategies. is there any way to do that.while running multiple strategies at the same time for same account.

Leave a Comment