@backtest/command-line
Version:
This project is a CLI build around Backtest, a library for trading developers
104 lines • 5.09 kB
JavaScript
// Must Define Params: lowSMA, midSMA, highSMA
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");
// Define globals
let position = '';
let timesBought = 0;
let bonusBuy = false;
exports.properties = {
params: ['lowSMA', 'midSMA', 'highSMA'],
dynamicParams: false
};
// Example 4 Triple SMA Strategy
function runStrategy(bth) {
return __awaiter(this, void 0, void 0, function* () {
// Get low, mid and high SMA from input
const lowSMAInput = bth.params.lowSMA;
const midSMAInput = bth.params.midSMA;
const highSMAInput = bth.params.highSMA;
// Get last candles based on low, mid and high SMA
const lowSMACandles = yield bth.getCandles('close', lowSMAInput, 0);
const midSMACandles = yield bth.getCandles('close', midSMAInput, 0);
const highSMACandles = yield bth.getCandles('close', highSMAInput, 0);
// Get low, mid and high SMA
const lowSMA = yield (0, moving_averages_1.indicatorSMA)(lowSMACandles, lowSMAInput, 1);
const midSMA = yield (0, moving_averages_1.indicatorSMA)(midSMACandles, midSMAInput, 1);
const highSMA = yield (0, moving_averages_1.indicatorSMA)(highSMACandles, highSMAInput, 1);
// Function to perfrom sell and define position
function sellLongShort() {
return __awaiter(this, void 0, void 0, function* () {
yield bth.sell();
timesBought = 0;
bonusBuy = false;
position = lowSMA > midSMA && lowSMA > highSMA ? 'high' : lowSMA < midSMA && lowSMA < highSMA ? 'low' : 'mid';
});
}
// Sell Long Scenario
if (bth.orderBook.boughtLong && (lowSMA < midSMA || lowSMA < highSMA)) {
if (timesBought === 1 && lowSMA < midSMA && lowSMA < highSMA)
yield sellLongShort();
if (timesBought === 2)
yield sellLongShort();
}
// Sell Short Scenario
else if (bth.orderBook.boughtShort && (lowSMA > midSMA || lowSMA > highSMA)) {
if (timesBought === 1 && lowSMA > midSMA && lowSMA > highSMA)
yield sellLongShort();
if (timesBought === 2)
yield sellLongShort();
}
// Buy Long Scenarios
if (position === 'low' && (lowSMA > midSMA || lowSMA > highSMA)) {
// Buy with 30% if low sma crosses mid or high sma from low position
if (timesBought === 0) {
yield bth.buy({ amount: '30%' });
timesBought++;
}
// Buy with 25% if low sma crosses mid and high sma
if (timesBought === 1 && lowSMA > midSMA && lowSMA > highSMA) {
yield bth.buy({ amount: bth.orderBook.preBoughtQuoteAmount * 0.25 });
timesBought++;
}
// Buy with 15% if mid sma crosses high sma
if (!bonusBuy && midSMA > highSMA) {
yield bth.buy({ amount: bth.orderBook.preBoughtQuoteAmount * 0.15 });
bonusBuy = true;
}
}
// Buy Short Scenarios
else if (position === 'high' && (lowSMA < midSMA || lowSMA < highSMA)) {
// Buy with 30% if low sma crosses mid or high sma from high position
if (timesBought === 0) {
yield bth.buy({ position: 'short', amount: '30%' });
timesBought++;
}
// Buy with 25% if low sma crosses mid and high sma
if (timesBought === 1 && lowSMA < midSMA && lowSMA < highSMA) {
yield bth.buy({ position: 'short', amount: bth.orderBook.preBoughtQuoteAmount * 0.25 });
timesBought++;
}
// Buy with 15% if mid sma crosses high sma
if (!bonusBuy && midSMA < highSMA) {
yield bth.buy({ position: 'short', amount: bth.orderBook.preBoughtQuoteAmount * 0.15 });
bonusBuy = true;
}
}
// Update the position if not bought in
if (!bth.orderBook.bought) {
position = lowSMA > midSMA && lowSMA > highSMA ? 'high' : lowSMA < midSMA && lowSMA < highSMA ? 'low' : 'mid';
}
});
}
//# sourceMappingURL=ex4TripleSMA.js.map
;