UNPKG

@backtest/command-line

Version:

This project is a CLI build around Backtest, a library for trading developers

92 lines 4.1 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.properties = void 0; exports.runStrategy = runStrategy; const indicator = __importStar(require("technicalindicators")); exports.properties = { params: ['lowSMA', 'highSMA'], dynamicParams: false }; function runStrategy(bth) { return __awaiter(this, void 0, void 0, function* () { const currentPrice = bth.currentCandle.close; if (bth.tradingCandle) { const takeProfit = 1.2; // short .8 const stopLoss = 0.95; // short 1.05 const percentSlippage = 1; const percentFee = 0.2; const lowSMAInput = bth.params.lowSMA; const highSMAInput = bth.params.highSMA; // Get last candles const lowSMACandles = yield bth.getCandles('close', lowSMAInput, 0); const highSMACandles = yield bth.getCandles('close', highSMAInput, 0); // Calculate low and high SMA const lowSMAs = indicator.SMA.calculate({ period: lowSMAInput, values: lowSMACandles }); const highSMAs = indicator.SMA.calculate({ period: highSMAInput, values: highSMACandles }); const lowSMA = lowSMAs[lowSMAs.length - 1]; const highSMA = highSMAs[highSMAs.length - 1]; // Buy if lowSMA crosses over the highSMA if (lowSMA > highSMA) { yield bth.buy({ position: 'short', // or 'long' (default) amount: '10%', // or baseAmount note: 'a simple note here', stopLoss: stopLoss * currentPrice, takeProfit: takeProfit * currentPrice, percentSlippage: percentSlippage, percentFee: percentFee }); } // Sell if lowSMA crosses under the highSMA else { yield bth.sell(); } } else { // do something else } }); } //# sourceMappingURL=testSMA.js.map