Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 2018kguo/47e7ff1a2be484f9a37fa3a3262fdac3 to your computer and use it in GitHub Desktop.
Save 2018kguo/47e7ff1a2be484f9a37fa3a3262fdac3 to your computer and use it in GitHub Desktop.
Buy all shares you can
def buy_holdings(potential_buys, profile_data, holdings_data):
""" Places orders to buy holdings of stocks. This method will try to order
an appropriate amount of shares such that your holdings of the stock will
roughly match the average for the rest of your portfoilio. If the share
price is too high considering the rest of your holdings and the amount of
buying power in your account, it will not order any shares.
Args:
potential_buys(list): List of strings, the strings are the symbols of stocks we want to buy
symbol(str): Symbol of the stock we want to sell
holdings_data(dict): dict obtained from r.build_holdings() or get_modified_holdings() method
"""
cash = float(profile_data.get('cash'))
prices = r.get_latest_price(potential_buys)
for i in range(0, len(potential_buys)):
stock_price = float(prices[i])
num_shares = int(cash/(stock_price*len(potential_buys))
if(num_shares <= 0):
print("####### Tried buying shares of " + potential_buys[i] + ", but not enough buying power to do so#######")
break
print("####### Buying " + str(num_shares) + " shares of " + potential_buys[i] + " #######")
r.order_buy_market(potential_buys[i], num_shares)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment