UNPKG

@backtest/framework

Version:

Backtesting trading strategies in TypeScript / JavaScript

237 lines 12 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.insertResult = insertResult; exports.getAllStrategyResults = getAllStrategyResults; exports.getAllStrategyResultNames = getAllStrategyResultNames; exports.getResult = getResult; exports.deleteStrategyResult = deleteStrategyResult; const prisma_historical_data_1 = require("./prisma-historical-data"); const client_1 = require("@prisma/client"); const error_1 = require("./error"); const logger = __importStar(require("./logger")); const prisma = new client_1.PrismaClient({ datasources: { db: { url: process.env.DATABASE_URL || 'file:./db/backtest.db' } } }); function insertResult(result) { return __awaiter(this, void 0, void 0, function* () { try { const strategyResult = yield prisma.strategyResult.create({ data: { name: result.name, historicalDataName: result.historicalDataName, strategyName: result.strategyName, startTime: BigInt(result.startTime), endTime: BigInt(result.endTime), txFee: result.txFee, slippage: result.slippage, startingAmount: result.startingAmount, params: JSON.stringify(result.params) } }); const runMetaData = yield prisma.runMetaData.create({ data: Object.assign(Object.assign({}, result.runMetaData), { highestAmountDate: BigInt(result.runMetaData.highestAmountDate), lowestAmountDate: BigInt(result.runMetaData.lowestAmountDate), startingAssetAmountDate: BigInt(result.runMetaData.startingAssetAmountDate), endingAssetAmountDate: BigInt(result.runMetaData.endingAssetAmountDate), highestAssetAmountDate: BigInt(result.runMetaData.highestAssetAmountDate), lowestAssetAmountDate: BigInt(result.runMetaData.lowestAssetAmountDate), StrategyResultId: strategyResult.id }) }); const allOrders = result.allOrders.map((order) => (Object.assign(Object.assign({}, order), { note: order.note || '', time: BigInt(order.time) }))); const allWorths = result.allWorths.map((worth) => (Object.assign(Object.assign({}, worth), { time: BigInt(worth.time) }))); yield prisma.strategyResult.update({ where: { id: strategyResult.id }, data: { runMetaDataId: runMetaData.id, allOrders: { create: allOrders }, allWorths: { create: allWorths } } }); logger.debug(`Successfully inserted result: ${result.name}`); return true; } catch (error) { throw new error_1.BacktestError(`Problem inserting result with error: ${error}`, error_1.ErrorCode.Insert); } }); } function getAllStrategyResults() { return __awaiter(this, void 0, void 0, function* () { try { const strategyResults = yield prisma.strategyResult.findMany({ select: { name: true } }); const results = yield Promise.all(strategyResults.map((result) => __awaiter(this, void 0, void 0, function* () { return yield getResult(result.name); }))); return results; } catch (error) { throw new error_1.BacktestError(`Problem getting results with error: ${error}`, error_1.ErrorCode.Retrieve); } }); } function getAllStrategyResultNames() { return __awaiter(this, void 0, void 0, function* () { try { const strategyResults = yield prisma.strategyResult.findMany({ select: { name: true } }); const names = strategyResults.map((result) => result.name); return names; } catch (error) { throw new error_1.BacktestError(`Problem getting results with error: ${error}`, error_1.ErrorCode.Retrieve); } }); } function getResult(name) { return __awaiter(this, void 0, void 0, function* () { try { const strategyResult = yield prisma.strategyResult.findUnique({ where: { name }, include: { runMetaData: true, allOrders: true, allWorths: true } }); if (!strategyResult) { throw new error_1.BacktestError(`StrategyResult with name ${name} does not exist`, error_1.ErrorCode.NotFound); } const candlesResult = yield (0, prisma_historical_data_1.getCandles)(strategyResult.historicalDataName); if (!candlesResult) { throw new error_1.BacktestError(`Candles with name ${strategyResult.historicalDataName} not found`, error_1.ErrorCode.NotFound); } let filteredCandles = candlesResult.candles.filter((candle) => candle.openTime >= Number(strategyResult.startTime) && candle.closeTime <= Number(strategyResult.endTime)); const allOrders = strategyResult.allOrders.map((order) => { const { id, StrategyResultId } = order, rest = __rest(order, ["id", "StrategyResultId"]); return Object.assign(Object.assign({}, rest), { time: Number(rest.time) }); }); const allWorths = strategyResult.allWorths.map((worth) => { const { id, StrategyResultId } = worth, rest = __rest(worth, ["id", "StrategyResultId"]); return Object.assign(Object.assign({}, rest), { time: Number(rest.time) }); }); if (strategyResult.runMetaData) { const _a = strategyResult.runMetaData, { id, StrategyResultId } = _a, runMetaDataRest = __rest(_a, ["id", "StrategyResultId"]); const runMetaData = Object.assign(Object.assign({}, runMetaDataRest), { highestAmountDate: Number(runMetaDataRest.highestAmountDate), lowestAmountDate: Number(runMetaDataRest.lowestAmountDate), highestAssetAmountDate: Number(runMetaDataRest.highestAssetAmountDate), lowestAssetAmountDate: Number(runMetaDataRest.lowestAssetAmountDate), startingAssetAmountDate: Number(runMetaDataRest.startingAssetAmountDate), endingAssetAmountDate: Number(runMetaDataRest.endingAssetAmountDate) }); const { id: strategyResultId } = strategyResult, strategyResultRest = __rest(strategyResult, ["id"]); const getResult = Object.assign(Object.assign({}, strategyResultRest), { startTime: Number(strategyResultRest.startTime), endTime: Number(strategyResultRest.endTime), params: JSON.parse(strategyResultRest.params), candleMetaData: candlesResult.metaCandles[0], candles: filteredCandles, allOrders, allWorths, runMetaData }); return getResult; } else { throw new error_1.BacktestError('Impossible to found runMetaData', error_1.ErrorCode.Retrieve); } } catch (error) { throw new error_1.BacktestError(`Failed to get result with error ${error}`, error_1.ErrorCode.Retrieve); } }); } function deleteStrategyResult(name) { return __awaiter(this, void 0, void 0, function* () { try { const strategyResult = yield prisma.strategyResult.findUnique({ where: { name } }); if (!strategyResult) { throw new error_1.BacktestError(`StrategyResult with name ${name} does not exist`, error_1.ErrorCode.NotFound); } const strategyResultId = strategyResult.id; try { yield prisma.order.deleteMany({ where: { StrategyResultId: strategyResultId } }); } catch (error) { throw new error_1.BacktestError(`Failed to delete related Order records for StrategyResult with name: ${name}. Error: ${error}`, error_1.ErrorCode.Delete); } try { yield prisma.worth.deleteMany({ where: { StrategyResultId: strategyResultId } }); } catch (error) { throw new error_1.BacktestError(`Failed to delete related Worth records for StrategyResult with name: ${name}. Error: ${error}`, error_1.ErrorCode.Delete); } try { yield prisma.runMetaData.deleteMany({ where: { StrategyResultId: strategyResultId } }); } catch (error) { throw new error_1.BacktestError(`Failed to delete related RunMetaData records for StrategyResult with name: ${name}. Error: ${error}`, error_1.ErrorCode.Delete); } yield prisma.strategyResult.delete({ where: { id: strategyResultId } }); logger.debug(`Successfully deleted ${name}`); return true; } catch (error) { throw new error_1.BacktestError(`Failed to delete StrategyResult with name: ${name}. Error: ${error}`, error_1.ErrorCode.Delete); } }); } //# sourceMappingURL=prisma-results.js.map