@steerprotocol/strategy-utils 2.2.0 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Candle } from "./types/Candle";
|
|
2
|
+
import { RawTradeData } from "./types/RawTradeData";
|
|
3
|
+
|
|
4
|
+
// CandlestickConverter class provides a static method to convert raw trade data into OHLCV format
|
|
5
|
+
export class CandlestickConverter {
|
|
6
|
+
// convertToOHLCV method accepts raw trade data and a period (default is 3600 seconds or 1 hour)
|
|
7
|
+
// and returns the data in OHLCV format
|
|
8
|
+
static convertToOHLCV(rawData: RawTradeData[], period: i32 = 3600): Candle[] {
|
|
9
|
+
// If the raw data is empty, an error is thrown
|
|
10
|
+
if (rawData.length === 0) {
|
|
11
|
+
throw new Error("Input data is empty");
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Array to hold the converted data
|
|
15
|
+
let ohlcvData: Candle[] = [];
|
|
16
|
+
|
|
17
|
+
// Initialize variables
|
|
18
|
+
let i: i32 = 0;
|
|
19
|
+
let currentTimestamp: i32 = Math.floor(rawData[0].timestamp / period) * period;
|
|
20
|
+
let open: f64 = rawData[0].price;
|
|
21
|
+
let high: f64 = rawData[0].price;
|
|
22
|
+
let low: f64 = rawData[0].price;
|
|
23
|
+
let close: f64 = rawData[0].price;
|
|
24
|
+
let volume: f64 = 0.0;
|
|
25
|
+
|
|
26
|
+
// Loop through all raw data
|
|
27
|
+
while (i < rawData.length) {
|
|
28
|
+
// For each period, set the initial values of open, high, low to the closing price of the last period
|
|
29
|
+
// and reset the volume to 0
|
|
30
|
+
open = close;
|
|
31
|
+
high = close;
|
|
32
|
+
low = close;
|
|
33
|
+
volume = 0.0;
|
|
34
|
+
|
|
35
|
+
// Process all raw data within the current period
|
|
36
|
+
while (i < rawData.length && rawData[i].timestamp < currentTimestamp + period) {
|
|
37
|
+
// Set the open price to the price of the first trade in the period
|
|
38
|
+
open = i == 0 ? rawData[i].price : open;
|
|
39
|
+
// Update high and low prices
|
|
40
|
+
high = Math.max(high, rawData[i].price);
|
|
41
|
+
low = Math.min(low, rawData[i].price);
|
|
42
|
+
// Update close price to the price of the last trade in the period
|
|
43
|
+
close = rawData[i].price;
|
|
44
|
+
// Update the total volume
|
|
45
|
+
volume += rawData[i].volume;
|
|
46
|
+
i++;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Create a new Candle object with the calculated OHLCV values and add it to the array
|
|
50
|
+
let ohlcv: Candle = new Candle(currentTimestamp, open, high, low, close, volume);
|
|
51
|
+
ohlcvData.push(ohlcv);
|
|
52
|
+
|
|
53
|
+
// Move to the next period
|
|
54
|
+
currentTimestamp += period;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Return the converted data
|
|
58
|
+
return ohlcvData;
|
|
59
|
+
}
|
|
60
|
+
}
|
package/assembly/utils/env.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export declare function generateCandles(data: string, candleSize: string): string
|
|
1
|
+
export declare function generateCandles(data: string, candleSize: string): string
|
|
2
|
+
export declare function ccxt_fetchOHLCV(exchangeId: string, symbol: string, timeframe: string, limit: number, since: number): string
|