本文整理汇总了Python中ParadoxTrading.Utils.DataStruct.addDict方法的典型用法代码示例。如果您正苦于以下问题:Python DataStruct.addDict方法的具体用法?Python DataStruct.addDict怎么用?Python DataStruct.addDict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ParadoxTrading.Utils.DataStruct
的用法示例。
在下文中一共展示了DataStruct.addDict方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ATRConstStop
# 需要导入模块: from ParadoxTrading.Utils import DataStruct [as 别名]
# 或者: from ParadoxTrading.Utils.DataStruct import addDict [as 别名]
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')
示例2: Kurtosis
# 需要导入模块: from ParadoxTrading.Utils import DataStruct [as 别名]
# 或者: from ParadoxTrading.Utils.DataStruct import addDict [as 别名]
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
示例3: LogReturn
# 需要导入模块: from ParadoxTrading.Utils import DataStruct [as 别名]
# 或者: from ParadoxTrading.Utils.DataStruct import addDict [as 别名]
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)
示例4: EMA
# 需要导入模块: from ParadoxTrading.Utils import DataStruct [as 别名]
# 或者: from ParadoxTrading.Utils.DataStruct import addDict [as 别名]
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,
})
示例5: STD
# 需要导入模块: from ParadoxTrading.Utils import DataStruct [as 别名]
# 或者: from ParadoxTrading.Utils.DataStruct import addDict [as 别名]
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),
})
示例6: EFF
# 需要导入模块: from ParadoxTrading.Utils import DataStruct [as 别名]
# 或者: from ParadoxTrading.Utils.DataStruct import addDict [as 别名]
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,
})
示例7: SharpRate
# 需要导入模块: from ParadoxTrading.Utils import DataStruct [as 别名]
# 或者: from ParadoxTrading.Utils.DataStruct import addDict [as 别名]
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
示例8: BIAS
# 需要导入模块: from ParadoxTrading.Utils import DataStruct [as 别名]
# 或者: from ParadoxTrading.Utils.DataStruct import addDict [as 别名]
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: FastVolatility
# 需要导入模块: from ParadoxTrading.Utils import DataStruct [as 别名]
# 或者: from ParadoxTrading.Utils.DataStruct import addDict [as 别名]
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
示例10: RateConstStop
# 需要导入模块: from ParadoxTrading.Utils import DataStruct [as 别名]
# 或者: from ParadoxTrading.Utils.DataStruct import addDict [as 别名]
class RateConstStop(StopIndicatorAbstract):
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):
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 _isStop(self, _data_struct: DataStruct):
price = _data_struct[self.use_key][0]
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')
示例11: settlement
# 需要导入模块: from ParadoxTrading.Utils import DataStruct [as 别名]
# 或者: from ParadoxTrading.Utils.DataStruct import addDict [as 别名]
def settlement(self, _backtest_key) -> DataStruct:
settlement_list = self.fetchSettlementRecords(_backtest_key)
keys = [
'tradingday', 'type',
'fund', 'commission', 'margin'
]
ret = DataStruct(keys, 'tradingday')
for d in settlement_list:
ret.addDict(d)
return ret
示例12: AdaKalman
# 需要导入模块: from ParadoxTrading.Utils import DataStruct [as 别名]
# 或者: from ParadoxTrading.Utils.DataStruct import addDict [as 别名]
class AdaKalman(IndicatorAbstract):
def __init__(
self, _ada_period: int=30,
_init_x: float = 0.0, _init_P: float =1.0,
_init_R: float = 0.1 ** 2, _init_Q: float = 0.01 ** 2,
_use_key: str = 'closeprice',
_idx_key: str = 'time', _ret_key: str = 'kalman'
):
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.ada_period = _ada_period
self.value_std = FastSTD(_ada_period, _use_key=self.use_key)
self.x_std = FastSTD(_ada_period, _use_key='x')
self.R = _init_R
self.Q = _init_Q
self.x = _init_x
self.P = _init_P
def _addOne(self, _data_struct: DataStruct):
index = _data_struct.index()[0]
value = _data_struct[self.use_key][0]
self.value_std.addOne(_data_struct)
if len(self.value_std) > 1:
self.R = self.value_std.getLastData()['std'][0] ** 2
if len(self.x_std) > 1:
self.Q = self.x_std.getLastData()['std'][0] ** 2
# predict
# self.x += 0.0 # x assume not changed
self.P += self.Q
# update
k = self.P / (self.P + self.R)
x_diff_value = k * (value - self.x)
self.x += x_diff_value
self.P = (1 - k) * self.P
self.data.addDict({
self.idx_key: index,
self.ret_key: self.x
})
示例13: CCI
# 需要导入模块: from ParadoxTrading.Utils import DataStruct [as 别名]
# 或者: from ParadoxTrading.Utils.DataStruct import addDict [as 别名]
class CCI(IndicatorAbstract):
"""
rolling ma
"""
def __init__(
self, _period: int, _constant: float = 0.15,
_close_key: str = 'closeprice',
_high_key: str = 'highprice',
_low_key: str = 'lowprice',
_idx_key: str = 'time', _ret_key: str = 'cci',
):
super().__init__()
self.close_key = _close_key
self.high_key = _high_key
self.low_key = _low_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.constant = _constant
self.tp_buf = deque(maxlen=self.period)
self.dev_buf = deque(maxlen=self.period)
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)
),
})
示例14: SimMA
# 需要导入模块: from ParadoxTrading.Utils import DataStruct [as 别名]
# 或者: from ParadoxTrading.Utils.DataStruct import addDict [as 别名]
class SimMA(IndicatorAbstract):
EMPTY = 0
LONG = 1
SHORT = 2
def __init__(
self, _period: int, _use_key: str = 'closeprice',
_idx_key: str = 'time', _ret_key: str = 'simma'
):
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.value = 1.0
self.last_price = None
self.last_status = self.EMPTY
self.period = _period
self.buf = deque(maxlen=self.period)
def _addOne(self, _data_struct: DataStruct):
index = _data_struct.index()[0]
price = _data_struct[self.use_key][0]
self.buf.append(price)
ma_value = sum(self.buf) / len(self.buf)
if self.last_status == self.LONG:
self.value *= price / self.last_price
if self.last_status == self.SHORT:
self.value /= price / self.last_price
self.data.addDict({
self.idx_key: index,
self.ret_key: self.value,
})
if price > ma_value:
self.last_status = self.LONG
elif price < ma_value:
self.last_status = self.SHORT
else:
self.last_status = self.EMPTY
self.last_price = price
示例15: ReturnRate
# 需要导入模块: from ParadoxTrading.Utils import DataStruct [as 别名]
# 或者: from ParadoxTrading.Utils.DataStruct import addDict [as 别名]
class ReturnRate(IndicatorAbstract):
def __init__(
self,
_smooth_period: int = 1,
_skip_period: int = 1,
_use_abs: bool=False,
_use_percent: bool=False,
_use_key: str = 'closeprice',
_idx_key: str = 'time',
_ret_key: str = 'returnrate'
):
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.smooth_period = _smooth_period
self.buf = deque(maxlen=self.skip_period)
self.last_rate = None
self.use_abs = _use_abs
self.use_percent = _use_percent
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)