def parseCap(code): url = "http://www.sse.com.cn/sseportal/webapp/datapresent/querythismyquat?productId=" + code + "&prodType=1" from lxml import etree from lxml.html import parse page = parse(url).getroot() # result = etree.tostring(page) # print result r = page.xpath("//strong") # print len(r) from stock import Stock stock = Stock(code) cap = [] for data in r: dataTree = etree.ElementTree(data) # print etree.tostring(dataTree) values = dataTree.xpath("//text()") temp = values[0].strip() # print temp cap.append(temp) # print cap stock.totalCap = float(cap[0]) stock.floatingCap = float(cap[1]) # print stock return stock
def get(self): s = Stock() _d = s.iterDump() for line in _d: self.write(str(line)) self.write("\n") s.Close()
def get(self): try: s = Stock() reply = "" #Check for price/quantity price = self.get_argument('price','') quantity = self.get_argument('quantity','') value = self.get_argument('value','') if(price=='true'): #Get the egg unit price price = s.getItemPrice('egg') reply += "egg unit price: " + str(price) if(quantity=='true'): quantity = s.getItemQuantity('egg') reply += "egg stock level: " + str(quantity) if(value=='true'): #Get item total value total = s.getItemTotal('egg') reply += "egg total stock value: " + str(total) if(reply==""): reply = "OK" self.write(reply) s.Close() except Exception as e: print(e)
def create_stock(quote, max_iterations, start, end, stocks, index): ''' Create a stock from a quote ''' if (quote['StockExchange'] == None): return success = False for i in range(max_iterations): if not success: url = historical_url(quote['symbol'], start, end) data = request_json(url) if 'error' in data and i == (max_iterations-1): raise Exception('Error in call to historical api', data) elif 'error' not in data: success = True quotes = data['query']['results']['quote'] close_prices = [float(q['Close']) for q in quotes] stock = Stock(quote, close_prices) stock.index = index lock.acquire() stocks.append(stock) lock.release()
def download_sp500_stock_info(self): stock_list = [] request = urllib2.Request(self.sp500_stock_url) request.add_header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36") response = urllib2.urlopen(request) resp_soup = BeautifulSoup(response.read(), 'html.parser') table_tag = resp_soup.find("table") table_soup = BeautifulSoup(str(table_tag).replace("\n", ""), 'html.parser') tr_tag_list = table_soup.find_all("tr") row_index = 0 for tr_tag in tr_tag_list: if row_index != 0: tr_tag_soup = BeautifulSoup(str(tr_tag), "html.parser") td_tag_list = tr_tag_soup.find_all("td") stock = Stock() stock.symbol = td_tag_list[0].string stock.name = td_tag_list[1].string stock.sector = td_tag_list[3].string stock.industry = td_tag_list[4].string stock_list.append(stock) row_index += 1 return stock_list
def parseCap(code): url = "http://www.sse.com.cn/sseportal/ps/zhs/hqjt/hqjy.shtml" from lxml import etree from lxml.html import parse page = parse(url).getroot() result = etree.tostring(page) print result r = page.xpath("//strong") # print len(r) from stock import Stock stock = Stock(code) cap = [] for data in r: dataTree = etree.ElementTree(data) # print etree.tostring(dataTree) values = dataTree.xpath("//text()") temp = values[0].strip() # print temp cap.append(temp) # print cap stock.totalCap = float(cap[0]) stock.floatingCap = float(cap[1]) # print stock return stock
def main(): s = Stock() memory = Queue(maxsize=ma_days) ma_arr = [] populate_mem(memory, s) #populate the moving avg array with the first one ma_arr.append(s.get_ma()) for x in range(1,sim_len,1): populate_ma(ma_arr, memory, s) map(print_ma, ma_arr)
def main(): output.print_header() with io.open(Config.SAMPLE_INFILE, buffering=Config.BUFFER_SIZE) as infile: stock = None for line in infile: row = Row(csv.reader([line]).next()) if row.permno.isdigit(): if not stock or not stock.same_stock(row): if stock: stock.done() stock = Stock(row) else: stock.next_row(row) stock.done()
def parseFinanceData(code): from lxml import etree from lxml.html import parse url = 'http://www.windin.com/home/stock/stock-div/600309.SH.shtml' #print url page = parse(url).getroot() result = etree.tostring(page) print result import io with io.open('test.xml','wb') as f: #f.writelines(result) pass r = page.xpath('//div[@class="tab01"]'); #print len(r) from stock import Stock stock = Stock(code) for a in r: tree= etree.ElementTree(a) #print etree.tostring(tree) datas = tree.xpath('//td') #print len(datas) index =0 for data in datas: dataTree = etree.ElementTree(data); #print etree.tostring(dataTree) values = dataTree.xpath('//text()') index +=1 #print index if (len(values)==1 ): #print values #print len(values[0]) #print str(values[0]) if (index == 32): mgsy = values[0] #print mgsy+'***************' stock.mgsy = mgsy elif (index == 52): mgjzc = values[0] #print mgjzc+'***************' stock.mgjzc = mgjzc elif (index == 2): last_update = values[0] #print last_update stock.lastUpdate = last_update return stock
def __init__(self): self.stock = Stock() #self.workers = Gegevensstruct("hashmap") self.workers = Stack() self.worker_list = Gegevensstruct("hashmap") self.orders = Queue(50) self.orders_busy = Gegevensstruct("hashmap") self.users = Gegevensstruct("hashmap")
def addStock(self, event = None): #Check for nothing or spaces if self.entry.get().strip() == "": messagebox.showwarning("No Entry", "Enter a valid stock ticker symbol") self.entry.delete(0, END) else: #Make the API request and save the response company = self.entry.get().strip().upper() query1 = "http://finance.yahoo.com/d/quotes.csv?s=" query2 = "&f=nl1c1p2" response = urllib.request.urlopen(query1 + company + query2) data = response.read() text = data.decode() #Invalid case if "N/A" in text: messagebox.showerror("Invalid Entry", "This stock does not exist" \ + " or data could not be pulled for it.") #Valid case else: #Create a new stock object with the raw parameters for processing newStock = Stock(company, text) #Check if stock is already in portfolio, else --> add it for s in self.myPortfolio: if s.getTicker() == newStock.getTicker(): messagebox.showwarning("Repeated Entry", "This stock is" \ + " already in your portfolio") self.resetEntry(None) return self.myPortfolio.append(newStock) toDisplay = newStock.stringify() #Add to portfolio and determine color (gain/loss) self.portfolio.insert(END, toDisplay[0]) self.portfolio.see(END) if toDisplay[1] == "gain": self.portfolio.itemconfig(END, fg=self.money, selectbackground="green") elif toDisplay[1] == "loss": self.portfolio.itemconfig(END, fg="red", selectbackground="red") else: self.portfolio.itemconfig(END, fg="#B2B200", selectbackground="yellow") #Clear the entry field regardless self.resetEntry(None)
def main(): stock_names = ['ba', 'cat', 'dd', 'ge', 'gs', 'ibm', 'jnj', 'jpm', 'mmm', 'xom'] #for stock in stocks: # print(stock) # Andy's comment 2 # andy's comment Stocks = [] for stock in stock_names: this_stock = Stock(stock, '../data') this_stock.populate() Stocks.append(this_stock) trainingData = learningData() trainingData.construct(Stocks,['1/1/1980', [50, 100, 150], 50]) trainingData.append(Stocks,['1/1/1982', [50, 100, 150], 50]) cv_data = learningData() cv_data.construct(Stocks,['1/1/1990', [50, 100, 150], 50]) cv_data.append(Stocks,['1/1/1992', [50, 100, 150], 50]) clf = linear_model.Ridge (alpha = 0.1, fit_intercept=False) clf.fit(trainingData.X, trainingData.y) # To look for overfitting the code loops through values of alpha plotting distance between # the predicted values and actual data and various alphas alph = 0.01 f = open('alpha.txt', 'w') while alph < 0.2: # First fit the data for each alpha clf = linear_model.Ridge (alpha = alph, fit_intercept=False) clf.fit(trainingData.X, trainingData.y) # Now measure how close model comes for both training data and cross validation data # The clf.predict produces a vector with one element for each element in the sample predict_data = clf.predict(trainingData.X) predict_cv = clf.predict(cv_data.X) # The linagl function in np takes a L2 norm of the difference diff_data = (1.0/trainingData.m) * np.linalg.norm(predict_data - trainingData.y) diff_cv = (1.0/cv_data.m) * np.linalg.norm(predict_cv - cv_data.y) # Write out the values f.write(str(alph) + " " + str(diff_data) + " " + str(diff_cv) + "\n") alph = alph * 1.5 # Increment alph
class StockTest(unittest.TestCase): def setUp(self): self.stock = Stock("INTC", "Intel Corporation", 20.50, 20.35) def test_get_name(self): self.assertEqual(self.stock.get_name(), self.stock._Stock__name) def test_get_symbol(self): self.assertEqual( self.stock.get_symbol(), self.stock._Stock__symbol ) def test_get_price_close(self): self.assertEqual( self.stock.get_price_close(), self.stock._Stock__price_close ) def test_get_price_current(self): self.assertEqual( self.stock.get_price_current(), self.stock._Stock__price_current ) def test_get_change_percent(self): percent = self.stock.get_price_close() / \ self.stock.get_price_current() self.assertEqual( self.stock.get_change_percent(), -(1 - percent) )
def update(symbol): stock = Stock(symbol) #update stock if stock.update_history() == False: print 'this one is a dud' return False print 'got history' #if no data, discard #if data, add to list #calculate 52 week high #calculate n week average volume #return stock return stock
def main(): # try: # sStock = Stock() # code , result = sStock.getEqu(ticker = '000786') # jDict = json.loads(result, encoding='utf-8') # print 'jDict ticker: ' + jDict['data'][0]['ticker'] # print 'jDict officeAddr: ' + jDict['data'][0]['officeAddr'].encode('utf-8') # except Exception as e: # raise e s = Stock() data = s.getEqu() if data is None: print 'get data object is failed' else: # print 'secID: ' + data[0]['secID'] # print 'ticker: ' + data[0]['ticker'] # print 'primeOperating: ' + data[0]['primeOperating'] # print 'TShEquity: ' + str(data[0]['TShEquity']) for i in range(len(data)): print data[i]['secID'] + ': ' + data[i]['secShortName'] + ': ' + str(data[i]['TShEquity'])
def main(): stock_names = ["ba", "cat", "dd", "ge", "gs", "ibm", "jnj", "jpm", "mmm", "xom"] Stocks = [] for stock in stock_names: this_stock = Stock(stock, "../data") this_stock.populate() Stocks.append(this_stock) trainingData = learningData() trainingData.construct(Stocks, ["1/1/1980", [50, 100, 150], 50]) trainingData.append(Stocks, ["1/1/1982", [50, 100, 150], 50]) cv_data = learningData() cv_data.construct(Stocks, ["1/1/1990", [50, 100, 150], 50]) cv_data.append(Stocks, ["1/1/1992", [50, 100, 150], 50]) clf = linear_model.Ridge(alpha=0.1, fit_intercept=False) clf.fit(trainingData.X, trainingData.y) # To look for overfitting the code loops through values of alpha plotting distance between # the predicted values and actual data and various alphas alph = 0.01 # filename = "output/filename.txt" # dir = os.path.dirname(filename) f = open("alpha.txt", "w") while alph < 0.2: # First fit the data for each alpha clf = linear_model.Ridge(alpha=alph, fit_intercept=False) clf.fit(trainingData.X, trainingData.y) # Now measure how close model comes for both training data and cross validation data # The clf.predict produces a vector with one element for each element in the sample predict_data = clf.predict(trainingData.X) predict_cv = clf.predict(cv_data.X) # The linagl function in np takes a L2 norm of the difference diff_data = (1.0 / trainingData.m) * np.linalg.norm(predict_data - trainingData.y) diff_cv = (1.0 / cv_data.m) * np.linalg.norm(predict_cv - cv_data.y) # Write out the values f.write(str(alph) + " " + str(diff_data) + " " + str(diff_cv) + "\n") alph = alph * 1.5 # Increment alph
def download_nasdaq100_stock_info(self): stock_list = [] request = urllib2.Request(self.nasdaq100_stock_url) request.add_header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36") response = urllib2.urlopen(request) resp_soup = BeautifulSoup(response.read(), "html.parser") ol_tag = resp_soup.find("ol") ol_tag_soup = BeautifulSoup(str(ol_tag).replace("\n", ""), "html.parser") li_tag_list = ol_tag_soup.find_all("li") for li_tag in li_tag_list: stock_str = unicode(li_tag.a["title"]).encode("utf-8") + unicode(li_tag.contents[1]).encode("utf-8") stock_name = stock_str[:stock_str.find("(")].lstrip().rstrip() stock_symbol = stock_str[stock_str.find("("):].replace("(", "").replace(")", "").lstrip().rstrip() stock = Stock() stock.name = stock_name stock.symbol = stock_symbol stock_list.append(stock) print stock.to_json() return stock_list
def simulation_for_single_stock(all_simulations, stock_file_path, stock_name_as_a_file): single_stock = Stock(stock_name_as_a_file.replace('.csv', '')) with open(stock_file_path, "r") as csv_file: data_reader = csv.reader(csv_file) for row in data_reader: if not row == []: #print(row) day_data = DayData(row[0], row[1], row[2], row[3], row[4], row[5]) single_stock.add_data(day_data) good_data = 0 has_2015 = False for day in single_stock.get_data(): #print(day.get_open() <= 5.0) if day.get_open() >= 5.0: good_data += 1 else: good_data -= 1 if day.get_date().split('/')[2] == '2015': has_2015 = True # Filters. if has_2015: if good_data > 0: if len(single_stock.get_data()) >= 90: my_timer = Timer() my_timer.start_timer() do_math_for_single_stock(all_simulations, 5, 247, single_stock, 'LONG') do_math_for_single_stock(all_simulations, 5, 247, single_stock, 'SHORT') #do_math_for_single_stock(all_simulations, 5, 247, single_stock, 'COMBINED') my_timer.end_timer() my_timer.print_time()
def evaluate (x, ticker): inputData = Stock(ticker).getData() #print shit #print shit.shape output = neuralNetwork(x) #print np.transpose(input_data[0]).reshape(1,60).shape with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver = tf.train.Saver() saver.restore(sess, model) #print output.shape #print sess.run(output, feed_dict={x:shit}) print sess.run(output, feed_dict={x:inputData})
def results2(days = 0): if (request.method == 'POST'): if request.form['submit_button'] == 'Do Something': return("owo") elif request.form['submit_button'] == 'Do Something Else': print(request.form['numBuy']) print(request.form['numShort']) for i in range(int(request.form['numBuy'])): myUserTwo.buyStock(Stock('AAPL')) print(myUserTwo.getStockList()) print(request.form['buySell']) return("uwu") else: return("hehe") return render_template('index.html') return render_template('index.html')
def __init__(self, n_days, n_steps_per_day, asset_config_path, agent_config_path): self.n_days = n_days self.n_steps_per_day = n_steps_per_day initial_shares_per_agent = 10 n_agents = AgentGenerator.n_agents(agent_config_path) print(n_agents) self.companies = CompanyGenerator.generate_companies(n_companies=10, n_sectors=3) self.tickers = [each.ticker for each in self.companies] # self.tickers = 'EQNR DNO AKER MHG NRS LSG'.split(' ') # self.companies = [] # for ticker in self.tickers: # comp = Company(ticker=ticker, # n_shares=initial_shares_per_agent * n_agents, # cash=100 * initial_shares_per_agent * n_agents, # yield_policy=0.10) # self.companies.append(comp) self.assets = [Stock(comp) for comp in self.companies] self.agents = AgentGenerator.generate(agent_config_path, self.assets, verbose=True) n_agents = len(self.agents) for agent in self.agents: for stock in self.assets: agent.portfolio.assets[stock] = initial_shares_per_agent self.cov_matrix = test.generate_corr_matrix(len(self.tickers), 2, 0.2) self.weights = np.ones(len(self.tickers)) self.weights = self.weights / self.weights.size self.book = Orderbook([], []) # self.assets = ... self.timestamp = Timestamp(0, 0) self.market_portfolio = Portfolio.empty_portfolio(self.assets) self.exchange = Exchange(self.agents, self.market_portfolio, n_steps_per_day) self.history = []
def put(self): try: s = Stock() price = self.get_argument('price','') quantity = self.get_argument('quantity','') if(price!=''): s.setEggPrice(float(price)) if(quantity!=''): s.setEggQuantity(int(quantity)) self.write("OK") except Exception as e: print(e)
def get_stock_values(parser, section): """ Get and save species values from configuration file. Args: parser (ConfigParser object): ConfigParser instance used as parser. section (str): Name of the section in configuration file corresponding to a specie. Returns: Stock object: Stock instance with specie's values. """ options = parser.options(section) stock_data = {} for opt in options: stock_data[opt] = parser.get(section, opt) return Stock(stock_data)
def test_stock_dividend_yield_common(self): """ Tests correct dividend yield calculation for `common` stock types. """ # Make a mock object for testing. sALE = Stock('ALE', 'common', 23, nan, 60) # A stock without trades has a default ticker price equal to its par # value. self.assertEqual(sALE.dividend_yield(), 23. / 60) # Add some mock Trades. sALE.buy(500, 25) sALE.sell(300, 15) self.assertEqual(len(sALE._trades), 2) # The dividend yield calculation should now use a ticker price # determined from the average trading price. self.assertEqual(sALE.dividend_yield(), 23. / (((500*25)+(300*15))/(500+300)))
def add_stocks(self, tickers): ''' :type tickers: List[str] :return assets: List[Stock] ''' assets = [None] * len(tickers) assets_str = [None] * len(tickers) for index, stock in enumerate(tickers): stock_obj = Stock(stock, self.AlphaVantage_Key) assets[index] = stock_obj assets_str[index] = stock self.curr_pf = self.curr_pf + assets self.list_pf = self.list_pf + assets_str return assets
def accumulate_assets(self): """ Run through the lots and count up how much of each stock we actually have. Store this in the dict assets. """ self.asset_counts = dict() for l in self.lots: if l.stock.ticker not in self.asset_counts.keys(): self.asset_counts[l.stock.ticker] = 0 self.asset_counts[l.stock.ticker] += l.nr # Only bother doing this once. if not self.assets: self.assets = list() for a in self.asset_counts.keys(): self.assets.append(Stock(a))
def get_best_stocks(self, limit): if not os.path.exists(SR_FOLDER_PATH): os.makedirs(SR_FOLDER_PATH) file_name = SR_FOLDER_PATH + self.date_from + '-' + self.date_to + '.json' if os.path.isfile(file_name): with open(file_name) as file: stock_params = json.load(file) else: print('Create cache for new estimation period from ' + self.date_from + ' to ' + self.date_to + '...') stock_params = [] symbols = get_stock_symbols() for symbol in symbols: stock_param = self.get_sharped_ratio_by_stock(symbol) if stock_param is not None: stock_params.append(stock_param) file = open(file_name, "w") file.write(json.dumps(stock_params)) file.close() stocks_sorted_by_ret = sorted(stock_params, key=lambda x: x['ret'], reverse=True)[:self.stocks_with_max_ret] stocks_sorted_by_sharpe = sorted(stocks_sorted_by_ret, key=lambda x: x['sharpe'], reverse=True) first_10 = stocks_sorted_by_sharpe[:10] valid_count = self.get_valid_count(first_10) result = [] current_stock_index = 0 while len(result) < limit: stock_param = stocks_sorted_by_sharpe[current_stock_index] current_stock_index += 1 quotes = get_stock_data_by_symbol(stock_param['symbol']) if len(quotes) != valid_count: continue info = self.get_stock_info_by_symbol(stock_param['symbol']) stock = Stock(info, quotes, stock_param) result.append(stock) return result
def __init__(self, stockList, level): self.stockList = stockList self.level = level self.liquidAssets = 100000 self.stocksBought = {stock: 0 for stock in stockList} self.stocksShorted = {stock: 0 for stock in stockList} self.stockValues = { ticker: Stock(ticker).getPrice() for ticker in stockList } self.totalAssets = 0 self.ipo = { 'AAPL': 22, 'BLK': 14, 'DIS': 13.88, 'GE': 0.8, 'JPM': 9.78, 'MSFT': 21, 'NYT': 42 }
def __init__(self, stock, start=None, end=None): if isinstance(stock, Stock): if (start is not None) and (end is not None): '''Raise Some exception that start/end dates were input''' print('Start/End won\'t be reset') stock = stock elif isinstance(stock, str): '''Include code to read in the Stock Object''' stock = Stock(stock, start=start, end=end) else: '''raise an InvalidInputType exception''' print('Invalid stock type') pass self.ticker = stock.ticker self.data = stock.data_table self.start_date = self.data.index.min() self.end_date = self.data.index.max()
def stock(update, context): if BOT_UPDATING: context.bot.send_message(chat_id=update.effective_chat.id, text='Bot Updating, Please Wait...') stock = Stock.Create(update.message.text[1:]) if stock is not None: stock_message = '' if (stock.state == 0): stock_message = stock.stockInfo() + '\n' + stock.stockNews() elif (stock.state == 1): stock_message = 'No Such Stock Code [{:s}]'.format(stock.code) elif (stock.state == 2): stock_message = 'Yahoo Server Connect Failed' else: stock_message = 'Unknown Error' context.bot.send_message(chat_id=update.effective_chat.id, text=stock_message, parse_mode=telegram.ParseMode.HTML, disable_web_page_preview=True)
def load_stocks(folder, days): infos = [] for root, dirs, files in os.walk(folder): for f in files: infos.append(f) infos.sort() for fname in infos[-days:]: f = open(folder + fname, 'r') count = 0 for line in f: count += 1 if count == 1: continue data = line.split(',') ticker = data[0] if ticker not in stocks: stocks[ticker] = Stock(ticker) d = data[1] day = date(int(d[:4]), int(d[4:6]), int(d[6:])) stocks[ticker].add_data(day, data[2], data[3], data[4], data[5], data[6])
def getKospi200(): for i in range(1, 22, 1): try: url = baseUrl + str(i) r = requests.get(url) soup = BeautifulSoup(r.text, 'lxml') items = soup.find_all('td',{'class':'ctg'}) for item in items: txt = item.a.get('href') k = re.search('[\d]+',txt) if k: code = k.group() name = item.text data = code, name kospi200Arr.append(Stock(code, name)) except: pass return kospi200Arr
def get_stocks(): """ Open the csv file making sure the file can be opened """ try: f = open('list.csv') except ValueError: print("Could not read {}".format('list.csv')) sys.exit() with f: reader = csv.reader(f) # loop through each row the csv file for row in reader: # create a dictionary stock_dict = {'name': row[0], 'tickler': row[1], 'price': row[2]} # create a stock object stock = Stock(stock_dict['name'], stock_dict['tickler'], stock_dict['price']) # append the stock object to the list of stock stocks.append(stock)
def getStockByStockID(stockid): stocks = [] conn = MySQLdb.connect(host='localhost', user='******', passwd='', db='stockdb', port=3306, charset='utf8') cur = conn.cursor() cur.execute("select * from stocktable WHERE stockid='" + stockid + "' group by stockdate order by stockdate") result = cur.fetchall() for row in result: stocks.append( Stock(stockid=row[1], stockname=row[2], stockprice=row[3], stockscore=row[4], stockadvice=row[5], stockdate=row[6])) return stocks
def getStockByDate(date, host='localhost', user='******', passwd='', db='stockdb', port=3306): stocks = [] conn = MySQLdb.connect(host, user, passwd, db, port, charset='utf8') cur = conn.cursor() cur.execute("SELECT * FROM stocktable WHERE stockdate='" + date + "' group by stockid order by stockid") result = cur.fetchall() for row in result: stocks.append( Stock(stockid=row[1], stockname=row[2], stockprice=row[3], stockscore=row[4], stockadvice=row[5], stockdate=row[6])) return stocks
def read_data(stocks_collection, symbol): def skiplimit(collection, page_size=50, page_num=0): """returns a set of documents belonging to page number `page_num` where size of each page is `page_size`. """ # Skip and limit cursor = collection.find({ "symbol": symbol }).sort("date").skip(page_num * page_size).limit(page_size) return [DailyStock(x["date"], x["close"]) for x in cursor] daily_data = [] page_num = 0 data = skiplimit(stocks_collection, 200, page_num) while data: daily_data += data page_num += 1 data = skiplimit(stocks_collection, 200, page_num) return Stock(symbol, daily_data)
def init_stocks(): global fields # try to open file containing company list try: f1 = open(src_file, "r+") companies = f1.readlines() f1.close() except: bad_input() # try to open file containing desired fields try: f2 = open("data_mapping.json", "r+") fields = utils.file_to_dict(f2.readlines()) f2.close() except: bad_input() # each stock will fetch data upon initialization for ticker in companies: stocks.append(Stock(ticker))
def grab_prices(symbol: str) -> object: """ Get the current price for a given symbol """ try: current_prices = get_last_price_data(symbol) now = get_last_trading_day() today = now.strftime("%Y-%m-%d") last_trading_day = current_prices[ PricePayloadKeys.last_trading_day.value] if last_trading_day != today: print(last_trading_day) raise ValueError('Data for {0} is not available'.format(today)) high = float(current_prices[PricePayloadKeys.high.value]) low = float(current_prices[PricePayloadKeys.low.value]) open_price = float(current_prices[PricePayloadKeys.open_price.value]) close_price = float(current_prices[PricePayloadKeys.price.value]) return Stock(high, low, close_price, open_price, symbol) except KeyError: print("JSON Response from AlphaVantage Corrupt")
def readDateStockDataFromDirs(path): stockDic = {} if not os.path.exists(path): return None fnames = os.listdir(path) for fname in fnames: stockDate = fname.split('.')[0] stockDic.setdefault(stockDate, []) f = open(path + '/' + fname) lines = f.readlines() for line in lines: stockDic[stockDate].append(Stock.parse(line)) idDic = {} for d, s in stockDic.items(): for stock in s: if stock.stockid not in idDic: idDic.setdefault(stock.stockid, []) idDic[stock.stockid].append(stock) for id, stocks in idDic.items(): stocks.sort(key=lambda stock: stock.stockdate) return idDic
def graph_mean_reversion_default(): # Constants k = 1.5 num_days = 200 time_span = 1000 # List that holds the data stock_data = retrieve_list("input/custom.txt") # 2d arr, arr holds list of stocks throughout time span, each arr is a different stock stock_data_list = [] # Go through backtest stocks for key in stock_data: temp = [] for start in range(time_span, 0, -1): temp.append(Stock(key, stock_data[key], k, start, num_days)) stock_data_list.append(temp) # Graph the historical data graph_mean_reversion(stock_data_list)
def update_db(): """ filters tickers based on price and volume and etc """ tickers = get_tickers(ALL_TICKERS_FILEPATH) stocks = [] new_tickers = [] for t in tickers: try: stock = Stock(t) if stock.avg_volume > MIN_VOLUME and stock.price > MIN_PRICE: stocks.append(stock) new_tickers.append(t) print(f'Added ${t} to db') else: print( f'Skipped stock ${t}: avg_vol = ${stock.avg_volume}, price = ${stock.price}' ) except Exception as e: print(f'Can\'t find ${t}', e) df = pd.DataFrame(new_tickers, columns=['ticker']) df.to_csv(FILTERED_TICKERS_FILEPATH, index=False)
def parseCurrentHoldings(json): positionsParsed = [] if (json is not None): for curHolding in json: try: x = Stock(curHolding["instrument"]["symbol"]) x.purchasePrice = curHolding["averagePrice"] x.plPercent = curHolding["currentDayProfitLossPercentage"] x.plDollars = curHolding["currentDayProfitLoss"] x.sharesHeld = curHolding["longQuantity"] positionsParsed.append(x) except Exception as ex: print("Couldn't parse order") print(curHolding) return positionsParsed
def main(): #enter a symbol and name symbol = "INTC" name = "Intel Corporation" #enter previous and current prices previousClosingPrice = 20.5 currentPrice = 20.35 #create stock object stock = Stock(symbol, name, previousClosingPrice, currentPrice) #display results print("The percentage of change for", stock.getSymbol(), stock.getName()) print("Where the previous day's closing price was", stock.getPreviousClosingPrice()) print("and the current day's closing price was", stock.getCurrentPrice()) print("is", format(stock.getChangePercent(), ".2f"), "%")
class StockTest(unittest.TestCase): def setUp(self): self.stock = Stock("INTC", "Intel Corporation", 20.50, 20.35) def test_get_name(self): self.assertEqual(self.stock.get_name(), self.stock._Stock__name) def test_get_symbol(self): self.assertEqual(self.stock.get_symbol(), self.stock._Stock__symbol) def test_get_price_close(self): self.assertEqual(self.stock.get_price_close(), self.stock._Stock__price_close) def test_get_price_current(self): self.assertEqual(self.stock.get_price_current(), self.stock._Stock__price_current) def test_get_change_percent(self): percent = self.stock.get_price_close() / \ self.stock.get_price_current() self.assertEqual(self.stock.get_change_percent(), -(1 - percent))
def verify(self): up5 = 0 up1 = 0 up9 = 0 up = [] chg_p = Stock.get_col().index("chg_p") for code in self.up_code: data_array = np.array(self.data[code]) try: chg_p_array = np.float32(data_array[:, chg_p]) if chg_p_array[-1] >= 1.0: up1 += 1 up.append((code, chg_p_array[-1])) if chg_p_array[-1] >= 5.0: up5 += 1 if chg_p_array[-1] >= 9.0: up9 += 1 except: logging.error("%s failed" % code) trace_log() print "up5 = %.2f, up1 = %.2f, up9 = %.2f" % ( float(up5) / float(len(self.up_code)), float(up1) / float(len(self.up_code)), float(up9) / float(len(self.up_code))) print up
def readStockRankFromDateDataDir(filepath): if not os.path.exists(filepath): return None fnames = os.listdir(filepath) stockrank = [i for i in range(len(fnames))] dateIndex = 0 fnames.sort() for fname in fnames: resultDate = fname.split('.')[0] f = open(filepath + '/' + fname) lines = f.readlines() stockrank[dateIndex] = {} stockList = [] for line in lines: stockList.append(Stock.parse(line.strip())) stockList.sort(key=lambda x: x.stockscore, reverse=True) rank = 1 for stock in stockList: stockrank[dateIndex][stock.stockid] = (stock.stockid, stock.stockscore, resultDate, rank) rank += 1 dateIndex += 1 return stockrank
def predict(self): result = {} for code in self.data: volume, chg_p, vma5, ma5, close, high = Stock.get_col().index( "volume"), Stock.get_col().index("chg_p"), Stock.get_col( ).index("vma5"), Stock.get_col().index("ma5"), Stock.get_col( ).index("close"), Stock.get_col().index("high") try: last_date = self.data[code][-1][0] if last_date != time.strftime("%Y-%m-%d", time.localtime()): continue except: continue data_array = np.array(self.data[code]) try: chg_p_array, vma5_array, volume_array, ma5_array, close_array, high_array = np.float32( data_array[:, chg_p]), np.float32( data_array[:, vma5]), np.float32( data_array[:, volume]), np.float32( data_array[:, ma5]), np.float32( data_array[:, close]), np.float32( data_array[:, high]) # 判断跳空 jumped = False for i in range(-1, -10, -1): if high_array[i] < close_array[i - 1]: jumped = True break if jumped and chg_p_array[-1] > 3.0: down_number = 0 for c in chg_p_array[-10:]: if c < 0.0: down_number += 1 result[code] = down_number except: trace_log() pass sorted_result = sorted(result.iteritems(), key=operator.itemgetter(1), reverse=True) return sorted_result
class DataReplayer(object): def __init__(self, symbol, date): self.stock = Stock(symbol=symbol, date=date) self.bars = [] self.bar_idx = 0 self.stock.fetch_chart() def calculate_bars(self): open_prices = self.stock.get_open_prices() close_prices = self.stock.get_close_prices() low_prices = self.stock.get_low_prices() high_prices = self.stock.get_high_prices() # theoretically these price collections have the same length lim = len(open_prices) for i in range(lim): bar = { 'open': open_prices[i], 'close': close_prices[i], 'low': low_prices[i], 'high': high_prices[i] } self.bars.append(bar) def print_next_bar(self): lim = len(self.bars) for i in range(lim): print(self.bars[self.bar_idx]) self.bar_idx += 1 sleep(1) def start_replay(self): self.calculate_bars() thread = Thread(target=self.print_next_bar) thread.start() thread.join()
from datetime import datetime, date from sys import argv as argv from stock import Stock import os start = datetime.now() cur_time = datetime.now() ticker_file = open(argv[1], 'r') stocks = [Stock(ticker.strip()) for ticker in ticker_file] header = '{0:>10}{1:>15}{2:>15}{3:>15}{4:>15}'.format('TICKER', 'DAY OPEN', 'LIVE PRICE', 'CHANGE', '% Change') while True: current = datetime.now() banner = '{0:^70}'.format( f'LAST UPDATED [{date.today()} {current.hour}:{str(current.minute).zfill(2)}:{str(current.second).zfill(2)}]' ) output = banner + '\n' + header for stock in stocks: stock.update() output += '\n' + str(stock) output += '\n' os.system('cls') print(output)
def test_sell(self): s = Stock('GOOG', 100, 490.1) s.sell(50) self.assertEqual(s.shares, 50)
#!/usr/bin/python import logging import sys sys.path.append('lib') from stock import Stock from util import setup_logging setup_logging() stock = Stock() report = stock.build_report() for stn in report.stns(): logging.info("Station: %s", stn.name) for loc in stn.locs(): logging.info(" Location: %s", loc.name) for item in loc.items(): output = [] output.append(" %s: %d" % (item.name, item.have)) if item.target > 0: output.append("/%d" % item.target) output.append(" (%.1f%%)" % item.percent) logging.info("".join(output))
def test_set_price(self): s = Stock('GOOG', 100, 490.1) s.price = 10.0 self.assertEqual(s.price, 10.0)
def setUp(self): self.stock = Stock("INTC", "Intel Corporation", 20.50, 20.35)
def test_bad_shares(self): s = Stock('GOOG', 100, 490.1) with self.assertRaises(TypeError): s.shares = 11.2
def test_bad_price(self): s = Stock('GOOG', 100, 490.1) with self.assertRaises(TypeError): s.price = 'cookies'
def setUp(self): self.goog = Stock('GOOG')
def strategy(symbol): stock = Stock(symbol) stock.update_history() stock.analyze() buy_dates = [] buy_prices = [] sell_dates = [] sell_prices = [] wiggly = piggy(sim=True,holdings=300) buy_flag = False staleness = 0 sell_flag = False earn_flag = False last_bought = 0 for itx, date in enumerate(stock.history_data['Date']): ptrn_lookahead = 5 prox_thres = .02 * float(stock.history_data['Close'][itx]) if float(stock.history_data['30_Day'][itx-ptrn_lookahead]) - prox_thres <= float(stock.history_data['10_Day'][itx-ptrn_lookahead]) <= float(stock.history_data['30_Day'][itx-ptrn_lookahead]) + prox_thres\ and float(stock.history_data['30_Day'][itx-ptrn_lookahead]) - prox_thres <= float(stock.history_data['5_Day'][itx-ptrn_lookahead]) <= float(stock.history_data['30_Day'][itx-ptrn_lookahead]) + prox_thres\ and float(stock.history_data['30_Day'][itx]) > float(stock.history_data['10_Day'][itx]) > float(stock.history_data['5_Day'][itx]): buy_flag = True staleness = 0 gains_thres = 1.1 buy_slop_err = 0.001 if buy_flag \ and float(stock.history_data['5_Day'][itx]) * gains_thres < float(stock.history_data['30_Day'][itx]) \ and float(stock.history_data['5_Day'][itx]) > float(stock.history_data['5_Day'][itx-1]) * (1+buy_slop_err): ##Once there is enough margin betweet the 5 day and 30 day, buy buy_dates.append(date) buy_prices.append(float(stock.history_data['Close'][itx])) buy_flag = False staleness = 0 earn_flag = True num = int(wiggly.holdings * .5 / float(stock.history_data['Close'][itx])) wiggly.buy(stock,num,date=date) buy_dates.append(date) buy_prices.append(float(stock.history_data['Close'][itx])) last_bought = itx if (buy_flag and staleness > 20 or (float(stock.history_data['5_Day'][itx]) > float(stock.history_data['10_Day'][itx]))): buy_flag = False staleness = 0 earn_thres = 1.2 if (earn_flag and float(stock.history_data['10_Day'][itx]) > float(stock.history_data['10_Day'][itx-1]) * (1+buy_slop_err) and float(stock.history_data['Close'][itx]) > float(stock.history_data['5_Day'][last_bought]) * earn_thres): ## and the 5 Day is increasing, then throw the EARNING flag earn_flag = False sell_flag = True ceiling = .5 if (sell_flag and float(stock.history_data['5_Day'][itx]) < float(stock.history_data['5_Day'][itx-1])) \ or (sell_flag and float(stock.history_data['5_Day'][itx]) > float(stock.history_data['5_Day'][itx]) * (ceiling + 1)): sell_flag = False wiggly.sell(stock,-1,date=date) sell_dates.append(date) sell_prices.append(float(stock.history_data['Close'][itx])) staleness += 1 if wiggly.current_stock[stock.symbol] > 0: print "\n\n#####Closing Out######" wiggly.sell(stock,-1,date=date) ##Make a plot import matplotlib.pyplot as plt import matplotlib.dates as plotdate import matplotlib.lines as line import numpy as np months = plotdate.MonthLocator() # every year days = plotdate.DayLocator() # every month monthsFmt = plotdate.DateFormatter('%m %d') fig, ax = plt.subplots() #ax2 = ax.twinx() t = [datetime.datetime.strptime(date,'%Y-%m-%d') for date in stock.history_data['Date']] ax.axis('auto') # format the ticks ax.xaxis.set_major_locator(months) ax.xaxis.set_major_formatter(monthsFmt) ax.xaxis.set_minor_locator(days) fig.autofmt_xdate() ax.plot(t, stock.history_data['5_Day'], '#0000FF') ax.plot(t, stock.history_data['10_Day'], '#5555FF') ax.plot(t, stock.history_data['30_Day'], '#9999FF') #ax.plot(t, stock.history_data['80_Day'], '#AAAAFF') #ax2.plot(t, stock.history_data['Volume'], '#CCFFCC') #ax2.plot(t, stock.history_data['10_Day_Vol'], '#88AA88') buy_dates = [datetime.datetime.strptime(date,'%Y-%m-%d') for date in buy_dates] ax.plot(buy_dates,buy_prices, 'g|',ms=100) sell_dates = [datetime.datetime.strptime(date,'%Y-%m-%d') for date in sell_dates] ax.plot(sell_dates,sell_prices, 'b|',ms=100) ax.plot(t, stock.history_data['Close'], 'r-') plt.title(stock.symbol) #ax.text(t[12], 250, 'hello') plt.show() return {'gains': wiggly.gains, 'symbol': stock.symbol, 'initial_value':stock.history_data['Open'][1]}
class StockTest(unittest.TestCase): """""" #---------------------------------------------------------------------- def setUp(self): self.goog = Stock('GOOG') #---------------------------------------------------------------------- def test_price_of_a_new_stock_class_should_be_None(self): self.assertIsNone(self.goog.price) #---------------------------------------------------------------------- def test_stock_update(self): """ An update should set the price on the stock object We will be using the `datetime` module for the timestamp """ self.goog.update(datetime(2014, 2, 12), price=10) self.assertEqual(10, self.goog.price) #---------------------------------------------------------------------- def test_negative_price_should_throw_ValueError(self): #self.assertRaises(ValueError, goog.update, #datetime(2014, 2, 13), -1) with self.assertRaises(ValueError): self.goog.update(datetime(2014, 2, 13), -1) #--------------------------------------------------------------------- def test_stock_price_should_give_the_latest_price(self): """""" self.goog.update(datetime(2014, 2, 12), price=10) self.goog.update(datetime(2014, 2, 13), price=8.4) self.assertAlmostEqual(8.4, self.goog.price, delta=0.0001) #---------------------------------------------------------------------- def given_a_series_of_prices(self, prices): """""" timestamps = [datetime(2014, 2, 10), datetime(2014, 2, 11), datetime(2014, 2, 12), datetime(2014, 2, 13)] for timestamp, price in zip(timestamps, prices): self.goog.update(timestamp, price) #---------------------------------------------------------------------- def test_increasing_trend_is_true_if_price_increase_for_3_updates(self): """""" self.given_a_series_of_prices([8, 10, 12]) self.assertTrue(self.goog.is_increasing_trend()) #---------------------------------------------------------------------- def test_increasing_trend_is_false_if_price_decreases(self): """""" self.given_a_series_of_prices([8, 12, 10]) self.assertFalse(self.goog.is_increasing_trend()) #---------------------------------------------------------------------- def test_increasing_trend_is_false_if_price_equal(self): """""" self.given_a_series_of_prices([8, 10, 10]) self.assertFalse(self.goog.is_increasing_trend()) #---------------------------------------------------------------------- def test_price_is_the_latest_even_if_updates_are_made_out_of_order(self): """""" self.goog.update(datetime(2014, 2, 13), price=8) self.goog.update(datetime(2014, 2, 12), price=10) self.assertEqual(8, self.goog.price)
def test_set_shares(self): s = Stock('GOOG', 100, 490.1) s.shares = 50 self.assertEqual(s.shares, 50)