Ejemplo n.º 1
0
 def forward(self, close_ts, volume_ts):
     output_tensor = tsf.rolling_std(
         torch.abs((close_ts / tsf.shift(close_ts, 1) - 1)) / volume_ts,
         20) / tsf.rolling_mean_(
             torch.abs(
                 (close_ts / tsf.shift(close_ts, 1) - 1)) / volume_ts, 20)
     return output_tensor
Ejemplo n.º 2
0
 def forward(self, close_ts, low_ts, high_ts):
     zeros = torch.zeros(low_ts.size())
     LD = tsf.shift(low_ts, 1) - low_ts
     HD = high_ts - tsf.shift(high_ts, 1)
     TR = torch.max(
         torch.max(high_ts - low_ts,
                   torch.abs(high_ts - tsf.shift(close_ts, 1))),
         torch.abs(low_ts - tsf.shift(close_ts, 1)))
     cond1 = (LD > 0) & (LD > HD)
     inner1 = torch.where(cond1, LD, zeros)
     cond2 = (HD > 0) & (HD > LD)
     inner2 = torch.where(cond2, HD, zeros)
     cond3 = (LD > 0) & (LD > HD)
     inner3 = torch.where(cond3, LD, zeros)
     cond4 = (HD > 0) & (HD > LD)
     inner4 = torch.where(cond4, HD, zeros)
     output_tensor = (tsf.rolling_mean_(
         torch.abs(
             tsf.rolling_sum_(inner1, 14) * 100 / tsf.rolling_sum_(TR, 14) -
             tsf.rolling_sum_(inner2, 14) * 100 / tsf.rolling_sum_(TR, 14))
         / tsf.rolling_sum_(inner3, 14) * 100 / tsf.rolling_sum_(TR, 14) +
         tsf.rolling_sum_(inner4, 14) * 100 / tsf.rolling_sum_(TR, 14) *
         100, 6) + tsf.shift(
             tsf.rolling_mean_(
                 torch.abs(
                     tsf.rolling_sum_(inner1, 14) * 100 / tsf.rolling_sum_(
                         TR, 14) - tsf.rolling_sum_(inner2, 14) * 100 /
                     tsf.rolling_sum_(TR, 14)) / tsf.rolling_sum_(
                         inner3, 14) * 100 / tsf.rolling_sum_(TR, 14) +
                 tsf.rolling_sum_(inner4, 14) * 100 /
                 tsf.rolling_sum_(TR, 14) * 100, 6), 6)) / 2
     return output_tensor
Ejemplo n.º 3
0
 def forward(self, high_ts, low_ts, close_ts):
     output_tensor = tsf.rolling_mean_(
         torch.max(
             torch.max((high_ts - low_ts),
                       torch.abs(tsf.shift(close_ts, 1) - high_ts)),
             torch.abs(tsf.shift(close_ts, 1) - low_ts)), 6)
     return output_tensor
Ejemplo n.º 4
0
 def forward(self, close_ts):
     inner = (((tsf.shift(close_ts, 20) - tsf.shift(close_ts, 10)) / 10) -
              ((tsf.shift(close_ts, 10) - close_ts) / 10)).squeeze()
     alpha = (-1 * tsf.diff(close_ts)).squeeze()
     ones = torch.ones(close_ts.size()).squeeze()
     cond = (inner < -0.05).squeeze()
     result = torch.where(cond, ones, alpha)
     return result
Ejemplo n.º 5
0
 def forward(self, high_ts, low_ts, close_ts):
     zeros = torch.zeros(high_ts.size())
     output_tensor = tsf.rolling_sum_(
         torch.max(zeros, high_ts - tsf.shift(close_ts, 1)),
         20) / tsf.rolling_sum_(
             torch.max(zeros,
                       tsf.shift(close_ts, 1) - low_ts), 20) * 100
     return output_tensor
Ejemplo n.º 6
0
 def forward(self, close_ts, volume_ts):
     delta_close = tsf.diff(close_ts, 1)
     inner = torch.sign(delta_close) + torch.sign(tsf.shift(
         delta_close, 1)) + torch.sign(tsf.shift(delta_close, 2))
     alpha = (
         (1.0 - tsf.rank(inner)) *
         tsf.rolling_sum_(volume_ts, 5)) / tsf.rolling_sum_(volume_ts, 20)
     return alpha
Ejemplo n.º 7
0
 def forward(self, open_ts, low_ts):
     cond = open_ts >= tsf.shift(open_ts, 1)
     zeros = torch.zeros(open_ts.size())
     inner = torch.where(
         cond, zeros,
         torch.max((open_ts - low_ts), (open_ts - tsf.shift(open_ts, 1))))
     output_tensor = tsf.rolling_sum_(inner, 20)
     return output_tensor
Ejemplo n.º 8
0
 def forward(self, close_ts, volume_ts):
     output_tensor = (-1 * ((tsf.rank(
         ((torch.sign((close_ts - tsf.shift(close_ts, 1))) + torch.sign(
             (tsf.shift(close_ts, 1) - tsf.shift(close_ts, 2)))) +
          torch.sign((tsf.shift(close_ts, 2) - tsf.shift(close_ts, 3)))))) *
                            tsf.rolling_sum_(volume_ts, 5)) /
                      tsf.rolling_sum_(volume_ts, 20))
     return output_tensor
Ejemplo n.º 9
0
 def forward(self, close_ts, volume_ts):
     cond1 = close_ts > tsf.shift(close_ts, 1)
     zeros = torch.zeros(close_ts.size())
     inner1 = torch.where(cond1, volume_ts, zeros)
     cond2 = close_ts <= tsf.shift(close_ts, 1)
     inner2 = tsf.rolling_sum_(torch.where(cond2, volume_ts, zeros), self.window)
     output_tensor = inner1 / inner2
     return output_tensor
Ejemplo n.º 10
0
def uos(high_ts, low_ts, close_ts, timeperiod1=7, timeperiod2=14, timeperiod3=28, timeperiod4=6):
    TH = torch.max(high_ts, tsf.shift(close_ts, window=1))
    TL = torch.min(low_ts, tsf.shift(close_ts, window=1))
    ACC1 = tsf.rolling_sum_(close_ts-TL, window=timeperiod1) / tsf.rolling_sum_(TH-TL, window=timeperiod1)
    ACC2 = tsf.rolling_sum_(close_ts-TL, window=timeperiod2) / tsf.rolling_sum_(TH-TL, window=timeperiod2)
    ACC3 = tsf.rolling_sum_(close_ts-TL, window=timeperiod3) / tsf.rolling_sum_(TH-TL, window=timeperiod3)
    UOS = (ACC1*timeperiod2*timeperiod3 + ACC2*timeperiod1*timeperiod3 + ACC3*timeperiod1*timeperiod2) * 100 / (timeperiod1*timeperiod2 + timeperiod1*timeperiod3 + timeperiod2*timeperiod3)
    MAUOS = tsf.ema(UOS, window=timeperiod4)
    return UOS, MAUOS
Ejemplo n.º 11
0
 def forward(self, close_ts):
     cond = ((tsf.diff((tsf.rolling_sum_(close_ts, 100) / 100), 100) /
              tsf.shift(close_ts, 100)) < 0.05) | (
                  (tsf.diff((tsf.rolling_sum_(close_ts, 100) / 100), 100) /
                   tsf.shift(close_ts, 100)) == 0.05)
     consequence1 = (-1 * (close_ts - tsf.rolling_min(close_ts, 100)))
     consequence2 = (-1 * tsf.diff(close_ts, 3))
     output_tensor = torch.where(cond, consequence1, consequence2)
     return output_tensor
Ejemplo n.º 12
0
 def forward(self, close_ts, low_ts, high_ts):
     cond1 = tsf.diff(close_ts, 1) > 0
     zeros = torch.zeros(close_ts.size())
     inner = torch.where(cond1, torch.min(low_ts, tsf.shift(close_ts, 1)),
                         torch.max(high_ts, tsf.shift(close_ts, 1)))
     cond2 = tsf.diff(close_ts, 1) == 0
     inner = torch.where(cond2, zeros, inner)
     output_tensor = tsf.rolling_sum_(inner, 20)
     return output_tensor
Ejemplo n.º 13
0
 def forward(self, close_ts, low_ts, high_ts):
     cond1 = close_ts == tsf.shift(close_ts, 1)
     cond2 = close_ts > tsf.shift(close_ts, 1)
     zeros = torch.zeros(close_ts.size())
     consequance1 = close_ts - torch.min(low_ts, tsf.shift(close_ts, 1))
     consequance2 = close_ts - torch.max(high_ts, tsf.shift(close_ts, 1))
     inner = torch.where(cond2, consequance1, consequance2)
     output_tensor = torch.where(cond1, zeros, inner)
     return output_tensor
Ejemplo n.º 14
0
def mfi(high_ts, low_ts, close_ts, total_turnover_ts, timeperiod=14):
    TP = (high_ts + low_ts + close_ts) / 3
    MF = TP * total_turnover_ts
    zero = torch.zeros_like(high_ts)
    PMF = torch.where(MF > tsf.shift(MF, window=1), MF, zero)
    NMF = torch.where(MF < tsf.shift(MF, window=1), MF, zero)
    MR = tsf.rolling_sum_(PMF, window=timeperiod) / tsf.rolling_sum_(NMF, window=timeperiod)
    MFI = 100 - (100 / (1 + MR))
    return MFI
Ejemplo n.º 15
0
 def forward(self, close_ts):
     cond1 = close_ts < tsf.shift(close_ts, 5)
     cond2 = close_ts == tsf.shift(close_ts, 5)
     inner = (close_ts - tsf.shift(close_ts, 5)) / tsf.shift(close_ts, 5)
     zeros = torch.zeros(close_ts.size())
     output_tensor = torch.where(
         cond1, inner, (close_ts - tsf.shift(close_ts, 5)) / close_ts)
     output_tensor = torch.where((~cond1 & cond2), zeros, output_tensor)
     return output_tensor
Ejemplo n.º 16
0
 def forward(self, close_ts):
     inner = ((tsf.shift(close_ts, 20) - tsf.shift(close_ts, 10)) /
              10).squeeze() - (
                  (tsf.shift(close_ts, 10) - close_ts) / 10).squeeze()
     cond_1 = inner < 0
     cond_2 = inner > 0.25
     alpha = (-1 * tsf.diff(close_ts).squeeze())
     ones = torch.ones(close_ts.size()).squeeze()
     result = torch.where((cond_1 | cond_2), -1 * ones, alpha)
     return result
Ejemplo n.º 17
0
def dmi(high_ts, low_ts, close_ts, timeperiod=14):
    up = high_ts - tsf.shift(high_ts, window=1)
    down = tsf.shift(low_ts, window=1) - low_ts
    zero = torch.zeros_like(high_ts, dtype=high_ts.dtype, device=high_ts.device)
    PDM = torch.where(up>torch.max(down, zero), up, zero)
    MDM = torch.where(down>torch.max(up, zero), down, zero)
    TR14 = tsf.rolling_mean_(torch.max(high_ts, tsf.shift(close_ts, window=1)) - torch.min(low_ts, tsf.shift(close_ts, window=1)), window=14)
    PDI = PDM / TR14[-1] * 100
    MDI = MDM / TR14[-1] * 100
    DX = torch.abs(PDI - MDI) / torch.abs(PDI + MDI)
    ADX = tsf.ema(DX, window=timeperiod)
    ADXR = tsf.ema(ADX, window=timeperiod)
    return PDM, MDM, PDI, MDI, DX, ADX, ADXR
Ejemplo n.º 18
0
 def forward(self, close_ts, volume_ts):
     output_tensor = (-1 * (
         (tsf.rank((tsf.rolling_sum_(tsf.shift(close_ts, 5), 20) / 20)) *
          tsf.rolling_corr(close_ts, volume_ts, 2)) * tsf.rank(
              tsf.rolling_corr(tsf.rolling_sum_(close_ts, 5),
                               tsf.rolling_sum_(close_ts, 20), 2))))
     return output_tensor
Ejemplo n.º 19
0
 def forward(self, high_ts, close_ts, volume_ts, vwap_ts):
     output_tensor = ((((tsf.rank(
         (1 / close_ts)) * volume_ts) / tsf.rolling_mean_(volume_ts, 20)) *
                       ((high_ts * tsf.rank((high_ts - close_ts))) /
                        (tsf.rolling_sum_(high_ts, 5) / 5))) - tsf.rank(
                            (vwap_ts - tsf.shift(vwap_ts, 5))))
     return output_tensor
Ejemplo n.º 20
0
 def forward(self, tensor):
     """defalt input is close"""
     returns = tsf.pct_change(tensor, period=1)
     sharp_ratio = tsf.rolling_mean_(returns,
                                     window=self._window) / tsf.rolling_std(
                                         returns, window=self._window)
     return tsf.shift(sharp_ratio, window=self._lag_window).squeeze(-1)
Ejemplo n.º 21
0
 def forward(self, close_ts, volume_ts):
     ts = tsf.rolling_corr(close_ts, volume_ts, 2)
     return -1 * (tsf.rank(tsf.rolling_mean_(tsf.shift(close_ts, 5), 20)) *
                  ts * tsf.rank(
                      tsf.rolling_corr(tsf.rolling_sum_(close_ts, 5),
                                       tsf.rolling_sum_(close_ts, 20), 2))
                  )  # .values
Ejemplo n.º 22
0
 def forward(self, low_ts, return_ts, volume_ts):
     output_tensor = ((
         ((-1 * tsf.rolling_min(low_ts, 5)) +
          tsf.shift(tsf.rolling_min(low_ts, 5), 5)) * tsf.rank(
              ((tsf.rolling_sum_(return_ts, 240) -
                tsf.rolling_sum_(return_ts, 20)) / 220))) *
                      tsf.rolling_min(volume_ts, 5))
     return output_tensor
Ejemplo n.º 23
0
 def forward(self, close_ts):
     cond = (tsf.diff(tsf.rolling_mean_(close_ts, 100), 100) /
             tsf.shift(close_ts, 100) <= 0.05).squeeze()
     alpha = -1 * tsf.diff(close_ts, 3).squeeze()
     result = torch.where(
         cond, -1 * (close_ts - tsf.rolling_min(close_ts, 100)).squeeze(),
         alpha)
     return result
Ejemplo n.º 24
0
 def forward(self, high_ts, low_ts, volume_ts, vwap_ts, close_ts):
     output_tensor = ((tsf.rank(
         tsf.shift(((high_ts - low_ts) /
                    (tsf.rolling_sum_(close_ts, 5) / 5)), 2)) *
                       tsf.rank(tsf.rank(volume_ts))) /
                      (((high_ts - low_ts) /
                        (tsf.rolling_sum_(close_ts, 5) / 5)) /
                       (vwap_ts - close_ts)))
     return output_tensor
Ejemplo n.º 25
0
 def test_shift_backward(self):
     output_diff = tsf.shift(self.test_2d_change, -1)
     output_diff = torch.where(torch.isnan(output_diff),
                               torch.full_like(output_diff, 666),
                               output_diff)  # fillna
     expected = torch.tensor([[2, 4], [3, 3], [4, 2], [5, 1],
                              [np.nan, np.nan]])
     expected = torch.where(torch.isnan(expected),
                            torch.full_like(expected, 666), expected)
     result = (expected == output_diff).all().item()
     self.assertTrue(result, 'test_shift_backward failed')
Ejemplo n.º 26
0
 def forward(self, close_ts, returns_ts):
     return tsf.rolling_min(
         tsf.rank(
             tsf.rank(
                 tsf.rolling_scale(
                     torch.log(
                         tsf.rolling_sum_(
                             tsf.rank(
                                 tsf.rank(-1 * tsf.rank(
                                     tsf.diff((close_ts - 1), 5)))), 2))))),
         5) + tsf.ts_rank(tsf.shift((-1 * returns_ts), 6), 5)
Ejemplo n.º 27
0
 def forward(self, high_ts, close_ts, low_ts):
     output_tensor = (close_ts - tsf.rolling_sum_(
         torch.min(low_ts, tsf.shift(close_ts, 1)), 6)) / tsf.rolling_sum_(
             torch.max(high_ts, tsf.shift(close_ts, 1)) -
             torch.min(low_ts, tsf.shift(close_ts, 1)), 6) * 12 * 24 + (
                 close_ts - tsf.rolling_sum_(
                     torch.min(low_ts, tsf.shift(close_ts, 1)), 12)
             ) / tsf.rolling_sum_(
                 torch.max(high_ts, tsf.shift(close_ts, 1)) -
                 torch.min(low_ts, tsf.shift(close_ts, 1)),
                 12) * 6 * 24 + (close_ts - tsf.rolling_sum_(
                     torch.min(low_ts, tsf.shift(close_ts, 1)),
                     24)) / tsf.rolling_sum_(
                         torch.max(high_ts, tsf.shift(close_ts, 1)) -
                         torch.min(low_ts, tsf.shift(close_ts, 1)),
                         24) * 6 * 24 * 100 / (6 * 12 + 6 * 24 + 12 * 24)
     return output_tensor
Ejemplo n.º 28
0
 def forward(self, open, close):
     shift_tensor = tsf.shift(close, window=1)
     diff = open - shift_tensor
     output = diff.div(shift_tensor)
     output_ts = tsf.pct_change(output, period=self._window)
     return output_ts
Ejemplo n.º 29
0
 def forward(self, close_ts):
     cond = tsf.diff(close_ts, 1) > 0
     zeros = torch.zeros(close_ts.size())
     output_tensor = tsf.rolling_sum_(torch.where(cond, close_ts - tsf.shift(close_ts, 1), zeros), self.window)
     return output_tensor
Ejemplo n.º 30
0
 def forward(self, close_ts):
     output_tensor = close_ts / tsf.shift(close_ts, self.window)
     return output_tensor