def find_x(start_date, xxx_todo_changeme1, xxx_todo_changeme2): (stop_start, stop_end, stop_step) = xxx_todo_changeme1 (days_min, days_max, days_step) = xxx_todo_changeme2 loopback = LoopbackTrend(None, start_date, None, -stop_start, stop_start, days_min, 'MA5', 'V_MA10', 1) loopback.init() saved_stocks = loopback.stocks stop_rate = stop_start rets = [] while stop_rate <= stop_end: days = days_min while days <= days_max: try: math_expt, stock_cnt, op_cnt, avg_hold_days = loopback_trend( saved_stocks, None, start_date, None, -stop_rate, stop_rate, days, 'MA5', 'V_MA10', 1) if op_cnt >= loopback.trade_days(): rets.append((math_expt, stock_cnt, op_cnt, avg_hold_days, stop_rate, days)) rets = sorted(rets, key=lambda x: x[0], reverse=True) best = rets[0] print_parameter('Current best parameter', best) except: log.exception('error happen') days += days_step stop_rate += stop_step rets = sorted(rets, key=lambda x: x[0], reverse=True) best = rets[0] print_parameter('Best parameter', best) for ret in rets: print_parameter('All items', ret) with open('results.dat', 'w') as f: pickle.dump(rets, f)
def _init_basis(self): for _ in range(10): try: self.basis = fetch_basis() break except: log.exception('Re-fetch basis') time.sleep(10)
def loopback_stock(info): (loopback, stock) = info try: result = loopback.loopback_one(stock) stock.set_loopback_result(result) except Exception as e: log.exception('Error occur when looping back %s', stock.code) return stock
def process_stock(stock): try: if len(stock.df) == 0: return stock stock.process() except: log.exception('Error occur when processing %s', stock.code) return stock
def run_loopback_one_by_code(self, code, name=None): if name is None: try: name = StockBasisMgr.get_stock_name(code) except Exception: log.exception('') name = 'unknown' info = {'name': name} stock = Stock(code, info) return self.run_loopback_one(stock)
def do_once(): d_from = '2016-01-01' try: start = time.time() stocks, underestimate_stocks = find_chances(d_from, None, 22) end = time.time() write_chances(stocks, underestimate_stocks, end - start) except KeyboardInterrupt: raise except: log.exception('Fail to write chances') raise