-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrid_trader.py
181 lines (149 loc) · 6.62 KB
/
grid_trader.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
import os
import asyncio
from time import time, time_ns
import binance as exchange
import sqlite3 as database
import logging as log
# import colorama as colour
from colorama import Fore as colour
import json
from binance.enums import *
# Need to make use of the following code
# for frame in cycle(r'-\|/-\|/'):
# print('\r', frame, sep='', end='', flush=True)
# sleep(0.2)
client = exchange.Client()
# client.order_market_buy(symbol,quantity,)
# order=client.create_test_order(symbol='BNBBTC',
# side=SIDE_SELL,
# type=ORDER_TYPE_MARKET,
# timeInForce=TIME_IN_FORCE_GTC,
# quantity=0.1
# )
class grid_trade:
def get_coins(type_of):
"""request the coin to create the bot i.e grid bot"""
if type_of == 0:
execution = "/home/puneeth/programmes/others/bot/tmp/binance-pump-bot-main/python/detector/bin/python3 /home/puneeth/programmes/others/bot/tmp/binance-pump-bot-main/python/detect_telegram.py"
return os.system(execution)
elif type_of == 1:
execution = "python3 -u /home/puneeth/programmes/others/bot/tmp/botter/detect.py"
return os.system(execution)
else:
return "no coin input found"
def get_values(file):
"""Take a file and prints config file values and returns the dictionary"""
file = json.load(file)
values = file['configs'][0]
for value in values:
print(colour.LIGHTMAGENTA_EX+str(value) +
" : "+values[value]+"\n"+colour.RESET)
return values
def client_connect(config):
"""gets input of config and returns the client connection"""
client = exchange.Client(config['api-key'], config['api-secret'])
start = time()
client.ping()
print(colour.BLUE+" PING time to api : {}".format(time()-start))
return client
# Binance API Helper Debug mode
def debug_mode(client):
"""This function will print the realtime utc standard time of server and status"""
# client.ping()
time_res = client.get_server_time()
print(" Server Time: {}".format(
time_res["serverTime"]))
status = client.get_system_status()
print(" System Status: {}".format(
status["msg"])+colour.RESET)
#need to do formating of the value of the amount to be placed for limit orders only
def market_orders(client, coin, value, type_of_order):
"""parameters :
client(binance client)
coin(coin name)
value(amount to spend)
type_of_order(order side buy or sell)
This function will send the market orders to the binance server depending on the type_of_order(buy or sell) , coin and value"""
if type_of_order == "buy":
market_order = client.order_market_buy(
symbol=coin,
quantity=value)
return market_order
elif type_of_order == "sell":
market_order = client.order_market_sell(
symbol=coin, quantity=value)
return market_order
else:
print(
colour.RED+" ---------------------------------------------------------------")
print(
colour.RED+" | check the type_of_order you passed it in neither buy nor sell |"+colour.RESET)
print(
colour.RED+" ---------------------------------------------------------------"+colour.RESET)
return exit(1)
def limit_orders(client, coin, value, type_of_order, which_price):
"""This function will send the limit orders to the binance server depending on the type_of_order(buy or sell) , coin , value
and price at which order should be placed"""
if type_of_order == "buy":
limit_order = client.order_limit_buy(
symbol=coin,
quantity=value,
price=which_price)
return limit_order
elif type_of_order == "sell":
limit_order = client.order_limit_sell(
symbol=coin,
quantity=value,
price=which_price)
return limit_order
else:
print(
colour.RED+" ---------------------------------------------------------------")
print(
colour.RED+" | check the type_of_order you passed it in neither buy nor sell |"+colour.RESET)
print(
colour.RED+" ---------------------------------------------------------------"+colour.RESET)
return exit(1)
def cancel_orders(coin,id):
open_orders=client.get_open_orders(symbol=coin)
if id in open_orders:
client.cancel_order(id)
return
else:
print("request for cancel failed order is filled !")
def get_values_monitor(coin,id):
"""This function plays a key role in this strategy
It monitors the market of the input coin and sends signals to the
buy sell order creator and stop loss function
grid level manager to adjust
the grid levels"""
# client.get_avg_price(coin)
# all_open=client.get_open_orders(symbol=coin)
# all_are=client.get_all_orders
# for open_orders in all_open:
# if open_orders["orderId"]==id:
# pass
all_are=client.get_all_orders(symbol=coin)
for orders in all_are:
if orders["status"]=="FILLED":
return orders
def main():
print("================================")
print(colour.YELLOW+"Starting grid_bot"+colour.RESET)
print("================================")
print(colour.GREEN+"getting values from configuration"+colour.RESET)
values = {}
with open("config.json") as file:
values = grid_trade.get_values(file)
print(colour.GREEN+" ================================connecting to server===================================="+colour.RESET)
client = grid_trade.client_connect(values)
grid_trade.debug_mode(client)
print(colour.GREEN+"For detect_telegram use 0 for detect_custom use 1 ")
print(colour.LIGHTCYAN_EX+"checking market of the pair {}/{}".format(1,
values["quote"])+colour.RESET)
# print(colors.BOLD+colors.pretty_output.write('BOLD'+"checking market of the pair {}/{}".format(grid_trade.get_values(1)),values["quote"]))
# print(client.get_all_tickers())
# print(colour.RESET)
# main()
# print(grid_trade.get_coins(0))
# grid_trade.market_orders("client","btcUSDT",10,"none")