def get_macd_data_by_ifcode(cls, date, ifcode, pre_time): today = str2day(date, DATE_DISPLAY_FORMAT) pre = pre_day(today) pre_str = pre.strftime('%Y-%m-%d') _ifcode = get_ifcode(pre) # 如果是上一个合约 _rs = [] if _ifcode != ifcode: _where = '''%s where (`time` >= '%s 14:%s:00' and `time` <= '%s 15:00:00') ''' % (_ifcode, pre_str, pre_time, pre_str) _sql = '''select `inserttime`, `time` as time_index, `now` as price, `volume` from ''' + _where + ''' order by inserttime;''' _rs = yhdb(_sql) where = '''%s where (`time` >= '%s 14:%s:00' and `time` <= '%s 15:00:00') or (`time` >= '%s 09:30:00' and `time` <= '%s 11:30:00') or (`time` >= '%s 13:00:00' and `time` <= '%s 15:00:00') ''' % (ifcode, pre_str, pre_time, pre_str, date, date, date, date) sql = '''select `inserttime`, `time` as time_index, `now` as price, `volume` from ''' + where + ''' order by inserttime;''' rs = yhdb(sql) if _rs: rs = _rs + rs return [obj_to_list(cls(*r)) for r in rs if r]
def macd(date, ifcode, period_short, period_long): if not date or not ifcode or not period_short or not period_long: return {'error': u'params is fault'} date = str2day(date, DATE_DISPLAY_FORMAT).strftime(DATE_DISPLAY_FORMAT) period_short = int(period_short) period_long = int(period_long) return DataAnalyzer.macd_chart(date, ifcode, period_short, period_long)
def boll(date, ifcode, period_short, period_long): if not date or not ifcode or not period_short or not period_long: return {'error': u'params is fault'} date = str2day(date, DATE_DISPLAY_FORMAT) period_short = int(period_short) period_long = int(period_long) return BollCalculator.boll_chart(date, ifcode, period_short, period_long)
def macd_report(): if not req.args.get('ifcode'): ifcode = get_ifcode(str2day(days_ago(0))) display_num = 6 period_short = 12 period_long = 26 trans_amount = 1 return redirect('/backtest/report/macd?ifcode=%s&period_short=%s&period_long=%s&display_num=%s&trans_amount=%s' % \ (ifcode, period_short, period_long, display_num, trans_amount)) display_num = int(req.args.get('display_num')) ifcode = req.args.get('ifcode') period_short = int(req.args.get('period_short')) period_long = int(req.args.get('period_long')) trans_amount = int(req.args.get('trans_amount')) date_list = ifcode_day_map(ifcode) if len(date_list) > int(display_num): date_list = date_list[:int(display_num)] rs = [] total = 0 pos_num = 0 nag_num = 0 trans_total_num = 0 date_list = ifcode_day_map(ifcode) for day in date_list: profit_infos = DataAnalyzer.macd(day, ifcode, period_short, period_long) profit_all = 0 trans_num = (len(profit_infos) - 1) / 2 trans_total_num += trans_num for item in profit_infos: if item['gain'] != '-': profit_all += int(item['gain']) * trans_amount rs.append({'date': day, 'profit': profit_all, 'trans_num': trans_num}) total += profit_all if profit_all >= 0: pos_num += 1 elif profit_all < 0: nag_num += 1 if nag_num == 0: profit_rate = pos_num else: profit_rate = pos_num * 1.0 / nag_num fees = trans_total_num * 2300 real_profit = total - fees ctx = { 'req': req, 'total': total, 'profit_rate': profit_rate, 'trans_total_num': trans_total_num, 'fees': fees, 'real_profit': real_profit, 'detail': rs, 'trans_amount': trans_amount } return tpl('/backtest/macd_report.html', **ctx)
def ema(date, ifcode, period_short, period_long): if not date or not ifcode or not period_short or not period_long: return {'error': u'params is fault'} date = str2day(date, DATE_DISPLAY_FORMAT).strftime(DATE_DISPLAY_FORMAT) period_short = int(period_short) period_long = int(period_long) return FittingDataCalculator.ema_chart(date, ifcode, period_short, period_long)
def fitting_data(): if not req.args.get('date') or not req.args.get( 'ifcode') or not req.args.get('period'): return {'error': u'params is fault'} date = str2day(req.args.get('date')).strftime(DATE_DISPLAY_FORMAT) ifcode = req.args.get('ifcode') period = int(req.args.get('period')) return FittingDataCalculator.fitting_series(period, date, ifcode)
def boll(): if not req.args.get('date'): date = days_ago(0) period_short = 50 period_long = 80 today = str2day(days_ago(0)) ifcode = get_ifcode(today) return redirect('/dashboard/boll?date=%s&ifcode=%s&period_short=%s&period_long=%s' % (date, ifcode, period_short, period_long)) date = str2day(req.args.get('date')).strftime(DATE_DISPLAY_FORMAT) ifcode = req.args.get('ifcode') period_short = int(req.args.get('period_short')) period_long = int(req.args.get('period_long')) ctx = {'req': req, 'date': date, 'ifcode': ifcode } ctx = {'req': req} return tpl('/realtime/boll.html', **ctx)
def ema_report(): if not req.args.get('ifcode'): ifcode = get_ifcode(str2day(days_ago(0))) display_num = 10 return redirect('/backtest/report/ema?ifcode=%s&display_num=%s' % (ifcode, display_num)) ctx = {'req': req} return tpl('/backtest/ema_report.html', **ctx)
def macd(): if not req.args.get('date'): date = day2str(get_week_day(str2day(days_ago(1)))) period_short = 12 period_long = 26 today = str2day(days_ago(0)) ifcode = get_ifcode(today) return redirect( '/backtest/macd?date=%s&ifcode=%s&period_short=%s&period_long=%s' % (date, ifcode, period_short, period_long)) _date = str2day(req.args.get('date')) ifcode = req.args.get('ifcode') date = _date.strftime(DATE_DISPLAY_FORMAT) if _date.weekday() in [5, 6]: ctx = { 'req': req, 'date': date, 'ifcode': ifcode, 'profit_infos': [], 'profit_all': 0 } return tpl('/backtest/macd.html', **ctx) period_short = int(req.args.get('period_short')) period_long = int(req.args.get('period_long')) profit_infos = DataAnalyzer.macd(date, ifcode, period_short, period_long) profit_all = 0 for item in profit_infos: if item['gain'] != '-': profit_all += int(item['gain']) ctx = { 'req': req, 'date': date, 'ifcode': ifcode, 'profit_infos': profit_infos, 'profit_all': profit_all } return tpl('/backtest/macd.html', **ctx)
def raw_data(date, ifcode): if not date or not ifcode: return {'error': u'params is fault'} date = str2day(date, DATE_DISPLAY_FORMAT).strftime(DATE_DISPLAY_FORMAT) return FittingDataCalculator.raw_series(date, ifcode)