-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathxrp.py
268 lines (228 loc) · 8.79 KB
/
xrp.py
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import ta
import time
import ccxt
import config
import pandas as pd
from pybit import HTTP
exchange = ccxt.bybit({'apiKey': config.api_key,'secret': config.api_secret})
client = HTTP(api_key=config.api_key,api_secret=config.api_secret)
qty1 = config.qty1
maxq = config.maxq
symbol = config.symbol
stoHm = config.stoHm
stoLm = config.stoLm
stoHs = config.stoHs
stoLs = config.stoLs
decimals = 4
def getOrderBook():
global ask
global bid
orderbook = exchange.fetchOrderBook(symbol=symbol,limit=10)
bid = orderbook['bids'][0][0] if len (orderbook['bids']) > 0 else None
ask = orderbook['asks'][0][0] if len (orderbook['asks']) > 0 else None
def get_6ema():
bars = exchange.fetchOHLCV(symbol=symbol,timeframe = '1m',limit=18)
df = pd.DataFrame(bars,columns=['Time','Open','High','Low','Close','Vol'])
df['EMA 6 High'] = ta.trend.EMAIndicator(df['High'], window=6).ema_indicator()
df['EMA 6 Low'] = ta.trend.EMAIndicator(df['Low'], window=6).ema_indicator()
global ema6hgh
global ema6low
ema6hgh = df['EMA 6 High'][17]
ema6low = df['EMA 6 Low'][17]
ema6hgh = round(ema6hgh,decimals)
ema6low = round(ema6low,decimals)
def get_60ema():
bars = exchange.fetchOHLCV(symbol=symbol,timeframe = '1m',limit=180)
df = pd.DataFrame(bars,columns=['Time','Open','High','Low','Close','Vol'])
df['EMA 60 Close'] = ta.trend.EMAIndicator(df['Close'], window=60).ema_indicator()
global ema60
ema60 = df['EMA 60 Close'][179]
ema60 = round(ema60,decimals)
def stoch15():
stochastic_bars = exchange.fetchOHLCV(symbol=symbol,timeframe = '1m',limit=16)
df = pd.DataFrame(stochastic_bars, columns=['Time','Open','High','Low','Close','Vol'])
df['Stoch 15 main'] = ta.momentum.StochasticOscillator(df['High'], df['Low'], df['Close'], window=15).stoch()
df['Stoch 15 sgnl'] = ta.momentum.StochasticOscillator(df['High'], df['Low'], df['Close'], smooth_window=3, window=15, fillna=True).stoch_signal()
stoch = float(df['Stoch 15 main'][15])
stoch_signal = float(df['Stoch 15 sgnl'][15])
global real_sto
global real_stos
real_sto = round(stoch,4)
real_stos = round(stoch_signal,4)
def get_position():
positions = client.my_position(symbol=symbol)
for position in positions['result']:
if position['side'] == 'Sell':
global sell_position_size
global sell_position_prce
sell_position_size = position['size']
sell_position_prce = position['entry_price']
def get_buy_order():
orders = client.get_active_order(symbol=symbol, limit=21)
for order in orders['result']['data']:
global tp_order
global buy_order_size
global buy_order_id
tp_order = order['order_status'] != 'Cancelled' and order['order_status'] != 'Filled' and order['order_status'] == 'New' and order['side'] == "Buy" and order['reduce_only'] == True
buy_order_size = order['qty']
buy_order_id = order['order_id']
def get_sell_order():
orders = client.get_active_order(symbol=symbol, limit=21)
for order in orders['result']['data']:
global sell_order
global sell_order_id
global sell_order_size
sell_order = order['order_status'] != 'Cancelled' and order['order_status'] != 'Filled' and order['order_status'] == 'New' and order['side'] == "Sell" and order['reduce_only'] == False
sell_order_id = order['order_id']
sell_order_size = order['qty']
def cancel_sell_orders():
try:
orders = client.get_active_order(symbol=symbol)
for order in orders['result']['data']:
if order['order_status'] != 'Filled' and order['side'] == 'Sell' and order['order_status'] != 'Cancelled':
client.cancel_active_order(symbol=symbol, order_id=order['order_id'])
else:
pass
except TypeError:
pass
def cancel_buy_orders():
try:
orders = client.get_active_order(symbol=symbol)
for order in orders['result']['data']:
if order['order_status'] != 'Filled' and order['side'] == 'Buy' and order['order_status'] != 'Cancelled':
client.cancel_active_order(symbol=symbol, order_id=order['order_id'])
else:
pass
except TypeError:
pass
while True:
print('♌',symbol)
try:
stoch15()
print('| Stoch Main:',real_sto, '|', real_stos, ':Stoch Sgnl')
except:
print('⛔ Error Loading Stochastic Data')
pass
try:
get_6ema()
print('| EMA 6 High:',ema6hgh, '|', ema6low, ':EMA 6 Low')
except:
print('⛔ Error Loading EMA 6 Data')
pass
# get_60ema()
# print('| EMA 60 Close:' ,ema60)
try:
getOrderBook()
print('| Ask:',ask, '|', bid, ':Bid')
except:
print('⛔ Error Getting Orderbook Data')
pass
try:
get_position()
except:
print('⛔ Error Getting Position Data')
pass
try:
get_buy_order()
except:
print('⛔ Error Getting TP Data')
pass
try:
get_sell_order()
except:
print('⛔ Error Getting Entry Data')
pass
min_tp_distance = round(sell_position_prce - ((ema6hgh - ema6low)/2),decimals)
tp_distance = round(ema6hgh - (ema6hgh - ema6low),decimals)
entry = round(ema6hgh + (ema6hgh - ema6low),decimals)
good_stoch_H = real_sto > stoHm and real_stos > stoHs
good_stoch_L = real_sto < stoLm and real_stos < stoLs
# good_EMA60 = bid > ema60
# TP
if tp_order == False and sell_position_size != 0 and bid > min_tp_distance:
try:
print('| ➟ Placing TP...')
place_active_order = client.place_active_order(
side="Buy",symbol=symbol,order_type="Limit",
price=min_tp_distance,
qty=sell_position_size,
time_in_force="GoodTillCancel",reduce_only=True,close_on_trigger=True)
except:
pass
else:
pass
if tp_order == False and sell_position_size != 0 and bid < min_tp_distance:
try:
print('| ➟ Placing TP...')
place_active_order = client.place_active_order(
side="Buy",symbol=symbol,order_type="Market",
qty=sell_position_size)
except:
pass
else:
pass
if tp_order == True and sell_position_size > buy_order_size:
try:
print('| ✗ Canceling TP...')
cancel_buy_orders()
print('| ➟ Placing TP...')
place_active_order = client.place_active_order(
side="Buy",symbol=symbol,order_type="Limit",
price=min_tp_distance,
qty=sell_position_size,
time_in_force="GoodTillCancel",reduce_only=True,close_on_trigger=True)
except:
pass
else:
pass
# Entry
if sell_position_size == 0:
try:
print('| ✗ Canceling Active Entry Order...')
cancel_sell_orders()
except:
pass
if ask > ema6hgh and good_stoch_H == True and sell_position_size == 0:
try:
print('| ➟ Placing Entry Order...')
place_active_order = client.place_active_order(
side="Sell",symbol=symbol,order_type="Limit",
price=ask,
qty=qty1,
time_in_force="GoodTillCancel",reduce_only=False,close_on_trigger=False)
except:
pass
# Entry 2
if sell_position_size >= qty1:
try:
print('| ✗ Canceling Active Average Entry Order...')
cancel_sell_orders()
except:
pass
if sell_position_size >= qty1 and ema6low > sell_position_prce and ask > ema6hgh and good_stoch_H == True and sell_position_size < maxq:
try:
print('| ➟ Placing Active Average Entry Order...')
place_active_order = client.place_active_order(
side="Sell",symbol=symbol,order_type="Limit",
price=ask,
qty=sell_position_size,
time_in_force="GoodTillCancel",reduce_only=False,close_on_trigger=False)
except:
pass
# Drawings
if ask > ema6hgh:
print('| 🗹 Ask > EMA 6 High')
else:
print('| Ask < EMA 6 High')
if good_stoch_H == True:
print('| 🗹 Stoch >',stoHs)
else:
print('| Stoch <',stoHs)
print('------------------------------------------')
if maxq > sell_position_size:
print('| 🗹 Size is OK')
print('------------------------------------------')
print('| ➢ Position Price:',sell_position_prce)
print('| ➢ Position Size:',sell_position_size)
# print('Entr id:',sell_order_id)
time.sleep(3)