INNER CODE UNIT · Python
buy
tejaslinge/Alpaca-ROC-Trading-Bot · main.py:152
def buy(stock_to_buy: str):
cashBalance = api.get_account().cash
price_stock = api.get_latest_trade(str(stock_to_buy)).price
targetPositionSize = ((float(cashBalance)) / (price_stock)) # Calculates required position size
api.submit_order(str(stock_to_buy), targetPositionSize, "buy", "market", "day") # Market order to open position
mail_content = '''ALERT
BUY Order Placed for {}: {} Shares at ${}'''.format(stock_to_buy, targetPositionSize, price_stock)
if os.path.isfile('Orders.csv'):
df = pd.read_csv('Orders.csv')
df.drop(columns= 'Unnamed: 0', inplace = True)
df.loc[len(df.index)] = [((dt.now()).astimezone(timezone('America/New_York'))).strftime("%Y-%m-%d %H:%M:%S"), stock_to_buy, 'buy',
price_stock, targetPositionSize, targetPositionSize*price_stock, api.get_account().cash]
else:
df = pd.DataFrame()