bfx-hf-backtest
Advanced tools
Comparing version 1.1.3 to 2.0.0
'use strict' | ||
const { onCandle, onTrade } = require('bfx-hf-strategy') | ||
const { onCandle } = require('bfx-hf-strategy') | ||
const N_TRADES = 5 | ||
const DUST = 0.0000001 | ||
module.exports = async (btState = {}, candle = {}) => { | ||
const { strategy } = btState | ||
const { open, high, low, close, vol, mts } = candle | ||
let nextStrategyState = await onCandle(strategy, candle) | ||
const nextStrategyState = await onCandle(strategy, candle) | ||
if (!(vol === 0 || (open === close && high === low && open === high))) { | ||
const trades = [] | ||
let dir = Math.random() > 0.5 ? 1 : -1 | ||
let highReached = open === high | ||
let lowReached = open === low | ||
let currPrice = open | ||
let priceStep = dir === 1 // go to high or low | ||
? high === open ? 0 : (high - open) / N_TRADES | ||
: open === low ? 0 : (open - low) / N_TRADES | ||
trades.push({ | ||
amount: 0, | ||
price: currPrice, | ||
mts | ||
}) | ||
while (true) { | ||
// Handle hitting high point (go to low, or close) | ||
if (!highReached && Math.abs(currPrice - high) < DUST) { | ||
dir *= -1 | ||
highReached = true | ||
priceStep = lowReached | ||
? high === close ? 0 : (high - close) / N_TRADES // go to close | ||
: high === low ? 0 : (high - low) / N_TRADES // go to low | ||
// Handle hitting low point (go to high, or close) | ||
} else if (!lowReached && Math.abs(currPrice - low) < DUST) { | ||
dir *= -1 | ||
lowReached = true | ||
priceStep = highReached | ||
? close === low ? 0 : (close - low) / N_TRADES // go to close | ||
: high === low ? 0 : (high - low) / N_TRADES // go to high | ||
} else if (highReached && lowReached && Math.abs(currPrice - close) < DUST) { | ||
break | ||
} | ||
if (priceStep === 0) { | ||
break | ||
} | ||
currPrice += priceStep * dir | ||
trades.push({ | ||
amount: 0, // set evenly at the end | ||
price: +currPrice.toFixed(7), // starts with open | ||
mts | ||
}) | ||
} | ||
// Set trade amounts & pass to strategy | ||
const singleTradeAmount = vol / trades.length | ||
let trade | ||
for (let i = 0; i < trades.length; i += 1) { | ||
trade = trades[i] | ||
trade.amount = singleTradeAmount | ||
nextStrategyState = await onTrade(nextStrategyState, trade) | ||
btState.lastPrice = trade.price | ||
btState.nTrades += 1 | ||
btState.strategy = nextStrategyState | ||
} | ||
} | ||
return nextStrategyState | ||
} |
{ | ||
"name": "bfx-hf-backtest", | ||
"version": "1.1.3", | ||
"version": "2.0.0", | ||
"description": "HF backtesting logic module", | ||
@@ -23,4 +23,3 @@ "main": "index.js", | ||
"test": "npm run lint && npm run unit", | ||
"unit": "DEBUG='bfx:hf:*' NODE_PATH=lib/ NODE_ENV=test istanbul cover _mocha -- -R spec -b --recursive", | ||
"test-without-coverage": "NODE_ENV=test mocha -R spec -b --recursive", | ||
"unit": "NODE_ENV=test mocha -R spec -b --recursive", | ||
"exec_docs": "node_modules/jsdoc-to-markdown/bin/cli.js lib/*.js > docs/exec.md", | ||
@@ -55,14 +54,8 @@ "docs": "npm run exec_docs" | ||
"devDependencies": { | ||
"@hyperswarm/replicator": "^1.7.2", | ||
"async": "^3.2.0", | ||
"bitfinex-terminal-key-encoding": "^1.0.0", | ||
"bitfinex-terminal-terms-of-use": "^1.0.0", | ||
"bitfinex-terminal-terms-of-use": "^4.0.0", | ||
"dazaar": "^1.2.0", | ||
"eslint": "^4.19.1", | ||
"eslint-config-standard": "^7.0.0", | ||
"eslint-plugin-promise": "^3.5.0", | ||
"eslint-plugin-standard": "^2.0.1", | ||
"hyperbee": "0.0.18", | ||
"hyperbee": "^1.3.0", | ||
"hypercore": "^9.5.0", | ||
"istanbul": "^0.4.5", | ||
"jsdoc-to-markdown": "^5.0.1", | ||
@@ -69,0 +62,0 @@ "mocha": "^6.2.0", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
267031
9
46
9761
3