def strategy_statistics(strategy_name, strategy_count, stock_model=''): all_qr = QR.objects(strategy_name=strategy_name) if not all_qr: print('Wrong Strategy Name!') return trading_date = QR.objects().distinct('date') trading_date.sort() trading_date = trading_date[0 - strategy_count:] bt_result = {} for d in trading_date: bt_result[str(d.date())] = back_test_success(strategy_name, d, stock_model) frame = DataFrame(bt_result) pd.set_option('display.width', 200) pd.set_option('display.max_rows', strategy_count + 100) print( frame.reindex([ 'count', 'one_back_test', 'one_yield', 'three_back_test', 'three_yield', 'five_back_test', 'five_yield', 'ten_back_test', 'ten_yield' ]).T) pd.set_option('display.width', None) pd.set_option('display.max_rows', None)
def back_test_success(strategy_name, date, stock_model=''): if stock_model: cursor = QR.objects( Q(strategy_name=strategy_name) & Q(date=date) & Q(stock_number__startswith=stock_model)) else: cursor = QR.objects(Q(strategy_name=strategy_name) & Q(date=date)) res_by_date = {} for k, v in back_test_attr.iteritems(): qualified_sample = [qr for qr in cursor if qr[k] is not None] if not qualified_sample: res_by_date['count'] = cursor.count() continue succ_sample = [q for q in qualified_sample if q[k] is True] res_by_date[k] = str( round(float(len(succ_sample)) / float(len(qualified_sample)), 4) * 100) + '%' yield_exp = 0.0 if 'long' in strategy_name: for i in qualified_sample: yield_exp += (i[v[0]] - i.init_price) / i.init_price elif 'short' in strategy_name: for i in qualified_sample: yield_exp += (i.init_price - i[v[0]]) / i.init_price res_by_date[v[1]] = str( round(yield_exp / len(qualified_sample), 4) * 100) + '%' res_by_date['count'] = cursor.count() return res_by_date
def back_test_success(strategy_name, date): cursor = QR.objects(Q(strategy_name=strategy_name) & Q(date=date)) res_by_date = {} for k, v in back_test_attr.iteritems(): qualified_sample = [qr for qr in cursor if qr[k] is not None] if not qualified_sample: continue succ_sample = [q for q in qualified_sample if q[k] is True] res_by_date[k] = str( round(float(len(succ_sample)) / float(len(qualified_sample)), 4) * 100) + '%' yield_expectation = 0.0 if 'long' in strategy_name: for i in qualified_sample: yield_expectation += (i[v[0]] - i.init_price) / i.init_price elif 'short' in strategy_name: for i in qualified_sample: yield_expectation += (i.init_price - i[v[0]]) / i.init_price res_by_date[v[1]] = str( round(yield_expectation / len(qualified_sample), 4) * 100) + '%' return res_by_date
def strategy_statistics(strategy_name): all_qr = QR.objects(strategy_name=strategy_name) if not all_qr: print "Wrong Strategy Name!" return trading_date = SDT.objects(stock_number__startswith="300").distinct("date") trading_date.sort() bt_result = {} for d in trading_date: bt_result[str(d.date())] = back_test_success(strategy_name, d) frame = DataFrame(bt_result) pd.set_option("display.width", 200) pd.set_option("display.max_rows", 400) print frame.reindex( [ "count", "one_back_test", "one_yield_expectation", "three_back_test", "three_yield_expectation", "five_back_test", "five_yield_expectation", ] ).T pd.set_option("display.width", None) pd.set_option("display.max_rows", None)
def check_duplicate(stock_number, date, strategy_name): cursor = QR.objects(Q(stock_number=stock_number) & Q(date=date) & Q(strategy_name=strategy_name)) if cursor: return True else: return False
def is_duplicate(stock_number, date, strategy_name): try: cursor = QR.objects( Q(stock_number=stock_number) & Q(date=date) & Q(strategy_name=strategy_name)) except Exception, e: logging.error('Query %s QR failed:%s' % (stock_number, e))
def check_duplicate(stock_number, date, strategy_name): cursor = QR.objects( Q(stock_number=stock_number) & Q(date=date) & Q(strategy_name=strategy_name)) if cursor: return True else: return False
def back_testing(): quant_result = QR.objects() for i in quant_result: for t in test_pattern: if i[test_pattern[t]['test']] is None and i[test_pattern[t]['price']] is None: try: test_by_day(i, t) except Exception, e: logging.error('Error occur when back testing %s: %s' % (i.stock_number, e))
def strategy_statistics(strategy_name, strategy_count): all_qr = QR.objects(strategy_name=strategy_name) if not all_qr: print 'Wrong Strategy Name!' return trading_date = QR.objects().distinct('date') trading_date.sort() trading_date = trading_date[0-strategy_count:] bt_result = {} for d in trading_date: bt_result[str(d.date())] = back_test_success(strategy_name, d) frame = DataFrame(bt_result) pd.set_option('display.width', 200) pd.set_option('display.max_rows', 2000) print frame.reindex(['count', 'one_back_test', 'one_yield_expectation', 'three_back_test', 'three_yield_expectation', 'five_back_test', 'five_yield_expectation']).T pd.set_option('display.width', None) pd.set_option('display.max_rows', None)
def check_duplicate(qr): if isinstance(qr, QR): try: cursor = QR.objects(Q(stock_number=qr.stock_number) & Q(strategy_name=qr.strategy_name) & Q(date=qr.date)) except Exception, e: logging.error('Error when check dupliate %s strategy %s date %s: %s' % (qr.stock_number, qr.strategy_name, qr.date, e)) if cursor: return True else: return False
def check_duplicate_strategy(qr): if isinstance(qr, QR): try: cursor = QR.objects(Q(stock_number=qr.stock_number) & Q(strategy_name=qr.strategy_name) & Q(date=qr.date)) except Exception, e: logging.error('Error when check dupliate %s strategy %s date %s: %s' % (qr.stock_number, qr.strategy_name, qr.date, e)) if cursor: return True else: return False
def generate_statement(): today = datetime.date.today() quant_res = QR.objects(date=today) if not quant_res: return file_name = today.strftime('%Y-%m-%d') + '.txt' strategy_li = [] for i in quant_res: if i.strategy_name not in strategy_li: strategy_li.append(i.strategy_name) current_path = dirname(abspath(__file__)) for s in strategy_li: strategy_path = join(current_path, s) if not isdir(strategy_path): mkdir(strategy_path) file_path = join(strategy_path, file_name) if exists(file_path): continue with codecs.open(file_path, 'w+', 'utf-8') as fd: fd.write('-------------%s-------------\n' % str(QR.objects(Q(strategy_name=s) & Q(date=today)).count())) for i in trade_sector: cursor = QR.objects(Q(strategy_name=s) & Q(date=today) & Q(stock_number__startswith=i['market_code'])).order_by('stock_number') fd.write(u'-----------%s--%s-----------\n' % (i['market_name'], cursor.count())) for j in cursor: fd.write('%s %s %s\n' % (j.stock_number, j.stock_name, j.init_price)) chdir(current_path) system('git add -A') system("git commit -m \'" + today.strftime('%Y-%m-%d') + " statement\'") system('git push origin') time.sleep(10) system('git push origin')
def strategy_statistics(strategy_name): all_qr = QR.objects(strategy_name=strategy_name) if not all_qr: print 'Wrong Strategy Name!' return trading_date = SDT.objects(stock_number__startswith='300').distinct('date') trading_date.sort() bt_result = {} for d in trading_date: bt_result[str(d.date())] = back_test_success(strategy_name, d) frame = DataFrame(bt_result) pd.set_option('display.width', 200) pd.set_option('display.max_rows', 200) print frame.reindex([ 'count', 'one_back_test', 'one_yield_expectation', 'three_back_test', 'three_yield_expectation', 'five_back_test', 'five_yield_expectation' ]).T pd.set_option('display.width', None) pd.set_option('display.max_rows', None)
def strategy_statistics(strategy_name): all_qr = QR.objects(strategy_name=strategy_name) if not all_qr: print 'Wrong Strategy Name!' return all_date = [] for i in all_qr: if i.date not in all_date: all_date.append(i.date) all_date.sort() bt_result = {} for d in all_date: bt_result[str(d.date())] = back_test_success(strategy_name, d) frame = DataFrame(bt_result) pd.set_option('display.width', 200) print frame.reindex(['one_back_test', 'one_yield_expectation', 'three_back_test', 'three_yield_expectation', 'five_back_test', 'five_yield_expectation']).T pd.set_option('display.width', None)
def back_test_success(strategy_name, date): cursor = QR.objects(Q(strategy_name=strategy_name) & Q(date=date)) res_by_date = {} for k, v in back_test_attr.iteritems(): qualified_sample = [qr for qr in cursor if qr[k] is not None] if not qualified_sample: continue succ_sample = [q for q in qualified_sample if q[k] is True] res_by_date[k] = str(round(float(len(succ_sample))/float(len(qualified_sample)), 4) * 100) + '%' yield_expectation = 0.0 if 'long' in strategy_name: for i in qualified_sample: yield_expectation += (i[v[0]] - i.init_price)/i.init_price elif 'short' in strategy_name: for i in qualified_sample: yield_expectation += (i.init_price - i[v[0]])/i.init_price res_by_date[v[1]] = str(round(yield_expectation/len(qualified_sample), 4) * 100) + '%' return res_by_date
def strategy_statistics(strategy_name): all_qr = QR.objects(strategy_name=strategy_name) if not all_qr: print 'Wrong Strategy Name!' return all_date = [] for i in all_qr: if i.date not in all_date: all_date.append(i.date) all_date.sort() bt_result = {} for d in all_date: bt_result[str(d.date())] = back_test_success(strategy_name, d) frame = DataFrame(bt_result) pd.set_option('display.width', 200) print frame.reindex([ 'one_back_test', 'one_yield_expectation', 'three_back_test', 'three_yield_expectation', 'five_back_test', 'five_yield_expectation' ]).T pd.set_option('display.width', None)
def is_duplicate(stock_number, date, strategy_name): try: cursor = QR.objects(Q(stock_number=stock_number) & Q(date=date) & Q(strategy_name=strategy_name)) except Exception, e: logging.error("Query %s QR failed:%s" % (stock_number, e))