forked from tducret/revolut-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
revolutbot.py
executable file
·134 lines (113 loc) · 4.12 KB
/
revolutbot.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import click
from revolut import Revolut, __version__
import revolut_bot
import sys
# Usage : revolutbot.py --help
_CLI_DEVICE_ID = 'revolut_cli'
_BOT_PERCENT_MARGIN = 1 # at least 1% benefit to exchange
_VERBOSE_MODE = False # can be changed with --verbose parameter
_RETURN_CODE_BUY = 0
_RETURN_CODE_DO_NOT_BUY = 1
_RETURN_CODE_ERROR = 2
@click.command()
@click.option(
'--token', '-t',
envvar="REVOLUT_TOKEN",
type=str,
help='your Revolut token (or set the env var REVOLUT_TOKEN)',
)
@click.option(
'--historyfile', '-f',
type=str,
help='csv file with the exchange history',
required=True,
)
@click.option(
'--forceexchange',
is_flag=True,
help='force the exchange, ignoring the bot decision (you may lose money)',
)
@click.option(
'--simulate', '-s',
is_flag=True,
help='do not really exchange your money if set',
)
@click.option(
'--verbose', '-v',
is_flag=True,
help='verbose mode',
)
@click.version_option(
version=__version__,
message='%(prog)s, based on [revolut] package version %(version)s'
)
def main(token, simulate, historyfile, verbose, forceexchange):
if token is None:
print("You don't seem to have a Revolut token")
print("Please execute revolut_cli.py first to get one")
sys.exit(_RETURN_CODE_ERROR)
global _VERBOSE_MODE
_VERBOSE_MODE = verbose
rev = Revolut(device_id=_CLI_DEVICE_ID, token=token)
to_buy_or_not_to_buy(revolut=rev,
simulate=simulate,
filename=historyfile,
forceexchange=forceexchange)
def log(log_str=""):
if _VERBOSE_MODE:
print(log_str)
def to_buy_or_not_to_buy(revolut, simulate, filename, forceexchange):
percent_margin = _BOT_PERCENT_MARGIN
last_transactions = revolut_bot.get_last_transactions_from_csv(
filename=filename)
last_tr = last_transactions[-1] # The last transaction
log()
log("Last transaction : {}\n".format(last_tr))
previous_currency = last_tr.from_amount.currency
current_balance = last_tr.to_amount # How much we currently have
current_balance_in_other_currency = revolut.quote(
from_amount=current_balance,
to_currency=previous_currency)
log("Today : {} in {} : {}\n".format(
current_balance, previous_currency, current_balance_in_other_currency))
last_sell = last_tr.from_amount # How much did it cost before selling
last_sell_plus_margin = revolut_bot.get_amount_with_margin(
amount=last_sell,
percent_margin=percent_margin)
log("Min value to buy : {} + {}% (margin) = {}\n".format(
last_sell,
percent_margin,
last_sell_plus_margin))
buy_condition = current_balance_in_other_currency.real_amount > \
last_sell_plus_margin.real_amount
if buy_condition or forceexchange:
if buy_condition:
log("{} > {}".format(
current_balance_in_other_currency,
last_sell_plus_margin))
elif forceexchange:
log("/!\\ Force exchange option enabled")
log("=> BUY")
if simulate:
log("(Simulation mode : do not really buy)")
else:
exchange_transaction = revolut.exchange(
from_amount=current_balance,
to_currency=previous_currency,
simulate=simulate)
log("{} bought".format(exchange_transaction.to_amount.real_amount))
log("Update history file : {}".format(filename))
revolut_bot.update_historyfile(
filename=filename,
exchange_transaction=exchange_transaction)
sys.exit(_RETURN_CODE_BUY)
else:
log("{} < {}".format(
current_balance_in_other_currency,
last_sell_plus_margin))
log("=> DO NOT BUY")
sys.exit(_RETURN_CODE_DO_NOT_BUY)
if __name__ == "__main__":
main()