-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathget-balance-binance.js
More file actions
174 lines (152 loc) · 5.66 KB
/
get-balance-binance.js
File metadata and controls
174 lines (152 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Import necessary packages
const express = require('express');
const bodyParser = require('body-parser');
const Binance = require('node-binance-api');
const config = require('./config');
const fs = require('fs');
// Create an instance of the Express application
const app = express();
// Set up middleware to parse incoming JSON requests
app.use(bodyParser.json());
// Set up the Binance API client with the API key and secret from the config file
const binance = new Binance().options({
APIKEY: config.binance.APIKEY,
APISECRET: config.binance.APISECRET,
'family': 4,
'baseUrl': 'https://api4.binance.com/api/',
});
// Get the USDT balance for the API key associated with the client
binance.balance((error, balances) => {
if ( error ) return console.error(error);
console.info("USDT balance: ", balances.USDT.available);
console.info("TUSD balance: ", balances.TUSD.available);
});
// Set up a POST endpoint to receive webhook messages from TradingView
app.post('/webhook', (req, res) => {
const alertData = req.body;
// Log the incoming message
console.log('Received message:', alertData);
// Process the alert data based on the strategy specified in the message
processAlertData(alertData);
// Send a response to the webhook
res.json({ status: 'success' });
});
// Function to process alert data based on the specified strategy
function processAlertData(alertData) {
const strategy = alertData.strategy;
if (strategy === 'USDINFLATION') {
// Call the function to process the USD inflation strategy
processUSDInflationStrategy(alertData);
} else if (strategy === 'TUSDT') {
// Call the function to process strategy 2
processTUSDT(alertData);
} else if (strategy === 'STRATEGY3') {
// Call the function to process strategy 3
processStrategy3(alertData);
} else if (strategy === 'STRATEGY4') {
// Call the function to process strategy 4
processStrategy4(alertData);
} else {
// Log an error message if the strategy is invalid or not supported
console.log('Invalid strategy or strategy not supported:', strategy);
}
}
// Function to process the USD inflation strategy
function processUSDInflationStrategy(alertData) {
// Extract the necessary data from the alert
const action = alertData.action;
const contracts = parseFloat(alertData.contracts);
const ticker = alertData.ticker;
const positionSize = parseFloat(alertData.position_size);
// Convert the ticker to the format required by the Binance API
const ccxtSymbol = `${ticker.slice(0, -4)}${ticker.slice(-4)}`;
if (action === 'buy') {
// Place a market buy order
binance.marketBuy(ccxtSymbol, contracts, (error, response) => {
if (error) {
console.error(error);
// Log the error message to a file
fs.appendFile('error-log.txt', JSON.stringify(error) + '\n', (err) => {
if (err) {
console.error('Error writing to error-log.txt:', err);
}
});
return;
}
// Log the order details
console.log('Order executed:', response);
});
} else if (action === 'sell') {
// Place a market sell order
binance.marketSell(ccxtSymbol, contracts, (error, response) => {
if (error) {
console.error(error);
fs.appendFile('error-log.txt', JSON.stringify(error) + '\n', (err) => {
if (err) {
console.error('Error writing to error-log.txt:', err);
}
});
return;
}
console.log('Order executed:', response);
});
} else {
console.log('Invalid action:', action);
}
}
function processTUSDT(alertData) {
// Code to handle TUSDT alerts
console.log('Processing TUSDT alert:', alertData);
// Extract the necessary data from the alert
const action = alertData.action;
const contracts = parseFloat(alertData.contracts).toFixed(5); // Round to 5 decimal places
const ticker = alertData.ticker;
const positionSize = parseFloat(alertData.position_size).toFixed(5); // Round to 5 decimal places
// Convert the ticker to the format required by the Binance API
const ccxtSymbol = `${ticker.slice(0, -4)}${ticker.slice(-4)}`;
if (action === 'buy') {
// Place a market buy order
binance.marketBuy(ccxtSymbol, contracts, (error, response) => {
if (error) {
console.error(error);
// Log the error message to a file
fs.appendFile('error-log.txt', JSON.stringify(error) + '\n', (err) => {
if (err) {
console.error('Error writing to error-log.txt:', err);
}
});
return;
}
// Log the order details
console.log('Order executed:', response);
});
} else if (action === 'sell') {
// Place a market sell order
binance.marketSell(ccxtSymbol, contracts, (error, response) => {
if (error) {
console.error(error);
fs.appendFile('error-log.txt', JSON.stringify(error) + '\n', (err) => {
if (err) {
console.error('Error writing to error-log.txt:', err);
}
});
return;
}
console.log('Order executed:', response);
});
} else {
console.log('Invalid action:', action);
}
}
function processStrategy3(alertData) {
// Code to handle STRATEGY3 alerts
console.log('Processing STRATEGY3 alert:', alertData);
}
function processStrategy4(alertData) {
// Code to handle STRATEGY4 alerts
console.log('Processing STRATEGY4 alert:', alertData);
}
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Webhook server listening on port ${PORT}`);
});