Ejemplo n.º 1
0
async def _report(strategy):
    global error_complete
    try:
        while True:
            await asyncio.sleep(1)
            if error_complete == 1:
                return
            clock = strategy.portfolio.context.clock
            if clock.current_timestamp > clock.end_timestamp:
                clock.run_end = time.time()
                strategy.portfolio.context.completed_time = round(
                    clock.run_end - clock.run_start, 4)
                # print('【handle总耗时】:{} s'.format(strategy.portfolio.context.completed_time))
                strategy.simple_report()
                strategy.complete_report()
                break

            if strategy.frequency in ['d', '1d', 'day', '1day']:
                if clock.bars > Config.bars_length():  #这个在分钟线里怎么解决出错的问题
                    clock.reset_bars()
                    strategy.simple_report()
            elif strategy.frequency in ['m', '1m', 'min', 'minute']:
                if clock.bars > Config.bars_length_1m():
                    clock.reset_bars()
                    strategy.simple_report()

            elif strategy.frequency in ['5m', '5mins', '5minutes']:
                if clock.bars > Config.bars_length_5m():
                    clock.reset_bars()
                    strategy.simple_report()

    except Exception as e:
        error_complete = 1
        raise e
Ejemplo n.º 2
0
def create_universe(universe=None):
    """
    :param universe: ['BTC/USDT.okex', 'ETH/BTC.okex']/'csi5'
    :return:
    """
    if universe:
        if universe in Config.benchmarks():
            return IndexUniverse(universe).get()
        else:
            return StaticUniverse(universe).get()
Ejemplo n.º 3
0
def output(report):
    if Config.mode() == 'jupyter':
        display(report)

    else:
        if isinstance(report, dict):
            for key, value in report.items():
                print(key, value)
        else:
            print(report)
Ejemplo n.º 4
0
def backtest(strategy, start, end, commission):
    """
    :param strategy: strategy
    :param start: '2019-06-01'  # 回测起始时间
    :param end: '2019-07-01'    # 回测结束时间
    :param commission: {'buy': 0.001, 'sell': 0.001, 'open': 0.001, 'close': 0.001}
    :return: portfolio
    """
    global error_complete
    error_complete = 0
    strategy._initialize(strategy, start, end, commission)
    loop = asyncio.get_event_loop()
    if Config.mode() == 'jupyter':
        loop.create_task(_handle(strategy))
        loop.create_task(_report(strategy))
    else:
        loop.run_until_complete(
            asyncio.gather(_handle(strategy), _report(strategy)))
        loop.close()
Ejemplo n.º 5
0
async def _handle(strategy):
    global error_complete
    try:
        while True:
            clock = strategy.portfolio.context.clock
            timenow = time.time()
            if (timenow - clock.datestart) > Config.heartbeat_timelength():
                clock.reset_datestart()
                output('heartbeating')
            if error_complete == 1:
                return
            if clock.current_timestamp > clock.end_timestamp:
                break
            error_complete = await strategy._handle_data()
            clock.next()
            await asyncio.sleep(0)
    except Exception as e:
        error_complete = 1
        raise e
Ejemplo n.º 6
0
async def _report(strategy):
    global error_complete
    try:
        while True:
            await asyncio.sleep(1)
            if error_complete == 1:
                return
            clock = strategy.portfolio.context.clock
            if clock.current_timestamp > clock.end_timestamp:
                clock.run_end = time.time()
                strategy.portfolio.context.completed_time = round(
                    clock.run_end - clock.run_start, 4)
                # print('【handle总耗时】:{} s'.format(strategy.portfolio.context.completed_time))
                strategy.simple_report()
                strategy.complete_report()
                break
            if clock.bars > Config.bars_length():
                clock.reset_bars()
                strategy.simple_report()
    except Exception as e:
        error_complete = 1
        raise e
Ejemplo n.º 7
0
def __get_path(exchange, symbol, freq, year):
    dir = Config.h5_root_dir() + '/' + exchange.lower() + '/' + freq.lower() + '/' + symbol.replace('/', '')
    # dir=dir.lower()
    if not os.path.exists(dir):
        os.makedirs(dir, 0o755)
    return dir + '/' + str(year)
Ejemplo n.º 8
0
import os
from matplotlib.font_manager import FontProperties
from vitu.data_api import API
from vitu.configuration import Config
from vitu.utils.log_utils import logger

api = API(Config.tushare_token())
log = logger

font = None
dirName = os.path.dirname(os.path.abspath(__file__))
font = FontProperties(fname=os.path.join(dirName, 'wqy-microhei.ttc'), size=10)
Ejemplo n.º 9
0
def get_date(exchange, symbol, freq, start_date, end_date):
    date = API(Config.tushare_token()).get_data(exchange, symbol, freq,
                                                start_date, end_date)
    return date
Ejemplo n.º 10
0
def get_price(exchange, symbol, freq, date):
    price = API(Config.tushare_token()).get_price(exchange, symbol, freq, date)
    return price