Esempio n. 1
0
def real2_list(args):
    td_db = get_mongodb(setup.trade_db_name)
    ss = td_db.find(si.STRATEGY_INSTANCE_COLLECTION_NAME, {"user": args.user})
    #pprint(ss)
    all_value = 0
    all_his_profit = 0
    all_flo_profit = 0
    all_commission = 0

    title_head_fmt = "%-30s  %10s%12s"
    head_fmt       = "%-30s  %10s(%10.0f)"

    title_profit_fmt = "%21s  %21s  %12s"
    profit_fmt       = "%12.2f(%6.2f%%)  %12.2f(%6.2f%%)  %12.2f"

    title_tail_fmt = "    %-60s  %-20s  %10s"


    print(title_head_fmt % ("instance_id", "value", "") + title_profit_fmt % ("history_profit", "floating_profit", "commission") + title_tail_fmt % ("config_path", "exchange", "status"))
    for s in ss:
        instance_id = s["instance_id"]
        exchange_name = s["exchange"]
        value = s["value"]
        config_path = s["config_path"]
        if "status" in s:
            status = s["status"]
        else:
            status = ""
        if status != args.status and status != "":
            continue

        all_value += value
        profit_info = ""
        try:
            config = xq.get_strategy_config(config_path)
            symbol = config['symbol']
            realEngine = RealEngine(instance_id, exchange_name, config, value)
            orders = realEngine.get_orders(symbol)
            pst_info = realEngine.get_pst_by_orders(orders)
            history_profit, history_profit_rate, history_commission = realEngine.get_history(pst_info)
            all_his_profit += history_profit
            floating_profit, floating_profit_rate, floating_commission, cur_price = realEngine.get_floating(symbol, pst_info)
            all_flo_profit += floating_profit
            commission = history_commission + floating_commission
            all_commission += commission
            profit_info = profit_fmt % (history_profit, history_profit_rate*100, floating_profit, floating_profit_rate*100, commission)

        except Exception as ept:
            profit_info = "error:  %s" % (ept)

        print(head_fmt % (instance_id, value, (value+history_profit+floating_profit)) + profit_info + title_tail_fmt % (config_path, exchange_name, status))

    if args.stat:
        print(head_fmt % ("all", all_value, all_value+all_his_profit+all_flo_profit) + profit_fmt % (all_his_profit, all_his_profit/all_value*100, all_flo_profit, all_flo_profit/all_value*100, all_commission))
Esempio n. 2
0
def get_strategy_instance(sii):
    db = get_mongodb(setup.trade_db_name)
    db.ensure_index(STRATEGY_INSTANCE_COLLECTION_NAME, [("instance_id", 1)])

    instances = db.find(STRATEGY_INSTANCE_COLLECTION_NAME,
                        {"instance_id": sii})
    #print(instances)
    if len(instances) is 0:
        print("strategy instance id (%s) not exist!" % (sii))
        exit(1)
    elif len(instances) > 1:
        exit(1)
    return instances[0]
Esempio n. 3
0
    def __init__(self, exchange):
        super().__init__(exchange)

        self.md_db = get_mongodb(exchange)

        self.tick_time = None

        self.k1ms_cache = {}
        self.k1ms_cache_s_time = {}
        '''
        self.k1ds_cache = None
        self.k1ds_cache_s_time = None
        self.k1ds_cache_e_time = None
        '''

        self.klines_cache = {}
Esempio n. 4
0
    def __init__(self, exchange_name, kline_data_type=kl.KLINE_DATA_TYPE_JSON):
        super().__init__(exchange_name)
        self.md_db = get_mongodb(exchange_name)
        self.kline_data_type = kline_data_type

        self.tick_time = None

        self.k1ms_cache = {}
        self.k1ms_cache_s_time = {}
        '''
        self.k1ds_cache = None
        self.k1ds_cache_s_time = None
        self.k1ds_cache_e_time = None
        '''

        self.klines_cache = {}
Esempio n. 5
0
    def __init__(self, instance_id, config, value, db_orders_name=None):
        self.instance_id = instance_id
        self.config = config
        self.value = value

        self.td_db = get_mongodb(db_order_name)

        if db_orders_name:
            self.db_orders_name = db_orders_name
            self.td_db.ensure_index(db_orders_name, [("instance_id", 1),
                                                     ("symbol", 1)])

        self.can_open_long_time = None
        self.can_open_short_time = None

        self.tp_cc = {"base_open": 0}
Esempio n. 6
0
    def __init__(self,
                 instance_id,
                 exchange_name,
                 config,
                 value,
                 log_switch=False):
        super().__init__(instance_id, config, value, log_switch)

        self.db_orders_name = DB_ORDERS_NAME
        self.td_db = get_mongodb(setup.trade_db_name)
        self.td_db.ensure_index(self.db_orders_name, [("instance_id", 1),
                                                      ("symbol", 1)])

        self.__exchange = create_exchange(exchange_name)
        if not self.__exchange:
            print("Wrong exchange name: %s" % exchange_name)
            exit(1)

        self.md = ExchangeMD(self.__exchange, kl.KLINE_DATA_TYPE_JSON)
Esempio n. 7
0
def real2_list(args):
    td_db = get_mongodb(setup.trade_db_name)
    ss = td_db.find(si.STRATEGY_INSTANCE_COLLECTION_NAME, {"user": args.user})
    #pprint(ss)
    title1_fmt = "%-30s  %10s"
    title2_fmt = "%-60s  %-20s  %10s"
    print((title1_fmt + "    %s    " + title2_fmt) %
          ("instance_id", "value", "     history_profit       floating_profit",
           "config_path", "exchange", "status"))
    for s in ss:
        instance_id = s["instance_id"]
        exchange_name = s["exchange"]
        value = s["value"]
        config_path = s["config_path"]
        if "status" in s:
            status = s["status"]
        else:
            status = ""
        if status != args.status and status != "":
            continue

        profit_info = ""
        try:
            config = xq.get_strategy_config(config_path)
            symbol = config['symbol']
            realEngine = RealEngine(instance_id, exchange_name, config, value)
            orders = realEngine.get_orders(symbol)
            pst_info = realEngine.get_pst_by_orders(orders)
            history_profit, history_profit_rate, history_commission = realEngine.get_history(
                pst_info)
            floating_profit, floating_profit_rate, floating_commission, cur_price = realEngine.get_floating(
                symbol, pst_info)
            profit_info = "%10.2f(%6.2f%%)   %10.2f(%6.2f%%)" % (
                history_profit, history_profit_rate * 100, floating_profit,
                floating_profit_rate * 100)

        except Exception as ept:
            profit_info = "error:  %s" % (ept)

        print((title1_fmt + "    %s    " + title2_fmt) %
              (instance_id, value, profit_info, config_path, exchange_name,
               status))
Esempio n. 8
0
if __name__ == "__main__":
    parser = add_common_arguments('Binance Importer')
    args = parser.parse_args()
    # print(args)
    if not (args.s and args.k and args.m):
        parser.print_help()
        exit(1)

    symbol = args.s
    interval = timedelta(seconds=xq.get_interval_seconds(args.k))

    collection = xq.get_kline_collection(symbol, args.k)
    #print("collection: ", collection)

    db = get_mongodb(args.m)
    db.ensure_index(collection, [("open_time",1)], unique=True)

    if args.r:
        start_time, end_time = split_time_range(args.r)
    else:
        # 续接db中最后一条记录,至今天之前
        klines = db.find_sort(collection, {}, 'open_time', -1, 1)
        if len(klines) > 0:
            start_time = (datetime.fromtimestamp(klines[0]["open_time"]/1000) + interval)
        else:
            start_time = None
        end_time = datetime.now()

    exchange = create_exchange(args.m)
    if not exchange:
Esempio n. 9
0
import utils.tools as ts
import common.xquant as xq
import common.kline as kl
import common.log as log
from exchange.exchange import get_exchange_names
from exchange.binanceExchange import BinanceExchange
import exchange.okex
from engine.backtestengine import BackTest
from engine.signalengine import TestSignal
from chart.chart import chart, chart_add_all_argument
from db.mongodb import get_mongodb
from md.dbmd import DBMD

BACKTEST_INSTANCES_COLLECTION_NAME = 'bt_instances'

bt_db = get_mongodb('backtest')


def create_instance_id():
    instance_id = datetime.now().strftime("%Y%m%d-%H%M%S_") + str(
        uuid.uuid1())  # 每次回测都是一个独立的实例
    print('new id of instance: %s' % instance_id)
    return instance_id


def get_time_range(md, symbol, time_range):
    if time_range:
        start_time, end_time = ts.parse_date_range(time_range)
    else:
        start_time = end_time = None
    oldest_time = md.get_oldest_time(symbol, kl.KLINE_INTERVAL_1MINUTE)
Esempio n. 10
0
def delete_strategy_instance(query):
    db = get_mongodb(setup.trade_db_name)
    db.delete_one(STRATEGY_INSTANCE_COLLECTION_NAME, query)
Esempio n. 11
0
def update_strategy_instance(query, record):
    db = get_mongodb(setup.trade_db_name)
    db.update(STRATEGY_INSTANCE_COLLECTION_NAME, query, record)
Esempio n. 12
0
def add_strategy_instance(record):
    db = get_mongodb(setup.trade_db_name)
    db.insert_one(STRATEGY_INSTANCE_COLLECTION_NAME, record)