-
Notifications
You must be signed in to change notification settings - Fork 1
/
algos.py
67 lines (50 loc) · 1.94 KB
/
algos.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
import bt
import pandas as pd
class SelectWhere(bt.Algo):
"""
Selects securities based on an indicator DataFrame.
Selects securities where the value is True on the current date (target.now).
Args:
* signal (DataFrame): DataFrame containing the signal (boolean DataFrame)
Sets:
* selected
"""
def __init__(self, signal):
self.signal = signal
def __call__(self, target):
# get signal on target.now
if target.now in self.signal.index:
sig = self.signal.ix[target.now]
# get indices where true as list
selected = list(sig.index[sig])
# save in temp - this will be used by the weighing algo
target.temp['selected'] = selected
# return True because we want to keep on moving down the stack
return True
class WeighTarget(bt.Algo):
"""
Sets target weights based on a target weight DataFrame.
Args:
* target_weights (DataFrame): DataFrame containing the target weights
Sets:
* weights
"""
def __init__(self, target_weights):
self.tw = target_weights
def __call__(self, target):
# get target weights on date target.now
if target.now in self.tw.index:
w = self.tw.ix[target.now]
## target.universe tem o dataset em contexto
# print('\n\r self.tw.ix[target.now] \n\r')
# print(self.tw.ix[target.now])
# print('\n\r target \n\r')
# print(target)
# w = pd.DataFrame(w)
# save in temp - this will be used by the weighing algo
# also dropping any na's just in case they pop up
target.temp['weights'] = w.dropna()
# if len(target.universe) > 500:
# print('kek')
# return True because we want to keep on moving down the stack
return True