Sample code: place orders and check how many shares in positions

Placing orders and checking how many shares in your account is a common step in algorithm trading programs.

This sample code is to provide users an example about placing orders and then checking how many shares of each position in the account. In the sample code, the first step is to clear all current positions in the account. Then, it places an order to buy 100 shares of AAPL. Finally, it checks how many shares of AAPL and GOOG (should be zero shares).

If you need help on coding any projects, please check out our well-known Rent-a-Coder service.

Rent-a-Coder

 

# -*- coding: utf-8 -*-
'''
There is a risk of loss when trading stocks, futures, forex, options and other
tradeable finacial instruments. Please trade with capital you can afford to 
lose. Past performance is not necessarily indicative of future results. 
Nothing in this computer program/code is intended to be a recommendation and/or 
solicitation to buy or sell any stocks or futures or options or any 
tradable securities/financial instruments. 
All information and computer programs provided here is for education and 
entertainment purpose only; accuracy and thoroughness cannot be guaranteed. 
Readers/users are solely responsible for how to use these information and 
are solely responsible any consequences of using these information.

If you have any questions, please send email to IBridgePy@gmail.com
'''

def initialize(context):
    context.GOOG = symbol('GOOG') # the stock of Alphabet Inc.
    context.AAPL = symbol('AAPL') # the stock of Apple Inc.
    
def handle_data(context, data):
    print('Start handle_data')
    print('Close all positions in the account')  
    
    # http://www.ibridgepy.com/ibridgepy-documentation/#close_all_positions
    # An IBridgePy function to close all positions in the account if there are
    # any positions. It will do nothing if there is no position.
    close_all_positions()
    
    # For the demostration purpose, IBridgePy will order 100 shares of AAPL
    # Then, follow up on the order status
    # IBridgePy will terminate itself if it does not receive the target status 
    # in 30 seconds
    orderId = order(context.AAPL, 100)
    order_status_monitor(orderId, target_status = 'Filled')
    
    # Check the position information
    # The account should have 100 shares of AAPL because the order was just placed
    # The account should not have any GOOG so the amount = 0
    print('exiting amount of GOOG=', context.portfolio.positions[context.GOOG].amount)        
    print('exiting amount of AAPL =', context.portfolio.positions[context.AAPL].amount)        
    
    display_all()
    end()


The typical results will look like the following.

At the beginning, there are a position of 100 shares of AAPL, so that IBridgePy closes this position.

Then, it places an order of 100 shares of AAPL as requested in the code.

Finally, it prints out the account information.

IBridgePy version 2.20180210
fileName = example.py
####    Starting to initialize trader    ####
##    ACCOUNT Balance  DU230626  ##
CASH=1121400.63
portfolio_value=1125736.09
positions_value=17322.0
##    POSITIONS DU230626   ##
Symbol Amount Cost_basis Latest_profit
STK,NASDAQ,SMART,AAPL,USD 100 173.05 nan
##    END    ##
##    NO any order    ##
####    Initialize trader COMPLETED    ####
Start handle_data
Close all positions in the account
IBridgePy.Trader_single_account::order_status_monitor: Filled SELL MKT 100 shares of STK,,SMART,AAPL,USD at 173.3
IBridgePy.Trader_single_account::order_status_monitor: Filled BUY MKT 100 shares of STK,,SMART,AAPL,USD at 173.32
('exiting amount of GOOG=', 0)
('exiting amount of AAPL =', 100)
##    ACCOUNT Balance  DU230626  ##
CASH=1121400.63
portfolio_value=1125736.09
positions_value=17322.0
##    POSITIONS DU230626   ##
Symbol Amount Cost_basis Latest_profit
STK,NASDAQ,SMART,AAPL,USD 100 173.32 nan
##    END    ##
##    Order Status DU230626   ##
reqId=297  Filled SELL MKT 100 shares of STK,,SMART,AAPL,USD at 173.3
reqId=298  Filled BUY MKT 100 shares of STK,,SMART,AAPL,USD at 173.32
##    END    ##
IBridgePy: Disconnect

Leave a Reply

Your email address will not be published.