def do_thread_loop(self): if ( self.running ): if ( self.mqtt_connected ): for stock in self.tickerlist: ticker = stock.ticker print "querrying for ", ticker now = datetime.datetime.now() open_time = now.replace( hour=self.openhour, minute=self.openmin, second=0 ) close_time = now.replace( hour=self.closehour, minute=self.closemin, second=0 ) open_day = False for day in self.tradingdow: if ( day == datetime.datetime.today().weekday() ): open_day = True if (( now > open_time) and ( now < close_time ) and open_day): self.mqttc.publish( self.basetopic + "/" + ticker + "/name", stock.name, qos = 2, retain=True) try: price = ystockquote.get_price( ticker ) self.mqttc.publish( self.basetopic + "/" + ticker + "/price", price, qos = 2, retain=True) #TODO add previous close value!!!!!!1 change = ystockquote.get_change( ticker ) self.mqttc.publish( self.basetopic + "/" + ticker + "/change", change, qos = 2, retain=True) volume = ystockquote.get_volume( ticker ) self.mqttc.publish( self.basetopic + "/" + ticker + "/volume", volume, qos = 2, retain=True) if ( stock.high_low ): yrhigh = ystockquote.get_52_week_high( ticker ) self.mqttc.publish( self.basetopic + "/" + ticker + "/yrhigh", yrhigh, qos = 2, retain=True) yrlow = ystockquote.get_52_week_low( ticker ) self.mqttc.publish( self.basetopic + "/" + ticker + "/yrlow", yrlow, qos = 2, retain=True) if ( stock.mavg ): avg50 = ystockquote.get_50day_moving_avg( ticker ) self.mqttc.publish( self.basetopic + "/" + ticker + "/50day-ma", avg50, qos = 2, retain=True) avg200 = ystockquote.get_200day_moving_avg( ticker ) self.mqttc.publish( self.basetopic + "/" + ticker + "/200day-ma", avg200, qos = 2, retain=True) self.mqttc.publish( self.basetopic + "/" + ticker + "/time", time.strftime( "%x %X" ), qos = 2, retain=True) except: print "querry error in ystockquote." else: print "Markets closed" self.mqttc.publish( self.basetopic + "/" + ticker + "/null", "", qos = 2, retain=True)
def test_get_200day_moving_avg(self): value = ystockquote.get_200day_moving_avg(self.symbol) self.assertIsInstance(value, str)
def strategyone(): bought_mode = False global server logging.info( "Strategy One:SPX OPENS MORE THAN 2% ABOVE ITS CLOSE OF 3 DAYS AGO AND IS ABOVE 200DMA. BUY ON CLOSE. SELL 6 DAYS" ) logging.info("Current price of SPX " + ystockquote.get_price("SPY")) #chance of working is 79% logging.info('\n') todays_date = time.strftime("%Y-%m-%d") logging.info("todays date is " + todays_date) date_N_days_ago = datetime.now() - timedelta(days=3) f = str(date_N_days_ago) date_three_days_ago = f.split(' ')[0] logging.info("the date three days ago is " + date_three_days_ago) historical_price = ystockquote.get_historical_prices( "SPY", date_three_days_ago, todays_date) histpricelist = historical_price.items() histpricelist.sort() try: logging.info("the close from 3 days ago is " + historical_price[date_three_days_ago]['Close']) threedaysagoclose = float( historical_price[date_three_days_ago]['Close']) except Exception: logging.info( "looks like the last closing day was a non trading day fixing the problem" ) logging.info("the close from the last trading day is " + str(histpricelist[0][1]['Close'])) threedaysagoclose = float(histpricelist[0][1]['Close']) content = urllib.urlopen("https://www.google.com/finance?q=SPY").read() m = re.search('(?:snapfield="open".Open\n<\/td>\n<td class="val">)(.*)', content) try: logging.info("The open today is " + m.group(1)) todaysopen = float(m.group(1)) except Exception: logging.info( "You got an excpetion trying to get open today, most likely due to the fact that the market isnt open yet, waiting two hours then trying again" ) time.sleep(7200) logging.info("The open today is " + m.group(1)) todaysopen = float(m.group(1)) logging.info( "the current price is this perecntage above or below its close of 3 days ago " + str((float(ystockquote.get_price("SPY")) / threedaysagoclose) - 1)) logging.info("the current 200 dma is " + str(float(ystockquote.get_200day_moving_avg("SPY")))) logging.info("when these show TRUE TRUE FALSE YOU SHOULD BUY") logging.info("todays open is 2% above its close from three days ago") logging.info(todaysopen >= 1.02 * threedaysagoclose) logging.info("Current price is above its 200 dma") logging.info( float(ystockquote.get_price("SPY")) > float( ystockquote.get_200day_moving_avg("SPY"))) logging.info("bought mode is set to") logging.info(bought_mode) if todaysopen >= 1.02 * threedaysagoclose and float( ystockquote.get_price("SPY")) > float( ystockquote.get_200day_moving_avg("SPY")) and (bought_mode is False): price_I_bought_at = float(ystockquote.get_price("SPY")) current_money = server.current_money() logging.info("Current money is" + str(current_money)) total_to_spend = (.79 / 3) * current_money logging.info('\n') logging.info("total to spend is" + str(total_to_spend)) shares = int(ystockquote.get_price("SPY")) / int(total_to_spend) logging.info('\n') logging.info("# of shares" + int(shares)) logging.info('\n') server.buy_stock("SPY", int(ystockquote.get_price("SPY")), shares) day_N_days_from_now = datetime.now() + timedelta(days=6) l = str(day_N_days_from_now) day_to_sell = l.split(' ')[0] logging.info(day_to_sell) bought_mode = True sendemail(from_addr='*****@*****.**', to_addr_list=['*****@*****.**'], cc_addr_list='', subject='StrategyOne Executed', message='StrategyOne has been Executed', login='******', password='******') if bought_mode is True and day_to_sell == todays_date: logging.info("going to sell") server.sell("SPY", int(ystockquote.get_price("SPY")), shares) bought_mode = False if bought_mode is True and (float( ystockquote.get_price("SPY"))) <= (price_I_bought_at * .93): logging.info("I hit my stop") server.sell("SPY", int(ystockquote.get_price("SPY")), shares) bought_mode = False threading.Timer(84600, strategyone).start()
summaryPage = requests.get('http://finance.yahoo.com/q/pr?s=' + symbol, stream=False) d = summaryPage.content soup1 = BeautifulSoup(d) getSummary = soup1.find_all('p') price = str(ystockquote.get_price(symbol)) change = str(ystockquote.get_change(symbol)) volume = str(ystockquote.get_volume(symbol)) stockExchange = str(ystockquote.get_stock_exchange(symbol)) marketCap = str(ystockquote.get_market_cap(symbol)) bValue = str(ystockquote.get_book_value(symbol)) ebitda = str(ystockquote.get_ebitda(symbol)) mvavg = str(ystockquote.get_200day_moving_avg(symbol)) High = str(ystockquote.get_52_week_high(symbol)) Low = str(ystockquote.get_52_week_low(symbol)) commaData = [] data=[price, change, volume, stockExchange, mvavg, bValue, ebitda, marketCap, High, Low] for individData in data: commaInt = humanize.intcomma(individData) commaData.append(commaInt) if float(price) > 0.00: try: