With BacktestJS you can

Easily download candle data from binance and export to a csv.

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

Using Globals

Estimated reading: 3 minutes 89 views

Summary: How and why to use global variables within your trading strategy.

About

Using globals in this context is not so related to BacktestJS but more to regular Javascript usage. Never the less it is used quite a bit in strategies and will be explained in more detail below.

The reason why globals are used a lot is because anything within the function itself will reset every candle as it gets run inside a for loop.

For example if you make a variable called counter and do something like below, counter will always be 0 or 1 but never 2.


import { BTH } from "../infra/interfaces"
export async function buyThreeTimes(bth: BTH) {
// Define counter and add 1 to it
let counter = 0
counter++
// This will always be 1 as the counter 
// var gets reset to 0 every candle iteration
console.log(counter)
}

In the example below we can see more of a use case in which you might use a global.


Example


// Define imports
import { BTH } from "../infra/interfaces"
// DEFINE A GLOBAL HERE (ABOVE THE FUNCTION)
let higherThanLastCandleCount = 0
// Buy 3 times strategy
export async function buyThreeTimes(bth: BTH) {
// Get last 2 candle closes
const candles = bth.getCandles('close', 0, 2)
// Buy if this close is more than last close
if (candles[0] > candles[1]) {
// Buy with 30% of total first time
if (higherThanLastCandleCount === 0) await bth.buy({ amount: '30%' })
// Buy with 20% of total second time
else if (higherThanLastCandleCount === 1) await bth.buy({ amount: bth.orderBook.preBoughtQuoteAmount * .2 })
// Buy with 10% of total first time
else if (higherThanLastCandleCount === 2) await bth.buy({ amount: bth.orderBook.preBoughtQuoteAmount * .1 })
// Increase higherThanLastCandleCount
higherThanLastCandleCount++
}
// Sell if current candle is less than last candle
else {
await bth.sell()
// Reset higher than last candle count
higherThanLastCandleCount = 0
}
}

In this example we buy if the current candle is more than the last candle. But we don’t buy with all of our money, we buy with 30% of our total the first time, then 20% if it happens next candle and lastly 10% of total if it happens for a third time. If the current candle is less than last candle then all gets sold and we reset the higher than last candle count to 0. Heres more of a breakdown

Total amount = $1000

  1. if current candle is more than last candle buy with $300
  2. If current candle is again higher than last candle buy with $200
  3. If current candle is again higher than last candle buy with $100
  4. If current candle is again higher than last candle don’t do anything (You only invest up to 60% of total)
  5. If current candle is lower than last candle sell and reset amountOfTimesBought to 0
  6. Lets say you sold and are now worth $2000
  7. if current candle is more than last candle buy with $600
  8. If current candle is again higher than last candle buy with $400
  9. If current candle is lower than last candle sell and reset higherThanLastCandleCount to 0 (You only invested 50% of the total this time)
  10. etc… the cycle continues

Leave a Comment

Share this Doc

Using Globals

Or copy link

CONTENTS