def calculation(): platos = MacdDict().getAll(); rateData = RateLoader().fetchLast(platos); for key in platos: plato = platos[key]; stockData = rateData.getSdf(plato.pair, plato.period) plato.calculateLast(stockData) del rateData return getAllPlatos()
def test(): end = int(time()) - DAY begin = end - 3 * DAY rates = RateLoader().fetchPeriods(DEFAULT_PAIR, begin, end, [1]).getSdf(DEFAULT_PAIR, 1) rates = RTBacktest.prepareRates(rates) penter = Plato(DEFAULT_PAIR, 12, 26, 9, 60) pexit = Plato(DEFAULT_PAIR, 12, 26, 9, 60) def add_d(rates: StockDataFrame): rates['d'] = to_datetime(rates.index, unit='s', utc=True) rates.d = rates.d.dt.strftime('%Y-%m-%d %H:%M:%S') return rates def gen(): maxp = max(penter.period, pexit.period) for i in range(len(rates) - 40 * maxp): yield rates[i:i + 40 * maxp] ts = time() deals = RTBacktest.calculateDeals(gen(), penter, pexit) print(deals) print(f'Total: {round(time()-ts, 3)}')
def calculationSingle(key): plato = MacdDict().get(key) if plato is None: return jsonpify({'message': 'No plato found', 'status': '1'}) stockData = RateLoader()\ .fetchLast({0: plato})\ .getSdf(plato.pair, plato.period) plato.calculateLast(stockData) return jsonpify(plato.json())
def runBacktestById(id): bt = Backtest.find(int(id)) if bt is None: return jsonpify(dict( result=0, message=f'There is no backtest #{id} found' )) penter = Plato('btc_usd', bt.buy_fast, bt.buy_slow, bt.buy_signal, bt.buy_period) pexit = Plato('btc_usd', bt.sell_fast, bt.sell_slow, bt.sell_signal, bt.sell_period) tsFrom = bt.ts_start tsTo = bt.ts_end if bt.is_rt: offset = max(penter.period, pexit.period) * 60 * 40 rates = RateLoader().fetchPeriods('btc_usd', tsFrom - offset, tsTo, [1]).getSdf('btc_usd', 1) statistics = RTBacktest(penter, pexit, rates, tsFrom, tsTo).run() else: rates = RateLoader().fetch(dict(enter=penter, exit=pexit), tsFrom, tsTo) calculation = BacktestHelper.calculate( penter=penter, pexit=pexit, denter=rates.getSdf(penter.pair, penter.period), dexit=rates.getSdf(pexit.pair, pexit.period), begin=tsFrom, end=tsTo, force=True ) _, _, _, _, statistics, _ = calculation bt.status = 3 bt.data = f'{dumps(dict(statistics=statistics))}' bt.total_month6 = round(float(statistics['4']['profit']), 2) bt.total_month3 = round(float(statistics['3']['profit']), 2) bt.total_month1 = round(float(statistics['2']['profit']), 2) bt.total_week = round(float(statistics['1']['profit']), 2) bt.save() return jsonpify(dict( result=1, message='Backtest successfully calculated' ))
fname = join(basedir, 'btrun', str(bt.id)) touch(fname) penter = Plato('btc_usd', bt.buy_fast, bt.buy_slow, bt.buy_signal, bt.buy_period) pexit = Plato('btc_usd', bt.sell_fast, bt.sell_slow, bt.sell_signal, bt.sell_period) tsFrom = bt.ts_start tsTo = bt.ts_end if bt.is_rt: offset = max(penter.period, pexit.period) * 60 * 40 rates = RateLoader().fetchPeriods('btc_usd', tsFrom - offset, tsTo, [1]).getSdf('btc_usd', 1) statistics = RTBacktest(penter, pexit, rates, tsFrom, tsTo).run() else: rates = RateLoader().fetch(dict(enter=penter, exit=pexit), tsFrom, tsTo) calculation = BacktestHelper.calculate( penter=penter, pexit=pexit, denter=rates.getSdf(penter.pair, penter.period), dexit=rates.getSdf(pexit.pair, pexit.period), begin=tsFrom, end=tsTo, force=True)
def runBacktest(): params = request.args for key in ['pair', 'from', 'to', 'coeffs[0]', 'coeffs[1]']: if params.get(key) is None: return jsonpify({'result': False, 'message': f'Param "{key}" is required'}) pair = params['pair'] tsFrom = int(params['from']) tsTo = int(params['to']) enter_params = list(map(int, params['coeffs[0]'].split('_'))) penter = Plato(pair, *enter_params) exit_params = list(map(int, params['coeffs[1]'].split('_'))) pexit = Plato(pair, *exit_params) rateData = RateLoader().fetch(dict(enter=penter, exit=pexit), tsFrom, tsTo) calculation = BacktestHelper.calculate( penter=penter, pexit=pexit, denter=rateData.getSdf(penter.pair, penter.period), dexit=rateData.getSdf(pexit.pair, pexit.period), begin=tsFrom, end=tsTo, force=True ) _, _, _, _, statistics, _ = calculation bt = Backtest( buy_fast=penter.fast, buy_slow=penter.slow, buy_signal=penter.signal, buy_period=penter.period, sell_fast=pexit.fast, sell_slow=pexit.slow, sell_signal=pexit.signal, sell_period=pexit.period, status='3', type='1', data=f'{dumps(dict(statistics=statistics))}', extend='|main.backtest|', name=f'Buy: {penter.key(":")}, Sell: {pexit.key(":")}', total_month6=round(float(statistics['4']['profit']), 2), total_month3=round(float(statistics['3']['profit']), 2), total_month1=round(float(statistics['2']['profit']), 2), total_week=round(float(statistics['1']['profit']), 2), ts_start=tsFrom, ts_end=tsTo, is_rt=0, active=1 ) bt.save() return jsonpify(dict( result=1, message='Backtest successfully calculated', backtest=dict( id=bt.id ) ))
end = int(time()) if args.till is not None: end = int(args.till) end -= end % 60 begin = end - INTERVALS[args.goback] begin_with_offset = begin - 1440 * 60 * 40 print(f'Start RT backtest') print( f'Period: {tsToTime(begin)} -> {tsToTime(end)}, wOffset: {tsToTime(begin_with_offset)}' ) ts = time() rates = RateLoader().fetchPeriods(args.pair, begin_with_offset, end, [1]).getSdf(args.pair, 1) print(f'Rates loaded (~{len(rates)}) in {round(time()-ts, 3)}s') generator = GenCustom('btc_usd', rates, begin, end, params) backtests = [] ts = time() pool = Pool(processes=8, maxtasksperchild=10) backtests = pool.starmap(calculate, generator.getItems()) pool.close() pool.join() #for item in generator.getItems(): # backtests.append(calculate(*item))