INNER CODE UNIT · Python
sell
tejaslinge/Alpaca-ROC-Trading-Bot · main.py:176
def sell(current_stock):
# sells current_stock
quantity = float(api.get_position(str(current_stock)).qty)
sell_price = api.get_latest_trade(str(current_stock)).price
api.cancel_all_orders() # cancels all pending (to be filled) orders
api.close_position(str(current_stock)) # sells current stock
mail_content = '''ALERT
SELL Order Placed for {}: {} Shares at ${}'''.format(current_stock, quantity, sell_price)
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"), current_stock, 'sell', sell_price, quantity, quantity*sell_price, api.get_account().cash]
# with open('Orders.csv', 'a') as f:
# df.to_csv(f, header=f.tell()==0)
df.to_csv('Orders.csv')