-
Notifications
You must be signed in to change notification settings - Fork 57
/
index.js_bk-WEN
156 lines (138 loc) · 5.26 KB
/
index.js_bk-WEN
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const backpack_client_1 = require("./backpack_client");
/// EDIT HERE ///
const API_KEY = "IUiXjx+DCon2KZJ/xxxxxxxxxxxxxxxxxxxxx+A="
const API_SECRET = "mHByUM48jVUGxxxxxxxxxxxxxxxxxxxxxxxx="
/////////////
function delay(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}
function getNowFormatDate() {
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
var month = date.getMonth() + 1;
var strDate = date.getDate();
var strHour = date.getHours();
var strMinute = date.getMinutes();
var strSecond = date.getSeconds();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
if (strHour >= 0 && strHour <= 9) {
strHour = "0" + strHour;
}
if (strMinute >= 0 && strMinute <= 9) {
strMinute = "0" + strMinute;
}
if (strSecond >= 0 && strSecond <= 9) {
strSecond = "0" + strSecond;
}
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
+ " " + strHour + seperator2 + strMinute
+ seperator2 + strSecond;
return currentdate;
}
let successbuy = 0;
let sellbuy = 0;
const init = async (client) => {
try {
console.log("\n============================")
console.log(`Total Buy: ${successbuy} | Total Sell: ${sellbuy}`);
console.log("============================\n")
console.log(getNowFormatDate(), "Waiting 10 seconds...");
await delay(10000);
let userbalance = await client.Balance();
if (userbalance.USDC.available > 5) {
await buyfun(client);
} else {
await sellfun(client);
return;
}
} catch (e) {
console.log(getNowFormatDate(), `Try again... (${e.message})`);
console.log("=======================")
await delay(3000);
init(client);
}
}
const sellfun = async (client) => {
let GetOpenOrders = await client.GetOpenOrders({ symbol: "WEN_USDC" });
if (GetOpenOrders.length > 0) {
let CancelOpenOrders = await client.CancelOpenOrders({ symbol: "WEN_USDC" });
console.log(getNowFormatDate(), "All pending orders canceled");
}
let userbalance2 = await client.Balance();
console.log(getNowFormatDate(), `My Account Infos: ${userbalance2.WEN.available} $WEN | ${userbalance2.USDC.available} $USDC`, );
let { lastPrice: lastPriceask } = await client.Ticker({ symbol: "WEN_USDC" });
console.log(getNowFormatDate(), "Price WEN_USDC:", lastPriceask);
let quantitys = (userbalance2.WEN.available - (userbalance2.WEN.available * 0.02)).toFixed(0).toString();
console.log(getNowFormatDate(), `Trade... ${quantitys} $WEN to ${(lastPriceask * quantitys).toFixed(2)} $USDC`);
let orderResultAsk = await client.ExecuteOrder({
orderType: "Limit",
price: lastPriceask.toString(),
quantity: quantitys,
side: "Ask",
symbol: "WEN_USDC",
timeInForce: "IOC"
})
if (orderResultAsk?.status == "Filled" && orderResultAsk?.side == "Ask") {
sellbuy += 1;
console.log(getNowFormatDate(), "Sold successfully:", `Order number:${orderResultAsk.id}`);
init(client);
} else {
if (orderResultAsk?.status == 'Expired'){
throw new Error("Sell Order Expired");
} else{
throw new Error(orderResultAsk?.status);
}
}
}
const buyfun = async (client) => {
let GetOpenOrders = await client.GetOpenOrders({ symbol: "WEN_USDC" });
if (GetOpenOrders.length > 0) {
let CancelOpenOrders = await client.CancelOpenOrders({ symbol: "WEN_USDC" });
console.log(getNowFormatDate(), "All pending orders canceled");
}
let userbalance = await client.Balance();
let balanceSol = 0;
if (userbalance.WEN) {
balanceSol = userbalance.WEN.available
}
console.log(getNowFormatDate(), `My Account Infos: ${balanceSol} $WEN | ${userbalance.USDC.available} $USDC`, );
let { lastPrice } = await client.Ticker({ symbol: "WEN_USDC" });
console.log(getNowFormatDate(), "Price of WEN_USDC:", lastPrice);
let quantitys = ((userbalance.USDC.available - 2) / lastPrice).toFixed(0).toString();
console.log(getNowFormatDate(), `Trade ... ${(userbalance.USDC.available - 2).toFixed(2).toString()} $USDC to ${quantitys} $WEN`);
let orderResultBid = await client.ExecuteOrder({
orderType: "Limit",
price: lastPrice.toString(),
quantity: quantitys,
side: "Bid",
symbol: "WEN_USDC",
timeInForce: "IOC"
})
if (orderResultBid?.status == "Filled" && orderResultBid?.side == "Bid") {
successbuy += 1;
console.log(getNowFormatDate(), "Bought successfully:", `Order number: ${orderResultBid.id}`);
init(client);
} else {
if (orderResultBid?.status == 'Expired'){
throw new Error("Buy Order Expired");
} else{
throw new Error(orderResultBid?.status);
}
}
}
(async () => {
const apisecret = API_SECRET;
const apikey = API_KEY;
const client = new backpack_client_1.BackpackClient(apisecret, apikey);
init(client);
})()