def market_status(): market_symbol = 'SPY' market_quotes = yahoo.get_historical_prices(market_symbol, start_date, end_date)[1:] market_direction = _close(market_quotes[0]) > _close(market_quotes[-1]) cur_quotes = yahoo.get_all(market_symbol) above_200ma = cur_quotes['price'] > cur_quotes['200day_moving_avg'] above_50ma = cur_quotes['price'] > cur_quotes['50day_moving_avg'] ma50_above_ma200 = cur_quotes['50day_moving_avg'] > cur_quotes['200day_moving_avg'] print "For period from %s to %s" % (start_date, end_date) print "SP 500 is %s " % ("UP" if market_direction else "DOWN") print "Current price level is %s its 50 MA" % ("above" if above_50ma else "below") print "Current price level is %s its 200 MA" % ("above" if above_200ma else "below") print "Current 50 MA is %s 200 MA" % ("above" if ma50_above_ma200 else "below") print
def measure_success(self, candidates, start_date, end_date, position): def register_profit(profit, signals): for signal in signals: total_profit = self.profits.setdefault(signal, 0.0) self.profits[signal] = (total_profit + profit) if profit > 0.0: self.losers.setdefault(signal, 0) count = self.winners.setdefault(signal, 0) self.winners[signal] = (count + 1) else: self.winners.setdefault(signal, 0) count = self.losers.setdefault(signal, 0) self.losers[signal] = (count + 1) def is_in_trade_range(close_price, open_price): delta = abs((close_price - open_price) / open_price) say("Delta is %.2f" % delta) return delta <= TRADE_RANGE def get_my_signals(signals, position): if position == LONG: my_signals = list(BULL_SIGNALS.intersection(signals)) if len(my_signals) > 0: return my_signals else: my_signals = list(BEAR_SIGNALS.intersection(signals)) if len(my_signals) > 0: return my_signals return None # historic quotes def is_a_trade(historic_quotes, position): start_day_close = _close(historic_quotes[0]) next_day_open = _open(historic_quotes[1]) if position == LONG: say("LONG: %.2f %.2f" % (start_day_close, next_day_open)) return next_day_open >= start_day_close and is_in_trade_range(start_day_close, next_day_open) else: say("SHORT: %.2f %.2f" % (start_day_close, next_day_open)) return next_day_open <= start_day_close and is_in_trade_range(start_day_close, next_day_open) def get_profit(historic_quotes, position): acquired_price = _open(historic_quotes[1]) exit_price = acquired_price previous_day_low = low(historic_quotes[0]) previous_day_high = high(historic_quotes[0]) for quote in historic_quotes[1:]: if position == LONG: today_low = low(quote) if today_low < (previous_day_low - 0.01): exit_price = previous_day_low break else: previous_day_low = today_low exit_price = _close(quote) else: today_high = high(quote) if today_high > (previous_day_high + 0.01): exit_price = previous_day_high break else: previous_day_high = today_high exit_price = _close(quote) if position == LONG: return (exit_price - acquired_price) / acquired_price else: return (acquired_price - exit_price) / acquired_price for symbol, signals in candidates.items(): my_signals = get_my_signals(signals, position) if my_signals is None: say("Skip symbol %s for non-interested signals %s " % (symbol, signals)) continue try: historic_quotes = yahoo.get_historical_prices(symbol, start_date, end_date)[1:] if historic_quotes: if is_a_trade(historic_quotes, position): profit = get_profit(historic_quotes, position) say("Register profit for %s" % symbol) register_profit(profit, my_signals) else: say("Skip symbol %s as it is out trading range " % (symbol)) else: print "No historic data for %s" % symbol except Exception, e: print "Failed to evaluate %s due to %s" % (symbol, e)
def _yahoo(self, symbol, time): today = datetime.datetime.today().date().strftime("%Y%m%d") #today = "20120428" start = time.strftime("%Y%m%d") data = ya.get_historical_prices(symbol, start, today) return np.sort(data, order='date')