Last active
April 25, 2019 21:25
-
-
Save 2018kguo/6acc1f5dde35c5fef9c4b9aff224ef81 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(positions_data[symbol].get("quantity"))) | |
| r.order_sell_market(symbol, shares_owned) | |
| print("####### Selling " + str(shares_owned) + " shares of " + symbol + " #######") | |
| 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')) | |
| portfolio_value = float(profile_data.get('equity')) - cash | |
| ideal_position_size = (portfolio_value/len(holdings_data)+cash/len(potential_buys))/(2 * len(potential_buys)) | |
| prices = r.get_latest_price(potential_buys) | |
| for i in range(0, len(potential_buys)): | |
| stock_price = float(prices[i]) | |
| if(ideal_position_size < stock_price < ideal_position_size*1.5): | |
| num_shares = int(ideal_position_size*1.5/stock_price) | |
| elif (stock_price < ideal_position_size): | |
| num_shares = int(ideal_position_size/stock_price) | |
| print("####### Buying " + str(num_shares) + " shares of " + potential_buys[i] + " #######") | |
| r.order_buy_market(potential_buys[i], num_shares) | |
| def scan_stocks(): | |
| """ The main method. Sells stocks in your portfolio if their 50 day moving average crosses | |
| below the 200 day, and buys stocks in your watchlist if the opposite happens. | |
| ############################################################################################### | |
| WARNING: Comment out the sell_holdings and buy_holdings lines if you don't actually want to execute the trade. | |
| ############################################################################################### | |
| If you sell a stock, this updates tradehistory.txt with information about the position, | |
| how much you've earned/lost, etc. | |
| """ | |
| print("----- Starting scan... -----\n") | |
| register_matplotlib_converters() | |
| watchlist_symbols = get_watchlist_symbols() | |
| portfolio_symbols = get_portfolio_symbols() | |
| holdings_data = get_modified_holdings() | |
| potential_buys = [] | |
| sells = [] | |
| print("Current Portfolio: " + str(portfolio_symbols) + "\n") | |
| print("Current Watchlist: " + str(watchlist_symbols) + "\n") | |
| print("----- Scanning portfolio for stocks to sell -----\n") | |
| for symbol in portfolio_symbols: | |
| cross = golden_cross(symbol, n1=50, n2=200, days=30, direction="below") | |
| if(cross == -1): | |
| sell_holdings(symbol, holdings_data) | |
| sells.append(symbol) | |
| profile_data = r.build_user_profile() | |
| print("\n----- Scanning watchlist for stocks to buy -----\n") | |
| for symbol in watchlist_symbols: | |
| if(symbol not in portfolio_symbols): | |
| cross = golden_cross(symbol, n1=50, n2=200, days=10, direction="above") | |
| if(cross == 1): | |
| potential_buys.append(symbol) | |
| if(len(potential_buys) > 0): | |
| buy_holdings(potential_buys, profile_data, holdings_data) | |
| if(len(sells) > 0): | |
| update_trade_history(sells, holdings_data, "tradehistory.txt") | |
| print("----- Scan over -----\n") | |
| #execute the scan | |
| scan_stocks() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment