Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Apr 27, 2024
1 parent e2b934d commit 91531d7
Show file tree
Hide file tree
Showing 36 changed files with 175 additions and 50 deletions.
1 change: 1 addition & 0 deletions core/models/strategy_ref.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def next(
[raw_action, price] = exports["strategy_next"](
self.store_ref,
self.id,
ohlcv.timestamp,
ohlcv.open,
ohlcv.high,
ohlcv.low,
Expand Down
15 changes: 14 additions & 1 deletion ta_lib/benches/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ use std::collections::VecDeque;

fn model(c: &mut Criterion) {
let mut group = c.benchmark_group("model");
let ts: Vec<i64> = vec![
1679827200, 1679827500, 1679827800, 1679828100, 1679828400, 1679828700, 1679829000,
1679829300, 1679829600, 1679829900, 1679830200, 1679830500, 1679830800, 1679831100,
1679831400, 1679831700, 1679832000, 1679832300, 1679832600, 1679832900, 1679833200,
1679833500, 1679833800, 1679834100, 1679834400, 1679834700, 1679835000, 1679835300,
1679835600, 1679835900, 1679836200, 1679836500, 1679836800, 1679837100, 1679837400,
1679837700, 1679838000, 1679838300, 1679838600, 1679838900, 1679839200, 1679839500,
1679839800, 1679840100, 1679840400, 1679840700, 1679841000, 1679841300, 1679841600,
1679841900, 1679842200,
];

let open: Vec<f32> = vec![
6.8430, 6.8660, 6.8635, 6.8610, 6.865, 6.8595, 6.8565, 6.852, 6.859, 6.86, 6.8580, 6.8605,
6.8620, 6.867, 6.859, 6.8670, 6.8640, 6.8575, 6.8485, 6.8350, 7.1195, 7.136, 7.1405, 7.112,
Expand Down Expand Up @@ -56,7 +67,9 @@ fn model(c: &mut Criterion) {
.zip(low.iter())
.zip(close.iter())
.zip(volume.iter())
.map(|((((&o, &h), &l), &c), &v)| OHLCV {
.zip(ts.iter())
.map(|(((((&o, &h), &l), &c), &v), &t)| OHLCV {
ts: t,
open: o,
high: h,
low: l,
Expand Down
2 changes: 2 additions & 0 deletions ta_lib/strategies/base/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub fn unregister_strategy(strategy_id: i32) -> i32 {
#[no_mangle]
pub fn strategy_next(
strategy_id: i32,
ts: i64,
open: f32,
high: f32,
low: f32,
Expand All @@ -53,6 +54,7 @@ pub fn strategy_next(
let mut strategies = STRATEGY_ID_TO_INSTANCE.write().unwrap();
if let Some(strategy) = strategies.get_mut(&strategy_id) {
let ohlcv = OHLCV {
ts,
open,
high,
low,
Expand Down
24 changes: 17 additions & 7 deletions ta_lib/strategies/base/src/model.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use core::prelude::*;
use std::collections::VecDeque;
use std::collections::{HashSet, VecDeque};

#[derive(Debug, Copy, Clone)]
pub struct OHLCV {
pub ts: i64,
pub open: f32,
pub high: f32,
pub low: f32,
Expand All @@ -29,12 +30,21 @@ impl OHLCVSeries {
let mut close = Vec::with_capacity(len);
let mut volume = Vec::with_capacity(len);

for ohlcv in data.iter() {
open.push(ohlcv.open);
high.push(ohlcv.high);
low.push(ohlcv.low);
close.push(ohlcv.close);
volume.push(ohlcv.volume);
let mut visited = HashSet::new();

let mut sorted_data: Vec<_> = data.iter().collect();
sorted_data.sort_by_key(|v| v.ts);

for ohlcv in sorted_data.iter() {
if !visited.contains(&ohlcv.ts) {
open.push(ohlcv.open);
high.push(ohlcv.high);
low.push(ohlcv.low);
close.push(ohlcv.close);
volume.push(ohlcv.volume);

visited.insert(ohlcv.ts);
}
}

Self {
Expand Down
6 changes: 6 additions & 0 deletions ta_lib/strategies/base/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ use core::prelude::*;
use price::prelude::*;

pub trait Source {
fn close(&self) -> Series<f32>;
fn hl2(&self) -> Series<f32>;
fn hlc3(&self) -> Series<f32>;
fn hlcc4(&self) -> Series<f32>;
fn ohlc4(&self) -> Series<f32>;
}

impl Source for OHLCVSeries {
#[inline]
fn close(&self) -> Series<f32> {
self.close.clone()
}

#[inline]
fn hl2(&self) -> Series<f32> {
median_price(&self.high, &self.low)
Expand Down
26 changes: 12 additions & 14 deletions ta_lib/strategies/base/src/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ impl BaseStrategy {
}

fn store(&mut self, data: OHLCV) {
if self.data.len() >= self.lookback_period {
let buf_size = (self.lookback_period as f32 * 1.3) as usize;

if self.data.len() > buf_size {
self.data.pop_front();
}

Expand All @@ -77,6 +79,7 @@ impl BaseStrategy {
self.data.len() >= self.lookback_period
}

#[inline(always)]
fn ohlcv_series(&self) -> OHLCVSeries {
OHLCVSeries::from_data(&self.data)
}
Expand All @@ -93,10 +96,10 @@ impl Strategy for BaseStrategy {
let theo_price = self.suggested_entry();

match self.trade_signals() {
(true, _, false, false) => TradeAction::GoLong(theo_price),
(_, true, false, false) => TradeAction::GoShort(theo_price),
(false, false, true, _) => TradeAction::ExitLong(data.close),
(false, false, _, true) => TradeAction::ExitShort(data.close),
(true, false, false, false) => TradeAction::GoLong(theo_price),
(false, true, false, false) => TradeAction::GoShort(theo_price),
(false, false, true, false) => TradeAction::ExitLong(data.close),
(false, false, false, true) => TradeAction::ExitShort(data.close),
_ => TradeAction::DoNothing,
}
}
Expand Down Expand Up @@ -129,14 +132,8 @@ impl BaseStrategy {
let (filter_long_baseline, filter_short_baseline) = self.base_line.filter(&series);
let (exit_long_eval, exit_short_eval) = self.exit.evaluate(&series);

let prev_go_long_trigger = go_long_trigger.shift(1);
let prev_go_short_trigger = go_short_trigger.shift(1);

let go_long_trigger_signal = go_long_trigger | prev_go_long_trigger;
let go_short_trigger_signal = go_short_trigger | prev_go_short_trigger;

let go_long_signal = go_long_trigger_signal | go_long_baseline;
let go_short_signal = go_short_trigger_signal | go_short_baseline;
let go_long_signal = go_long_trigger | go_long_baseline;
let go_short_signal = go_short_trigger | go_short_baseline;

let go_long = (go_long_signal & filter_long_baseline & go_long_confirm & go_long_momentum)
.last()
Expand All @@ -153,7 +150,7 @@ impl BaseStrategy {
}

fn suggested_entry(&self) -> f32 {
self.ohlcv_series().hlc3().last().unwrap_or(std::f32::NAN)
self.ohlcv_series().ohlc4().last().unwrap_or(std::f32::NAN)
}

fn stop_loss_levels(&self) -> (f32, f32) {
Expand Down Expand Up @@ -307,6 +304,7 @@ mod tests {

let ohlcvs = vec![
OHLCV {
ts: 1710297600000,
open: 1.0,
high: 2.0,
low: 0.5,
Expand Down
2 changes: 1 addition & 1 deletion ta_lib/strategies/baseline/src/ma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl MaBaseLine {
period: period as usize,
signal: vec![
Box::new(MaSurpassSignal::new(ma, period)),
Box::new(MaQuadrupleSignal::new(ma, period)),
// Box::new(MaQuadrupleSignal::new(ma, period)),
],
}
}
Expand Down
16 changes: 15 additions & 1 deletion ta_lib/strategies/confirm/src/dso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,112 +42,126 @@ impl Confirm for DsoConfirm {
#[cfg(test)]
mod tests {
use super::*;
use core::prelude::*;
use std::collections::VecDeque;

#[test]
fn test_confirm_dso() {
let confirm = DsoConfirm::new(Smooth::EMA, 13.0, 8.0, 9.0);
let data = VecDeque::from([
OHLCV {
ts: 1679827200,
open: 4.8914,
high: 4.9045,
low: 4.8895,
close: 4.8995,
volume: 100.0,
},
OHLCV {
ts: 1679827500,
open: 4.8995,
high: 4.9073,
low: 4.8995,
close: 4.9061,
volume: 100.0,
},
OHLCV {
ts: 1679827800,
open: 4.9061,
high: 4.9070,
low: 4.9001,
close: 4.9001,
volume: 100.0,
},
OHLCV {
ts: 1679828100,
open: 4.9001,
high: 4.9053,
low: 4.8995,
close: 4.9053,
volume: 100.0,
},
OHLCV {
ts: 1679828400,
open: 4.9053,
high: 4.9093,
low: 4.9046,
close: 4.9087,
volume: 100.0,
},
OHLCV {
ts: 1679828700,
open: 4.9087,
high: 4.9154,
low: 4.9087,
close: 4.9131,
volume: 100.0,
},
OHLCV {
ts: 1679829000,
open: 4.9131,
high: 4.9131,
low: 4.9040,
close: 4.9041,
volume: 100.0,
},
OHLCV {
ts: 1679829300,
open: 4.9041,
high: 4.9068,
low: 4.8988,
close: 4.9023,
volume: 100.0,
},
OHLCV {
ts: 1679829600,
open: 4.9023,
high: 4.9051,
low: 4.8949,
close: 4.9010,
volume: 100.0,
},
OHLCV {
ts: 1679829900,
open: 4.9010,
high: 4.9052,
low: 4.8969,
close: 4.8969,
volume: 100.0,
},
OHLCV {
ts: 1679830200,
open: 4.8969,
high: 4.8969,
low: 4.8819,
close: 4.8895,
volume: 100.0,
},
OHLCV {
ts: 1679830500,
open: 4.8895,
high: 4.8928,
low: 4.8851,
close: 4.8901,
volume: 100.0,
},
OHLCV {
ts: 1679830800,
open: 4.8901,
high: 4.8910,
low: 4.8813,
close: 4.8855,
volume: 100.0,
},
OHLCV {
ts: 1679831100,
open: 4.8855,
high: 4.8864,
low: 4.8816,
close: 4.8824,
volume: 100.0,
},
OHLCV {
ts: 1679831400,
open: 4.8824,
high: 4.8934,
low: 4.8814,
Expand Down
Loading

0 comments on commit 91531d7

Please sign in to comment.