def update_predictions(): predictions = Prediction.select().where((Prediction.actual == 0)) for p in predictions: # Monday = 0, Sunday = 6 # If Friday or Saturday or Sunday then search for Monday if 4 <= p.time.weekday() <= 6: for i in [0, 1, 2, 3, 4, 5, 6]: price = Price.select(Price.current).where( (Price.time > p.time) & (fn.WEEKDAY(Price.time) == i) & (Price.company == p.company)).order_by( Price.id.desc()).limit(1) if len(price) > 0 or (p.time + timedelta(days=i)) > datetime.now(): break else: for i in [1, 2, 3, 4, 5, 6]: weekday = (p.time.weekday() + i) % 7 price = Price.select( Price.current, Price.time).where((Price.time > p.time) & (fn.WEEKDAY(Price.time) == weekday) & (Price.company == p.company)).order_by( Price.id.desc()).limit(1) if len(price) > 0 or p.time + timedelta( days=i) > datetime.now(): break price = price[0] if len(price) > 0 else None if price is not None: p.actual = price.current p.actual_price_time = price.time p.save()
def compare(days_count : int): TODAY_PRICE = 0 PAST_PRICE = 1 days = [day.date for day in Price.select().distinct(Price.date)] # Get all days if days_count >= len(days): print(f"Parser has only {len(days)} day(s) info") return elif days_count <= 0: print(f"Error with days_count <= 0") return else: for product in Product.select().execute(): today_prices = Price.select().where((Price.date == date.today()) & (Price.product == product.id)).order_by(Price.shop).execute() past_prices = Price.select().where((Price.date == days[-days_count-1]) & (Price.product == product.id)).order_by(Price.shop).execute() print('-------') for prices in zip(today_prices, past_prices): print("Price for {:s} has {:s} by {:.2f} in shop '{:s}' for {:s} user".format( prices[TODAY_PRICE].product.group, 'increased' if prices[TODAY_PRICE].price > prices[PAST_PRICE].price else 'decreased' if prices[TODAY_PRICE].price < prices[PAST_PRICE].price else 'not changed', prices[TODAY_PRICE].price - prices[PAST_PRICE].price if prices[TODAY_PRICE].price >= prices[PAST_PRICE].price else prices[PAST_PRICE].price - prices[TODAY_PRICE].price, prices[TODAY_PRICE].shop, 'authorized' if prices[TODAY_PRICE].authorized else 'not authorized'))
def parse_price(): request_green = requests.Session() request_evroopt = requests.Session() authorized_request_green = green_parser.authorize(requests.Session()) authorized_request_evroopt = e_parser.authorize(requests.Session()) if date.today() in [price.date for price in Price.select().execute()]: return else: products = Product.select().order_by(Product.id).execute() bar = Bar('Parse prices', max=len(products)) for product in products: with conn.atomic() as trn: try: main_parser(green_parser, request_green, product.id, product.link_g) main_parser(green_parser, authorized_request_green, product.id, product.link_g) main_parser(e_parser, request_evroopt, product.id, product.link_e) main_parser(e_parser, authorized_request_evroopt, product.id, product.link_e) except AttributeError: trn.rollback() finally: bar.next() bar.finish()
def get_analytics(date_from, date_to, ticker_code): trades = Price.select(Price.stock, Price.date, (Price.open - Price.close).alias("delta_open_close"), (Price.high - Price.low).alias("delta_high_low")).where( (Price.stock == ticker_code) & (Price.date.between(date_from, date_to))) return trades
def get_historical(ticker_code): prices = Price.select().where(Price.stock == ticker_code.upper()).order_by( Price.date.desc()) return prices