· 5 years ago · Feb 09, 2020, 10:10 PM
1import time,sys, smtplib, json, request
2from coinbase.wallet.client import Client #the api library to check prices
3from requests.auth import AuthBase #to authorize the API
4from email.mime.multipart import MIMEMultipart
5from email.mime.text import MIMEText
6
7below = 8120
8over = 0
9braked = False #A variable that doesnt let the program constantly email me
10goingUp = False #if its went past the "below" (which tells me to buy) and is now going up, this will be true and will reset the alerter
11goingDown = False #if its went past the "over" (which tells me to sell) and is now going up, this will be true and will reset the alerter
12i = 0
13
14#Email Addresses + Messages:
15senderEmail = "hasanemailer@gmail.com"
16receiverEmail = "rennannance@aim.com"
17senderPass = "T@Up@ssw0rd"
18msg = MIMEMultipart()
19msg['From'] = senderEmail
20msg['To'] = receiverEmail
21
22#Autharizing COinBase API
23API_KEY = ' Y6oFKBGTjgsmtNxc'
24API_SECRET = 'DI1ArTESVkr0ZHGvyjWHavrfhmdrQG10'
25class CoinbaseWalletAuth(AuthBase):
26 def __init__(self, api_key, secret_key):
27 self.api_key = api_key
28 self.secret_key = secret_key
29 def __call__(self, request):
30 timestamp = str(int(time.time()))
31 message = timestamp + request.method + request.path_url + (request.body or '')
32 signature = hmac.new(self.secret_key, message, hashlib.sha256).hexdigest()
33 request.headers.update({
34 'CB-ACCESS-SIGN': signature,
35 'CB-ACCESS-TIMESTAMP': timestamp,
36 'CB-ACCESS-KEY': self.api_key,
37 })
38 return request
39def checkAndEmail():
40 global braked, goingDown, goingUp
41 if (price<below) and (over == 0):
42 x = "Alert: below %s" %below
43 msg['Subject'] = x
44 body = "BUY"
45 braked = True
46 goingDown = True
47 print(x)
48 emailMe(body)
49 if (price>over) and (below == 0):
50 x = "Alert: OVER %s" %over
51 msg['Subject'] = x
52 body = "SELL" #Email Body
53 braked = True
54 goingUp = True
55 print(x)
56 emailMe(body)
57 elif (price<below) and (price>over) and (below != 0) and (over != 0):
58 x = "Alert: BETWEEN %s and %s" %(over, below)
59 msg['Subject'] = x
60 body = "SAFE?"
61 braked = True
62 print(x)
63 emailMe(body)
64def emailMe(body):
65 print("sending...")
66 sys.stdout.write(str(braked)+"_2 ")
67 msg.attach(MIMEText(body, 'plain'))
68 text = msg.as_string() #Turn the msg into a string
69 #Logging Into Sender's EMail:
70 server = smtplib.SMTP('smtp.gmail.com', 587)
71 server.starttls()
72 server.login(senderEmail, senderPass)
73 #Send Mail:
74 server.sendmail(senderEmail, receiverEmail, text)
75 print("sent")
76 server.quit()
77
78print("MONITORING...")
79client = Client(API_KEY, API_SECRET, api_version='2018-01-24')
80currency_code = 'USD'
81while True:
82 price = client.get_spot_price(currency=currency_code)
83 price = float(price.amount)
84 if (braked == False):
85 checkAndEmail()
86 if (braked == True):
87 if (goingDown == True) and (price>below) and (over == 0): #if it was going down (sent me email to BUY) now its going up, reset the alerter
88 braked = False
89 print("Restarting Alerts")
90 if (goingUp == True) and (price<over) and (below == 0): #if it was going up (sent me email to SELL) now its going down, reset the alerter
91 braked = False
92 print("Restarting Alerts")
93 sys.stdout.write(str(i)+" ")
94 print(price)
95 i = i+1
96 time.sleep(.3)