-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCrypto Bot W:GUI_Public.py
161 lines (107 loc) · 3.99 KB
/
Crypto Bot W:GUI_Public.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
#!/usr/bin/env python
# coding: utf-8
# Coinbase Pro Sandbox API with functional GUI
import pandas as pd
import cbpro
import PySimpleGUI as sg
import requests
import json
api_secret = ''
api_key = ''
api_pass = ''
#websocket
class TextWebsocketClient(cbpro.WebsocketClient):
def on_open(self):
self.url = 'wss://ws-feed-public.sandbox.pro.coinbase.com' #connection endpoint
self.message_count = 0
def on_message(self,msg): # creates message for onscreen
self.message_count += 1
msg_type = msg.get('type',None)
if msg_type == 'ticker':
# returns a dict of keys&values.
# we then select the ones we want- 'time','price',ect
time_val = msg.get('time',('-'*27))
price_val = msg.get('price',None)
if price_val is not None:
price_val = float(price_val)
product_id = msg.get('product_id',None)
print(f"{time_val:40} {price_val:.3f} {product_id}\tchannel type: {msg_type}")
def on_close(self):
print(f"<---Websocket connection closed--->\n\tTotal messages: {self.message_count}")
# Could add these output prices to a database
# Closes after n messages or when loses connection
stream = TextWebsocketClient(products=['ETH-BTC'],channels=['ticker'])
stream.start()
#stream.close()
print ("Stream closed")
# creates client object
url='https://api-public.sandbox.pro.coinbase.com'
client = cbpro.AuthenticatedClient(
api_key,
api_secret,
api_pass,
api_url=url)
#Placing a BUY market order
def buy():
client.place_market_order(product_id='ETH-BTC',side='buy',funds=0.00010)
return "Buy Complete"
def sell():
client.place_market_order(product_id='ETH-BTC',side='sell',funds=0.00003)
return "Sell Complete"
# AVERAGE PRCIE
def average_price():
buy_data = 'https://api.coinbase.com/v2/accounts/:account_id/buys/:buy_id'
price = (buy_data['subtotal']['amount'] / buy_data['amount']['amount'])
return price
# Displaying the price
def price():
ticker = requests.get('https://api.pro.coinbase.com/products/ETH-USD/ticker').json()
return ticker['ask'] # return the asking price
# Gets the account history
accounts = client.get_accounts()
for acc in accounts:
currency = acc.get('currency')
if currency=='ETH':
acc_id = acc.get('id')
acc_history = client.get_account_history(acc_id)
def percentage_change():
url = 'https://api.pro.coinbase.com/products/ETH-USD/stats'
headers = {"Accept": "application/json"}
response = requests.get(url, headers=headers)
response = response.json()
# conver to floats
openval= float(response['open'])
highval = float(response['high'])
lowval = float(response['low'])
lastval = float(response['last'])
percent = (((lastval - openval ) / openval *100 )) # percent math
return float(round(percent, 2)) # returns percent with 2 decimal places
# GUI--------------------------------------
theme_name_list = sg.theme('DarkGreen')
sg.set_options(font=("Courier New", 20))
#Buttons
layout = [
[sg.Text("Welcome to your\nCrypto Command Center!\n\nSelect a command...")],
[sg.Button('Buy')],
[sg.Button('Sell')],
[sg.Button('Current Price')],
[sg.Button('24 hr percent change')],
[sg.Button('Exit')]
]
# Name and sizing
window = sg.Window('Crypto Buy/Sell Launch Pad', layout, size = (400, 400)) # title of window
# saves the actions(clicks) and the inputted values
while True:
event, values = window.read()
print(event, values)
if event is None or event == "Exit":
break
if event == "Buy": # Connected to Buy Function
buy()
if event == 'Sell':
sell()
if event == 'Current Price':
sg.popup(price(), keep_on_top=True)
if event == '24 hr percent change':
sg.popup(percentage_change(), keep_on_top=True)
window.close()