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 accountCode1, 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_account1(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')

2 comments on “multiple strategies on multiple accounts
  1. linhui718611 says:

    Great,
    I want to run multiple files with multi accounts.
    Can you explain me how can implement?

  2. Amit.Kehat says:

    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

Leave a Reply

Your email address will not be published.