def run_bot(args): # Initialize flags # risk r = None # default_stop d = None # min_share_price n = None # max_share_price x = None # min_last_dv l = None # verbose for debugging verbose = True # Check to make sure parameters are correct, meets minimum amount # if(len(sys.argv) < 4): # raise IOError("trading_bot -flags <base_url> <api_key_id> <api_secret_key> ") # Checks if there is flags j = 1 if (len(sys.argv) != 4): for i in range(1, len(sys.argv)): chars = split(sys.argv[i]) if chars[0] is '-': j = i + 2 if chars[1] is 'r': r = sys.argv[i + 1] elif chars[1] is 'd': d = sys.argv[i + 1] elif chars[1] is 'n': n = sys.argv[i + 1] elif chars[1] is 'x': x = sys.argv[i + 1] elif chars[1] is 'l': l = sys.argv[i + 1] elif chars[1] is 'v': verbose = True j = i + 1 # Replace these with your API connection info from preset info f = open('.alpaca_profile.txt', 'r') data = json.load(f) base_url = data['base-url'] api_key_id = data['api-key'] api_secret = data['secret-key'] if verbose: print('API connection info is set.') api = tradeapi.REST(base_url=base_url, key_id=api_key_id, secret_key=api_secret) session = requests.session() # Initialize parameters min_share_price = 0 max_share_price = 0 min_last_dv = 0 default_stop = 0 risk = 0 # We only consider stocks with per-share prices inside this range if n is None: min_share_price = 2.0 if verbose: print('The minimum share price is set to the default, ', min_share_price) else: min_share_price = float(n) if verbose: print('The minimum share price is custom, ', min_share_price) if x is None: max_share_price = 13.0 if verbose: print('The maximum share price is set to the default, ', max_share_price) else: max_share_price = float(x) if verbose: print('The maximum share price is custom, ', max_share_price) # Minimum previous-day dollar volume for a stock we might consider if l is None: min_last_dv = 500000 if verbose: print( 'The minimum previous-day dollar volume for a stock is set to the default, ', min_last_dv) else: min_last_dv = float(l) if verbose: print( 'The minimum previous-day dollar volume for a stock is custom, ', min_last_dv) # Stop limit to default to if d is None: default_stop = .95 if verbose: print('The stop limit is set to the default, ', default_stop) else: default_stop = float(d) if verbose: print('The stop limit is set custom, ', default_stop) # How much of our portfolio to allocate to any one position if r is None: risk = 0.01 if verbose: print( 'The percent of portfolio to allocate to one position is set to the default, ', risk) else: risk = float(r) if verbose: print( 'The percent of portfolio to allocate to one position is set custom, ', risk) def get_1000m_history_data(symbols): print('Getting historical data...') minute_history = {} c = 0 for symbol in symbols: minute_history[symbol] = api.polygon.historic_agg(size="minute", symbol=symbol, limit=1000).df c += 1 print('{}/{}'.format(c, len(symbols))) print('Success.') return minute_history def get_tickers(): print('Getting current ticker data...') tickers = api.polygon.all_tickers() print('Success.') assets = api.list_assets() symbols = [asset.symbol for asset in assets if asset.tradable] return [ ticker for ticker in tickers if (ticker.ticker in symbols and ticker.lastTrade['p'] >= min_share_price and ticker.lastTrade['p'] <= max_share_price and ticker.prevDay['v'] * ticker.lastTrade['p'] > min_last_dv and ticker.todaysChangePerc >= 3.5) ] def find_stop(current_value, minute_history, now): series = minute_history['low'][-100:] \ .dropna().resample('5min').min() series = series[now.floor('1D'):] diff = np.diff(series.values) low_index = np.where((diff[:-1] <= 0) & (diff[1:] > 0))[0] + 1 if len(low_index) > 0: return series[low_index[-1]] - 0.01 return current_value * default_stop def run(tickers, market_open_dt, market_close_dt): # Establish streaming connection conn = tradeapi.StreamConn(base_url=base_url, key_id=api_key_id, secret_key=api_secret) # Update initial state with information from tickers volume_today = {} prev_closes = {} for ticker in tickers: symbol = ticker.ticker prev_closes[symbol] = ticker.prevDay['c'] volume_today[symbol] = ticker.day['v'] symbols = [ticker.ticker for ticker in tickers] print('Tracking {} symbols.'.format(len(symbols))) minute_history = get_1000m_history_data(symbols) portfolio_value = float(api.get_account().portfolio_value) open_orders = {} positions = {} # Cancel any existing open orders on watched symbols existing_orders = api.list_orders(limit=500) for order in existing_orders: if order.symbol in symbols: api.cancel_order(order.id) stop_prices = {} latest_cost_basis = {} # Track any positions bought during previous executions existing_positions = api.list_positions() for position in existing_positions: if position.symbol in symbols: positions[position.symbol] = float(position.qty) # Recalculate cost basis and stop price latest_cost_basis[position.symbol] = float(position.cost_basis) stop_prices[position.symbol] = (float(position.cost_basis) * default_stop) # Keep track of what we're buying/selling target_prices = {} partial_fills = {} # Use trade updates to keep track of our portfolio @conn.on(r'trade_update') async def handle_trade_update(conn, channel, data): symbol = data.order['symbol'] last_order = open_orders.get(symbol) if last_order is not None: event = data.event if event == 'partial_fill': qty = int(data.order['filled_qty']) if data.order['side'] == 'sell': qty = qty * -1 positions[symbol] = (positions.get(symbol, 0) - partial_fills.get(symbol, 0)) partial_fills[symbol] = qty positions[symbol] += qty open_orders[symbol] = data.order elif event == 'fill': qty = int(data.order['filled_qty']) if data.order['side'] == 'sell': qty = qty * -1 positions[symbol] = (positions.get(symbol, 0) - partial_fills.get(symbol, 0)) partial_fills[symbol] = 0 positions[symbol] += qty open_orders[symbol] = None elif event == 'canceled' or event == 'rejected': partial_fills[symbol] = 0 open_orders[symbol] = Noneta @conn.on(r'A$') async def handle_second_bar(conn, channel, data): symbol = data.symbol # First, aggregate 1s bars for up-to-date MACD calculations ts = data.start ts -= timedelta(seconds=ts.second, microseconds=ts.microsecond) try: current = minute_history[data.symbol].loc[ts] except KeyError: current = None new_data = [] if current is None: new_data = [ data.open, data.high, data.low, data.close, data.volume ] else: new_data = [ current.open, data.high if data.high > current.high else current.high, data.low if data.low < current.low else current.low, data.close, current.volume + data.volume ] minute_history[symbol].loc[ts] = new_data # Next, check for existing orders for the stock existing_order = open_orders.get(symbol) if existing_order is not None: # Make sure the order's not too old submission_ts = existing_order.submitted_at.astimezone( timezone('America/New_York')) order_lifetime = ts - submission_ts if order_lifetime.seconds // 60 > 1: # Cancel it so we can try again for a fill api.cancel_order(existing_order.id) return # Now we check to see if it might be time to buy or sell since_market_open = ts - market_open_dt until_market_close = market_close_dt - ts if (since_market_open.seconds // 60 > 15 and since_market_open.seconds // 60 < 60): # Check for buy signals # See if we've already bought in first position = positions.get(symbol, 0) if position > 0: return # See how high the price went during the first 15 minutes lbound = market_open_dt ubound = lbound + timedelta(minutes=15) high_15m = 0 try: high_15m = minute_history[symbol][lbound:ubound][ 'high'].max() except Exception as e: # Because we're aggregating on the fly, sometimes the datetime # index can get messy until it's healed by the minute bars return # Get the change since yesterday's market close daily_pct_change = ((data.close - prev_closes[symbol]) / prev_closes[symbol]) if (daily_pct_change > .04 and data.close > high_15m and volume_today[symbol] > 30000): # check for a positive, increasing MACD hist = macd(minute_history[symbol]['close'].dropna(), n_fast=12, n_slow=26) if (hist[-1] < 0 or not (hist[-3] < hist[-2] < hist[-1])): return hist = macd(minute_history[symbol]['close'].dropna(), n_fast=40, n_slow=60) if hist[-1] < 0 or np.diff(hist)[-1] < 0: return # Stock has passed all checks; figure out how much to buy stop_price = find_stop(data.close, minute_history[symbol], ts) stop_prices[symbol] = stop_price target_prices[symbol] = data.close + ( (data.close - stop_price) * 3) shares_to_buy = portfolio_value * risk // (data.close - stop_price) if shares_to_buy == 0: shares_to_buy = 1 shares_to_buy -= positions.get(symbol, 0) if shares_to_buy <= 0: return print('Submitting buy for {} shares of {} at {}'.format( shares_to_buy, symbol, data.close)) try: o = api.submit_order(symbol=symbol, qty=str(shares_to_buy), side='buy', type='limit', time_in_force='day', limit_price=str(data.close)) open_orders[symbol] = o latest_cost_basis[symbol] = data.close except Exception as e: print(e) return if (since_market_open.seconds // 60 >= 24 and until_market_close.seconds // 60 > 15): # Check for liquidation signals # We can't liquidate if there's no position position = positions.get(symbol, 0) if position == 0: return # Sell for a loss if it's fallen below our stop price # Sell for a loss if it's below our cost basis and MACD < 0 # Sell for a profit if it's above our target price hist = macd(minute_history[symbol]['close'].dropna(), n_fast=13, n_slow=21) if (data.close <= stop_prices[symbol] or (data.close >= target_prices[symbol] and hist[-1] <= 0) or (data.close <= latest_cost_basis[symbol] and hist[-1] <= 0)): print('Submitting sell for {} shares of {} at {}'.format( position, symbol, data.close)) try: o = api.submit_order(symbol=symbol, qty=str(position), side='sell', type='limit', time_in_force='day', limit_price=str(data.close)) open_orders[symbol] = o latest_cost_basis[symbol] = data.close except Exception as e: print(e) return elif (until_market_close.seconds // 60 <= 15): # Liquidate remaining positions on watched symbols at market try: position = api.get_position(symbol) except Exception as e: # Exception here indicates that we have no position return print('Trading over, liquidating remaining position in {}'. format(symbol)) api.submit_order(symbol=symbol, qty=position.qty, side='sell', type='market', time_in_force='day') symbols.remove(symbol) if len(symbols) <= 0: conn.close() conn.deregister( ['A.{}'.format(symbol), 'AM.{}'.format(symbol)]) # Replace aggregated 1s bars with incoming 1m bars @conn.on(r'AM$') async def handle_minute_bar(conn, channel, data): ts = data.start ts -= timedelta(microseconds=ts.microsecond) minute_history[data.symbol].loc[ts] = [ data.open, data.high, data.low, data.close, data.volume ] volume_today[data.symbol] += data.volume channels = ['trade_updates'] for symbol in symbols: symbol_channels = ['A.{}'.format(symbol), 'AM.{}'.format(symbol)] channels += symbol_channels print('Watching {} symbols.'.format(len(symbols))) run_ws(conn, channels) # Handle failed websocket connections by reconnecting def run_ws(conn, channels): try: conn.run(channels) except Exception as e: print(e) conn.close() run_ws(conn, channels) if __name__ == "__main__": # Get when the market opens or opened today nyc = timezone('America/New_York') today = datetime.today().astimezone(nyc) today_str = datetime.today().astimezone(nyc).strftime('%Y-%m-%d') calendar = api.get_calendar(start=today_str, end=today_str)[0] market_open = today.replace(hour=calendar.open.hour, minute=calendar.open.minute, second=0) market_open = market_open.astimezone(nyc) market_close = today.replace(hour=calendar.close.hour, minute=calendar.close.minute, second=0) market_close = market_close.astimezone(nyc) # Wait until just before we might want to trade current_dt = datetime.today().astimezone(nyc) since_market_open = current_dt - market_open while since_market_open.seconds // 60 <= 14: time.sleep(1) since_market_open = current_dt - market_open run(get_tickers(), market_open, market_close)
import sys sys.path.append("/Users/kylespringfield/Dev/MoneyTree/") import config import alpaca_trade_api as tradeapi api = tradeapi.REST(config.PAPER_API_KEY, config.PAPER_SECRET_KEY, base_url=config.PAPER_API_URL) response = api.close_all_positions() print(response)
from config import * import datetime if input('Override Market Wait?') == 'n': print("Waiting for market open...") while True: now = datetime.datetime.now() current_time = int(now.strftime("%H%M")) print(current_time) if current_time >= 630 and current_time <= 800: print('Start time: {}'.format(current_time)) break time.sleep(60) print("Market has opened. Beginning program...") api = tradeapi.REST(API_KEY, API_SECRET, API_BASE_URL) print("---") print("Cleaning portfolio...") api.close_all_positions() time.sleep(5) account = api.get_account() buying_power = int(float( account.buying_power)) #set to 1000 during actual trading portfolio = api.list_positions() orders = api.list_orders(status='open') print(portfolio) config = ConvertJson( "/Users/oceanhawk/Documents/Python/Stock-Trading-Bots/Version-1/json/Var.json"
import pandas as pd import alpaca_trade_api as tradeapi from dotenv import load_dotenv import os from pandas import DataFrame ## Get Market Data for S&P500 #engage API keys by activating .env file for Alpaca Api load_dotenv() alpaca_api_key = os.getenv("ALPACA_API_KEY") alpaca_secret_key = os.getenv("ALPACA_SECRET_KEY") alpaca_base_url = os.getenv("APCA_API_BASE_URL") api = tradeapi.REST(alpaca_api_key, alpaca_secret_key, alpaca_base_url, api_version='v2') #function to read the api data for stock ticker #returns dataframe of closing price and daily returns for a given ticker symbol def get_ticker_data(api, ticker): #load in historical data for provided ticker stock_data_df = api.alpha_vantage.historic_quotes(ticker, adjusted=True, output_format='pandas', outputsize='compact') #Clean Data #Sort earliest to latest. so that .pct_change() function works right. stock_data_df.sort_index(inplace=True, ascending=True)
#################################### # Name: Jeremy Marks # # Purpose: trades stocks real time # #################################### import ta import alpaca_trade_api as tradeapi from text import send_text from datetime import date #authentication variables api_key = 'MY_API_KEY_FOR_ALPACA' api_secret = 'MY_API_SECRET_KEY_FOR_ALPAC' base_url = 'https://paper-api.alpaca.markets' #connects to api api = tradeapi.REST(api_key, api_secret, base_url, api_version='v2') #buys stock through api def orderMarket(stock, shares=5): api.submit_order( symbol=stock, qty=shares, side='buy', type='market', time_in_force='gtc' ) #sells stock through api def sellMarket(stock, shares = 5): api.submit_order( symbol=stock,
def __init__(self, ticker_list, time_interval, drl_lib, agent, cwd, net_dim, state_dim, action_dim, API_KEY, API_SECRET, APCA_API_BASE_URL, tech_indicator_list, turbulence_thresh=30, max_stock=1e2, latency=None): #load agent self.drl_lib = drl_lib if agent == 'ppo': if drl_lib == 'elegantrl': from elegantrl.agent import AgentPPO from elegantrl.run import Arguments, init_agent #load agent config = { 'state_dim': state_dim, 'action_dim': action_dim, } args = Arguments(agent=AgentPPO, env=StockEnvEmpty(config)) args.cwd = cwd args.net_dim = net_dim # load agent try: agent = init_agent(args, gpu_id=0) self.act = agent.act self.device = agent.device except BaseException: raise ValueError("Fail to load agent!") elif drl_lib == 'rllib': from ray.rllib.agents import ppo from ray.rllib.agents.ppo.ppo import PPOTrainer config = ppo.DEFAULT_CONFIG.copy() config['env'] = StockEnvEmpty config["log_level"] = "WARN" config['env_config'] = { 'state_dim': state_dim, 'action_dim': action_dim, } trainer = PPOTrainer(env=StockEnvEmpty, config=config) trainer.restore(cwd) try: trainer.restore(cwd) self.agent = trainer print("Restoring from checkpoint path", cwd) except: raise ValueError('Fail to load agent!') elif drl_lib == 'stable_baselines3': from stable_baselines3 import PPO try: #load agent self.model = PPO.load(cwd) print("Successfully load model", cwd) except: raise ValueError('Fail to load agent!') else: raise ValueError( 'The DRL library input is NOT supported yet. Please check your input.' ) else: raise ValueError('Agent input is NOT supported yet.') #connect to Alpaca trading API try: self.alpaca = tradeapi.REST(API_KEY, API_SECRET, APCA_API_BASE_URL, 'v2') except: raise ValueError( 'Fail to connect Alpaca. Please check account info and internet connection.' ) #read trading time interval if time_interval == '1s': self.time_interval = 1 elif time_interval == '5s': self.time_interval = 5 elif time_interval == '1Min': self.time_interval = 60 elif time_interval == '5Min': self.time_interval = 60 * 5 elif time_interval == '15Min': self.time_interval = 60 * 15 else: raise ValueError('Time interval input is NOT supported yet.') #read trading settings self.tech_indicator_list = tech_indicator_list self.turbulence_thresh = turbulence_thresh self.max_stock = max_stock #initialize account self.stocks = np.asarray([0] * len(ticker_list)) #stocks holding self.stocks_cd = np.zeros_like(self.stocks) self.cash = None #cash record self.stocks_df = pd.DataFrame(self.stocks, columns=['stocks'], index=ticker_list) self.asset_list = [] self.price = np.asarray([0] * len(ticker_list)) self.stockUniverse = ticker_list self.turbulence_bool = 0 self.equities = []
import alpaca_trade_api as tradeapi import requests import time from ta import macd import numpy as np from datetime import datetime, timedelta from pytz import timezone # Replace these with your API connection info from the dashboard base_url = 'Your API URL' api_key_id = 'Your API Key' api_secret = 'Your API Secret' api = tradeapi.REST(base_url=base_url, key_id=api_key_id, secret_key=api_secret) session = requests.session() # We only consider stocks with per-share prices inside this range min_share_price = 2.0 max_share_price = 13.0 # Minimum previous-day dollar volume for a stock we might consider min_last_dv = 500000 # Stop limit to default to default_stop = .95 # How much of our portfolio to allocate to any one position risk = 0.001 def get_1000m_history_data(symbols):
def __init__(self, key_id, secret_key, endpoint): super().__init__(key_id, secret_key, endpoint) self.api = tradeapi.REST(key_id=self.key_id, secret_key=self.secret_key, base_url=self.endpoint)
def app(): api = tradeapi.REST(config.API_KEY, config.SECRET_KEY, base_url=config.API_URL) Positions = api.list_positions() Orders = api.list_orders(status='closed') Orders ##### Positions ##### df = [] df2 = [] df3 = [] df4 = [] df5 = [] df6 = [] for position in Positions: df.append(position.symbol) df2.append(position.side) df3.append(position.qty) df5.append(position.cost_basis) df6.append(position.market_value) df4.append(float(position.unrealized_plpc) * 100) Total_Positions = pd.DataFrame(df) Total_Positions['Side'] = df2 Total_Positions['Qty'] = df3 Total_Positions['Cost Value'] = df5 Total_Positions['Market Value'] = df6 Total_Positions['Unrealized P/L %'] = df4 Total_Positions = Total_Positions.rename(columns={ 0: 'Symbol' }).set_index('Symbol') ##### Orders ###### d = [] d2 = [] d3 = [] d4 = [] d5 = [] for order in Orders: d.append(order.submitted_at) d2.append(order.symbol) d3.append(order.side) d4.append(order.filled_avg_price) d5.append(order.qty) Order = pd.DataFrame(d) Order['Symbol'] = d2 Order['Side'] = d3 Order['Qty'] = d5 Order['Filled_Avg_Price'] = d4 Order = Order.rename(columns={0: 'Timestamp'}).set_index('Timestamp') Order ############ ##### Portfolio Value ##### Account = api.get_account() balance_change = float(Account.equity) - float(Account.last_equity) percent_change = (float(Account.equity) / float(Account.last_equity) - 1) percent_change = '{:.3f}%'.format(percent_change * 100) percent_change st.header(f"Portfolio Performance Today: {percent_change}") st.dataframe(Total_Positions) st.table(Order)
import alpaca_trade_api as tradeapi import requests from alpha_vantage.techindicators import TechIndicators # important keys api_key = "PKPYALRHNINVZLHLIXQX" secret_key = "nIgH1J4D6SDVQ4QaC0RDHmvjBAXm744QsGOijFGr" alpha_api_key = " INYRU0Y7TU47R2BN" # URLS BASE_URL = "https://paper-api.alpaca.markets" ACCOUNT_URL = "{}/v2/account".format(BASE_URL) # create the api object api = tradeapi.REST(api_key, secret_key, BASE_URL) ti = TechIndicators(key=alpha_api_key, output_format='pandas') data_ti, meta_data_ti = ti.get_macd(symbol='AAPL', interval='daily', series_type='close', fastperiod=12, slowperiod=26, signalperiod=9) # print(data_ti['MACD_Hist'][-1]) # Create Clock entity clock = api.get_clock()
def pairs_trading_algo(self): #Specify paper trading environment os.environ["APCA_API_BASE_URL"] = "https://paper-api.alpaca.markets" #Insert API Credentials api = tradeapi.REST('KEYID', 'SECRETKEY', api_version='v2') # or use ENV Vars shown below account = api.get_account() #The mail addresses and password sender_address = '*****@*****.**' sender_pass = '******' receiver_address = '[email protected]@gmail.com' #Setup the MIME message = MIMEMultipart() message['From'] = 'Trading Bot' message['To'] = receiver_address message['Subject'] = 'Pairs Trading Algo' #The subject line #Selection of stocks days = 1000 stock1 = 'ADBE' stock2 = 'AAPL' #Put Hisrorical Data into variables stock1_barset = api.get_barset(stock1, 'day', limit=days) stock2_barset = api.get_barset(stock2, 'day', limit=days) stock1_bars = stock1_barset[stock1] stock2_bars = stock2_barset[stock2] #Grab stock1 data and put in to a array data_1 = [] times_1 = [] for i in range(days): stock1_close = stock1_bars[i].c stock1_time = stock1_bars[i].t data_1.append(stock1_close) times_1.append(stock1_time) #Grab stock2 data and put in to an array data_2 = [] times_2 = [] for i in range(days): stock2_close = stock2_bars[i].c stock2_time = stock1_bars[i].t data_2.append(stock2_close) times_2.append(stock2_time) #Putting them together hist_close = pd.DataFrame(data_1, columns=[stock1]) hist_close[stock2] = data_2 #Current Spread between the two stocks stock1_curr = data_1[days - 1] stock2_curr = data_2[days - 1] spread_curr = (stock1_curr - stock2_curr) #Moving Average of the two stocks move_avg_days = 5 #Moving averge for stock1 stock1_last = [] for i in range(move_avg_days): stock1_last.append(data_1[(days - 1) - i]) stock1_hist = pd.DataFrame(stock1_last) stock1_mavg = stock1_hist.mean() #Moving average for stock2 stock2_last = [] for i in range(move_avg_days): stock2_last.append(data_2[(days - 1) - i]) stock2_hist = pd.DataFrame(stock2_last) stock2_mavg = stock2_hist.mean() #Sread_avg spread_avg = min(stock1_mavg - stock2_mavg) #Spread_factor spreadFactor = .01 wideSpread = spread_avg * (1 + spreadFactor) thinSpread = spread_avg * (1 - spreadFactor) #Calc_of_shares_to_trade cash = float(account.buying_power) limit_stock1 = cash // stock1_curr limit_stock2 = cash // stock2_curr number_of_shares = int(min(limit_stock1, limit_stock2) / 2) #Trading_algo portfolio = api.list_positions() clock = api.get_clock() if clock.is_open == True: if bool(portfolio) == False: #detect a wide spread if spread_curr > wideSpread: #short top stock api.submit_order(symbol=stock1, qty=number_of_shares, side='sell', type='market', time_in_force='day') #Long bottom stock api.submit_order(symbol=stock2, qty=number_of_shares, side='buy', type='market', time_in_force='day') mail_content = "Trades have been made, short top stock and long bottom stock" #detect a tight spread elif spread_curr < thinSpread: #long top stock api.submit_order(symbol=stock1, qty=number_of_shares, side='buy', type='market', time_in_force='day') #short bottom stock api.submit_order(symbol=stock2, qty=number_of_shares, side='sell', type='market', time_in_force='day') mail_content = "Trades have been made, long top stock and short bottom stock" else: wideTradeSpread = spread_avg * (1 + spreadFactor + .03) thinTradeSpread = spread_avg * (1 + spreadFactor - .03) if spread_curr <= wideTradeSpread and spread_curr >= thinTradeSpread: api.close_position(stock1) api.close_position(stock2) mail_content = "Position has been closed" else: mail_content = "No trades were made, position remains open" pass else: mail_content = "The Market is Closed" #The body and the attachments for the mail message.attach(MIMEText(mail_content, 'plain')) #Create SMTP session for sending the mail session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port session.starttls() #enable security session.login(sender_address, sender_pass) #login with mail_id and password text = message.as_string() session.sendmail(sender_address, receiver_address, text) session.quit() done = 'Mail Sent' return done
format='%(name)s - %(levelname)s - %(message)s') logging.warning('{} logging started'.format( datetime.datetime.now().strftime("%x %X"))) #Configure twitter and alpaca API tz = timezone('EST') twitter_api_key = 'YOUR-API-KEY' twitter_api_secret = 'YOUR-API-SECRET' twitter_access_token = 'YOUR-API-ACCESS-TOKEN' twitter_token_secret = 'YOUR-API-TOKEN-SECRET' twitter_auth = tweepy.OAuthHandler(twitter_api_key, twitter_api_secret) twitter_auth.set_access_token(twitter_access_token, twitter_token_secret) alpaca_api_key = 'YOUR-API-KEY' alpaca_api_secret = 'YOUR-API-SECRET' alpaca_endpoint = 'https://paper-api.alpaca.markets' alpaca_api = tradeapi.REST(alpaca_api_key, alpaca_api_secret, alpaca_endpoint) #Evaluate the sentiment of a given set of ticker symbols #The default number of tweets to analyze is set at 1000 #When deciding the number of tweets to fetch for each call, consider reviewing the twitter API rate limits at #https://developer.twitter.com/en/docs/twitter-api/rate-limits def get_sentiment(auth, tickers, date_since, include_retweets=False, num_tweets=1000): sentiment_signals = {} for ticker in tickers:
import config import psycopg2 import psycopg2.extras import alpaca_trade_api as tradeapi #Connect to Postgres SQL server. Credentials imported from config.py connection = psycopg2.connect(host=config.DB_HOST, database=config.DB_NAME, user=config.DB_USER, password=config.DB_PASS) cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) #Aplaca API to populate stocks symbols api = tradeapi.REST(config.API_KEY, config.API_SECRET, base_url=config.API_URL) assets = api.list_assets() for asset in assets: print(f"Inserting stock {asset.name} {asset.symbol}") cursor.execute( """ INSERT INTO stock (name, symbol, exchange, isEtf) VALUES (%s, %s, %s, %s) """, (asset.name, asset.symbol, asset.exchange, False)) connection.commit() try: cursor.execute("SELECT COUNT(*) FROM stock") rows = cursor.fetchall() print('Total Count of Stocks populated:', rows)
def authenticate(auth_path="", mode="paper"): """ Alpaca Trade API Authentication ----------------------------------------------------------------- Authenticating access to the API can be done in two ways. 1. Save your "Key ID" (public key) and "Secret Key" (private key) in a config file, such as [config/secrets.yaml] 2. Save your public and private keys as environment variables. On UNIX, run the following commands in the terminal: >> export APCA_API_KEY_ID: key ID >> export APCA_API_SECRET_KEY: secret key """ if mode == "paper": os.environ["APCA_API_BASE_URL"] = "https://paper-api.alpaca.markets" elif mode == "live": os.environ["APCA_API_BASE_URL"] = "https://api.alpaca.markets" else: print('[mode] must either "paper" or "live".') sys.exit(1) # Saving keys in config file - example: [config/secrets.json] if auth_path: with open(auth_path, "r") as auth_file: auth = yaml.load(auth_file) try: keys = auth[mode] api = tradeapi.REST(keys["public_key"], keys["private_key"]) api.get_account() except KeyError: print( "[{}] must have 'public_key' and 'private_key' fields set for " "the [{}] field.".format(auth_path, mode)) sys.exit(1) except tradeapi.rest.APIError: print("Authentication failed.") print(authenticate.__doc__) sys.exit(2) except TypeError: print("YAML file [{}] didn't have proper keys.".format(auth_path)) sys.exit(3) # Saving keys as environment variables else: try: assert os.environ.get("APCA_API_KEY_ID") is not None assert os.environ.get("APCA_API_SECRET_KEY") is not None except AssertionError: print("Authentication failed.") print("Make sure APCA_API_KEY_ID / APCA_API_SECRET_KEY are set!") sys.exit(1) try: # This will check the environment variables under the hood api = tradeapi.REST() api.get_account() except tradeapi.rest.APIError: print("Authentication failed.") print(authenticate.__doc__) sys.exit(2) print("Authentication successful!") return api
import alpaca_trade_api as tradeapi from config import * import json APCA_API_BASE_URL = 'https://paper-api.alpaca.markets' api = tradeapi.REST(APCA_API_KEY_ID, APCA_API_SECRET_KEY, APCA_API_BASE_URL) api.submit_order(symbol='SPY', qty='1', side='buy', type='market', time_in_force='day')
import requests import json import os import alpaca_trade_api as tradeapi from config import * # Setting up environmental variables os.environ['APCA_API_KEY_ID'] = '[use your own key id here]' os.environ['APCA_API_SECRET_KEY'] = '[use your own key here]' os.environ['APCA_API_BASE_URL'] = 'https://paper-api.alpaca.markets' BASE_URL = "https://paper-api.alpaca.markets" ACCOUNT_URL = "{}/v2/account".format(BASE_URL) ORDERS_URL = "{}/v2/orders".format(BASE_URL) HEADERS = {'APCA-API-KEY-ID': API_KEY, 'APCA-API-SECRET-KEY': SECRET_KEY} api = tradeapi.REST(api_version='v2') def main(): response = create_order("VIX", 3, "buy", "limit", "gtc", 36.00) print(response) # orders = get_orders() # print(orders) account = api.get_account() aapl = api.alpha_vantage.historic_quotes('AAPL', adjusted=True, output_format='csv', cadence='weekly') # print(aapl) def create_order(symbol, qty, side, type, time_in_force, limit_price=None, stop_price=None, client_order_id=None,
import alpaca_trade_api as tradeapi import pandas as pd import time import logging from .universe import Universe logger = logging.getLogger(__name__) logging.basicConfig(level=logging.DEBUG) NY = 'America/New_York' api = tradeapi.REST(key_id='PKLVG8DFGC2AEER5MQ5H', secret_key='OYRNG032jeHbLe/kC7GTEYVlA9AOYJhNY3lLvsNi', base_url='https://paper-api.alpaca.markets') def _dry_run_submit(*args, **kwargs): logging.info(f'submit({args}, {kwargs})') # api.submit_order =_dry_run_submit def _get_prices(symbols, end_dt, max_workers=5): '''Get the map of DataFrame price data from Alpaca's data API.''' start_dt = end_dt - pd.Timedelta('50 days') start = start_dt.strftime('%Y-%m-%d') end = end_dt.strftime('%Y-%m-%d') def get_barset(symbols):
import alpaca_trade_api as tradeapi from twython import Twython import time base_url = 'https://paper-api.alpaca.markets' api_key_id = 'REPLACE' api_secret = 'REPLACE' api = tradeapi.REST(base_url=base_url, key_id=api_key_id, secret_key=api_secret, api_version="v2") account = api.get_account() if account.trading_blocked: print('Account is currently restricted from trading.') print('${} is available as buying power.'.format(account.buying_power)) order = api.submit_order(symbol='AAPL', qty=1, side='buy', type='market', time_in_force='gtc') time.sleep(2) my_order = api.get_order(order.id) twitter = Twython("REPLACE", "REPLACE", "REPLACE", "REPLACE")
@author: tuann """ import alpaca_trade_api as tradeapi """ With the Alpaca API, you can check on your daily profit or loss by comparing your current balance to yesterday's balance. """ # First, open the API connection # api = tradeapi.REST() api = tradeapi.REST( 'PKQYKLCSI9ZKRMXDZNMM', 'Uu2caxZ3FZf1aaE8n5Ly0KFjOM50RvzcucxKiHy8', 'https://paper-api.alpaca.markets' ) # Get account info account = api.get_account() # Check if our account is restricted from trading. if account.trading_blocked: print('Account is currently restricted from trading.') # Check how much money we can use to open new positions. print('${} is available as buying power.'.format(account.buying_power)) # Check our current balance vs. our balance at the last market close balance_change = float(account.equity) - float(account.last_equity)
symbol = stock['symbol'] stock_ids[symbol] = stock['id'] for symbol in symbols: # get data start_date = datetime(2020, 1, 1).date() #start_date = f"{start_date}T00:00:00-05:00" end_date_range = datetime(2020, 11, 20).date() #end_date_range = f"{end_date_range}T23:59:59-05:00" while start_date < end_date_range: end_date = start_date + timedelta(days=4) print(f"=== Fetching minute bars {start_date} - {end_date} for {symbol}") api = tradeapi.REST(config.API_KEY, config.SECRET_KEY, config.API_URL) end_date_bars = f"{end_date}T23:59:59-05:00" start_date_bars = f"{start_date}T00:00:00-05:00" minutes = api.get_barset(symbol, 'minute', start=start_date_bars, end=end_date_bars).df print(minutes) # forward filling of missing data minutes = minutes.resample('1min').ffill() for index, row in minutes.iterrows(): #going through minutes data and insert into db cursor.execute(""" INSERT INTO stock_price_minute (stock_id, datetime, open, high, low, close, volume) VALUES (?, ?, ?, ?, ?, ?, ?) """, (stock_ids[symbol], index.tz_localize(None).isoformat(), row[symbol]['open'], row[symbol]['high'], row[symbol]['low'], row[symbol]['close'], row[symbol]['volume']))
import alpaca_trade_api as tradeapi from iexfinance.stocks import Stock import csv # Gets account.txt as a list of lists account = open('account.txt', 'r').read().splitlines() api = tradeapi.REST(account[0].split()[1], account[1].split()[1], account[2].split()[1]) account = api.get_account() print("$" + account.buying_power + " left in cash.") with open('portfolio_targets.csv', 'r') as csvfile: target_file = csv.reader(csvfile, delimiter=',') targets = [] target_tickers = [] for stock in target_file: targets.append([stock[0], float(stock[1])]) target_tickers.append(stock[0]) current_positions = [] for line in targets: print("Targeting " + str(line[1]) + " " + line[0]) for stock in api.list_positions(): current_positions.append(stock.symbol) for stock in targets: if stock[0] not in current_positions: print(stock[0] + " not currently in portfolio")
# ib.errorEvent += onError # error handlers for historical data - done def signint_handler(a, b): # define the handler #print("Signal Number:", a, " Frame: ", b) print('Program termination - CTRL+C clean.') ib.disconnect() #end timing loop for JUST logging endtime = timecode.time() print("script time:", endtime - starttime, "seconds.") exit() signal.signal(signal.SIGINT, signint_handler) # assign the handler to the signal SIGINT #error handlers for CTRL-C - done print("about to get order list") api = tradeapi.REST() # Get the last 10 of our closed orders closed_orders = api.list_orders(status='closed', limit=500) print(closed_orders) #jj = json.loads(output) for o in closed_orders: writeData(o) # print(o) con.close() print("break")
def run(args): symbol = args.symbol max_shares = args.quantity opts = {} if args.key_id: opts['PKWN2459FP6IFHSCDMO9'] = args.key_id if args.secret_key: opts['ibvqEPLK7BQvb/7nCfqIsW6Jsi8kMf4xE/QOYz4u'] = args.secret_key if args.base_url: opts['base_url'] = args.base_url elif 'PKWN2459FP6IFHSCDMO9' in opts and opts['PKWN2459FP6IFHSCDMO9'].startswith('PK'): opts['base_url'] = 'https://paper-api.alpaca.markets' # Create an API object which can be used to submit orders, etc. api = tradeapi.REST(**opts) symbol = symbol.upper() quote = Quote() qc = 'Q.%s' % symbol tc = 'T.%s' % symbol position = Position() # Establish streaming connection conn = tradeapi.StreamConn(**opts) # Define our message handling @conn.on(r'Q$') async def on_quote(conn, channel, data): # Quote update received quote.update(data) @conn.on(r'T$') async def on_trade(conn, channel, data): if quote.traded: return # We've received a trade and might be ready to follow it if ( data.timestamp <= ( quote.time + pd.Timedelta(np.timedelta64(50, 'ms')) ) ): # The trade came too close to the quote update # and may have been for the previous level return if data.size >= 100: # The trade was large enough to follow, so we check to see if # we're ready to trade. We also check to see that the # bid vs ask quantities (order book imbalance) indicate # a movement in that direction. We also want to be sure that # we're not buying or selling more than we should. if ( data.price == quote.ask and quote.bid_size > (quote.ask_size * 1.8) and ( position.total_shares + position.pending_buy_shares ) < max_shares - 100 ): # Everything looks right, so we submit our buy at the ask try: o = api.submit_order( symbol=symbol, qty='100', side='buy', type='limit', time_in_force='day', limit_price=str(quote.ask) ) # Approximate an IOC order by immediately cancelling api.cancel_order(o.id) position.update_pending_buy_shares(100) position.orders_filled_amount[o.id] = 0 print('Buy at', quote.ask, flush=True) quote.traded = True except Exception as e: print(e) elif ( data.price == quote.bid and quote.ask_size > (quote.bid_size * 1.8) and ( position.total_shares - position.pending_sell_shares ) >= 100 ): # Everything looks right, so we submit our sell at the bid try: o = api.submit_order( symbol=symbol, qty='100', side='sell', type='limit', time_in_force='day', limit_price=str(quote.bid) ) # Approximate an IOC order by immediately cancelling api.cancel_order(o.id) position.update_pending_sell_shares(100) position.orders_filled_amount[o.id] = 0 print('Sell at', quote.bid, flush=True) quote.traded = True except Exception as e: print(e) @conn.on(r'trade_updates') async def on_trade_updates(conn, channel, data): # We got an update on one of the orders we submitted. We need to # update our position with the new information. event = data.event if event == 'fill': if data.order['side'] == 'buy': position.update_total_shares( int(data.order['filled_qty']) ) else: position.update_total_shares( -1 * int(data.order['filled_qty']) ) position.remove_pending_order( data.order['id'], data.order['side'] ) elif event == 'partial_fill': position.update_filled_amount( data.order['id'], int(data.order['filled_qty']), data.order['side'] ) elif event == 'canceled' or event == 'rejected': position.remove_pending_order( data.order['id'], data.order['side'] ) conn.run( ['trade_updates', tc, qc] )
#!/usr/bin/python import alpaca_trade_api as tradeapi import datetime import pandas as pd import pymysql.cursors import pymysql import numpy as np import time import pickle ALPACA_KEY_ID = 'PKD6W26XBA1JGWOSJSU0' ALPACA_SECRET_KEY = r'ysaezMtOX8sUghmR534ZGzEXzCTVpkDt26BEIY8e' api = tradeapi.REST(key_id=ALPACA_KEY_ID, secret_key=ALPACA_SECRET_KEY, base_url='https://paper-api.alpaca.markets') class Strategy: def isHammerBar(self, bar): if True == np.where(bar['open'] <= bar['high'], True, False): if True == np.where(bar['open'] > bar['close'], True, False): if True == np.where(bar['low'] * 1.005 < bar['close'], True, False): return True else: return False def simpleMovingAverageAcrossTime(self, workingSet, start, end): sum = 0.0 simpleMovingAverage = 0.0
from datetime import datetime import numpy as np import talib import alpaca_trade_api as tradeapi import pandas as pd import matplotlib.pyplot as plt base_url = 'https://paper-api.alpaca.markets' api_key_id = 'PKQVV0XBE9EL7L873JAH' api_secret = '4YWpFeBVSxjGcMKsemrLHjMkY7yXgLWSW9KvVbTv' api = tradeapi.REST(key_id=api_key_id,secret_key=api_secret, api_version='v2', base_url=base_url) barTimeframe = "1D" # 1Min, 5Min, 15Min, 1H, 1D assetsToTrade = ["AMZN","FB","MSFT","NFLX"] positionSizing = 0.25 startDate = "2016-01-01T00:00:00.000Z" # Tracks position in list of symbols to download iteratorPos = 0 assetListLen = len(assetsToTrade) while iteratorPos < assetListLen: symbol = assetsToTrade[iteratorPos] returned_data = api.get_barset(symbol,barTimeframe,start=startDate)
SELECT id from strategy where name = 'opening_range_breakout' """) strategy_id = cursor.fetchone()['id'] print(strategy_id) cursor.execute(""" SELECT symbol, name from stock join stock_strategy on stock_strategy.stock_id = stock.id where stock_strategy.strategy_id = ? """, (strategy_id,)) stocks = cursor.fetchall() symbols = [stock['symbol'] for stock in stocks] api = tradeapi.REST(ALPACA_API_KEY, ALPACA_SECRET_KEY, base_url=ALPACA_PAPER_BASE_URL) current_date = date.today().isoformat() # start_minute_bar = f"{current_date} 09:30:00-4:00" # end_minute_bar = f"{current_date} 09:45:00-4:00" start_minute_bar = "2020-10-29 09:30:00-4:00" end_minute_bar = "2020-10-29 09:45:00-4:00" for symbol in symbols: minute_bars = api.polygon.historic_agg_v2(symbol, 1, 'minute', _from='2020-10-29', to='2020-10-29').df print(symbol) print(minute_bars)
import sqlite3, config import alpaca_trade_api as tradeapi connection = sqlite3.connect('app.db') connection.row_factory = sqlite3.Row cursor = connection.cursor() cursor.execute("""SELECT symbol, name FROM stock""") rows = cursor.fetchall() symbols = [row['symbol'] for row in rows] api = tradeapi.REST(config.API_KEY, config.SECRET_KEY, base_url=config.BASE_URL) assets = api.list_assets() for asset in assets: try: if asset.status == 'active' and asset.tradable and asset.symbol not in symbols: print(f"Added a new stock {asset.symbol} {asset.name}") cursor.execute( "INSERT INTO stock (symbol, name, exchange) VALUES (?, ?, ?)", (asset.symbol, asset.name, asset.exchange)) except Exception as e: print(asset.symbol) print(e) connection.commit()
endPoint = 'https://paper-api.alpaca.markets' # Specify this for demo account # Define companies of interest tickers = [ 'AXP', 'AAPL', 'BA', 'CAT', 'CVX', 'CSCO', 'DIS', 'DOW', 'XOM', 'HD', 'IBM', 'INTC', 'JNJ', 'KO', 'MCD', 'MMM', 'MRK', 'MSFT', 'NKE', 'PFE', 'PG', 'TRV', 'UTX', 'UNH', 'VZ', 'V', 'WMT', 'WBA' ] # Define dates between which we extract the stock data startDate = '2020-01-01' endDate = '2020-05-28' # %% 3. Connect to api and pull data for the defined tickers api = tradeapi.REST(apiKey, secretKey, endPoint, api_version='v2') account = api.get_account() # api.list_positions() # Define the dictionary that will hold all the data for all the companies data = {} for ticker in tickers: try: # Pull the stock details for that ticker data[ticker] = api.get_aggs(ticker, 1, 'day', startDate, endDate).df # Calculate their daily return data[ticker]['daily return'] = data[ticker]['close'].pct_change() print('Pulling ohlcv data for {:s}'.format(ticker)) except: # If nothing is found, throw error and continue print('Error encountered pulling ohlcv data for {:s}'.format(ticker))
self.valleyTime1 = 0.0 self.valleyTime2 = 0.0 self.resistanceSlope = 0.0 self.supportSlope = 0.0 self.change = 0.01 * opening_price # account keys key = 'PK2490RRZ4FGDLO4SO4M' sec = 'Lnjn97piqx44HZPl4WarYntA6SXALa627iUkJK7t' # API endpoint URL base_url = 'https://paper-api.alpaca.markets' # ensure api version is correct api = tradeapi.REST(key, sec, base_url, api_version='v2') # init an account variable account = api.get_account() # create stocks to subscribe to htz = Stock('EKSO') stocks = [htz] # get opening prices for all stocks for stock in stocks: url = "https://finance.yahoo.com/quote/" + stock.ticker driver = webdriver.Chrome() # Go to yahoo finance driver.get(url) # Get the opening price
class MACrossPaper(Strategy): API = tradeapi.REST(key_id='PKIG33U7XYR8ECVMMF4A', secret_key='e83t4Cn5oY07EENvlhRKwjKyTbykd8wn8Phesmze', base_url='https://paper-api.alpaca.markets') def __init__(self, params): self.logger = logging.getLogger(__name__) logging.basicConfig(level=logging.DEBUG) self.NY = 'America/New_York' self.id = 1 self.params = params def sort_func(self, sma_obj): return sma_obj.sma[-1] def rank(self, smas): return sorted(smas, key=self.sort_func) def checkToSell(self, sma, price): if sma > price: return True return False def checkToBuy(self, smas): toBuy = [] for sma in smas: if sma.sma[-1] < sma.prices.iloc[-1, :].close: toBuy.append(sma) return toBuy def get_orders(self, context, prices_df, position_size=.02, max_positions=5): # rank the stocks based on the indicators. smas = [] symbols = set() for col in prices_df.columns: symbols.add(col[0]) c = 0 for symbol in symbols: sma = SMA(self.params.get('period'), prices_df[symbol].dropna(), symbol) c += 1 smas.append(sma) ranked = self.rank(smas) ranked = ranked[::-1] to_buy = [] to_sell = [] account = context.get_account() # now get the current positions and see what to buy, # what to sell to transition to today's desired portfolio. positions = context.list_positions() self.logger.info(positions) holdings = {p.symbol: p for p in positions} holding_symbols = list(holdings.keys()) if holdings: to_sell = [ sma.ticker for sma in smas if sma.sma[-1] > sma.prices.iloc[-1, :].close and sma.ticker in holding_symbols ] ranked = self.checkToBuy(ranked[:max_positions]) to_buy = [sma.ticker for sma in ranked] to_buy = to_buy[:len(to_sell) - 1] orders = [] # if a stock is in the portfolio, and not in the desired # portfolio, sell it for symbol in to_sell: shares = holdings[symbol].qty orders.append({ 'symbol': symbol, 'qty': shares, 'side': 'sell', }) self.logger.info(f'order(sell): {symbol} for {shares}') # likewise, if the portfoio is missing stocks from the # desired portfolio, buy them. We sent a limit for the total # position size so that we don't end up holding too many positions. max_to_buy = max_positions - (len(positions) - len(to_sell)) portfolio_value = float(self.API.get_account().portfolio_value) for symbol in to_buy: if max_to_buy <= 0: break shares = (portfolio_value * position_size) // float( prices_df[symbol].close.values[-1]) if shares == 0.0: continue orders.append({ 'symbol': symbol, 'qty': shares, 'side': 'buy', }) self.logger.info(f'order(buy): {symbol} for {shares}') max_to_buy -= 1 return orders