-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathorder.py
More file actions
98 lines (82 loc) · 3.19 KB
/
Copy pathorder.py
File metadata and controls
98 lines (82 loc) · 3.19 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
from functools import reduce
from config import Connect
import datetime as dt
import talib
import numpy
class Order:
def __init__(self):
self.client = Connect().make_connection()
# leverage = self.client.LinearPositions.LinearPositions_saveLeverage(symbol="ETHUSDT", buy_leverage=50, sell_leverage=50).result()
balance = self.client.Wallet.Wallet_getBalance(coin="USDT").result()[0]['result']['USDT']['equity']
eth_last_price = float(self.client.LinearMarket.LinearMarket_trading(symbol="ETHUSDT").result()[0]['result'][0]['price'])
##### Extract Klines data
date_before = int((dt.datetime.now() - dt.timedelta(days = 60)).timestamp())
klines=(self.client.LinearKline.LinearKline_get(symbol="ETHUSDT", interval="D", **{'from':date_before}).result()[0]['result'])
close = []
for i in klines:
close.append(float(i['close']))
close_arr = numpy.asarray(close)
close_rsi = talib.RSI(close_arr, 14)
max_rsi = talib.MAX(close_rsi, 14)
min_rsi = talib.MIN(close_rsi, 14)
stochrsi = (close_rsi - min_rsi)/(max_rsi - min_rsi)
k = talib.SMA(stochrsi, 3)*100
d = talib.SMA(k, 3)
quantity = 0.16
if k[-1]<d[-1]:
buy_quantity = quantity*0.75
sell_quantity = quantity
elif k[-1]>d[-1]:
sell_quantity = quantity*0.75
buy_quantity = quantity
else:
sell_quantity = quantity
buy_quantity = quantity
# print(close)
print ("buy quantity",k[-1])
print("sell quantity",d[-1])
self.sell_quan = round(float(balance*50/eth_last_price*sell_quantity),2)
self.buy_quan = round(float(balance*50/eth_last_price*buy_quantity),2)
print(self.sell_quan)
print(self.buy_quan)
def sell(self):
order = self.client.LinearOrder.LinearOrder_new(
side="Sell",
symbol="ETHUSDT",
order_type="Market",
qty=self.sell_quan,
time_in_force="GoodTillCancel",
reduce_only=False,
close_on_trigger=False).result()
return(order)
def buy(self):
order = self.client.LinearOrder.LinearOrder_new(
side="Buy",
symbol="ETHUSDT",
order_type="Market",
qty=self.buy_quan,
time_in_force="GoodTillCancel",
reduce_only=False,
close_on_trigger=False).result()
return(order)
def close_order(self, qty, side):
if side == "BUY":
order = self.client.LinearOrder.LinearOrder_new(
side="Sell",
symbol="ETHUSDT",
order_type="Market",
qty=qty,
time_in_force="GoodTillCancel",
reduce_only=True,
close_on_trigger=False
).result()
elif side == "SELL":
order = self.client.LinearOrder.LinearOrder_new(
side="Buy",
symbol="ETHUSDT",
order_type="Market",
qty=qty,
time_in_force="GoodTillCancel",
reduce_only=True,
close_on_trigger=False
).result()