def _addOne(self, _data_struct: DataStruct): index = _data_struct.index()[0] price = _data_struct[self.use_key][0] if self.last_price is not None: rate = math.log(price) - math.log(self.last_price) if self.new_mean is not None: self.new_info = rate - self.new_mean self.return_buf.append(rate) self.fit_count += 1 # retrain model if self.fit_count > self.fit_period and \ len(self.return_buf) >= self.fit_begin: self.model.fit(self.return_buf) self.phi = self.model.getPhis()[0] self.theta = self.model.getThetas()[0] self.alpha = self.model.getAlphas()[0] self.beta = self.model.getBetas()[0] self.const = self.model.getConst()[0] self.new_info = self.model.latent_arma_arr[-1] self.new_var = self.model.latent_garch_arr[-1] self.fit_count = 0 if self.new_info is not None: # predict value self.new_mean = self.phi * rate + self.theta * self.new_info self.new_var = self.alpha * self.new_info ** 2 + \ self.beta * self.new_var + self.const self.data.addDict({ self.idx_key: index, self.ret_key[0]: self.new_mean, self.ret_key[1]: math.sqrt(self.new_var), }) self.last_price = price
def _addOne(self, _data_struct: DataStruct): index_value = _data_struct.index()[0] self.buf.append(_data_struct.getColumn(self.use_key)[0]) if len(self.data) > self.period: const_std = statistics.pstdev(self.buf[-self.period:]) self.dynamic_n *= const_std / self.prev_std self.dynamic_n = max(self.min_n, self.dynamic_n) self.dynamic_n = min(self.max_n, self.dynamic_n) tmp_n = int(round(self.dynamic_n)) mean = statistics.mean(self.buf[-tmp_n:]) std = statistics.pstdev(self.buf[-tmp_n:]) self.data.addRow( [index_value, mean + self.rate * std, mean, mean - self.rate * std], self.keys ) self.prev_std = const_std else: if len(self.data) == self.period: self.prev_std = statistics.pstdev(self.buf) self.data.addRow( [index_value, None, None, None], self.keys )
def _addOne(self, _data_struct: DataStruct): index_value = _data_struct.index()[0] price_value = _data_struct[self.use_key][0] if self.last_price is not None: chg_rate = price_value / self.last_price - 1 if len(self.buf) >= self.period: last_value = self.buf.popleft() self.buf.append(chg_rate) self.sum_of_pow += chg_rate ** 2 self.sum_of_pow -= last_value ** 2 self.mean += (chg_rate - last_value) / self.period else: n = len(self.buf) self.buf.append(chg_rate) self.sum_of_pow += chg_rate ** 2 self.mean = (self.mean * n + chg_rate) / len(self.buf) var = self.sum_of_pow / len(self.buf) - self.mean ** 2 std_value = math.sqrt(max(0.0, var)) * self.factor if self.smooth > 1 and len(self.data): last_std_value = self.data[self.ret_key][-1] std_value = ( (self.smooth - 1) * last_std_value + std_value ) / self.smooth self.data.addDict({ self.idx_key: index_value, self.ret_key: std_value, }) self.last_price = price_value
def _addOne(self, _data_struct: DataStruct): self.high_buf.append(_data_struct[self.high_key][0]) self.low_buf.append(_data_struct[self.low_key][0]) closeprice = _data_struct[self.close_key][0] if self.last_close_price is not None: index_value = _data_struct.index()[0] # atr tr_value = max( _data_struct[self.high_key][0], self.last_close_price ) - min(_data_struct[self.low_key][0], self.last_close_price) self.atr_buf.append(tr_value) atr_value = sum(self.atr_buf) / len(self.atr_buf) # ema self.fast_ema_value = (closeprice - self.fast_ema_value) / \ self.fast_ema_period + self.fast_ema_value self.slow_ema_value = (closeprice - self.slow_ema_value) / \ self.slow_ema_period + self.slow_ema_value # plunge if self.fast_ema_value > self.slow_ema_value: plunge_value = (max(self.high_buf) - closeprice) / atr_value elif self.fast_ema_value < self.slow_ema_value: plunge_value = (closeprice - min(self.low_buf)) / atr_value else: plunge_value = 0.0 self.ret_buf.append(plunge_value) self.data.addDict({ self.idx_key: index_value, self.ret_key: sum(self.ret_buf) / len(self.ret_buf), }) else: self.fast_ema_value = closeprice self.slow_ema_value = closeprice self.last_close_price = closeprice
def __init__( self, _data: DataStruct, _stop_type: int, _stop_rate: float = 0.05, _use_key: str = 'closeprice', _idx_key: str = 'time', _ret_key: str = 'stopprice', ): super().__init__() assert len(_data) == 1 self.stop_type = _stop_type self.stop_rate = _stop_rate self.use_key = _use_key self.idx_key = _idx_key self.ret_key = _ret_key self.best_price = _data.toDict()[self.use_key] time = _data.index()[0] self.data = DataStruct([self.idx_key, self.ret_key], self.idx_key, [[time, self.get_stop_price()]])
def _addOne(self, _data_struct: DataStruct): index_value = _data_struct.index()[0] self.buf.append(_data_struct.getColumn(self.use_key)[0]) if len(self.data) > self.period: const_std = statistics.pstdev(self.buf[-self.period:]) self.dynamic_n *= const_std / self.prev_std self.dynamic_n = max(self.min_n, self.dynamic_n) self.dynamic_n = min(self.max_n, self.dynamic_n) tmp_n = int(round(self.dynamic_n)) mean = statistics.mean(self.buf[-tmp_n:]) std = statistics.pstdev(self.buf[-tmp_n:]) self.data.addRow([ index_value, mean + self.rate * std, mean, mean - self.rate * std ], self.keys) self.prev_std = const_std else: if len(self.data) == self.period: self.prev_std = statistics.pstdev(self.buf) self.data.addRow([index_value, None, None, None], self.keys)
def __init__( self, _data: DataStruct, _stop_type: int, _stop_rate: float = 0.05, _use_key: str = 'closeprice', _idx_key: str = 'time', _ret_key: str = 'stopprice', ): super().__init__() assert len(_data) == 1 self.stop_type = _stop_type self.stop_rate = _stop_rate self.use_key = _use_key self.idx_key = _idx_key self.ret_key = _ret_key price = _data[self.use_key][0] if self.stop_type == SignalType.LONG: stop_price = price * (1 - self.stop_rate) elif self.stop_type == SignalType.SHORT: stop_price = price * (1 + self.stop_rate) else: raise Exception('unknown type') time = _data.index()[0] self.data = DataStruct( [self.idx_key, self.ret_key], self.idx_key, [[time, stop_price]] )
def _addOne(self, _data_struct: DataStruct): index_value = _data_struct.index()[0] self.buf.append(_data_struct[self.use_key][0]) self.data.addDict({ self.idx_key: index_value, self.ret_key: statistics.mean(self.buf), })
def __init__( self, _data: DataStruct, _stop_type: int, _stop_rate: float = 0.05, _use_key: str = 'closeprice', _idx_key: str = 'time', _ret_key: str = 'stopprice', ): super().__init__() assert len(_data) == 1 self.stop_type = _stop_type self.stop_rate = _stop_rate self.use_key = _use_key self.idx_key = _idx_key self.ret_key = _ret_key self.best_price = _data.toDict()[self.use_key] time = _data.index()[0] self.data = DataStruct( [self.idx_key, self.ret_key], self.idx_key, [[time, self.get_stop_price()]] )
def _addOne(self, _data_struct: DataStruct): index_value = _data_struct.index()[0] price_value = _data_struct[self.use_key][0] if self.last_price is not None: chg_rate = price_value / self.last_price - 1 if len(self.buf) >= self.period: last_value = self.buf.popleft() self.buf.append(chg_rate) self.sum_of_pow += chg_rate**2 self.sum_of_pow -= last_value**2 self.mean += (chg_rate - last_value) / self.period else: n = len(self.buf) self.buf.append(chg_rate) self.sum_of_pow += chg_rate**2 self.mean = (self.mean * n + chg_rate) / len(self.buf) std_value = math.sqrt(self.sum_of_pow / len(self.buf) - self.mean**2) * self.factor if self.smooth > 1 and len(self.data): last_std_value = self.data[self.ret_key][-1] std_value = ((self.smooth - 1) * last_std_value + std_value) / self.smooth self.data.addDict({ self.idx_key: index_value, self.ret_key: std_value, }) self.last_price = price_value
def _addOne( self, _price_data: DataStruct, ): self.data.addDict({ self.idx_key: _price_data.index()[0], self.ret_key: self.stopprice, })
def _addOne(self, _data_struct: DataStruct): index_value = _data_struct.index()[0] self.buf.append(_data_struct.getColumn(self.use_key)[0]) self.data.addDict({ self.idx_key: index_value, self.ret_key: min(self.buf), })
def __init__( self, _data: DataStruct, _stop_type: int, _stop_rate: float = 0.05, _use_key: str = 'closeprice', _idx_key: str = 'time', _ret_key: str = 'stopprice', ): super().__init__() assert len(_data) == 1 self.stop_type = _stop_type self.stop_rate = _stop_rate self.use_key = _use_key self.idx_key = _idx_key self.ret_key = _ret_key price = _data[self.use_key][0] if self.stop_type == SignalType.LONG: stop_price = price * (1 - self.stop_rate) elif self.stop_type == SignalType.SHORT: stop_price = price * (1 + self.stop_rate) else: raise Exception('unknown type') time = _data.index()[0] self.data = DataStruct([self.idx_key, self.ret_key], self.idx_key, [[time, stop_price]])
def _addOne(self, _data_struct: DataStruct): index_value = _data_struct.index()[0] self.buf.append(_data_struct.getColumn(self.use_key)[0]) self.data.addDict({ self.idx_key: index_value, self.ret_key: statistics.pstdev(self.buf), })
def _addOne(self, _data_struct: DataStruct): index_value = _data_struct.index()[0] self.buf.append(_data_struct.getColumn(self.use_key)[0]) mean = statistics.mean(self.buf) std = statistics.pstdev(self.buf, mu=mean) self.data.addRow([ index_value, mean + self.rate * std, mean, mean - self.rate * std ], self.keys)
def _addOne(self, _data_struct: DataStruct): stop_price = self.data[self.ret_key][-1] time = _data_struct.index()[0] self.data.addDict({ self.idx_key: time, self.ret_key: stop_price, })
def _addOne( self, _price_data: DataStruct, _volatility_data: DataStruct = None, ): assert len(_volatility_data) == 1 price = _price_data.toDict()[self.price_use_key] volatility = _volatility_data.toDict()[self.volatility_use_key] price_time = _price_data.index()[0] volatility_time = _volatility_data.index()[0] assert price_time == volatility_time self.data.addDict({ self.idx_key: price_time, self.ret_key: self.get_stop_price(price, volatility), })
def _addOne( self, _price_data: DataStruct, _atr_data: DataStruct = None, ): assert len(_atr_data) == 1 price = _price_data.toDict()[self.price_use_key] atr = _atr_data.toDict()[self.atr_use_key] price_time = _price_data.index()[0] atr_time = _atr_data.index()[0] assert price_time == atr_time self.data.addDict({ self.idx_key: price_time, self.ret_key: self.get_stop_price(price, atr), })
def _addOne(self, _data_struct: DataStruct): index_value = _data_struct.index()[0] cur_value = _data_struct[self.use_key][0] self.buf.append(cur_value) self.data.addDict({ self.idx_key: index_value, self.ret_key: cur_value / self.buf[0], })
def _addOne(self, _data_struct: DataStruct): index_value = _data_struct.index()[0] tmp_value = _data_struct[self.use_key][0] if len(self) > 0: last_ret = self.getLastData().toDict()[self.ret_key] tmp_value = (tmp_value - last_ret) / self.period + last_ret self.data.addDict({ self.idx_key: index_value, self.ret_key: tmp_value, })
def _addOne(self, _data_struct: DataStruct): index_value = _data_struct.index()[0] cur_value = _data_struct[self.use_key][0] if self.last_value is not None: diff_value = cur_value - self.last_value self.data.addDict({ self.idx_key: index_value, self.ret_key: diff_value, }) self.last_value = cur_value
def _addOne(self, _data_struct: DataStruct): index = _data_struct.index()[0] price = _data_struct[self.use_key][0] if self.last_price: self.buf.append(math.log(price / self.last_price)) if len(self.buf) >= self.period: self.data.addDict({ self.idx_key: index, self.ret_key: kurtosis(self.buf), }) self.last_price = price
def _addOne(self, _data_struct: DataStruct): index_value = _data_struct.index()[0] price = _data_struct[self.use_key][0] self.buf.append(price) price_mean = statistics.mean(self.buf) self.data.addDict({ self.idx_key: index_value, self.ret_key: (price - price_mean) / price_mean * 100, })
def _addOne(self, _data_struct: DataStruct): index = _data_struct.index()[0] value = _data_struct[self.use_key][0] if len(self.buf) >= self.skip_period: last_value = self.buf.popleft() chg_rate = math.log(value / last_value) self.data.addDict({ self.idx_key: index, self.ret_key: chg_rate, }) self.buf.append(value)
def _addOne(self, _data_struct: DataStruct): index_value = _data_struct.index()[0] self.buf.append(_data_struct.getColumn(self.use_key)[0]) if len(self.buf) == self.period: buf_list = list(self.buf) tmp = 0.0 for a, b in zip(buf_list[:-1], buf_list[1:]): tmp += abs(b - a) self.data.addDict({ self.idx_key: index_value, self.ret_key: (self.buf[-1] - self.buf[0]) / tmp, })
def _addOne(self, _data_struct: DataStruct): index_value = _data_struct.index()[0] price_value = _data_struct[self.use_key][0] if self.last_price is not None: chg_rate = price_value / self.last_price - 1 self.buf.append(chg_rate) buf_std = statistics.pstdev(self.buf) if buf_std != 0: self.data.addDict({ self.idx_key: index_value, self.ret_key: statistics.mean(self.buf) / buf_std, }) self.last_price = price_value
def _addOne(self, _data_struct: DataStruct): index = _data_struct.index()[0] value = _data_struct[self.use_key][0] self.P += self.Q k = self.P / (self.P + self.R) self.x += k * (value - self.x) self.P = (1 - k) * self.P self.data.addDict({ self.idx_key: index, self.ret_key: self.x })
def _addOne(self, _data_struct: DataStruct): price = _data_struct.toDict()[self.use_key] if self.stop_type == SignalType.LONG: self.best_price = max(price, self.best_price) elif self.stop_type == SignalType.SHORT: self.best_price = min(price, self.best_price) else: raise Exception('unknown type') time = _data_struct.index()[0] self.data.addDict({ self.idx_key: time, self.ret_key: self.get_stop_price(), })
def _addOne(self, _data_struct: DataStruct): index_value = _data_struct.index()[0] self.high_buf.append(_data_struct[self.use_key[0]][-1]) self.low_buf.append(_data_struct[self.use_key[1]][-1]) if len(self.high_buf) >= self.N and len(self.low_buf) >= self.N: x = np.arange(self.N) high_beta = linregress(x, self.high_buf)[0] low_beta = linregress(x, self.low_buf)[0] self.data.addDict({ self.idx_key: index_value, self.ret_key: high_beta - low_beta, })
def _addOne(self, _data_struct: DataStruct): time = _data_struct.index()[0] price = _data_struct[self.use_key][0] self._set_best_price(price) profit_rate = self._profit_rate(price) # if self.stop_type == SignalType.SHORT: # input((profit_rate, self.status)) self._update_status(profit_rate) self.data.addDict({ self.idx_key: time, self.status_key: self.status, self.ret_key: self._calc_stop_price(), })
def _addOne(self, _data_struct: DataStruct): index = _data_struct.index()[0] value = _data_struct[self.use_key][0] if len(self.buf) >= self.period: last_value = self.buf.popleft() self.buf.append(value) self.mean += (value - last_value) / self.period else: n = len(self.buf) self.buf.append(value) self.mean = (self.mean * n + value) / len(self.buf) self.data.addDict({ self.idx_key: index, self.ret_key: self.mean, })
def _addOne(self, _data_struct: DataStruct): if self.last_close_price is not None: index_value = _data_struct.index()[0] tr_value = max( _data_struct[self.high_key][0], self.last_close_price ) - min( _data_struct[self.low_key][0], self.last_close_price ) if self.last_atr is None: self.last_atr = tr_value else: self.last_atr = (tr_value - self.last_atr) / self.period + self.last_atr self.data.addDict({ self.idx_key: index_value, self.ret_key: self.last_atr, }) self.last_close_price = _data_struct[self.close_key][0]
def _addOne(self, _data_struct: DataStruct): index_value = _data_struct.index()[0] close_price = _data_struct[self.close_key][0] high_price = _data_struct[self.high_key][0] low_price = _data_struct[self.low_key][0] if self.status == self.RISING: # last status if rising if low_price < self.sar: # if break sar, change status self.status = self.FALLING self.sar = self.ep # new sar if the ep of last status self.ep = low_price self.step = self.init_step else: # else continue rising self.sar += self.step * (self.ep - self.sar) if high_price > self.ep: self.ep = high_price self.step = min(self.max_step, self.step + self.init_step) elif self.status == self.FALLING: if high_price > self.sar: # if break sar, change status self.status = self.RISING self.sar = self.ep self.ep = high_price self.step = self.init_step else: # else continue falling self.sar -= self.step * (self.sar - self.ep) if low_price < self.ep: self.ep = low_price self.step = min(self.max_step, self.step + self.init_step) else: # just begin if close_price >= (high_price + low_price) / 2: # close price greater than mid of high and low self.status = self.RISING self.ep = high_price self.step = self.init_step self.sar = low_price else: self.status = self.FALLING self.ep = low_price self.step = self.init_step self.sar = high_price self.data.addDict({ self.idx_key: index_value, self.ret_key: self.sar, })
def __init__( self, _data: DataStruct, _stop_type: int, _init_stop: float = 0.05, _profit_thresh: typing.Sequence[float] = ( 0.1, 0.2, 0.3, 0.5 ), _stop_thresh: typing.Sequence[float] = ( 1.0, 0.5, 0.3, 0.15 ), _use_key: str = 'closeprice', _idx_key: str = 'time', _status_key: str = 'status', _ret_key: str = 'stopprice', ): super().__init__() assert len(_data) == 1 self.stop_type = _stop_type self.status = 0 self.profit_thresh = _profit_thresh self.stop_thresh = _stop_thresh self.use_key = _use_key self.idx_key = _idx_key self.status_key = _status_key self.ret_key = _ret_key self.init_price = _data.toDict()[self.use_key] if self.stop_type == SignalType.LONG: self.init_stop_price = self.init_price * (1 - _init_stop) elif self.stop_type == SignalType.SHORT: self.init_stop_price = self.init_price * (1 + _init_stop) else: raise Exception('unknown type') self.best_price = self.init_price self.data = DataStruct( [self.idx_key, self.status_key, self.ret_key], self.idx_key, [[_data.index()[0], self.status, self.init_stop_price]] )
def _addOne(self, _data_struct: DataStruct): index_value = _data_struct.index()[0] price_value = _data_struct[self.use_key][0] if self.last_price is not None: chg_rate = price_value / self.last_price - 1 self.buf.append(chg_rate) std_value = statistics.pstdev(self.buf) * self.factor if self.smooth > 1 and len(self.data): last_std_value = self.data[self.ret_key][-1] std_value = ( (self.smooth - 1) * last_std_value + std_value ) / self.smooth self.data.addDict({ self.idx_key: index_value, self.ret_key: std_value, }) self.last_price = price_value
def _addOne(self, _data_struct: DataStruct): price = _data_struct[self.use_key][0] if self.last_price is not None: price_diff = price - self.last_price if price_diff >= 0: self.gain_buf.append(price_diff) self.loss_buf.append(0) else: self.gain_buf.append(0) self.loss_buf.append(-price_diff) index_value = _data_struct.index()[0] gain_mean = statistics.mean(self.gain_buf) loss_mean = max(statistics.mean(self.loss_buf), 0.01) self.data.addDict({ self.idx_key: index_value, self.ret_key: 100 - 100 / (1 + gain_mean / loss_mean), }) self.last_price = price
def _addOne(self, _data_struct: DataStruct): index_value = _data_struct.index()[0] close_price = _data_struct[self.close_key][0] high_price = _data_struct[self.high_key][0] low_price = _data_struct[self.low_key][0] tp = (close_price + high_price + low_price) / 3 if len(self.tp_buf) == 0: dev = high_price - low_price else: dev = abs(tp - self.tp_buf[-1]) self.tp_buf.append(tp) self.dev_buf.append(dev) self.data.addDict({ self.idx_key: index_value, self.ret_key: (tp - statistics.mean(self.tp_buf)) / ( self.constant * statistics.mean(self.dev_buf) ), })
def _addOne(self, _data_struct: DataStruct): index = _data_struct.index()[0] value = _data_struct[self.use_key][0] if len(self.buf) >= self.skip_period: last_value = self.buf.popleft() chg_rate = value / last_value - 1 if self.use_abs: chg_rate = abs(chg_rate) if self.use_percent: chg_rate *= 100.0 if self.last_rate is None: self.last_rate = chg_rate else: self.last_rate = (chg_rate - self.last_rate) / \ self.smooth_period + self.last_rate self.data.addDict({ self.idx_key: index, self.ret_key: self.last_rate }) self.buf.append(value)
def _addOne(self, _data: DataStruct): index_value = _data.index()[0] closeprice = _data[self.close_key][0] highprice = _data[self.high_key][0] lowprice = _data[self.low_key][0] self.high_buf.append(highprice) self.low_buf.append(lowprice) high_mean = statistics.mean(self.high_buf) low_mean = statistics.mean(self.low_buf) k = 100 * (closeprice - high_mean) / (high_mean - low_mean) self.k_buf.append(k) d = statistics.mean(self.k_buf) j = self.j_period * k - (self.j_period - 1) * d self.data.addRow( [index_value, k, d, j], self.keys )
def _addOne(self, _data: DataStruct): index_value = _data.index()[0] price = _data[self.use_key][0] if self.fast_value is None and self.slow_value is None \ and self.macd_avg is None: self.fast_value = price self.slow_value = price macd_value = self.fast_value - self.slow_value self.macd_avg = macd_value else: self.fast_value = (price - self.fast_value) / self.fast_period + self.fast_value self.slow_value = (price - self.slow_value) / self.slow_period + self.slow_value macd_value = self.fast_value - self.slow_value self.macd_avg = (macd_value - self.macd_avg) / self.macd_period + self.macd_avg macd_diff = macd_value - self.macd_avg self.data.addRow( [index_value, macd_value, self.macd_avg, macd_diff], self.keys )
def matchMarket(self, _symbol: str, _data: DataStruct): assert len(_data) == 1 time = _data.index()[0] for index in sorted(self.order_dict.keys()): order = self.order_dict[index] if order.symbol == _symbol and time > order.datetime: exec_price: float = _data[self.price_idx][0] comm = self.commission_rate * order.quantity * exec_price self.addEvent(FillEvent( _index=order.index, _symbol=order.symbol, _tradingday=self.engine.getTradingDay(), _datetime=self.engine.getDatetime(), _quantity=order.quantity, _action=order.action, _direction=order.direction, _price=exec_price, _commission=comm )) del self.order_dict[index]