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

Perform Buy Sell

Estimated reading: 4 minutes 213 views

Summary: How to perform buy / sells within your strategy.

About

A strategy must contain times to buy and sell. This is where the power of the application comes into place as all these buy / sells are tracked and given to nice reports upon completion of running the strategy.

Buy / Sell Params

Both buy and sell functions can take the following parameters in which none are mandatory
Param Type Default Info Mandatory
price
int
current candle close
Price of which you will buy the asset
False
position
string
long
Can be "long" or "short" based on desired buy / sell direction
False
amount
int / string
All asset amount
Amount of asset to buy can be number or string (string must include a %)
False
baseAmount
int
none
buy / sell with the base amount (cannot use with "amount")
False
stopLoss
int
none
Price of which a stop loss will trigger (all will be sold on the stop loss)
False
takeProfit
int
none
Price of which a take profit will trigger (all will be sold on the take profit price)
False


Rules

  • You CAN short and long at the same time but they need to be two seperate calls
  • If you try to buy or sell but you already bought or sold everything, the buy or sell will be skipped and not recorded
  • You cannot use “amount” and “baseAmount” params together
  • If in a short and a long you cannot use “amount” or “baseAmount” when selling without specifying a position.
  • You cannot use stopLoss if you long and short at the same time
  • You cannot use takeProfit if you long and short at the same time
  • amount param can be a number or a string, if a string it must contain a percent sign “%”

Examples

Example 1


// Lets say you have $1000 and want to trade bitcoin
// Put in a long order and buy all which is $1000 worth of bitcoin
await buy()
// Lets say you bought bitcoin and are now worth $1000
// Put in a sell order and sell all which is $1000 worth of bitcoin
await sell()


Example 2


// Lets say you have $1000 and want to trade bitcoin
// Put in a long order of $400 worth of bitcoin
await buy({ amount: 400 })
// Same thing can be achieved here
await buy({ amount: "40%" })
// Lets say you bought bitcoin and are now worth $1000
// in bitcoin and put in a sell order of $400 worth of bitcoin
await sell({ amount: 400 })
// Same thing can be achieved here
await sell({ amount: "40%" })


Example 3


// Lets say you have $1000 and
// want to trade bitcoin
// Put a short order in with all which 
// is $1000 and a stop loss at $24,000
await buy({ position: "short", stopLoss: 24000 })
// The application is smart enough to know that its a 
// short and only sell if a candles high goes above $24,000
// Lets say you bought bitcoin in a long and a 
// short but only want to sell some of the shorted amount
// Put in a sell order to sell 50% of the shorted amount
await sell({ position: "short", amount "50%"})

Example 4


// Lets say you have $1000 and bitcoin 
// is currently worth $2000
// Put a long order in of .25 
// bitcoin which is $500 worth
await buy({ baseAmount: .25 })
// This can also be achieved by doing
await buy({ amount: 500 })
// You cannot use amount with baseAmount 
// in the same buy / sell call
// Lets say you bought bitcoin and are worth 
// $1000 and bitcoin is worth $2000 
// Put a short order in of .25
// bitcoin which is $500 worth
await sell({ baseAmount: .25 })
// This can also be achieved by doing
await sell({ amount: 500 })


Example 5


// Lets say you have $1000 and bitcoins close 
// was $2100 but you had a trigger to buy at $2000
// Put a long order in of $1000 worth 
// but bitcoin at a price of $2000
await buy({ price: 2000 })
// Lets say you bought and bitcoin is worth 
// $2200 but you had a trigger to sell at $2100
// Put a sell order in where 
// bitcoin is worth $2100
await sell({ price: 2100 })


Full Example


// MANDATORY IMPORT
import { BTH } from "../infra/interfaces"
// Check if higher than last candle
export async function checkLastCandle(bth: BTH) {
// Get last 2 candle closes
const candles = bth.getCandles('close', 0, 2);
// Buy if current candle close is more than last candle
if (candles[0] > candles[1]) {
await buy()
}
// Sell if current candle is less than last candle
else {
await sell()
}
}

In this example the common question is lets say im on a daily timeframe and Monday is higher then Sunday so i buy with everything, then Tuesday is higher Monday so im supposed to buy again but cant as i already invested everything. In this case the buy will trigger but be ignored and not recorded in the ending orders results. Same can be said with the sell.

Leave a Comment

Share this Doc

Perform Buy Sell

Or copy link

CONTENTS