Skip to content

Commit

Permalink
Update deps
Browse files Browse the repository at this point in the history
  • Loading branch information
pegaltier committed Dec 15, 2024
1 parent 65f1d14 commit 29d85b9
Show file tree
Hide file tree
Showing 7 changed files with 472 additions and 737 deletions.
1,129 changes: 440 additions & 689 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 13 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
"version": "1.0.0",
"description": "Free and easy way to write backtest trading strategies in TS / JS with built in candle data retrieval",
"main": "dist/main.js",
"keywords": ["backtest", "finance", "trading", "download historical candle data", "indicators", "multi value", "multi symbol"],
"keywords": [
"backtest",
"finance",
"trading",
"download historical candle data",
"indicators",
"multi value",
"multi symbol"
],
"scripts": {
"start": "npx ts-node src/main.ts",
"dev": "npx ts-node src/main.ts",
Expand All @@ -30,17 +38,17 @@
"license": "Apache-2.0",
"dependencies": {
"@prisma/client": "^5.0.0",
"axios": "^1.4.0",
"axios": "^1.7.9",
"chalk": "^4.1.0",
"cli-progress": "^3.12.0",
"csvtojson": "^2.0.10",
"express": "^4.18.2",
"express": "^4.21.2",
"fuzzy": "^0.1.3",
"indicatorts": "^2.2.1",
"inquirer": "^8.2.5",
"inquirer-autocomplete-prompt": "^2.0.0",
"inquirer-date-prompt": "^2.0.1",
"lightweight-charts": "^4.0.1",
"tulind": "^0.8.20"
"lightweight-charts": "^4.2.2"
},
"devDependencies": {
"@types/cli-progress": "^3.11.5",
Expand Down
42 changes: 9 additions & 33 deletions src/indicators/moving-averages.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,17 @@
// ----------------------------------------------------
// | MOVING AVERAGE INDICATORS |
// ----------------------------------------------------

// ----------------------------------------------------
// | GLOBALS |
// ----------------------------------------------------

const tulind = require('tulind')

// ----------------------------------------------------
// | FUNCTIONS |
// ----------------------------------------------------
import { simpleMovingAverage, exponentialMovingAverage } from 'indicatorts';

// Get SMA
export async function indicatorSMA(candles: number[], length: number, limit?: number) {
// Call SMA function from tulind with the candles and desired sma length
const tulindReturn = await tulind.indicators.sma.indicator([candles], [length])

// Tulind can return a few things, in this case the SMA data is in the first item in its return
const sma = tulindReturn[0]
if (limit === undefined) limit = 1

// If you requested more SMA's then were generated return all that you have
export function indicatorSMA(candles: number[], length: number, limit: number = 1): number[] {
const sma = simpleMovingAverage(candles, { period: length });
if (limit >= sma.length) return sma
// Return the most recent SMA
if (limit === 1) return sma[sma.length - 1]
// Return only a specific amount of SMA's
if (limit === 1) return [sma[sma.length - 1]]
else return sma.slice(sma.length - limit)
}

// Get EMA
export async function indicatorEMA(candles: number[], length: number, limit?: number) {
const ema = (await tulind.indicators.ema.indicator([candles], [length]))[0]
if (limit !== undefined) {
if (limit >= ema.length) return ema
if (limit === 1) return ema[ema.length - 1]
else return ema.slice(ema.length - limit)
}
return ema
export function indicatorEMA(candles: number[], length: number, limit: number = 1): number[] {
const ema = exponentialMovingAverage(candles, { period: length });
if (limit >= ema.length) return ema
if (limit === 1) return [ema[ema.length - 1]]
else return ema.slice(ema.length - limit)
}
6 changes: 3 additions & 3 deletions src/strategies/ex1SMA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ export async function ex1SMA(bth: BTH) {
const highSMACandles = await bth.getCandles('close', 0, highSMAInput) // If there are not highSMA candles back will return array of 0's and block buy / sell until can return highSMA candles

// Get low and high SMA
const lowSMA = await indicatorSMA(lowSMACandles, lowSMAInput)
const highSMA = await indicatorSMA(highSMACandles, highSMAInput)
const lowSMA = indicatorSMA(lowSMACandles, lowSMAInput)
const highSMA = indicatorSMA(highSMACandles, highSMAInput)

// Buy if lowSMA crosses over the highSMA
if (lowSMA > highSMA) {
if (lowSMA[0] > highSMA[0]) {
await bth.buy()
}

Expand Down
4 changes: 2 additions & 2 deletions src/strategies/ex2SMAStopLoss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export async function ex2SMAStopLoss(bth: BTH) {
const highSMACandles = await bth.getCandles('close', 0, highSMAInput)

// Get low and high SMA
const lowSMA = await indicatorSMA(lowSMACandles, lowSMAInput)
const highSMA = await indicatorSMA(highSMACandles, highSMAInput)
const lowSMA = indicatorSMA(lowSMACandles, lowSMAInput)
const highSMA = indicatorSMA(highSMACandles, highSMAInput)

// Buy if lowSMA crosses over the highSMA
if (lowSMA > highSMA) {
Expand Down
4 changes: 2 additions & 2 deletions src/strategies/ex3SMAShort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export async function ex3SMAShort(bth: BTH) {
const highSMACandles = await bth.getCandles('close', 0, highSMAInput)

// Get low and high SMA
const lowSMA = await indicatorSMA(lowSMACandles, lowSMAInput)
const highSMA = await indicatorSMA(highSMACandles, highSMAInput)
const lowSMA = indicatorSMA(lowSMACandles, lowSMAInput)
const highSMA = indicatorSMA(highSMACandles, highSMAInput)

// Buy long if lowSMA crosses over the highSMA
if (lowSMA > highSMA) {
Expand Down
6 changes: 3 additions & 3 deletions src/strategies/ex4TripleSMA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export async function ex4TripleSMA(bth: BTH) {
const highSMACandles = await bth.getCandles('close', 0, highSMAInput)

// Get low, mid and high SMA
const lowSMA = await indicatorSMA(lowSMACandles, lowSMAInput, 1)
const midSMA = await indicatorSMA(midSMACandles, midSMAInput, 1)
const highSMA = await indicatorSMA(highSMACandles, highSMAInput, 1)
const lowSMA = indicatorSMA(lowSMACandles, lowSMAInput, 1)
const midSMA = indicatorSMA(midSMACandles, midSMAInput, 1)
const highSMA = indicatorSMA(highSMACandles, highSMAInput, 1)

// Function to perfrom sell and define position
async function sellLongShort() {
Expand Down

0 comments on commit 29d85b9

Please sign in to comment.