-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
executable file
·241 lines (183 loc) · 10.1 KB
/
app.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
import sys
import os
import time
import socket
import math
from datetime import datetime, timezone, timedelta
tzinfo = timezone(timedelta(hours=+2.0))
from pprint import pprint
from copy import deepcopy
from termcolor import colored
from dotenv import load_dotenv
import asyncio
from telethon import TelegramClient, events
import ccxt
from ftx.client import FtxClient
from ftx.ftx_perpetuals import ftx_perpetuals
from kucoin.utils import *
from kucoin.contracts import kucoinfutures_contract_size
from utils.channels import *
from utils.params import *
from utils.printer import print_message, print_start
from parsers.parser_wolfxsignals import parser_wolfxsignals
def opposite(type_order):
if type_order=='buy':
return 'sell'
return 'buy'
def sort_orders(op_data_structure):
if op_data_structure['side'] == 'buy':
op_data_structure['take_profits'] = sorted(op_data_structure['take_profits'])
op_data_structure['stop_losses'] = sorted(op_data_structure['stop_losses'],reverse=True)
else:
op_data_structure['take_profits'] = sorted(op_data_structure['take_profits'],reverse=True)
op_data_structure['stop_losses'] = sorted(op_data_structure['stop_losses'])
return op_data_structure
def string_to_float_prices(op_data_structure):
columns = ['entry_prices','take_profits','stop_losses']
for col in columns:
for i in range(len(op_data_structure[col])):
op_data_structure[col][i] = float(op_data_structure[col][i])
return op_data_structure
async def trader_kucoinfutures(order_data, exchange, maintenance_capital =0.05):
'''
get aviable balance
get minimal order
rebalance with a multiple of the minimal order
'''
order_data = string_to_float_prices(op_data_structure=order_data)
order_data = sort_orders(op_data_structure=order_data)
amount_usd_position = await get_amount_position_usdt()
if print_op: print('position in usdt $ ',amount_usd_position,'leverage ',order_data['leverage'])
n_entry_prices = len(order_data['entry_prices'])
n_take_profits = len(order_data['take_profits'])
n_stop_losses = len(order_data['stop_losses'])
if get_free_balance(exchange) < (1+maintenance_capital) * amount_usd_position:
return None
for i in range(len(order_data['entry_prices'])):
entry_price = deepcopy(order_data['entry_prices'][i])
amount_token_position = round( amount_usd_position / n_entry_prices / entry_price , 8 )
if print_op: print('amount_token_position ',amount_token_position)
# ENTRY LONG
entry_order = exchange.create_limit_order(symbol=order_data['symbol'],
side=order_data['side'],
amount=amount_token_position,
price=entry_price,
params={'leverage':order_data['leverage'], # TRIGGER PRICE #,'stop':'down','stopPriceType':'TP','stopPrice':entry_price,
'forceHold':False})
# TAKE PROFIT
take_profit_quantities = balance_kucoinfutures_contract_quantity(order_data['take_profits'], amount_token_position)
if print_op: print('take_profit_quantities ',take_profit_quantities)
for j in range(len(order_data['take_profits'])):
order = exchange.create_limit_order(symbol=order_data['symbol'],
side=opposite(order_data['side']),
amount=take_profit_quantities[j],
price=order_data['take_profits'][j],
params={'leverage':order_data['leverage'],'reduceOnly':True,
'stop':kucoin_side(side=order_data['side'],trigger='TAKEPROFIT'),'stopPriceType':'TP','stopPrice':order_data['take_profits'][j]})
# STOP LOSS
stop_loss_quantities = balance_kucoinfutures_contract_quantity(order_data['stop_losses'], amount_token_position)
if print_op: print('stop_loss_quantities ',stop_loss_quantities)
for k in range(len(order_data['stop_losses'])):
order = exchange.create_limit_order(symbol=order_data['symbol'],
side=opposite(order_data['side']),
amount=stop_loss_quantities[k],
price=order_data['stop_losses'][k],
params={'leverage':order_data['leverage'],'reduceOnly':True
,'stop':kucoin_side(side=order_data['side'],trigger='STOPLOSS'),'stopPriceType':'TP','stopPrice':order_data['stop_losses'][k],
'forceHold':False})
async def trader_ftx(order_data, exchange,maintenance_capital =0.05):
order_data = string_to_float_prices(op_data_structure=order_data)
order_data = sort_orders(op_data_structure=order_data)
amount_usd_position = await get_amount_position_usdt()
if print_op: print('position in usdt $ ',amount_usd_position,'real LEVERAGE on FTX: 2 ')
n_entry_prices = len(order_data['entry_prices'])
n_take_profits = len(order_data['take_profits'])
n_stop_losses = len(order_data['stop_losses'])
if get_free_balance() < (1+maintenance_capital) * amount_usd_position:
return None
for i in range(len(order_data['entry_prices'])):
entry_price = deepcopy(order_data['entry_prices'][i])
amount_token_position = round( amount_usd_position / n_entry_prices / entry_price , 8 )
# ENTRY LONG
if print_op: print('amount_token_position ',amount_token_position)
entry_order = exchange.create_limit_order(symbol = order_data['symbol'],
side = order_data['side'],
amount = amount_token_position,
price = entry_price)
# TAKE PROFIT
take_profit_quantities = balance_trigger_orders_quantity(order_data['take_profits'], amount_token_position)
if print_op: print('take_profit_quantities ',take_profit_quantities)
for j in range(len(order_data['take_profits'])):
take_profit_order = exchange.create_order(symbol = order_data['symbol'],
type = 'takeProfit',
side = opposite(order_data['side']),
amount = take_profit_quantities[j] ,
price = order_data['take_profits'][j],
params = {'triggerPrice':order_data['take_profits'][j],'reduceOnly':True })
# STOP LOSS
stop_loss_quantities = balance_trigger_orders_quantity(order_data['stop_losses'], amount_token_position)
if print_op: print('stop_loss_quantities ',stop_loss_quantities)
for k in range(len(order_data['stop_losses'])):
stop_loss_order = exchange.create_order(symbol = order_data['symbol'],
type = 'stop',
side = opposite(order_data['side']),
amount = stop_loss_quantities[k],
price = order_data['stop_losses'][k],
params = {'triggerPrice':order_data['stop_losses'][k],'reduceOnly':True })
async def deamon_trader(op_data,channel):
"""
ranking exchange from the hieghst leverage to the least
and open operation if free balance avaiable
"""
FTX_C1 = os.getenv('FTX_C1')
FTX_C1_HASH = os.getenv('FTX_C1_HASH')
ftx_c1 = ccxt.ftx({
'headers': {
'FTX-SUBACCOUNT': 'c1',
},
'apiKey': FTX_C1,
'secret': FTX_C1_HASH,
'enableRateLimit': True,
})
KUCOIN_MAIN_KEY = os.getenv('KUCOIN_MAIN_KEY')
KUCOIN_MAIN_SECRET = os.getenv('KUCOIN_MAIN_SECRET')
KUCOIN_MAIN_PASS = os.getenv('KUCOIN_MAIN_PASS')
kucoinfutures_main = ccxt.kucoinfutures({
'adjustForTimeDifference': True,
"apiKey": KUCOIN_MAIN_KEY,
"secret": KUCOIN_MAIN_SECRET,
'password': KUCOIN_MAIN_PASS,
})
await trader_kucoinfutures( order_data = op_data , exchange = kucoinfutures_main)
# To implement
# ftx_trader with FTX API in subs
# for kucoinfutures with ccxt API in particular conditions
async def main():
load_dotenv()
TELEGRAM_USERNAME = os.getenv('TELEGRAM_USERNAME')
TELEGRAM_ID = os.getenv('TELEGRAM_ID')
TELEGRAM_HASH = os.getenv('TELEGRAM_HASH')
client = TelegramClient(TELEGRAM_USERNAME, TELEGRAM_ID, TELEGRAM_HASH)
if print_op: print_start()
while True:
# t.me/test_channel_pubblico_24991204
# t.me/freecrypto_signals
@client.on(events.NewMessage( chats = CHANNEL_1 ))
async def trader_CHANNEL_1( event ):
NEW_MESSAGE = event.message.message
signal = parser_wolfxsignals( new_message = NEW_MESSAGE)
if signal:
print_message( message = NEW_MESSAGE, channel = CHANNEL_1 )
await deamon_trader()
await asyncio.sleep(1)
async with client:
if client.is_connected():
await client.run_until_disconnected()
#await client.loop.run_until_complete()
else:
await client.start()
if __name__ == "__main__":
load_dotenv()
FTX_READONLY_C1 = os.getenv('FTX_READONLY_C1')
FTX_READONLY_C1_HASH = os.getenv('FTX_READONLY_C1_HASH')
asyncio.run(main())