binbot-charts 0.2.9 → 0.2.12

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.
@@ -38,7 +38,22 @@ function TVChartContainer(_ref) {
38
38
  } = _ref;
39
39
  const containerRef = (0, _react.useRef)(null);
40
40
  const [chartOrderLines, setChartOrderLines] = (0, _useImmer.useImmer)([]);
41
+ const [widgetState, setWidgetState] = (0, _react.useState)(null);
42
+ const [symbolState, setSymbolState] = (0, _react.useState)(symbol);
41
43
  (0, _react.useEffect)(() => {
44
+ if (!widgetState) {
45
+ initializeChart();
46
+ }
47
+
48
+ if (orderLines && orderLines.length > 0) {
49
+ updateOrderLines(orderLines);
50
+ } // if (symbol !== symbolState) {
51
+ // widgetState.setSymbol(symbol, interval);
52
+ // }
53
+
54
+ }, [orderLines]);
55
+
56
+ const initializeChart = () => {
42
57
  const widgetOptions = {
43
58
  symbol: symbol,
44
59
  datafeed: new _datafeed.default(timescaleMarks),
@@ -57,13 +72,8 @@ function TVChartContainer(_ref) {
57
72
  };
58
73
  const tvWidget = new _charting_library.widget(widgetOptions);
59
74
  tvWidget.onChartReady(() => {
60
- if (orderLines.length > 0) {
61
- orderLines.forEach(order => {
62
- updateOrderLines(tvWidget, order);
63
- });
64
- }
65
-
66
- tvWidget.subscribe("onTick", event => onTick(event)); // get latest bar for last price
75
+ tvWidget.subscribe("onTick", event => onTick(event));
76
+ setWidgetState(tvWidget); // get latest bar for last price
67
77
 
68
78
  const prices = async () => {
69
79
  const data = await tvWidget.activeChart().exportData({
@@ -75,33 +85,33 @@ function TVChartContainer(_ref) {
75
85
  };
76
86
 
77
87
  prices();
78
- }); // returned function will be called on component unmount
79
-
80
- return () => {
81
- if (!tvWidget) {
82
- tvWidget.remove();
83
- }
84
- };
85
- }, [orderLines, timescaleMarks]);
88
+ });
89
+ };
86
90
 
87
- const updateOrderLines = (tvWidget, order) => {
91
+ const updateOrderLines = orderLines => {
88
92
  if (chartOrderLines && chartOrderLines.length > 0) {
89
93
  chartOrderLines.forEach(item => {
90
- if (item._data.bodyText == order.text) {
91
- item.setText(order.text).setTooltip(order.tooltip).setQuantity(order.quantity).setPrice(order.price);
92
- }
94
+ orderLines.forEach(order => {
95
+ if (item._data.bodyText == order.text) {
96
+ item.setText(order.text).setTooltip(order.tooltip).setQuantity(order.quantity).setPrice(order.price);
97
+ }
98
+ });
93
99
  });
94
100
  } else {
95
- const lineStyle = order.lineStyle || 0;
96
- const chartOrderLine = tvWidget.chart().createOrderLine().setText(order.text).setTooltip(order.tooltip).setQuantity(order.quantity).setQuantityFont("inherit 14px Arial").setQuantityBackgroundColor(order.color).setQuantityBorderColor(order.color).setLineStyle(lineStyle).setLineLength(25).setLineColor(order.color).setBodyFont("inherit 14px Arial").setBodyBorderColor(order.color).setBodyTextColor(order.color).setPrice(order.price);
97
- setChartOrderLines(draft => {
98
- draft.push(chartOrderLine);
99
- return draft;
100
- });
101
+ if (orderLines && orderLines.length > 0) {
102
+ orderLines.forEach(order => {
103
+ const lineStyle = order.lineStyle || 0;
104
+ const chartOrderLine = widgetState.chart().createOrderLine().setText(order.text).setTooltip(order.tooltip).setQuantity(order.quantity).setQuantityFont("inherit 14px Arial").setQuantityBackgroundColor(order.color).setQuantityBorderColor(order.color).setLineStyle(lineStyle).setLineLength(25).setLineColor(order.color).setBodyFont("inherit 14px Arial").setBodyBorderColor(order.color).setBodyTextColor(order.color).setPrice(order.price);
105
+ setChartOrderLines(draft => {
106
+ draft.push(chartOrderLine);
107
+ return draft;
108
+ });
109
+ });
110
+ }
101
111
  }
102
112
  };
103
113
 
104
- return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, console.log(chartOrderLines), /*#__PURE__*/_react.default.createElement("div", {
114
+ return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("div", {
105
115
  ref: containerRef,
106
116
  style: {
107
117
  height: height
@@ -49,32 +49,36 @@ class Datafeed {
49
49
  onResultReadyCallback(symbols);
50
50
  });
51
51
 
52
- _defineProperty(this, "resolveSymbol", (symbolName, onSymbolResolvedCallback, onResolveErrorCallback) => {
52
+ _defineProperty(this, "resolveSymbol", async (symbolName, onSymbolResolvedCallback, onResolveErrorCallback) => {
53
53
  if (!symbolName) {
54
- onResolveErrorCallback("cannot resolve symbol");
54
+ await onResolveErrorCallback("cannot resolve symbol");
55
55
  return;
56
56
  }
57
57
 
58
- const symbolInfo = {
59
- ticker: symbolName,
60
- name: symbolName,
61
- ticker: symbolName,
62
- description: symbolName,
63
- type: "crypto",
64
- session: "24x7",
65
- timezone: "Etc/UTC",
66
- exchange: "Binance",
67
- minmov: 100,
68
- pricescale: 100000,
69
- has_intraday: true,
70
- has_no_volume: false,
71
- volume: "hundreds",
72
- has_weekly_and_monthly: true,
73
- volume_precision: 9,
74
- data_status: "streaming",
75
- resolution: "1h"
58
+ const symbolInfo = () => {
59
+ return {
60
+ ticker: symbolName,
61
+ name: symbolName,
62
+ ticker: symbolName,
63
+ description: symbolName,
64
+ type: "crypto",
65
+ session: "24x7",
66
+ timezone: "Etc/UTC",
67
+ exchange: "Binance",
68
+ minmov: 100,
69
+ pricescale: 100000,
70
+ has_intraday: true,
71
+ has_no_volume: false,
72
+ volume: "hundreds",
73
+ has_weekly_and_monthly: true,
74
+ volume_precision: 9,
75
+ data_status: "streaming",
76
+ resolution: "1h"
77
+ };
76
78
  };
77
- onSymbolResolvedCallback(symbolInfo);
79
+
80
+ const symbol = await symbolInfo();
81
+ onSymbolResolvedCallback(symbol);
78
82
  });
79
83
 
80
84
  _defineProperty(this, "getBars", async (symbolInfo, resolution, periodParams, onHistoryCallback, onErrorCallback) => {
@@ -3,18 +3,11 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.generateSymbol = generateSymbol;
7
6
  exports.getAllSymbols = getAllSymbols;
8
7
  exports.makeApiRequest = makeApiRequest;
9
- exports.parseFullSymbol = parseFullSymbol;
10
8
 
11
9
  require("core-js/modules/es.promise.js");
12
10
 
13
- require("core-js/modules/es.regexp.exec.js");
14
-
15
- require("core-js/modules/es.string.match.js");
16
-
17
- // Make requests to CryptoCompare API
18
11
  async function makeApiRequest(path) {
19
12
  try {
20
13
  const response = await fetch("https://api.binance.com/".concat(path));
@@ -22,29 +15,6 @@ async function makeApiRequest(path) {
22
15
  } catch (error) {
23
16
  throw new Error("Binance request error: ".concat(error.status));
24
17
  }
25
- } // Generate a symbol ID from a pair of the coins
26
-
27
-
28
- function generateSymbol(exchange, fromSymbol, toSymbol) {
29
- const short = "".concat(fromSymbol, "/").concat(toSymbol);
30
- return {
31
- short,
32
- full: "".concat(exchange, ":").concat(short)
33
- };
34
- }
35
-
36
- function parseFullSymbol(fullSymbol) {
37
- const match = fullSymbol.match(/^(\w+):(\w+)\/(\w+)$/);
38
-
39
- if (!match) {
40
- return null;
41
- }
42
-
43
- return {
44
- exchange: match[1],
45
- fromSymbol: match[2],
46
- toSymbol: match[3]
47
- };
48
18
  }
49
19
 
50
20
  async function getAllSymbols(symbol) {
package/package.json CHANGED
@@ -1,10 +1,9 @@
1
1
  {
2
- "version": "0.2.9",
2
+ "version": "0.2.12",
3
3
  "name": "binbot-charts",
4
4
  "dependencies": {
5
5
  "react": "^17.0.1",
6
6
  "react-dom": "^17.0.1",
7
- "react-scripts": "^5.0.1",
8
7
  "use-immer": "^0.7.0"
9
8
  },
10
9
  "devDependencies": {
@@ -12,7 +11,8 @@
12
11
  "@babel/core": "^7.18.13",
13
12
  "@babel/polyfill": "^7.12.1",
14
13
  "@babel/preset-env": "^7.18.10",
15
- "@babel/preset-typescript": "^7.18.6"
14
+ "@babel/preset-typescript": "^7.18.6",
15
+ "react-scripts": "^5.0.1"
16
16
  },
17
17
  "scripts": {
18
18
  "start": "react-scripts start",