Basic Usage
Installation
npm install @backtest/framework
Quick Start
- Create a basic strategy:
import { BTH } from '@backtest/framework'
export async function runStrategy(bth: BTH) {
const closePrice = await bth.getCandles('close', 1)
const sma20 = await bth.getCandles('close', 20, 0)
if (closePrice > sma20) {
await bth.buy()
} else {
await bth.sell()
}
}
- Configure environment:
DATABASE_URL=file:./db/backtest.db
FRAMEWORK_LOG_LEVEL=ERROR
- Run the strategy:
import { runStrategy } from '@backtest/framework'
const result = await runStrategy({
strategyName: 'simpleMovingAverage',
historicalData: ['BTCEUR-1d'],
params: { period: 20 },
startingAmount: 1000
})
Historical Data
Import data from CSV or download from exchanges:
import { downloadHistoricalData, importFileCSV } from '@backtest/framework'
// Download from exchange
await downloadHistoricalData('BTCEUR', {
interval: '1d',
start: '2023-01-01',
end: '2023-12-31'
})
// Import from CSV
await importFileCSV('./data/BTCEUR.csv', {
symbol: 'BTCEUR',
interval: '1d'
})