UNPKG

@backtest/command-line

Version:

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

57 lines 2.78 kB
"use strict"; // Must Define Params: lowSMA, highSMA, stopLoss 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 moving_averages_1 = require("../indicators/moving-averages"); exports.properties = { params: ['lowSMA', 'highSMA', 'stopLoss'], dynamicParams: false }; // Example 2 SMA Strategy with short buys and a stop loss function runStrategy(bth) { return __awaiter(this, void 0, void 0, function* () { // Get low / high SMA and the stop loss from input const lowSMAInput = bth.params.lowSMA; const highSMAInput = bth.params.highSMA; const stopLoss = bth.params.stopLoss; // Get last candles based on low and high SMA const lowSMACandles = yield bth.getCandles('close', lowSMAInput, 0); const highSMACandles = yield bth.getCandles('close', highSMAInput, 0); // Get low and high SMA const lowSMA = yield (0, moving_averages_1.indicatorSMA)(lowSMACandles, lowSMAInput); const highSMA = yield (0, moving_averages_1.indicatorSMA)(highSMACandles, highSMAInput); // Buy long if lowSMA crosses over the highSMA if (lowSMA > highSMA) { // Sell if bought into a short if (bth.orderBook.boughtShort) yield bth.sell(); // Buy if not bought into a long if (!bth.orderBook.boughtLong) { // Buy with a stop loss yield bth.buy({ stopLoss: bth.currentCandle.close * (1 - stopLoss / 100) }); } } // Buy short if lowSMA crosses under the highSMA else { // Sell if bought into a long if (bth.orderBook.boughtLong) yield bth.sell(); // Buy if not bought into a short if (!bth.orderBook.boughtShort) { // Buy short with a stop loss yield bth.buy({ position: 'short', stopLoss: bth.currentCandle.close * (1 + stopLoss / 100) }); } } }); } //# sourceMappingURL=ex3SMAShort.js.map