├── README.md ├── Order Management ├── example_place_market_and_limit_order.py ├── example_get_order.py ├── example_get_open_order_by_security.py └── example_get_all_open_order.py └── SMA Strategy └── example_moving_average_cross.py /README.md: -------------------------------------------------------------------------------- 1 | # automated-trading-with-ibridgepy-using-interactive-brokers-platform -------------------------------------------------------------------------------- /Order Management/example_place_market_and_limit_order.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Wed Mar 14 11:49:59 2018 4 | 5 | @author: Quantra 6 | Tested on Python Version 2.7 7 | """ 8 | 9 | def initialize(context): 10 | context.place_order=True 11 | 12 | def handle_data(context, data): 13 | if context.place_order: 14 | # Market Order to buy 2 shares of AAPL 15 | order(symbol('AAPL'), 2) 16 | 17 | # Market Order to sell 1 share of AAPL 18 | order(symbol('AAPL'), 1) 19 | 20 | # Limit Order to buy 2 shares of AAPL at limit price of 170 21 | order(symbol('AAPL'), 2, style=LimitOrder(170)) 22 | 23 | # Limit Order to sell 2 shares of AAPL at limit price of 180 24 | order(symbol('AAPL'), -2, style=LimitOrder(180)) 25 | 26 | context.place_order=False 27 | 28 | else: 29 | time.sleep(10) 30 | display_all() 31 | end() 32 | -------------------------------------------------------------------------------- /Order Management/example_get_order.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Wed Mar 14 11:49:59 2018 4 | 5 | @author: Quantra 6 | Tested on Python Version 2.7 7 | """ 8 | 9 | def initialize(context): 10 | context.place_order = True 11 | 12 | def handle_data(context, data): 13 | if context.place_order == True: 14 | context.orderId = order(symbol('AAPL'), 20, style=MarketOrder()) 15 | context.place_order = False 16 | 17 | order_aapl = context.portfolio.orderStatusBook[context.orderId] 18 | message = '{status}: {filled}/{amount} shares filled' 19 | print message.format(status=order_aapl.status, 20 | amount=order_aapl.amount, 21 | filled=order_aapl.filled) 22 | 23 | if order_aapl.status == 'Filled': 24 | print "The order is filled with average price of %s price" % (order_aapl.avgFillPrice) 25 | end() 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /Order Management/example_get_open_order_by_security.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Wed Mar 14 11:49:59 2018 4 | 5 | @author: Quantra 6 | Tested on Python Version 2.7 7 | """ 8 | 9 | def initialize(context): 10 | context.place_order = True 11 | 12 | def handle_data(context, data): 13 | if context.place_order == True: 14 | order(symbol('AAPL'), 20, style=LimitOrder(175)) 15 | order(symbol('AAPL'), -10, style=LimitOrder(180)) 16 | context.place_order = False 17 | 18 | # Get open orders for a AAPL 19 | open_orders = get_open_orders(symbol('AAPL')) 20 | if open_orders: 21 | print "Open Order(s)" 22 | # iterate over the open orders 23 | for oo in open_orders: 24 | message = 'AAPL: {filled}/{amount} shares filled' 25 | print message.format(amount=oo.amount, filled=oo.filled) 26 | else: 27 | print "No Open Orders" 28 | end() 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /Order Management/example_get_all_open_order.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Wed Mar 14 11:49:59 2018 4 | 5 | @author: Quantra 6 | Tested on Python Version 2.7 7 | """ 8 | 9 | def initialize(context): 10 | context.place_order = True 11 | 12 | def handle_data(context, data): 13 | if context.place_order == True: 14 | order(symbol('AAPL'), 20, style=LimitOrder(175)) 15 | order(symbol('AMZN'), -2, style=LimitOrder(1600)) 16 | context.place_order = False 17 | 18 | # open_orders is a dictionary 19 | # keyed by security object, 20 | # with values that are lists of orders object. 21 | open_orders = get_open_orders() 22 | 23 | if open_orders: 24 | # iterate over the dictionary 25 | # iterate over each securities 26 | for security in open_orders: 27 | print "Open Orders for %s" % (security.symbol) 28 | # iterate over the open orders 29 | for oo in open_orders[security]: 30 | message = '{filled}/{amount} shares filled' 31 | print message.format(amount=oo.amount, filled=oo.filled) 32 | 33 | else: 34 | print "No Open Orders" 35 | end() 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /SMA Strategy/example_moving_average_cross.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | ''' 3 | There is a risk of loss when trading stocks, futures, forex, options and other 4 | tradeable finacial instruments. Please trade with capital you can afford to 5 | lose. Past performance is not necessarily indicative of future results. 6 | Nothing in this computer program/code is intended to be a recommendation and/or 7 | solicitation to buy or sell any stocks or futures or options or any 8 | tradable securities/financial instruments. 9 | All information and computer programs provided here is for education and 10 | entertainment purpose only; accuracy and thoroughness cannot be guaranteed. 11 | Readers/users are solely responsible for how to use these information and 12 | are solely responsible any consequences of using these information. 13 | 14 | If you have any questions, please send email to IBridgePy@gmail.com 15 | ''' 16 | 17 | import pandas as pd 18 | 19 | def initialize(context): 20 | context.run_once=False # To show if the handle_data has been run in a day 21 | context.security=symbol('AAPL') # Define a security for the following part 22 | 23 | def handle_data(context, data): 24 | sTime=get_datetime() 25 | # sTime is the IB server time. 26 | # get_datetime() is the build-in fuciton to obtain IB server time 27 | if sTime.weekday()<=4: 28 | # Only trade from Mondays to Fridays 29 | if sTime.hour==13 and sTime.minute==35 and context.run_once==True: 30 | # 2 minutes before the market closes, reset the flag 31 | # get ready to trade 32 | context.run_once=False 33 | if sTime.hour==13 and sTime.minute==36 and context.run_once==False: 34 | # 1 minute before the market closes, do moving average calcualtion 35 | # if MA(5) > MA(15), then BUY the security if there is no order 36 | # Keep the long positions if there is a long position 37 | # if MA(5) < MA(15), clear the position 38 | data=request_historical_data(context.security, '1 day', '20 D') 39 | mv_5=data.close.rolling(5).mean().iloc[-1] 40 | mv_15=data.close.rolling(15).mean().iloc[-1] 41 | if mv_5>mv_15: 42 | orderId=order_target(context.security, 100) 43 | order_status_monitor(orderId, target_status='Filled') 44 | else: 45 | orderId=order_target(context.security, 0) 46 | order_status_monitor(orderId, target_status='Filled') 47 | context.run_once=True 48 | # end() --------------------------------------------------------------------------------