def get_all_trans(ifcode, ammout=1): rs = [] total = 0 pos_num = 0 nag_num = 0 trans_all = 0 date_list = ifcode_day_map(ifcode) for day in date_list: profit_infos = DataAnalyzer.boll(day, ifcode) profit_all = 0 trans_num = (len(profit_infos) - 1) / 2 trans_all += trans_num for item in profit_infos: if item['gain'] != '-': profit_all += int(item['gain']) * ammout rs.append({day: profit_all, 'trans_num': trans_num}) total += profit_all if profit_all >= 0: pos_num += 1 elif profit_all < 0 : nag_num += 1 pprint(rs) print '%s total: %s' % (ifcode, total) print '%s profit rate: %.2f' % (ifcode, pos_num*1.0/nag_num) print '%s trans all number: %s' % (ifcode, trans_all) print '%s fees : %s' % (ifcode, trans_all * 2300) print '%s real profit : %s' % (ifcode, total - trans_all * 2300)
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 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 main(ifcode, date): ''' ema_signals = DataAnalyzer.ema(date, ifcode, 480, 1400) macd_signals = DataAnalyzer.macd(date, ifcode, period_short=12, period_long=26, period_dif=9, \ pre_point=init_point, pre_time=init_time, offset=init_offset) conbine_sig(ema_signals, macd_signals) ''' boll_sig = DataAnalyzer.boll(date, ifcode) print len(boll_sig) for item in boll_sig: print item['event']
def analysis_by_param(params_conbine, ifcode, period_short, period_long, trans_amount): rs = {} for param, v in params_conbine.items(): pre_time = v[0] pre_offset = v[1] pre_point = v[2] print '************' print param rs[param] = DataAnalyzer.macd_if_analysis(ifcode, pre_point, pre_time, pre_offset, \ period_short=12, period_long=26, period_dif=9) _file = '%s/%s' % (macd_param_analysis_dir, ifcode) f = open(_file, 'w') f.write(simplejson.dumps(rs)) f.close()
def get_first_trans(ifcode): rs = [] total = 0 pos_num = 0 nag_num = 0 date_list = ifcode_day_map(ifcode) for day in date_list: profit_infos = DataAnalyzer.boll(day, ifcode) profit_first = 0 for item in profit_infos: if item['gain'] != '-': profit_first = int(item['gain']) break rs.append({day: profit_first}) if profit_first >= 0: pos_num += 1 elif profit_first < 0 : nag_num += 1 print '每天第一笔交易' pprint(rs) print '%s profit rate: %.2f' % (ifcode, pos_num*1.0/nag_num)
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)