From f6fab547369bc1b12ca8e0adafe20de01b0fe670 Mon Sep 17 00:00:00 2001 From: m5l14i11 Date: Mon, 15 Apr 2024 17:14:02 +0300 Subject: [PATCH] upd --- strategy/generator/bootstrap/_trend_follow.py | 5 ++- strategy/generator/confirm/base.py | 1 + strategy/generator/confirm/vi.py | 11 ++++++ ta_lib/strategies/confirm/src/vi.rs | 34 +++++++++++++++++++ .../trend_follow/src/config/confirm_config.rs | 4 +++ .../trend_follow/src/mapper/confirm_mapper.rs | 1 + 6 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 strategy/generator/confirm/vi.py create mode 100644 ta_lib/strategies/confirm/src/vi.rs diff --git a/strategy/generator/bootstrap/_trend_follow.py b/strategy/generator/bootstrap/_trend_follow.py index 6ceea7f5..49dccdc0 100644 --- a/strategy/generator/bootstrap/_trend_follow.py +++ b/strategy/generator/bootstrap/_trend_follow.py @@ -23,6 +23,7 @@ from strategy.generator.confirm.rsi_neutrality import RsiNeutralityConfirm from strategy.generator.confirm.rsi_signalline import RsiSignalLineConfirm from strategy.generator.confirm.stc import StcConfirm +from strategy.generator.confirm.vi import ViConfirm from strategy.generator.exit.ast import AstExit from strategy.generator.exit.cci import CciExit from strategy.generator.exit.highlow import HighLowExit @@ -43,6 +44,7 @@ from strategy.generator.signal.flip.supertrend_flip import SupertrendFlipSignal from strategy.generator.signal.ma.ma2_rsi import Ma2RsiSignal from strategy.generator.signal.ma.ma3_cross import Ma3CrossSignal +from strategy.generator.signal.ma.ma_cross import MaCrossSignal from strategy.generator.signal.ma.ma_testing_ground import MaTestingGroundSignal from strategy.generator.signal.ma.vwap_cross import VwapCrossSignal from strategy.generator.signal.neutrality.dso_neutrality_cross import ( @@ -181,6 +183,7 @@ def _generate_strategy(self): StcConfirm(), DsoConfirm(), CciConfirm(), + ViConfirm(), ] ) pulse = np.random.choice( @@ -374,7 +377,7 @@ def _generate_signal(self, signal: TrendSignalType): MaTestingGroundSignal(), # MaQuadrupleSignal(), # MaSurpassSignal(), - # MaCrossSignal(), + MaCrossSignal(), ] ) if signal == TrendSignalType.BREAKOUT: diff --git a/strategy/generator/confirm/base.py b/strategy/generator/confirm/base.py index c94e43d1..bb76f271 100644 --- a/strategy/generator/confirm/base.py +++ b/strategy/generator/confirm/base.py @@ -14,6 +14,7 @@ class ConfirmType(Enum): RsiSignalLine = "RsiSignalLine" RsiNeutrality = "RsiNeutrality" Stc = "Stc" + Vi = "Vi" def __str__(self): return self.value.upper() diff --git a/strategy/generator/confirm/vi.py b/strategy/generator/confirm/vi.py new file mode 100644 index 00000000..9923c496 --- /dev/null +++ b/strategy/generator/confirm/vi.py @@ -0,0 +1,11 @@ +from dataclasses import dataclass + +from core.models.parameter import Parameter, StaticParameter +from strategy.generator.confirm.base import Confirm, ConfirmType + + +@dataclass(frozen=True) +class ViConfirm(Confirm): + type: ConfirmType = ConfirmType.Vi + atr_period: Parameter = StaticParameter(1.0) + period: Parameter = StaticParameter(14.0) diff --git a/ta_lib/strategies/confirm/src/vi.rs b/ta_lib/strategies/confirm/src/vi.rs new file mode 100644 index 00000000..725b1ce6 --- /dev/null +++ b/ta_lib/strategies/confirm/src/vi.rs @@ -0,0 +1,34 @@ +use base::prelude::*; +use core::prelude::*; +use trend::vi; + +pub struct ViConfirm { + atr_period: usize, + period: usize, +} + +impl ViConfirm { + pub fn new(atr_period: f32, period: f32) -> Self { + Self { + atr_period: atr_period as usize, + period: period as usize, + } + } +} + +impl Confirm for ViConfirm { + fn lookback(&self) -> usize { + std::cmp::max(self.atr_period, self.period) + } + + fn validate(&self, data: &OHLCVSeries) -> (Series, Series) { + let (vip, vim) = vi( + &data.high, + &data.low, + &data.atr(self.atr_period, Smooth::SMMA), + self.period, + ); + + (vip.sgt(&vim), vip.slt(&vim)) + } +} diff --git a/ta_lib/strategies/trend_follow/src/config/confirm_config.rs b/ta_lib/strategies/trend_follow/src/config/confirm_config.rs index 691497cc..de392eb9 100644 --- a/ta_lib/strategies/trend_follow/src/config/confirm_config.rs +++ b/ta_lib/strategies/trend_follow/src/config/confirm_config.rs @@ -49,4 +49,8 @@ pub enum ConfirmConfig { k_period: f32, d_period: f32, }, + Vi { + atr_period: f32, + period: f32, + }, } diff --git a/ta_lib/strategies/trend_follow/src/mapper/confirm_mapper.rs b/ta_lib/strategies/trend_follow/src/mapper/confirm_mapper.rs index bbfcec0b..485b3adf 100644 --- a/ta_lib/strategies/trend_follow/src/mapper/confirm_mapper.rs +++ b/ta_lib/strategies/trend_follow/src/mapper/confirm_mapper.rs @@ -66,6 +66,7 @@ pub fn map_to_confirm(config: ConfirmConfig) -> Box { threshold, )), ConfirmConfig::Roc { period } => Box::new(RocConfirm::new(period)), + ConfirmConfig::Vi { atr_period, period } => Box::new(ViConfirm::new(atr_period, period)), ConfirmConfig::Stc { smooth_type, fast_period,