本文整理汇总了Python中ParadoxTrading.Utils.DataStruct类的典型用法代码示例。如果您正苦于以下问题:Python DataStruct类的具体用法?Python DataStruct怎么用?Python DataStruct使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DataStruct类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: LogReturn
class LogReturn(IndicatorAbstract):
def __init__(
self,
_skip_period: int = 1,
_use_key: str = 'closeprice',
_idx_key: str = 'time',
_ret_key: str = 'logreturn'
):
super().__init__()
self.use_key = _use_key
self.idx_key = _idx_key
self.ret_key = _ret_key
self.data = DataStruct(
[self.idx_key, self.ret_key],
self.idx_key
)
self.skip_period = _skip_period
self.buf = deque(maxlen=self.skip_period)
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)
示例2: EMA
class EMA(IndicatorAbstract):
def __init__(
self, _period: int, _use_key: str='closeprice',
_idx_key: str = 'time', _ret_key: str = 'ema'
):
super().__init__()
self.use_key = _use_key
self.idx_key = _idx_key
self.ret_key = _ret_key
self.data = DataStruct(
[self.idx_key, self.ret_key],
self.idx_key
)
self.period = _period
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,
})
示例3: _addOne
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
)
示例4: __init__
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]]
)
示例5: BBands
class BBands(IndicatorAbstract):
def __init__(
self, _period: int = 26, _use_key: str = 'closeprice',
_rate: float = 2.0, _idx_key: str = 'time',
_ret_key=('upband', 'midband', 'downband')
):
super().__init__()
self.use_key = _use_key
self.idx_key = _idx_key
self.keys = [self.idx_key] + list(_ret_key)
self.data = DataStruct(
self.keys, self.idx_key
)
self.period = _period
self.rate = _rate
self.buf = deque(maxlen=self.period)
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)
示例6: __init__
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()]]
)
示例7: _addOne
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),
})
示例8: BIAS
class BIAS(IndicatorAbstract):
"""
rolling ma
"""
def __init__(
self, _period: int, _use_key: str = 'closeprice',
_idx_key: str = 'time', _ret_key: str = 'bias'
):
super().__init__()
self.use_key = _use_key
self.idx_key = _idx_key
self.ret_key = _ret_key
self.data = DataStruct(
[self.idx_key, self.ret_key],
self.idx_key
)
self.period = _period
self.buf = deque(maxlen=self.period)
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,
})
示例9: SharpRate
class SharpRate(IndicatorAbstract):
def __init__(
self, _period: int, _use_key: str = 'closeprice',
_idx_key: str = 'time', _ret_key: str = 'sharprate',
):
super().__init__()
self.use_key = _use_key
self.idx_key = _idx_key
self.ret_key = _ret_key
self.data = DataStruct(
[self.idx_key, self.ret_key],
self.idx_key
)
self.last_price = None
self.period = _period
self.buf = deque(maxlen=self.period)
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
示例10: STD
class STD(IndicatorAbstract):
def __init__(
self, _period: int, _use_key: str = 'closeprice',
_idx_key: str = 'time', _ret_key: str = 'std'
):
super().__init__()
self.use_key = _use_key
self.idx_key = _idx_key
self.ret_key = _ret_key
self.data = DataStruct(
[self.idx_key, self.ret_key],
self.idx_key
)
self.period = _period
self.buf = deque(maxlen=self.period)
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),
})
示例11: ATRConstStop
class ATRConstStop(StopIndicatorAbstract):
def __init__(
self,
_price_data: DataStruct,
_atr_data: DataStruct,
_stop_type: int,
_rate: float = 3,
_price_use_key: str = 'closeprice',
_atr_use_key: str = 'atr',
_idx_key: str = 'time',
_ret_key: str = 'stopprice',
):
super().__init__()
assert len(_price_data) == 1
assert len(_atr_data) == 1
self.stop_type = _stop_type
self.rate = _rate
self.price_use_key = _price_use_key
self.atr_use_key = _atr_use_key
self.idx_key = _idx_key
self.ret_key = _ret_key
self.data = DataStruct(
[self.idx_key, self.ret_key],
self.idx_key,
)
price_value = _price_data[self.price_use_key][0]
atr_value = _atr_data[self.atr_use_key][0]
if self.stop_type == SignalType.LONG:
self.stopprice = price_value - self.rate * atr_value
elif self.stop_type == SignalType.SHORT:
self.stopprice = price_value + self.rate * atr_value
else:
raise Exception('unknown type')
self._addOne(_price_data)
def _addOne(
self, _price_data: DataStruct,
):
self.data.addDict({
self.idx_key: _price_data.index()[0],
self.ret_key: self.stopprice,
})
def _isStop(self, _data_struct: DataStruct):
price = _data_struct.toDict()[self.price_use_key]
stop_price = self.data[self.ret_key][-1]
if self.stop_type == SignalType.LONG:
if price < stop_price:
self.is_stop = True
elif self.stop_type == SignalType.SHORT:
if price > stop_price:
self.is_stop = True
else:
raise Exception('unknown type')
示例12: EFF
class EFF(IndicatorAbstract):
def __init__(
self, _period: int, _use_key: str = 'closeprice',
_idx_key: str = 'time', _ret_key: str = 'eff'
):
super().__init__()
self.use_key = _use_key
self.idx_key = _idx_key
self.ret_key = _ret_key
self.data = DataStruct(
[self.idx_key, self.ret_key],
self.idx_key
)
self.period = _period
self.buf = deque(maxlen=self.period)
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,
})
示例13: Kurtosis
class Kurtosis(IndicatorAbstract):
def __init__(
self, _period: int, _use_key: str = 'closeprice',
_idx_key: str = 'time', _ret_key: str = 'kurtosis'
):
super().__init__()
self.use_key = _use_key
self.idx_key = _idx_key
self.ret_key = _ret_key
self.data = DataStruct(
[self.idx_key, self.ret_key],
self.idx_key
)
self.period = _period
self.last_price = None
self.buf = deque(maxlen=self.period)
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
示例14: FastVolatility
class FastVolatility(IndicatorAbstract):
def __init__(
self, _period: int,
_factor: int = 1,
_smooth: int = 1,
_use_key: str = 'closeprice',
_idx_key: str = 'time',
_ret_key: str = 'volatility',
):
super().__init__()
self.use_key = _use_key
self.idx_key = _idx_key
self.ret_key = _ret_key
self.data = DataStruct(
[self.idx_key, self.ret_key],
self.idx_key
)
self.last_price = None
self.period = _period
self.factor = math.sqrt(_factor)
self.smooth = _smooth
self.buf = deque(maxlen=self.period)
self.mean = 0.0
self.sum_of_pow = 0.0
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
示例15: _addOne
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)