-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
89 lines (70 loc) · 2.61 KB
/
main.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
# Import the libraries we need
import time, logging, os
import pandas as pd
import krakenex
from pykrakenapi import KrakenAPI
from dotenv import load_dotenv
# Grab environment variables
load_dotenv()
API_KEY = os.getenv("KRAKEN_API_KEY")
API_SECRET = os.getenv("KRAKEN_API_SECRET")
TRACK_TICKER = os.getenv("TRACK_TICKER")
TRADE_TICKER = os.getenv("TRADE_TICKER")
api = krakenex.API(key=API_KEY, secret=API_SECRET)
kraken = KrakenAPI(api)
# Set up the logging
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)
logging.basicConfig(
level=logging.INFO, format="%(asctime)s: %(levelname)s: %(message)s"
)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s: %(levelname)s: %(message)s")
console.setFormatter(formatter)
logging.getLogger("").addHandler(console)
# Create the main script logic
while True:
logging.info("--------- Start of current 5 minute period ---------")
logging.info(pd.Timestamp.now())
try:
BTC_old = float((kraken.get_ticker_information(TRACK_TICKER))["b"][0][0])
except Exception as e:
logging.warning("Error obtaining data")
time.sleep(300)
try:
BTC_new = float((kraken.get_ticker_information(TRACK_TICKER))["b"][0][0])
except Exception as e:
logging.warning("Error obtaining data")
percent = ((BTC_new - BTC_old) * 100) / BTC_old
logging.info(f"BTC moved {percent:.2f}% over the last 5 minutes")
if percent >= 5:
try:
ETH = float((kraken.get_ticker_information(TRADE_TICKER))["a"][0][0]) + 2
except Exception as e:
logging.warning("Error obtaining data")
try:
response = kraken.add_standard_order(
pair=TRADE_TICKER,
type="buy",
ordertype="limit",
volume="0.007",
price=ETH,
validate=False,
)
except Exception as e:
logging.warning("Error placing order")
logging.info(f"Order details:\n{response}")
time.sleep(2)
try:
check_order = kraken.query_orders_info(response["txid"][0])
except Exception as e:
logging.warning("Error checking order")
if check_order["status"][0] == "open" or "closed":
logging.info("Order executed sucessfully")
break
else:
logging.info("Order rejected")
break
else:
logging.info("--------- End of current 5 minute period ---------")