-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransferwise.py
123 lines (106 loc) · 4.23 KB
/
transferwise.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
from . import tw_api
#import tw_api
import datetime
import requests
import json
import pytz
import pandas as pd
import numpy as np
import math
from pandas_datareader import data as pdr
import yfinance as yf
import scipy.optimize as sco
import sys
import os
import os.path
import time
import yaml
import xlrd
import urllib.request
class transferwise :
def __init__(self, config='') :
# reading config parameters
if config == '' :
file = open(os.path.join(os.path.split(os.path.abspath(__file__))[0], 'config.yaml'), 'r')
else :
file = open(config, 'r')
data = file.read()
data = yaml.load(data, Loader=yaml.FullLoader)
for param in data :
setattr(self, param, data[param])
self.data_path = os.path.join(os.path.split(os.path.abspath(__file__))[0], self.data_path)
self.record_file = os.path.join(self.data_path, 'records.txt')
self.tw = tw_api.tw_api(api_key=self.api_key)
def clear_quotes(self) :
# clear historical quotes
if os.path.isfile(self.record_file) :
file = open(self.record_file, 'r')
records = file.readlines()
file.close()
else :
records = []
if len(records) > 0 :
for record in records :
record = record.replace('\n' , '')
record_parts = record.split(',')
if datetime.datetime.strptime(record_parts[2], '%Y-%m-%dT%H:%M:%SZ') < datetime.datetime.utcnow() :
if len(record_parts) >= 7 :
transfer = self.tw.check_transfer(record_parts[6])
if transfer['status'] == 'incoming_payment_waiting' :
self.tw.cancel_transfer(record_parts[6])
file = open(self.record_file, 'w+')
file.write('')
file.close()
def create_quote(self, amount=0) :
# get historical quotes
if os.path.isfile(self.record_file) :
file = open(self.record_file, 'r')
records = file.readlines()
file.close()
else :
records = []
# compare quotes
quote = self.tw.get_quote(self.profile, self.from_currency, self.to_currency, amount)
now = datetime.datetime.now().strftime('%Y/%m/%d-%H:%M:%S')
amount = 0
for option in quote['paymentOptions'] :
if option['payIn'] == 'BANK_TRANSFER' and option['payOut'] == 'BANK_TRANSFER' :
amount = option['targetAmount']
write_record = True
if len(records) > 0 :
max_rate = 0.0
max_date = ''
for record in records :
record = record.replace('\n' , '')
record_parts = record.split(',')
if len(record_parts) > 6 and float(record_parts[3]) > float(max_rate) :
max_rate = record_parts[3]
max_date = record_parts[2]
if float(max_rate) > float(quote['rate']) :
write_record = False
elif float(max_rate) == float(quote['rate']) and max_date == quote['rateExpirationTime'] :
write_record = False
new_record = []
if write_record == True :
transfer = self.tw.create_transfer(quote['id'], self.target_account)
new_record = [now, quote['rateTimestamp'], quote['rateExpirationTime'], quote['rate'], amount, quote['id'], transfer['id']]
if len(new_record) > 0 :
if len(records) > 0 :
for record in records :
record = record.replace('\n' , '')
record_parts = record.split(',')
if len(record_parts) >= 7 :
self.tw.cancel_transfer(record_parts[6])
file = open(self.record_file, 'w+')
file.write('')
file.close()
file = open(self.record_file, 'a')
file.write(','.join(map(str, new_record)) + '\n')
file.close()
if write_record == True :
return new_record
return False
if __name__ == '__main__' :
transferwise = transferwise()
transferwise.create_quote(500000)
# transferwise.get_quotes()