-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdaemon.py
More file actions
166 lines (138 loc) · 4.58 KB
/
daemon.py
File metadata and controls
166 lines (138 loc) · 4.58 KB
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
from pyroboadvisor import PyRoboAdvisor
import pandas as pd
import shutil
import time
p={
"fecha_inicio": "2018-01-01",
"money": 100000,
"numberStocksInPortfolio": 10,
"orderMarginBuy": 0.005, # margen de ordenes de compra y venta
"orderMarginSell": 0.005, # margen de ordenes de compra y venta
"ring_size": 252,
"rlog_size": [11,22,44],
"skip_days": 252,
"cabeza": 5,
"prediccion": 1,
"apalancamiento": 1.6,
"percentil": 90,
"seeds": 1000,
"multiploMantenimiento": 6,
"har":1,
"hretorno":1,
"hrandom":1,
"random_seed": 23,
"key": "",
"email": "",
"b": True
}
def run():
today = pd.Timestamp.now(tz='America/New_York').normalize()
stoday = today.strftime("%Y-%m-%d")
p["fecha_fin"]=stoday
from download_us_money_supply import UsMoneySupply
b=True
if b:
from download_us_money_supply import MakerUsMoneySupply
mum=MakerUsMoneySupply("2018-01-01")
usms=[]
for meses in [6,12]:
for percentil in [10,20,30,40,50,60,70,80,90]:
usms.append(mum.get(meses,percentil).date2usms)
p["signoMultiplexado"]=list(range(0,len(usms)))
pra = PyRoboAdvisor(p,program={
"email":"",
"key":"",
"hora":"", # Coloque una hora fija entre las 10:00 y las 12:00
"apalancamiento": 1.6, # Si tu cuenta es de tipo efectivo/cash, debes poner aquí 1
"tipo":"3",
"source":0,
})
try:
shutil.rmtree(pra.cache)
except:
print("No cache folder")
# pra.readTickersFromEODHD()
pra.readTickersFromWikipedia()
pra.completeTickersWithIB() # Completa los tickers de IB que no están en el SP500, para que pueda venderlos
pra.prepare() # Prepara los datos y la estrategia
if b:
pra.simulate(signoMultiplexado=usms)
else:
pra.simulate()
pra.automatizeOrders()
# --- Soporte de teclado no bloqueante ---
import sys, time
import pandas as pd
TARGET_HHMM = "05:00"
if sys.platform.startswith("win"):
import msvcrt
class KeyReader:
def __enter__(self): return self
def __exit__(self, *exc): pass
def read(self):
# Devuelve 'ENTER', 'ESC' o None
if msvcrt.kbhit():
ch = msvcrt.getwch()
if ch == '\r': return 'ENTER'
if ch == '\x1b': return 'ESC'
return None
else:
import tty, termios, select
class KeyReader:
def __enter__(self):
self.fd = sys.stdin.fileno()
self.old = termios.tcgetattr(self.fd)
tty.setcbreak(self.fd) # modo cbreak (no canónico)
return self
def __exit__(self, *exc):
termios.tcsetattr(self.fd, termios.TCSADRAIN, self.old)
def read(self):
# Devuelve 'ENTER', 'ESC' o None
r, _, _ = select.select([sys.stdin], [], [], 0)
if r:
ch = sys.stdin.read(1)
if ch in ('\n', '\r'): return 'ENTER'
if ch == '\x1b': return 'ESC'
return None
keys=KeyReader()
def wait(hoy=False):
ahora = pd.Timestamp.now(tz='America/New_York')
if not hoy:
while True:
print(ahora.strftime("%Y-%m-%d %H:%M:%S"),">","24:00", end="\r")
ahora = pd.Timestamp.now(tz='America/New_York')
# sleep 1 second
time.sleep(1)
if ahora.strftime("%H:%M") >= "23:58":
break
time.sleep(120) # wait 2 minutes
# Arranque aleatorio
import random
from datetime import datetime, timedelta
# Definir el rango de horas
start_time = datetime.strptime("05:00", "%H:%M")
end_time = datetime.strptime("08:00", "%H:%M")
# Calcular la diferencia en minutos
delta_minutes = int((end_time - start_time).total_seconds() // 60)
# Elegir minutos aleatorios dentro del rango
random_minutes = random.randint(0, delta_minutes)
# Calcular la hora aleatoria
random_time = start_time + timedelta(minutes=random_minutes)
# Mostrar en formato HH:MM
arranque=random_time.strftime("%H:%M")
while True:
print(ahora.strftime("%Y-%m-%d %H:%M:%S"),"<",arranque, end="\r")
ahora = pd.Timestamp.now(tz='America/New_York')
# sleep 1 second
time.sleep(1)
if ahora.strftime("%H:%M") >= arranque:
break
k = keys.read()
if k in ("ENTER",):
print(f"\nEspera interrumpida por teclado ({k}).")
break
if __name__ == "__main__":
wait(hoy=True)
while True:
run()
wait()