Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Apr 18, 2024
1 parent d4b83e0 commit 0dc0433
Showing 1 changed file with 14 additions and 45 deletions.
59 changes: 14 additions & 45 deletions ta_lib/indicators/trend/src/supertrend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ pub fn supertrend(
let mut dn = hl2 + &atr_mul;

let len = hl2.len();
let prev_close = close.shift(1);

for _ in 0..len {
let mut prev_up = up.shift(1);
let mut prev_dn = dn.shift(1);

prev_up = iff!(prev_up.na(), up, prev_up);
prev_dn = iff!(prev_dn.na(), dn, prev_dn);
let mut prev_up = up.shift(1);
let mut prev_dn = dn.shift(1);

prev_up = iff!(prev_up.na(), up, prev_up);
prev_dn = iff!(prev_dn.na(), dn, prev_dn);

for _ in 0..len {
let prev_close = close.shift(1);
up = iff!(prev_close.sgt(&prev_up), up.max(&prev_up), up);
dn = iff!(prev_close.slt(&prev_dn), dn.min(&prev_dn), dn);
}
Expand All @@ -29,9 +29,6 @@ pub fn supertrend(
let trend_bull = Series::one(len);
let trend_bear = trend_bull.negate();

let prev_up = up.shift(1);
let prev_dn = dn.shift(1);

for _ in 0..len {
let prev_direction = direction.shift(1);

Expand All @@ -49,11 +46,8 @@ pub fn supertrend(
);
}

let supertrend = iff!(
direction.seq(&ONE),
up,
iff!(direction.seq(&MINUS_ONE), dn, Series::zero(len))
);
let mut supertrend = iff!(direction.seq(&MINUS_ONE), dn, Series::zero(len));
supertrend = iff!(direction.seq(&ONE), up, supertrend);

(direction, supertrend)
}
Expand All @@ -66,44 +60,19 @@ mod tests {

#[test]
fn test_supertrend() {
let high = Series::from([
7.1135, 7.1135, 7.116, 7.1225, 7.121, 7.136, 7.142, 7.1405, 7.1125, 7.1360, 7.1350,
7.1550, 7.1590, 7.1505, 7.1460, 7.1505, 7.1555, 7.1575, 7.1550, 7.1445, 7.1540, 7.1525,
7.1495, 7.1390, 7.1375, 7.1395, 7.1290, 7.1280, 7.1280, 7.1355, 7.1280, 7.1310, 7.1275,
7.1250, 7.1270, 7.1230, 7.1210, 7.1325, 7.1355, 7.142, 7.14, 7.1260, 7.1870, 7.1670,
7.1580,
]);
let low = Series::from([
7.0935, 7.088, 7.088, 7.1075, 7.1135, 7.1185, 7.119, 7.112, 7.1, 7.1055, 7.1160,
7.1285, 7.1480, 7.1375, 7.1370, 7.1405, 7.1440, 7.1460, 7.1375, 7.1355, 7.1440, 7.1420,
7.1365, 7.1280, 7.1305, 7.1250, 7.1145, 7.1035, 7.1135, 7.1175, 7.1220, 7.1225, 7.1180,
7.1180, 7.1125, 7.1055, 7.1115, 7.12, 7.126, 7.1295, 7.114, 7.1160, 7.1235, 7.1565,
7.1510,
]);
let close = Series::from([
7.1135, 7.088, 7.112, 7.1205, 7.1195, 7.136, 7.1405, 7.112, 7.1095, 7.1220, 7.1310,
7.1550, 7.1480, 7.1435, 7.1405, 7.1440, 7.1495, 7.1515, 7.1415, 7.1445, 7.1525, 7.1440,
7.1370, 7.1305, 7.1375, 7.1250, 7.1190, 7.1135, 7.1280, 7.1220, 7.1230, 7.1225, 7.1180,
7.1250, 7.1230, 7.1130, 7.1210, 7.13, 7.134, 7.132, 7.116, 7.1235, 7.1645, 7.1565,
7.1560,
]);
let high = Series::from([6.5600, 6.6049, 6.5942, 6.5541, 6.5300, 6.5700, 6.5630]);
let low = Series::from([6.5418, 6.5394, 6.5301, 6.4782, 6.4882, 6.5131, 6.5126]);
let close = Series::from([6.5541, 6.5942, 6.5348, 6.4950, 6.5298, 6.5616, 6.5223]);
let hl2 = median_price(&high, &low);
let atr_period = 2;
let atr = atr(&high, &low, &close, Smooth::SMMA, atr_period);

let factor = 3.0;
let expected_supertrend = vec![
7.0435004, 7.0435004, 7.0435004, 7.054437, 7.075719, 7.080235, 7.080235, 7.080235,
7.080235, 7.080235, 7.080235, 7.080235, 7.1008663, 7.1008663, 7.105092, 7.1122966,
7.115898, 7.1175737, 7.1175737, 7.1175737, 7.1175737, 7.1175737, 7.1175737, 7.1175737,
7.1175737, 7.1175737, 7.1175737, 7.158781, 7.158781, 7.158781, 7.158781, 7.157001,
7.152126, 7.1466875, 7.1466875, 7.1466875, 7.1466875, 7.1466875, 7.1466875, 7.1466875,
7.1466875, 7.1466875, 7.1074786, 7.1074786, 7.1074786,
6.4963, 6.4963, 6.446601, 6.403225, 6.3497434, 6.376522, 6.3796854,
];
let expected_direction = vec![
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0,
-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
];

let (direction, supertrend) = supertrend(&hl2, &close, &atr, factor);
Expand Down

0 comments on commit 0dc0433

Please sign in to comment.