def ema_checks(stats: HourData, history: History, ema_threshold: float, reset_perc: float): if stats.ema_percent_diff_positive > ema_threshold: logging.info( f"Current price is outside threshold difference ({stats.formatted_info()})" ) return False logging.info( f"Current price not outside threshold ({stats.formatted_info()})") # If EMA hasn't been reset then check whether we should reset it if not history.ema_reset: if history.rising: target = history.price * (1 - reset_perc / 100) should_reset = stats.ema > target else: target = history.price * (1 + reset_perc / 100) should_reset = stats.ema < target if should_reset: logging.info("Resetting EMA") history.ema_reset = True history.save() return True
cb = Coinbase(Currencies.default(), args.interval) prices = cb.price_list() cur_price = prices[0] # Get history from last runs, use it to work out what test to make history = History(DATA_FILE) # Get stats from coinbase data # If change isn't large enough, then update history and exit stats = HourData(prices, EMA_NUM_HOURS) if Analysis.ema_checks(stats, history, EMA_THRESHOLD_PERCENT, EMA_RESET_PERCENT): sys.exit(1) if not Analysis.should_post(history, stats, prices, EMA_THRESHOLD_PERCENT): sys.exit(1) logging.info("Message should be posted, generating attachment") attachments = Slack.generate_post(prices, stats, Currencies.default()) image_url = SlackImages.get_image(stats.is_diff_positive) logging.info("Posting to slack") Slack.post_to_slack(BOT_NAME, image_url, "", attachments, SLACK_URL, SLACK_CHANNEL) history.price = stats.cur_price history.rising = stats.is_diff_positive history.ema_reset = False history.save() logging.info("Done")