def sell_shares(symbol, amount): print(f'\nSelling {amount} share(s) from {symbol}') print('------------------------------------') try: bot_trader.order_sell_market(symbol=symbol, quantity=amount) except Exception as holdings_err: exception_log(holdings_err) print('------------------------------------\n')
def sell_holdings(symbol, holdings_data): """ Place an order to sell all holdings of a stock. Args: symbol(str): Symbol of the stock we want to sell holdings_data(dict): dict obtained from get_modified_holdings() method """ shares_owned = int(float(holdings_data[symbol].get("quantity"))) r.order_sell_market(symbol, shares_owned) print("####### Selling " + str(shares_owned) + " shares of " + symbol + " #######")
def sell_short(self, stock): if self.my_stocks: try: for i in self.my_stocks: if i == stock: bal = float(i['quantity']) r.order_sell_market(stock, bal) print('sold ' + str(bal) + ' : ' + stock) self.my_stocks = r.build_holdings() except Exception as e: print(e)
def sellStock(stock, shareAmount): try: rStocks.order_sell_market(stock, shareAmount) print(str(shareAmount) + ' shares of ' + str(stock).upper() + ' sold!') except Exception as e: print('Log in failed...') if hasattr(e, 'message'): print(e.message) else: print(e) pass
def marketSell(self, stock, quantity): # submit a sell order at market price try: robin.order_sell_market(stock, quantity) except Exception as ex: logger.logError('Unable to sell %s with market sell. \nError: %s' % (stock, ex)) else: logger.logInfo( 'Successfully purchased %i shares of %s with market sell' % (quantity, stock))
def prompt_user(action, ticker, current_price): if action == "BUY": answer = None while answer not in ("y", "n"): answer = input("Proceed with " + action + "? (y/n) ") if answer == "y": #buy qty of specified stock from Robinhood try: robin_stocks.order_buy_market( ticker, share_qty) #place the buy in Robinhood print("Bought " + str(share_qty) + " share(s) of " + ticker + " on " + str(date.today()) + " at $" + str(current_price)) append_to_log(action, "COMPLETED", ticker, current_price) except Exception as err: print('Error ' + action.lower() + 'ing ' + ticker + ": " + err) append_to_log(action, "ERROR: " + err, ticker, current_price) elif answer == "n": append_to_log(action, "MANUALLY CANCELLED", ticker, current_price) else: print("Please enter y or n") elif action == "SELL": answer = None while answer not in ("y", "n"): answer = input("Proceed with " + action + "? (y/n) ") if answer == "y": #sell qty of specified stock from Robinhood try: robin_stocks.order_sell_market( ticker, share_qty) #place the sell in Robinhood print("Sold " + str(share_qty) + " share(s) of " + ticker + " on " + str(date.today()) + " at $" + str(current_price)) append_to_log(action, "COMPLETED", ticker, current_price) except Exception as err: print('Error ' + action.lower() + 'ing ' + ticker + ": " + err) append_to_log(action, "ERROR: " + err, ticker, current_price) elif answer == "n": append_to_log(action, "MANUALLY CANCELLED", ticker, current_price) else: print("Please enter y or n")
def sell(quantity, symbol, limit): if limit is not None: ui.success(f"\nSelling {quantity} of {symbol} at ${limit}\n") result = rh.order_sell_limit(symbol, quantity, limit) else: ui.success(f'\nSelling {quantity} of {symbol} at market price....\n') result = rh.order_sell_market(symbol, quantity) ui.chec_ref(result)
def sell(quantity, symbol, limit): if limit is not None: ui.success("Selling {} quantity of {} at {}".format(quantity, symbol, limit)) result = rh.order_buy_limit(symbol, quantity, limit) else: ui.success("Selling {} quantity of {} at market price".format(quantity, symbol)) result = rh.order_sell_market(symbol, quantity) ui.success(result)
def sell(quantity, symbol, limit): if limit is not None: ui.success("Selling {} of {} at {}".format(quantity, symbol, limit)) result = rh.order_sell_limit(symbol, quantity, limit) else: # market order ui.success("Selling {} of {} at market price".format(quantity, symbol)) result = rh.order_sell_market(symbol, quantity) if 'ref_id' in result: ui.success(result) else: ui.error(result)
def sell(quantity,symbol,limit): if limit is not None: click.echo(click.style("Selling {} of {} at ${}".format(quantity,symbol,limit), fg = "green", bold = True)) result = rh.order_sell_limit(symbol,quantity,limit) else: click.echo(click.style("Selling {} of {} at market value".format(quantity,symbol), fg = "green", bold = True)) result = rh.order_sell_market(symbol,quantity) if 'detail' in result: red_msg = click.style("ERROR",fg="red",blink = True,bold = True) print(red_msg) else: click.style(str(result),fg = "green",bold = True)
def sell(quantity, symbol, limit=None): content = open('config.json').read() config = json.loads(content) rh.login(config['username'], config['password']) if limit is not None: ui.success("Selling {} of {} at ${}".format(quantity, symbol, limit)) result = rh.order_sell_limit(symbol, quantity, limit) else: ui.success("Selling {} of {}".format(quantity, symbol)) result = rh.order_sell_market(symbol, quantity) ui.success(result) if 'detail' in result: ui.error(result) else: ui.success(result)
def sell_shares_at_market_price(symbol: str, quantity: int) -> None: order_sell_market(symbol=symbol, quantity=quantity)
def order_sell_market(self, symbol, quantity): sell_status = r.order_sell_market(symbol, quantity) self.my_stocks = self.refresh_holdings() return sell_status
rate20_Buy = his20 / cur if (rate20_Buy >= 1.02 and already_Buy20 == False): # buy when DOWN hold_price20 = cur # update hold robin_stocks.order_buy_market('SAVE', V) # buy! already_Buy20 = True # update flag print('buy - 20 --> ' + str(datetime.datetime.now())) print('rate20_Buy ' + str(round(rate20_Buy, 4))) rate20_Sell = cur / hold_price20 if ((rate20_Sell >= 1.01) and already_Buy20 == True): # sell when up 0.1 print('hold_price20: ' + str(hold_price20)) robin_stocks.order_sell_market('SAVE', V) already_Buy20 = False print('sell - 20 --> ' + str(datetime.datetime.now())) print('Earning!!! --> ' + str(cur - hold_price20)) hold_price20 = 77777 print('rate20_Sell ' + str(round(rate20_Sell, 4)) + ' Cur: ' + str(cur) + ' His: ' + str(hold_price2)) print('his20 price: ' + str(his20)) print('--5--') ## ************************ Get 5 minutes history ************************ file.close file = open('2020_0521_SAVE.csv', 'r') his5 = getTail(file, 5) his5 = his5[2:]
def main(): #log start of daily run append_to_log("DAILY RUN", "STARTED", "N/A", "N/A") #load monitored stock tickers from CSV file with open(mt_file, 'r') as ticker_file: csv_reader = csv.reader(ticker_file) for row in csv_reader: monitored_tickers.append(row[0]) #iterate through each ticker in array and run daily checks for ticker in monitored_tickers: #set variables day_emas = [] week_emas = [] keys = [] vals = [] intersections = [] sell_triggers = [] buy_triggers = [] #get current stock price current_price = si.get_live_price(ticker) print("_____________________ " + ticker + " - [" + str(round(current_price, 2)) + "]") #get actual price actual_data, meta_data = ts.get_weekly(symbol=ticker) #get day ema day_ema, meta_deta = ti.get_ema(symbol=ticker, interval='daily') #get weekly ema weekly_ema, meta_deta = ti.get_ema(symbol=ticker, interval='weekly') for val in day_ema.iterrows(): day_emas.append([val[0], val[1].values]) for val in weekly_ema.iterrows(): week_emas.append([val[0], val[1].values]) for day in day_emas: for week in week_emas: if week[0] == day[0]: #print("Date: " + str(day[0]) + " : day ema Price: " + str(day[1][0]) + " : week ema Price: " + str(week[1][0])) keys.append([day[0], day[1][0], week[1][0], ticker]) vals.append(day[1][0] - week[1][0]) for i, v in enumerate(vals): if i > 0: if v / vals[i - 1] < 0: intersections.append(keys[i]) #print("Intersection Happened: " + str(keys[i])) #decide if the intersection triggers a buy or sell #if the week ema is less than the month ema, sell; if the week ema is more than the month ema, buy for intersect in intersections: if intersect[1] < intersect[2]: sell_triggers.append(intersect) elif intersect[1] > intersect[2]: buy_triggers.append(intersect) print("____________________ v BUY TRIGGERS v ____________________") for buy in buy_triggers: print("Buy Trigger: " + str(buy)) print("____________________ ^ BUY TRIGGERS ^ ____________________") print("____________________ v SELL TRIGGERS v ____________________") for sell in sell_triggers: print("Sell Trigger: " + str(sell)) print("____________________ ^ SELL TRIGGERS ^ ____________________") try: #if the latest buy trigger is today's date, place Robinhood order if str(date.today()) == str( buy_triggers[-1][0].to_pydatetime())[:10]: print("##### STOCK BUY HAS BEEN TRIGGERED #####") action = "BUY" append_to_log(action, "STARTED", ticker, current_price) #log into Robinhood rs_login() #plot data with matplotlib generate_plot(actual_data, day_ema, weekly_ema, ticker, action) #confirm action with user #prompt_user(action, ticker, current_price) #buy qty of specified stock from Robinhood try: robin_stocks.order_buy_market( ticker, share_qty) #place the buy in Robinhood append_to_log(action, "COMPLETED", ticker, current_price) except Exception as err: print('Error ' + action.lower() + 'ing ' + ticker + ": " + err) append_to_log(action, "ERROR: " + err, ticker, current_price) #if the latest sell trigger is today's date, sell shares of that stock elif str(date.today()) == str( sell_triggers[-1][0].to_pydatetime())[:10]: print("##### STOCK SELL HAS BEEN TRIGGERED #####") action = "SELL" append_to_log(action, "STARTED", ticker, current_price) #log into Robinhood rs_login() #plot data with matplotlib generate_plot(actual_data, day_ema, weekly_ema, ticker, action) #confirm action with user #prompt_user(action, ticker, current_price) #sell qty of specified stock from Robinhood try: robin_stocks.order_sell_market( ticker, share_qty) #place the sell in Robinhood append_to_log(action, "COMPLETED", ticker, current_price) except Exception as err: print('Error ' + action.lower() + 'ing ' + ticker + ": " + err) append_to_log(action, "ERROR: " + err, ticker, current_price) except Exception as err: print(err) #generate_plot(actual_data, day_ema, weekly_ema, ticker, "RUN") time.sleep( 60 ) #sleep for a minute to wait out the query limit on the free AlphaVantage API #log completion of daily run append_to_log("DAILY RUN", "COMPLETED", "N/A", "N/A") '''
def sell(self, symbol, quantity): quantity = super().sell(symbol, quantity, receipt=True) if quantity > 0: result = r.order_sell_market(symbol, quantity) print(result["state"]) return quantity