With BacktestJS you can

Get power by backtesting strategies with multi values

leaf leaf leaf Docy banner shape 01 Docy banner shape 02 Man illustration Flower illustration
Trading Strategy Examples

Triple SMA (Advanced Buy / Sell)

Estimated reading: 4 minutes 84 views

Summary: Code an advanced sma trading strategy with more advanced buy / sells.

About

This trading strategy is quite hard to explain and is not a very practical strategy but does show off some more advanced features.

Strategy Explanation

We will use 3 different sma that we define as low, mid and high. I then define a “position” based on where the low sma is. If the low sma is higher then both the mid and high sma the position is “high”, if the low sma is in the middle of the mid and high sma then its position is “mid” if the low sma is lower then the mid and high sma then its position is “low”.

In order to enter a long trade you must be in a “low” position. If the low sma goes above either the mid or high sma then buy with 30% of your value. If the low sma crosses both the mid and the high sma then buy again with 25% of your value. Lastly theres a third buy opportunity which i call the bonus buy. If you enter any long bid you start looking to see if the mid sma is higher then the high sma and if so do a one time buy of 10% of your total value.

The sell opportunity for a long comes if the low sma ever goes under the mid or high sma then sell all.

Now when it comes to buy and sell opportunities for shorting its the same as long but the exact opposite 🙂 .

I wont go into a huge explanation on the code below, challenge yourself to try to line the code up to the explanation above and reach out to the community if you have any questions!


// Must Define Params: lowSMA, midSMA, highSMA
// Define imports
import { BTH } from "../infra/interfaces"
import { indicatorSMA } from "../indicators/moving-averages"
// Define globals
let position = ''
let timesBought = 0
let bonusBuy = false
// Triple SMA Strategy
export async function tripleSMA(bth: BTH) {
// Get low 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 and high SMA
const lowSMACandles = await bth.getCandles('close', 0, lowSMAInput)
const midSMACandles = await bth.getCandles('close', 0, midSMAInput)
const highSMACandles = await bth.getCandles('close', 0, highSMAInput)
// Get low and high SMA
const lowSMA = await indicatorSMA(lowSMACandles, lowSMAInput, 1)
const midSMA = await indicatorSMA(midSMACandles, midSMAInput, 1)
const highSMA = await indicatorSMA(highSMACandles, highSMAInput, 1)
async function sellLongShort() {
await 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) await sellLongShort()
if (timesBought === 2) await sellLongShort()
}
// Sell Short Scenario
else if (bth.orderBook.boughtShort && (lowSMA > midSMA || lowSMA > highSMA)) {
if (timesBought === 1 && lowSMA > midSMA && lowSMA > highSMA) await sellLongShort()
if (timesBought === 2) await 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) {
await bth.buy({ amount: '30%' })
timesBought++
}
// Buy with 25% if low sma crosses mid and high sma
if (timesBought === 1 && lowSMA > midSMA && lowSMA > highSMA) {
await bth.buy({ amount: bth.orderBook.preBoughtQuoteAmount * .25 })
timesBought++
}
// Buy with 15% if mid sma crosses high sma
if (!bonusBuy && midSMA > highSMA) {
await bth.buy({ amount: bth.orderBook.preBoughtQuoteAmount * .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 low position
if (timesBought === 0) {
await 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) {
await bth.buy({ position: 'short', amount: bth.orderBook.preBoughtQuoteAmount * .25 })
timesBought++
}
// Buy with 15% if mid sma crosses high sma
if (!bonusBuy && midSMA < highSMA) {
await bth.buy({ position: 'short', amount: bth.orderBook.preBoughtQuoteAmount * .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'
}
}


Bonus

Some things that can make this trading strategy even more interesting is adding a stop loss param as well as making the buy amounts parameters. As of now the first buy is 30%, the second buy is 25% and the bonus buy is 10% but if parameterized it can be very easy to check which of these buy ins have the highest impact and if they should be higher or lower values (using the heatmap in the results will help with this greatly as you can sort by the params)

Leave a Comment

Share this Doc

Triple SMA (Advanced Buy / Sell)

Or copy link

CONTENTS