-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
411 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,5 +10,6 @@ NEXT_PUBLIC_PROCESSOR_ENABLE_TRPC= | |
NEXT_PUBLIC_STATIC= | ||
DATABASE_URL="postgresql://postgres:[email protected]:5432/postgres" | ||
ADMIN_PASSWORD=opentrader | ||
|
||
NEXT_PUBLIC_CANDLES_SERVICE_API_URL="http://localhost:5001" | ||
NEXT_PUBLIC_CANDLES_SERVICE_API_KEY="opentrader" | ||
########## SHARED END ###### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/bot-processor/src/effects/common/types/use-indicators-effect.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { IndicatorBarSize, IndicatorName } from "@opentrader/types"; | ||
import type { BaseEffect } from "./base-effect"; | ||
import type { USE_INDICATORS } from "./effect-types"; | ||
|
||
export type UseIndicatorsEffect = BaseEffect< | ||
typeof USE_INDICATORS, | ||
{ | ||
indicators: IndicatorName[]; | ||
barSize: IndicatorBarSize; | ||
} | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { IndicatorBarSize, IndicatorName } from "@opentrader/types"; | ||
import type { UseIndicatorsEffect } from "./common/types/use-indicators-effect"; | ||
import { USE_INDICATORS } from "./common/types/effect-types"; | ||
import { makeEffect } from "./utils/make-effect"; | ||
|
||
export function useIndicators<I extends IndicatorName>( | ||
indicators: IndicatorName[], | ||
barSize: IndicatorBarSize, | ||
): UseIndicatorsEffect { | ||
return makeEffect(USE_INDICATORS, { | ||
indicators, | ||
barSize, | ||
}); | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/bot-processor/src/effects/utils/isUseIndicatorsEffect.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { UseIndicatorsEffect } from "../common/types/use-indicators-effect"; | ||
import { USE_INDICATORS } from "../common/types/effect-types"; | ||
|
||
export function isUseIndicatorsEffect( | ||
effect: unknown, | ||
): effect is UseIndicatorsEffect { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any -- this is required | ||
return (effect && (effect as any).type) === USE_INDICATORS; | ||
} |
2 changes: 1 addition & 1 deletion
2
packages/bot-processor/src/types/bot/bot-configuration.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** @type {import("eslint").Linter.Config} */ | ||
module.exports = { | ||
root: true, | ||
extends: ["@opentrader/eslint-config/module.js"], | ||
rules: {}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "@opentrader/indicators", | ||
"version": "0.0.1", | ||
"description": "", | ||
"main": "src/index.ts", | ||
"types": "src/index.ts", | ||
"scripts": { | ||
"build": "tsc", | ||
"lint": "eslint . --quiet", | ||
"lint:fix": "eslint . --fix" | ||
}, | ||
"author": "bludnic", | ||
"devDependencies": { | ||
"@opentrader/eslint-config": "workspace:*", | ||
"@opentrader/tsconfig": "workspace:*", | ||
"@opentrader/types": "workspace:*", | ||
"@types/node": "^20.10.0", | ||
"eslint": "8.54.0", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^5.2.2" | ||
}, | ||
"dependencies": { | ||
"axios": "^1.6.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import type { | ||
ExchangeCode, | ||
IndicatorBarSize, | ||
IndicatorName, | ||
IndicatorsResult, | ||
XCandle, | ||
} from "@opentrader/types"; | ||
import { fetchCandles } from "./fetch-candles"; | ||
import { indicatorsMap } from "./indicators"; | ||
|
||
type ComputeIndicatorsParams<I> = { | ||
exchangeCode: ExchangeCode; | ||
symbol: string; | ||
barSize: IndicatorBarSize; | ||
untilDate: string; | ||
indicators: I[]; | ||
}; | ||
|
||
export async function computeIndicators<I extends IndicatorName>({ | ||
exchangeCode, | ||
symbol, | ||
barSize, | ||
untilDate, | ||
indicators, | ||
}: ComputeIndicatorsParams<I>): Promise<XCandle<I>> { | ||
// Find the maximum number of periods across all indicators. | ||
// This allows to fetch all needed candles in one request. | ||
const maxPeriods = Math.max( | ||
...indicators.map((i) => indicatorsMap[i].periods), | ||
); | ||
|
||
const candles = await fetchCandles({ | ||
exchangeCode, | ||
symbol, | ||
untilDate, | ||
barSize, | ||
limit: maxPeriods, | ||
}); | ||
|
||
const result: IndicatorsResult<I> = {} as IndicatorsResult<I>; | ||
for (const indicator of indicators) { | ||
result[indicator] = indicatorsMap[indicator].compute(candles); | ||
} | ||
|
||
const lastCandle = candles[candles.length - 1]; | ||
|
||
return { | ||
timestamp: new Date(lastCandle.timestamp).getTime(), | ||
open: lastCandle.open, | ||
high: lastCandle.high, | ||
low: lastCandle.low, | ||
close: lastCandle.close, | ||
volume: lastCandle.volume, | ||
indicators: result, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import axios from "axios"; | ||
import type { OHLCVData, OHLCVRequest, OHLCVResponse } from "./types"; | ||
|
||
export async function fetchCandles(params: OHLCVRequest): Promise<OHLCVData[]> { | ||
console.log("Fetch candles", params); | ||
|
||
const { data } = await axios.get<OHLCVResponse>( | ||
`${process.env.NEXT_PUBLIC_CANDLES_SERVICE_API_URL}/api/trpc/candles.list`, | ||
{ | ||
headers: { | ||
Authorization: process.env.NEXT_PUBLIC_CANDLES_SERVICE_API_KEY, | ||
}, | ||
params: { | ||
input: JSON.stringify(params), | ||
}, | ||
}, | ||
); | ||
|
||
return data.result.data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./types"; | ||
export * from "./compute-indicators"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { OHLCVData } from "../types"; | ||
import { SMA } from "./sma"; | ||
|
||
export function computeSMA(candles: OHLCVData[], periods: 5 | 10 | 15 | 30) { | ||
if (candles.length < periods) { | ||
throw new Error( | ||
`Not enough data points for the given period: ${periods}. Candles provided: ${candles.length}`, | ||
); | ||
} | ||
|
||
const lastCandles = candles.slice(-periods); | ||
|
||
const result = SMA( | ||
lastCandles.map((x) => x.close), | ||
periods, | ||
); | ||
|
||
return result[0]; | ||
} |
Oops, something went wrong.