Example #1
0
 def forward(self, high_ts, low_ts, close_ts):
     output_tensor = ((high_ts + low_ts + close_ts) / 3 - tsf.rolling_mean_(
         (high_ts + low_ts + close_ts) / 3, 12)) / (
             0.015 * tsf.rolling_mean_(
                 torch.abs(close_ts - tsf.rolling_mean_(
                     (high_ts + low_ts + close_ts) / 3, 12)), 12))
     return output_tensor
 def forward(self, close_ts):
     cond1 = (tsf.rolling_mean_(close_ts, self.window) + tsf.rolling_std(close_ts, self.window)) < close_ts
     cond2 = close_ts < (tsf.rolling_mean_(close_ts, self.window) - tsf.rolling_std(close_ts, self.window))
     ones = torch.ones(close_ts.size())
     zeros = torch.zeros(close_ts.size())
     output_tensor = torch.where((cond1 & cond2), ones, zeros)
     return output_tensor
Example #3
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
Example #4
0
 def forward(self, close_ts, volume_ts):
     cond1 = (tsf.rolling_mean_(close_ts, 8) +
              tsf.rolling_std(close_ts, 8)) < tsf.rolling_mean_(
                  close_ts, 2)
     cond2 = tsf.rolling_mean_(close_ts, 2) < (
         tsf.rolling_mean_(close_ts, 8) - tsf.rolling_std(close_ts, 8))
     cond3 = (volume_ts / tsf.rolling_mean_(volume_ts, 20)) >= 1
     ones = torch.ones(close_ts.size())
     output_tensor = torch.where((cond1 | ~(cond1 & cond2 & cond3)),
                                 -1 * ones, ones)
     return output_tensor
Example #5
0
 def forward(self, close_ts, volume_ts):
     cond_1 = (
         tsf.rolling_mean_(close_ts, 8) + tsf.rolling_std(close_ts, 8) <
         tsf.rolling_mean_(close_ts, 2)).squeeze()
     cond_2 = (tsf.rolling_mean_(volume_ts, 20) / volume_ts < 1).squeeze()
     cond_3 = (
         tsf.rolling_mean_(close_ts, 8) - tsf.rolling_std(close_ts, 8) <
         tsf.rolling_mean_(close_ts, 2)).squeeze()
     ones = torch.ones(close_ts.size()).squeeze()
     result = torch.where((cond_1 | (cond_2 & cond_3)), -1 * ones, ones)
     return result
Example #6
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
Example #7
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
Example #8
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
Example #9
0
 def forward(self, volume_ts, close_ts):
     output_tensor = (
         ((-1 * tsf.rank(tsf.ts_rank(close_ts, 10))) *
          tsf.rank(tsf.diff(tsf.diff(close_ts, 1), 1))) * tsf.rank(
              tsf.ts_rank(
                  (volume_ts / tsf.rolling_mean_(volume_ts, 20)), 5)))
     return output_tensor
Example #10
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
Example #11
0
 def forward(self, vwap_ts, low_ts, volume_ts):
     output_tensor = (
         tsf.rank(tsf.rolling_corr(vwap_ts, volume_ts, 4)) * tsf.rank(
             tsf.rolling_corr(tsf.rank(low_ts),
                              tsf.rank(tsf.rolling_mean_(volume_ts, 50)),
                              12)))
     return output_tensor
Example #12
0
 def forward(self, close_ts, open_ts, vwap_ts, volume_ts):
     output_tensor = (
         tsf.rank(tsf.diff(
             ((close_ts * 0.6 + open_ts * 0.4)), 1)) * tsf.rank(
                 tsf.rolling_corr(vwap_ts, tsf.rolling_mean_(
                     volume_ts, 150), 15)))
     return output_tensor
Example #13
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)
Example #14
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
Example #15
0
 def forward(self, high_ts, low_ts, close_ts, volume_ts):
     output_tensor = (tsf.rank(
         tsf.rolling_corr(
             ((high_ts * 0.9) + (close_ts * 0.1)),
             tsf.rolling_mean_(volume_ts, 30), 10))**tsf.rank(
                 tsf.rolling_corr(tsf.ts_rank(((high_ts + low_ts) / 2), 4),
                                  tsf.ts_rank(volume_ts, 10), 7)))
     return output_tensor
Example #16
0
 def forward(self, volume_ts, vwap_ts):
     output_tensor = ((tsf.rank(
         (vwap_ts - tsf.rolling_min(vwap_ts, 12)))**tsf.ts_rank(
             tsf.rolling_corr(
                 tsf.ts_rank(vwap_ts, 20),
                 tsf.ts_rank(tsf.rolling_mean_(volume_ts, 60), 2), 18), 3))
                      * -1)
     return output_tensor
Example #17
0
 def forward(self, tensor_x, tensor_y):
     """
     tensor_x is returns: sequence_window x order_book_ids
     tensor_y is turnover ratio: sequency_window x order_book_ids
     """
     output = abs(tensor_x) / tensor_y
     #pdb.set_trace()
     return tsf.rolling_mean_(output, window=self._window)
Example #18
0
 def forward(self, close_ts, volume_ts):
     adv20 = tsf.rolling_mean_(volume_ts, 20)
     alpha = -1 * tsf.ts_rank(abs(tsf.diff(close_ts, 7)), 60) * torch.sign(
         tsf.diff(close_ts, 7))
     cond = adv20 >= volume_ts
     ones = torch.ones(close_ts.size())
     output_tensor = torch.where(
         cond, -1 * ones, alpha.float())  # confusing why must .float()
     return output_tensor
Example #19
0
 def forward(self, vwap_ts, volume_ts, low_ts):
     output_tensor = (tsf.rank(
         tsf.rolling_corr(
             tsf.rolling_sum_(((low_ts * 0.35) + (vwap_ts * 0.65)), 20),
             tsf.rolling_sum_(tsf.rolling_mean_(volume_ts, 40), 20), 7)) +
                      tsf.rank(
                          tsf.rolling_corr(tsf.rank(vwap_ts),
                                           tsf.rank(volume_ts), 6)))
     return output_tensor
Example #20
0
 def forward(self, tensor_x, tensor_y):
     if tensor_x.dim() < tensor_y.dim():
         tensor_x = tensor_x.expand_as(tensor_y)
     residual = calc_residual3d(tensor_x,
                                tensor_y,
                                window=self._window_train,
                                keep_first_nan=True)
     residual = residual.squeeze(-1).transpose(0, 1)
     varmom = tsf.rolling_mean_(
         residual, window=self._window) / tsf.rolling_std_dof_0(
             residual, window=self._window)
     return varmom
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
Example #22
0
 def forward(self, tensor):
     tensor_rolling_mean = tsf.rolling_mean_(
         tensor, window=self.timeperiod).squeeze(-1)
     volume_pct_change = tsf.pct_change(tensor_rolling_mean)
     return torch.atan(volume_pct_change)
Example #23
0
 def forward(self, tensor):
     tensor_rolling_mean = tsf.rolling_mean_(tensor, window=self.timeperiod)
     tensor_rolling_mean = tensor_rolling_mean.squeeze(-1)
     tensor = torch.clamp(tensor, min=1)
     relative = (tensor_rolling_mean - tensor).div(tensor)
     return torch.atan(relative)
Example #24
0
 def forward(self, tensor):
     tensor_rolling_mean = tsf.rolling_mean_(
         tensor, window=self.timeperiod).squeeze(-1)
     return tsf.pct_change(tensor_rolling_mean)
Example #25
0
 def forward(self, tensor):
     tensor_rolling_mean = tsf.rolling_mean_(
         tensor, window=self.timeperiod).squeeze(-1)
     #tensor_rolling_mean = tensor_rolling_mean.squeeze(-1)
     relative = (tensor_rolling_mean - tensor).div(tensor)
     return relative
 def forward(self, volume_ts):
     cond1 = (volume_ts / tsf.rolling_mean_(volume_ts, self.window)) >= 1
     ones = torch.ones(volume_ts.size())
     output_tensor = torch.where(cond1, ones, -1 * ones)
     return output_tensor
Example #27
0
 def forward(self, high_ts, low_ts, close_ts, volume_ts):
     adv20 = tsf.rolling_mean_(volume_ts, 20)
     df = tsf.rolling_cov(adv20, low_ts, 5)
     alpha = tsf.rolling_scale(((df + ((high_ts + low_ts) / 2)) - close_ts))
     return alpha
 def forward(self, close_ts):
     output_tensor = tsf.rolling_mean_(torch.abs(close_ts - tsf.rolling_mean_(close_ts, self.window)),
                                       self.window)
     return output_tensor
 def forward(self, close_ts):
     output_tensor = (close_ts - tsf.rolling_mean_(close_ts, self.window)) / tsf.rolling_mean(close_ts,
                                                                                                   self.window) * 100
     return output_tensor
Example #30
0
 def forward(self, close_ts, volume_ts):
     adv20 = tsf.rolling_mean_(volume_ts, 20)
     alpha = tsf.ts_rank(volume_ts / adv20, 20) * tsf.ts_rank(
         (-1 * tsf.diff(close_ts, 7)), 8)
     return alpha