This repository has been archived by the owner on Oct 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cryptobook.py
executable file
·213 lines (178 loc) · 5.65 KB
/
cryptobook.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
#! /usr/bin/env python3.6
import sys
import argparse
import os
import time
import json
from terminaltables import SingleTable
from colr import color
from blessed import Terminal
from function.function import *
from classes.order import Order
from api.bittrex import API_Bittrex
def price(args):
price = get_last_price(args)
if not price:
return
if args.quiet:
print(price)
else:
print(get_exchange(args), get_market(args), price)
def buy(args):
path = args.config['book-path']
# Check if the file book.json exist and if not create it
# and iniate it with '[]'
if not os.path.isfile(path):
data = "[]"
file = open(path, 'a+')
file.write(data)
file.close()
order = Order(args)
data = json.loads(open(path).read())
data.append(order.toDict())
with open(path, 'w') as outfile:
json.dump(data, outfile, indent=4)
# Display the order with a nice table
table_data = [
['id', 'Exchange', 'Market', 'Price'],
[order.id , order.exchange, order.market, order.last]
]
table = SingleTable(table_data)
table.title = 'Buy Order'
print (table.table)
def close(args):
path = args.config['book-path']
if not os.path.isfile(path):
print('Order book empty, use buy command to fill it')
return
data = json.loads(open(path).read())
i = 0
while i < len(data):
if data[i]['id'] == args.id:
table_data = [['id', 'Exchange', 'Market', 'Price', 'Current', 'Profit']]
current = get_last_price_tmp(data[i]['market'])
profit = get_profit(data[i]['last'], current)
table_data.append([data[i]['id'], data[i]['exchange'],
data[i]['market'], data[i]['last'], current , profit])
table = SingleTable(table_data)
table.title = 'Close Order'
print (table.table)
data.pop(i)
break
i += 1
with open('./book.json', 'w') as outfile:
json.dump(data, outfile, indent=4)
def position(args):
path = args.config['book-path']
if not os.path.isfile(path):
print('Order book empty, use buy command to fill it')
return
data = json.loads(open(path).read())
table_data = [['id', 'Exchange', 'Market', 'Price', 'Current', 'Profit']]
i = 0
while i < len(data):
current = get_last_price_tmp(data[i]['market'])
profit = get_profit(data[i]['last'], current)
table_data.append([data[i]['id'], data[i]['exchange'],
data[i]['market'], data[i]['last'], current , profit])
i += 1
table = SingleTable(table_data)
if args.live:
return(table.table)
else:
print(table.table)
def refresh(args):
"""Ugly way to make a auto refresh tab"""
term = Terminal()
key = ""
with term.fullscreen(), term.cbreak():
while key != 'q':
tab = position(args)
print(term.move_y(0) + ('Press Q to exit the live mode').rstrip() +
'\n' + term.center(tab).rstrip() + term.clear_eos)
loop = term.inkey(timeout=0.5)
def main(args):
config_path = "./config.json"
try:
args.config = json.loads(open(config_path).read())
except:
print('Error loading config.json')
sys.exit(0)
if hasattr(args, 'function'):
if hasattr(args, 'live'):
refresh(args)
else:
args.function(args)
else:
print('Use -h to see the usage')
if __name__ == '__main__':
# Create the top-level parser
parser = argparse.ArgumentParser(prog='Cryptobook')
parser.add_argument('--version',
action='version',
version='%(prog)s 0.0.1')
# Creat subparser for the Price command
subparsers = parser.add_subparsers(help='Use -h with the subcommande to see the help section')
# Create the parser for the "price" command
parser_price = subparsers.add_parser('price',
help='Show the current price of the currency specified in currency')
parser_price.add_argument('currency',
type=str,
help='Currency you want to see the price default exchange in BTC')
parser_price.add_argument('-m', '--market',
action='store',
type=str,
dest='market',
nargs='?',
choices=['BTC', 'ETH', 'USDT'],
help='Choose the market BTC ETH USDT| default is BTC')
parser_price.add_argument('-e', '--exchange',
action='store',
type=str,
dest='exchange',
nargs='?',
help='Choose the market default is Bittrex')
parser_price.add_argument('-q', '--quiet',
action='store_true',
help='Only price is Ouput')
parser_price.set_defaults(function=price)
# Create the parser for the "buy" command
parser_buy = subparsers.add_parser('buy',
help='Show the current price of the currency specified in currency')
parser_buy.add_argument('currency',
type=str,
help='Currency you want to see the price default exchange in BTC')
parser_buy.add_argument('-m', '--market',
action='store',
type=str,
dest='market',
nargs='?',
choices=['BTC', 'ETH', 'USDT'],
help='Choose the market BTC ETH | default is BTC')
parser_buy.add_argument('-e', '--exchange',
action='store',
type=str,
dest='exchange',
nargs='?',
help='Choose the exchange default is Bittrex')
parser_buy.add_argument('-p', '--price',
type=float,
help='If you want to be more accurate you can specify the price, ignoring the one from the API')
parser_buy.set_defaults(function=buy)
# Create the parser for the "position" command
parser_pos = subparsers.add_parser('position',
help='Show your current position')
parser_pos.add_argument('-l', '--live',
action='store_true',
help='Choose the market default is Bittrex')
parser_pos.set_defaults(function=position)
# Create the parser for the "close" command
parser_close = subparsers.add_parser('close',
help='Show your current position')
parser_close.add_argument('id',
type=int,
help='Close the id from the order book')
parser_close.set_defaults(function=close)
# Parse argument lists
args = parser.parse_args()
main(args)