-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinance_old_sell.R
215 lines (106 loc) · 3.98 KB
/
binance_old_sell.R
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
library(httr,quietly=TRUE)
library(cowplot,quietly=TRUE)
library(dplyr,quietly=TRUE)
library(data.table,quietly=TRUE)
library(ggplot2,quietly=TRUE)
library(ranger,quietly=TRUE)
library(quantmod,quietly=TRUE)
library(binancer,quietly=TRUE)
library(stringr,quietly=TRUE)
s_round <- function(a,b)
{
c = a
while(round(c,b)>a)
{
c=c*0.99
}
return(round(c,b))
}
binance_new_oco <- function(symbol,side,price,stopprice,quantity) {
params <- list(symbol = symbol,
side = side,
price = price,
stopLimitPrice = stopprice,
stopPrice = stopprice,
quantity = quantity,
stopLimitTimeInForce = "GTC")
ord <- binancer:::binance_query(endpoint = 'api/v3/order/oco', method = 'POST', params = params, sign = TRUE)
}
binance_sell <- function(i)
{
a <- binancer::binance_filters(i) %>% .[filterType=="LOT_SIZE",minQty]
#tot am
name=str_remove(i,"USDT")
if (a<1){a <- abs(log(a,base=10))}
qt <- binance_balances(usdt = TRUE) %>% .[asset==name,free]
price <- binance_depth(i) %>% .$bids %>% .[1,price]
binance_new_order(symbol=i,side="SELL",type="LIMIT",quantity = s_round(qt,a),price=price,test=FALSE,time_in_force = "GTC")
}
finding_old <- function()
{
al <- binance_open_orders() %>% .[price*orig_qty>9 & price*orig_qty<33]
al[,time_diff:=as.numeric( difftime(Sys.time(),time,units="min") ) ]
#if they upen under both line we should sell them
under <- c()
for(i in al[,symbol])
{
stats <- binance_klines(i,interval="15m")
stats[,sma100:=SMA(close,n=100)]
stats[,sma50:=SMA(close,n=50)]
stats <- tail(stats,5)
if (((nrow(stats[sma50 > open ])>1 || nrow(stats[sma100 > open ])>1)))
{
under <- c(under,i)
print(under)
}
}
return(al[symbol %in% under & time_diff > 14 ])
}
replacing_stoploss <- function()
{
qt <- binance_balances(usdt = TRUE,threshold = 0)
#check those which were halved
qt
name_pair <- paste0(qt[usd>15 & usd < 20,asset],"USDT")
if(name_pair[1]=="USDT") break;
al <- binance_open_orders()
tmp <- al[symbol%in%name_pair]#,.N,by=symbol] %>% .[N<=2,symbol]
for ( i in name_pair)
{
binance_new_oco(symbol=i,side="SELL",price=round(15*1.15/qty,abs(a)),quantity = qt,stopprice =round(15/qty,abs(a)))
}
#for all in halved delete the existing order move the stoploss at 15 usdt and limitprice at 17 dollars
for (i in al[symbol%in%tmp,symbol])
{
try({
print ("deleting old order")
z <- binancer::binance_filters(i)
a <- log(z[filterType=="PRICE_FILTER",minPrice],base=10)
qty <- al[symbol==i,orig_qty][1]
binance_cancel_order(symbol=i,order_id=al[symbol==i,order_id][1],client_order_id = al[symbol==i,client_order_id][1])
print("creating new increased stoporder at breakeven")
binance_new_oco(symbol=i,side="SELL",price=round(15*1.15/qty,abs(a)),quantity = qt,stopprice =round(15/qty,abs(a)))
})
}
}
li <- fread("./bin_cred.txt",header=FALSE)
key <- paste(do.call(c,li[1]))
secret <- paste(do.call(c,li[2]))
binance_credentials(key=key,secret=secret)
print("Finding old coins:")
old <- finding_old()
old<- old[type=="STOP_LOSS_LIMIT"]
for (i in old[,symbol])
{
print("we have an old order")
print(i)
print("trying to delete the order")
try({
binance_cancel_order(symbol=i,order_id=old[symbol==i,order_id][1],client_order_id = old[symbol==i,client_order_id][1])
})
print("deleting order,executing sell")
try(binance_sell(i))
print("executed sell")
print("finding halved")
# try(replacing_stoploss())
}