binance-futures-connector
Advanced tools
Comparing version 1.4.1 to 1.4.2
{ | ||
"name": "binance-futures-connector", | ||
"version": "1.4.1", | ||
"version": "1.4.2", | ||
"description": "binance-futures-spot 币安-合约+现货-sdk,持续更新,欢迎PR一起完善。微信:wkc19891", | ||
@@ -19,2 +19,3 @@ "main": "src/index.js", | ||
"axios": "^0.25.0", | ||
"decimal.js": "^10.4.2", | ||
"https-proxy-agent": "^5.0.0", | ||
@@ -21,0 +22,0 @@ "node-localstorage": "^2.2.1", |
@@ -1,2 +0,2 @@ | ||
const Decimal = require('decimal.js'); | ||
const Std = { | ||
@@ -748,2 +748,85 @@ _skip: function (arr, period) { | ||
/** | ||
* SAR 抛物线指标 标识趋势 以及 趋势反转 TA.SAR(数据,加速步长,最大加速因子,初始因子) 默认为(0.02,0.2,0.02)。 | ||
* @param {*} records | ||
* @param {*} accelerationStep | ||
* @param {*} maxAccelerationFactor | ||
* @param {*} initialFactor | ||
* @returns | ||
*/ | ||
const SAR2 = (records, accelerationStep, maxAccelerationFactor, initialFactor) => { | ||
accelerationStep = typeof (accelerationStep) === 'undefined' ? 0.02 : Number(accelerationStep); | ||
maxAccelerationFactor = typeof (maxAccelerationFactor) === 'undefined' ? 0.2 : Number(maxAccelerationFactor); | ||
initialFactor = typeof (initialFactor) === 'undefined' ? 0.02 : Number(initialFactor); | ||
let length = records.length; | ||
let results = Std._zeros(length); | ||
var q0 = ""; | ||
if (length == 0) { | ||
return results; | ||
} else { | ||
q0 = records[0] | ||
} | ||
let accelerationFactor = initialFactor; | ||
let extremePoint = q0.High; | ||
let priorSar = q0.Low; | ||
let isRising = true; | ||
// let _sar = 0; | ||
for (let i = 0; i < length; i++) { | ||
if (i == 0) { | ||
continue; | ||
} | ||
let q = records[i]; | ||
if (isRising) { | ||
let sar = priorSar + (accelerationFactor * (extremePoint - priorSar)); | ||
sar = new Decimal(extremePoint).sub(new Decimal(priorSar)).mul(new Decimal(accelerationFactor)).add(new Decimal(priorSar)).toNumber(); | ||
// SAR cannot be higher than last two lows | ||
if (i >= 2) { | ||
let minLastTwo = Math.min(records[i - 1].Low, records[i - 2].Low); | ||
sar = Math.min(sar, minLastTwo); | ||
} | ||
//turn down | ||
if (q.Low < sar) { | ||
results[i] = extremePoint; | ||
isRising = false; | ||
accelerationFactor = initialFactor; | ||
extremePoint = q.Low; | ||
} else { | ||
results[i] = sar; | ||
// new high extreme point | ||
if (q.High > extremePoint) { | ||
extremePoint = q.High; | ||
accelerationFactor = Math.min(accelerationFactor + accelerationStep, maxAccelerationFactor) | ||
} | ||
} | ||
} else { | ||
//was fail | ||
let sar = priorSar - (accelerationFactor * (priorSar - extremePoint)); | ||
sar = new Decimal(priorSar).sub((new Decimal(priorSar).sub(new Decimal(extremePoint)).mul(new Decimal(accelerationFactor)))); | ||
// SAR cannot be lower than last two highs | ||
if (i >= 2) { | ||
let maxLastTwo = Math.max(records[i - 1].High, records[i - 2].High); | ||
sar = Math.max(sar, maxLastTwo); | ||
} | ||
// turn up | ||
if (q.High > sar) { | ||
results[i] = extremePoint; | ||
isRising = true; | ||
accelerationFactor = initialFactor; | ||
extremePoint = q.High; | ||
} | ||
// continue falling | ||
else { | ||
results[i] = sar; | ||
// new low extreme point | ||
if (q.Low < extremePoint) { | ||
extremePoint = q.Low; | ||
accelerationFactor = Math.min(accelerationFactor + accelerationStep, maxAccelerationFactor); | ||
} | ||
} | ||
} | ||
priorSar = results[i]; | ||
} | ||
return results; | ||
} | ||
module.exports = { | ||
@@ -765,4 +848,5 @@ Highest, | ||
getDate, | ||
SAR | ||
SAR, | ||
SAR2 | ||
} | ||
@@ -8,90 +8,90 @@ const { Future, TA, Sleep, _G, _N, Log, LogProfit, DateFormat, buy, sell, buy_close, sell_close } = require('../index') | ||
/** | ||
* 记录收益 打印收益曲线 | ||
*/ | ||
async function PrintProfit() { | ||
//记录10次收益 | ||
for (let i = 0; i < 10; i++) { | ||
LogProfit(i + 1000) | ||
await Sleep(200)//休息0.2秒 | ||
} | ||
console.log(LogProfit()) | ||
LogProfit(null)//重置收益曲线 | ||
console.log('重置收益曲线', LogProfit()) | ||
// ``` 打印输出接口 | ||
// [ | ||
// { income: 1000, time: '2022-11-27 10:39:07' }, | ||
// { income: 1001, time: '2022-11-27 10:39:07' }, | ||
// { income: 1002, time: '2022-11-27 10:39:08' }, | ||
// { income: 1003, time: '2022-11-27 10:39:08' }, | ||
// { income: 1004, time: '2022-11-27 10:39:08' }, | ||
// { income: 1005, time: '2022-11-27 10:39:08' }, | ||
// { income: 1006, time: '2022-11-27 10:39:08' }, | ||
// { income: 1007, time: '2022-11-27 10:39:09' }, | ||
// { income: 1008, time: '2022-11-27 10:39:09' }, | ||
// { income: 1009, time: '2022-11-27 10:39:09' } | ||
// ] | ||
// ``` | ||
} | ||
PrintProfit() | ||
// /** | ||
// * 记录收益 打印收益曲线 | ||
// */ | ||
// async function PrintProfit() { | ||
// //记录10次收益 | ||
// for (let i = 0; i < 10; i++) { | ||
// LogProfit(i + 1000) | ||
// await Sleep(200)//休息0.2秒 | ||
// } | ||
// console.log(LogProfit()) | ||
// LogProfit(null)//重置收益曲线 | ||
// console.log('重置收益曲线', LogProfit()) | ||
// // ``` 打印输出接口 | ||
// // [ | ||
// // { income: 1000, time: '2022-11-27 10:39:07' }, | ||
// // { income: 1001, time: '2022-11-27 10:39:07' }, | ||
// // { income: 1002, time: '2022-11-27 10:39:08' }, | ||
// // { income: 1003, time: '2022-11-27 10:39:08' }, | ||
// // { income: 1004, time: '2022-11-27 10:39:08' }, | ||
// // { income: 1005, time: '2022-11-27 10:39:08' }, | ||
// // { income: 1006, time: '2022-11-27 10:39:08' }, | ||
// // { income: 1007, time: '2022-11-27 10:39:09' }, | ||
// // { income: 1008, time: '2022-11-27 10:39:09' }, | ||
// // { income: 1009, time: '2022-11-27 10:39:09' } | ||
// // ] | ||
// // ``` | ||
// } | ||
// PrintProfit() | ||
/** | ||
* 记录10次日志 | ||
*/ | ||
async function PrintLog() { | ||
for (let i = 0; i < 10; i++) { | ||
Log(i + 1000) | ||
await Sleep(200)//延迟0.2秒 | ||
} | ||
console.log(Log()) | ||
Log(null)//清空日志 | ||
console.log('清空日志', Log()) | ||
// ``` 日志结构 | ||
// [ | ||
// { key: 1, logs: '1000', time: '2022-11-27 12:12:10' }, | ||
// { key: 2, logs: '1001', time: '2022-11-27 12:12:11' }, | ||
// { key: 3, logs: '1002', time: '2022-11-27 12:12:11' }, | ||
// { key: 4, logs: '1003', time: '2022-11-27 12:12:11' }, | ||
// { key: 5, logs: '1004', time: '2022-11-27 12:12:11' }, | ||
// { key: 6, logs: '1005', time: '2022-11-27 12:12:11' }, | ||
// { key: 7, logs: '1006', time: '2022-11-27 12:12:12' }, | ||
// { key: 8, logs: '1007', time: '2022-11-27 12:12:12' }, | ||
// { key: 9, logs: '1008', time: '2022-11-27 12:12:12' }, | ||
// { key: 10, logs: '1009', time: '2022-11-27 12:12:12' } | ||
// ] | ||
// ``` | ||
} | ||
PrintLog(); | ||
// /** | ||
// * 记录10次日志 | ||
// */ | ||
// async function PrintLog() { | ||
// for (let i = 0; i < 10; i++) { | ||
// Log(i + 1000) | ||
// await Sleep(200)//延迟0.2秒 | ||
// } | ||
// console.log(Log()) | ||
// Log(null)//清空日志 | ||
// console.log('清空日志', Log()) | ||
// // ``` 日志结构 | ||
// // [ | ||
// // { key: 1, logs: '1000', time: '2022-11-27 12:12:10' }, | ||
// // { key: 2, logs: '1001', time: '2022-11-27 12:12:11' }, | ||
// // { key: 3, logs: '1002', time: '2022-11-27 12:12:11' }, | ||
// // { key: 4, logs: '1003', time: '2022-11-27 12:12:11' }, | ||
// // { key: 5, logs: '1004', time: '2022-11-27 12:12:11' }, | ||
// // { key: 6, logs: '1005', time: '2022-11-27 12:12:11' }, | ||
// // { key: 7, logs: '1006', time: '2022-11-27 12:12:12' }, | ||
// // { key: 8, logs: '1007', time: '2022-11-27 12:12:12' }, | ||
// // { key: 9, logs: '1008', time: '2022-11-27 12:12:12' }, | ||
// // { key: 10, logs: '1009', time: '2022-11-27 12:12:12' } | ||
// // ] | ||
// // ``` | ||
// } | ||
// PrintLog(); | ||
async function UseG() { | ||
_G('key1', '123', 888, 777)//设置key1值 | ||
_G('key2', 'hello world')//设置key2值 | ||
_G('key3', JSON.stringify({ name: 'top', age: 18 })) | ||
console.log('key1=', _G('key1'))//获取key1值 | ||
console.log('key2=', _G('key2'))//获取key2值 | ||
console.log('key3=', _G('key3'))//获取key3值 | ||
console.log('key3.name=', JSON.parse(_G('key3')).name)//获取key3.name值 | ||
// async function UseG() { | ||
// _G('key1', '123', 888, 777)//设置key1值 | ||
// _G('key2', 'hello world')//设置key2值 | ||
// _G('key3', JSON.stringify({ name: 'top', age: 18 })) | ||
// console.log('key1=', _G('key1'))//获取key1值 | ||
// console.log('key2=', _G('key2'))//获取key2值 | ||
// console.log('key3=', _G('key3'))//获取key3值 | ||
// console.log('key3.name=', JSON.parse(_G('key3')).name)//获取key3.name值 | ||
// ```输出 | ||
// key1= 123888|777 | ||
// key2= hello world | ||
// key3= {"name":"top","age":18} | ||
// key3.name= top | ||
// ``` | ||
} | ||
UseG() | ||
async function UseN() { | ||
let a = 1.34159; | ||
console.log('a=', a); | ||
let a1 = _N(a, 2) | ||
console.log('a2=', a1); | ||
let a2 = _N(a, 3) | ||
console.log('a2=', a2); | ||
// ```输出 | ||
// a= 1.34059 | ||
// a2= 1.34 | ||
// a2= 1.341 | ||
// ``` | ||
} | ||
UseN(); | ||
// // ```输出 | ||
// // key1= 123888|777 | ||
// // key2= hello world | ||
// // key3= {"name":"top","age":18} | ||
// // key3.name= top | ||
// // ``` | ||
// } | ||
// UseG() | ||
// async function UseN() { | ||
// let a = 1.34159; | ||
// console.log('a=', a); | ||
// let a1 = _N(a, 2) | ||
// console.log('a2=', a1); | ||
// let a2 = _N(a, 3) | ||
// console.log('a2=', a2); | ||
// // ```输出 | ||
// // a= 1.34059 | ||
// // a2= 1.34 | ||
// // a2= 1.341 | ||
// // ``` | ||
// } | ||
// UseN(); | ||
const client = new Future(apiKey, apiSecret, { ip: proxyIp, port: proxy }) | ||
@@ -103,27 +103,11 @@ /** | ||
function GetR() { | ||
client.records("BTCUSDT", "1m", 10).then(records => { | ||
console.log('获取BTCUSDT 1m 10条数据=', records) | ||
// ``` 输出 | ||
// [ | ||
// { | ||
// Time: 1669522920000, | ||
// Open: 16524.4, | ||
// High: 16526.3, | ||
// Low: 16522.9, | ||
// Close: 16526.2, | ||
// Volume: 69.66 | ||
// }, | ||
// { | ||
// Time: 1669522980000, | ||
// Open: 16526.3, | ||
// High: 16536.3, | ||
// Low: 16526.2, | ||
// Close: 16530.3, | ||
// Volume: 128.14 | ||
// } | ||
// ] | ||
// ``` | ||
client.records("ETHUSDT", "1h", 1500).then(records => { | ||
//获取SAR | ||
let sar1 = TA.SAR(records, 0.008, 0.2, 0.008); | ||
console.log('sar11', sar1[sar1.length - 2], 'sar21', sar1[sar1.length - 3]) | ||
console.log('sar11', sar1[sar1.length - 2], 'sar12', sar1[sar1.length - 3]) | ||
let sar2 = TA.SAR2(records, 0.008, 0.2, 0.008); | ||
console.log('sar21', sar2[sar2.length - 2], 'sar22', sar2[sar2.length - 3], sar2[sar2.length - 2] - sar2[sar2.length - 3]) | ||
// let art = TA.ATR(records, 14) | ||
// console.log('art21', art[art.length - 2], 'art22', art[art.length - 3], art[art.length - 2] - art[art.length - 3]) | ||
}) | ||
@@ -130,0 +114,0 @@ } |
const { validateRequiredParameters } = require('../helpers/validation') | ||
const { isEmptyValue } = require('../helpers/utils') | ||
const WebSocketClient = require('ws') | ||
let url = require('url'); | ||
let HttpsProxyAgent = require('https-proxy-agent'); | ||
@@ -221,6 +222,7 @@ | ||
const url = `${this.wsURL}/stream?streams=${streams.join('/')}` | ||
return this.subscribe(url, callbacks) | ||
} | ||
subscribe(url, callbacks) { | ||
subscribe(urls, callbacks) { | ||
let options = {} | ||
@@ -235,3 +237,3 @@ if (this.ip != '' && this.ip != undefined && this.port != '' && this.port != undefined) { | ||
const initConnect = () => { | ||
const ws = new WebSocketClient(url, options) | ||
const ws = new WebSocketClient(urls, options) | ||
wsRef.ws = ws | ||
@@ -268,3 +270,3 @@ Object.keys(callbacks).forEach((event, _) => { | ||
} | ||
this.logger.debug(url) | ||
this.logger.debug(urls) | ||
initConnect() | ||
@@ -271,0 +273,0 @@ return wsRef |
264590
7568
5
+ Addeddecimal.js@^10.4.2
+ Addeddecimal.js@10.5.0(transitive)