-
Notifications
You must be signed in to change notification settings - Fork 5
/
position_consolidator.py
executable file
·58 lines (45 loc) · 1.78 KB
/
position_consolidator.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
#!/usr/bin/env python3
"""
Position consolidator, converts all held currency into the chosen currency
usage position_consolidator.py --endpoint 'http://localhost:8080' --apikey 'api_key1' --currency 'JPY'
"""
import argparse
from fxbattleclient import FxClient, FxClientError
from time import sleep
import random
from math import ceil
currencies=["GBP", "EUR", "JPY", "USD"]
parser = argparse.ArgumentParser(description='Position consolidator')
parser.add_argument('--endpoint',
default='http://localhost:8080',
help='the api endpoint')
parser.add_argument('--apikey',
default='api_key1',
help='the api key')
parser.add_argument('--currency',
default='GBP', choices=currencies,
help='the selected currency')
parser.add_argument('--buffer', type=int,
default=10,
help='small buffer amount, to overcome the fee')
args = parser.parse_args()
print("Position consolidator", "endpoint:", args.endpoint, "apikey:", args.apikey, "currency:", args.currency)
client = FxClient(args.endpoint, args.apikey)
targetCurrncy = args.currency
buff = args.buffer
try:
account = client.account()
if "error" in account:
print("could not get account details", account["error"])
raise
print("account", account)
for cur in currencies:
tradedPair = cur + targetCurrncy
if cur in account and cur != targetCurrncy:
if account[cur] > 1:
account = client.sell(tradedPair, account[cur])
elif account[cur] < 0:
account = client.buy(tradedPair, ceil(-account[cur]) + buff)
print("account", account)
except(FxClientError):
print("error contacting the endpoint")