forked from tonyarroyave/BitsoTicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Decider.py
184 lines (154 loc) · 7.32 KB
/
Decider.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import sys
import time
import decimal
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import pandas as pd
pd.options.mode.chained_assignment = None
import numpy as np
import bitso
from bitso.errors import ApiError
from btc_apikeys import *
import bitso_functions as fun
import ticker_bitso as tick
api = bitso.Api(API_KEY, API_SECRET)
transcurrido = 30 #Tienen que transcurrir 30 seg para hacer una accion
count = 0
while(True):
if (transcurrido >= 29):
transcurrido = 0
scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)
Historical = client.open("Historical Bitso")
BTC = Historical.get_worksheet(0)
#ETH = Historical.get_worksheet(1)
#XRP = Historical.get_worksheet(2)
#BCH = Historical.get_worksheet(3)
def get_df(WS):
col_names=True
row_names=True
raw_data = WS.get_all_values()
start_row_int, start_col_int = WS.get_int_addr('A1')
rows, cols = np.shape(raw_data)
raw_data = [row[start_col_int-1:] for row in raw_data[start_row_int-1:]]
if row_names and col_names:
row_names = [row[0] for row in raw_data[1:]]
col_names = raw_data[0][1:]
raw_data = [row[1:] for row in raw_data[1:]]
elif row_names:
row_names = [row[0] for row in raw_data]
col_names = np.arange(len(raw_data[0]) - 1)
raw_data = [row[1:] for row in raw_data]
elif col_names:
row_names = np.arange(len(raw_data) - 1)
col_names = raw_data[0]
raw_data = raw_data[1:]
else:
row_names = np.arange(len(raw_data))
col_names = np.arange(len(raw_data[0]))
df = pd.DataFrame([pd.Series(row) for row in raw_data], index=row_names)
df.columns = col_names
df = df.convert_objects(convert_numeric=True)
return df
#Definidos por pruebas anteriores
pm1 = 70
pm2 = 71
print ('Veamos como va todo...')
def get_strategy(pm1, pm2, Bitso):
Bitso['PM1'] = Bitso['LAST'].rolling(pm1).mean()
Bitso['PM2'] = Bitso['LAST'].rolling(pm2).mean()
Bitso.dropna(inplace = True)
Bitso = Bitso[['LAST','PM1','PM2']]
Bitso['Posicion'] = np.where(Bitso['PM1'] > Bitso['PM2'], 1, -1)
Bitso['Retornos'] = np.log(Bitso['LAST']/Bitso['LAST'].shift(1))
Bitso.dropna(inplace = True)
Bitso['Estrategia'] = Bitso['Retornos']* Bitso['Posicion'].shift(1)
Bitso.dropna(inplace = True)
Bitso['Retacum'] = Bitso['Retornos'].cumsum().apply(np.exp)
Bitso['Estracum'] = Bitso['Estrategia'].cumsum().apply(np.exp)
return Bitso
estrategia = get_strategy(pm1,pm2,get_df(BTC))
_ = estrategia['Posicion'].tail(1).copy()
decision = _[0]
last_hist = estrategia['LAST'].tail(1).copy()
last_price = tick.get_values_btc(api)
print ('------------------------------------------')
print(' Ultimo valor en historico: ' + str(last_hist[0]))
print(' Ultimo valor en el mercado: ' + str(last_price[5]))
print ('------------------------------------------')
ob = fun.btc_update(api)
mxn = fun.get_mxn_balance(api)
btc = fun.get_btc_balance(api)
if (decision == 1): #significa que hay que comprar
try:
print (' Tendencia a la ALZA')
print ('------------------------------------------')
if int(mxn) > 0:
price = fun.max_bid_price(ob)
price_r = price.quantize(decimal.Decimal('.01'), rounding=decimal.ROUND_UP)
monto = mxn/price_r
monto_r = monto.quantize(decimal.Decimal('.00000001'), rounding=decimal.ROUND_DOWN) #BTC
fun.place_order_btc(api, side='buy', amount=str(monto_r), price=str(price_r))
else:
print(' Balance actual:')
print(' MXN: $0.00')
print(' BTC: {}'.format(btc))
print ('------------------------------------------')
hay_orden = fun.view_orders(api)
if hay_orden == True:
count = count + 1
elif hay_orden == False and count > 0:
print(' Ultima orden efectuada con exito!')
count = 0
if count > 5:
count = 0
print('Pagale tantillo mas JTO...')
fun.cancel_all_orders(api)
price_r = price_r/decimal.Decimal('0.997')
fun.place_order_btc(api, side='buy', amount=str(monto_r), price=str(price_r))
print('Vamos a esperarnos entonces...')
print(' ')
except ApiError as Er:
print(Er)
pass
else:
if sys.exc_info()[0] != None:
print("Unexpected error:", sys.exc_info()[0])
pass
elif (decision == -1): #significa que hay que vender
try:
print (' Tendencia a la BAJA')
print ('------------------------------------------')
if btc > 0:
price = fun.min_ask_price(ob)
price_r = price.quantize(decimal.Decimal('.01'), rounding=decimal.ROUND_UP)
fun.place_order_btc(api, side='sell', amount=str(btc), price=str(price_r))
else:
print(' Balance actual:')
print(' MXN: {}'.format(mxn))
print(' BTC: 0.0000')
print ('------------------------------------------')
hay_orden = fun.view_orders(api)
if hay_orden == True:
count = count + 1
elif hay_orden == False and count > 0:
print(' Ultima orden efectuada con exito!')
count = 0
if count > 5:
count = 0
print('A mi se me hace que eso no se va a vender... vamos a bajarle tantillo')
fun.cancel_all_orders(api)
price_r = price_r*decimal.Decimal('0.997')
fun.place_order_btc(api, side='sell', amount=str(btc), price=str(price_r))
print('Vamos a esperarnos entonces...')
print(' ')
except ApiError as Er:
print(Er)
pass
else:
if sys.exc_info()[0] != None:
print("Unexpected error:", sys.exc_info()[0])
pass
time.sleep(10)
transcurrido = transcurrido + 10