Skip to content

Commit

Permalink
Chandelier Exit indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
rdbuf committed Mar 30, 2019
1 parent e018520 commit 206e638
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -2,7 +2,7 @@ CC ?= gcc
AR ?= ar
RANLIB ?= ranlib

CCFLAGS ?= -Wall -Wextra -Wshadow -Wconversion -std=c89 -pedantic -Wno-declaration-after-statement -O2
CCFLAGS ?= -Wall -Wextra -Wshadow -Wconversion -std=c89 -pedantic -Wno-declaration-after-statement -O2 -g

SRCS=$(wildcard indicators/*.c)
SRCS+=$(wildcard utils/*.c)
Expand Down
1 change: 1 addition & 0 deletions indicators.tcl
Expand Up @@ -154,6 +154,7 @@ lappend indicators [list overlay "Weighted Close Price" wcprice 3 0 1 {high low

#Volatility
lappend indicators [list indicator "Average True Range" atr 3 1 1 {high low close} {period} {atr}]
lappend indicators [list indicator "Chandelier Exit" ce 3 2 2 {high low close} {period coef} {ce_high ce_low}]
lappend indicators [list indicator "Normalized Average True Range" natr 3 1 1 {high low close} {period} {natr}]
lappend indicators [list indicator "True Range" tr 3 0 1 {high low close} {} {tr}]
lappend indicators [list indicator "Annualized Historical Volatility" volatility 1 1 1 {real} {period} {volatility}]
Expand Down
114 changes: 114 additions & 0 deletions indicators/ce.c
@@ -0,0 +1,114 @@
/*
* Tulip Indicators
* https://tulipindicators.org/
* Copyright (c) 2010-2019 Tulip Charts LLC
* Lewis Van Winkle (LV@tulipcharts.org)
*
* This file is part of Tulip Indicators.
*
* Tulip Indicators is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Tulip Indicators is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Tulip Indicators. If not, see <http://www.gnu.org/licenses/>.
*
*/

#include "../indicators.h"
#include <stdio.h>


int ti_ce_start(TI_REAL const *options) {
return (int)options[0];
}

/* Name: Chandelier Exit
* Sources:
* - Alexander Elder. Come Into My Trading Room, 2002, pp. 180-181. CE, original description
* ISBN: 9780471225348
* - J. Welles Wilder. New Concepts in Technical Trading Systems, 1978, pp. 21-23. ATR, original description
* ISBN: 9780894590276
*/

int ti_ce(int size, TI_REAL const *const *inputs, TI_REAL const *options, TI_REAL *const *outputs) {
TI_REAL const *high = inputs[0];
TI_REAL const *low = inputs[1];
TI_REAL const *close = inputs[2];
const int period = options[0];
const TI_REAL coef = options[1];
TI_REAL *ce_high = outputs[0];
TI_REAL *ce_low = outputs[1];

if (size <= ti_ce_start(options)) return TI_OKAY;
if (period < 1) return TI_INVALID_OPTION;

int i;
TI_REAL atr = 0.;

#define MAX3(a, b, c) ((a) > (b) ? (a) : (b) > (c) ? (b) : (c))
#define TR(i) (MAX3(high[i] - low[i], fabs(high[i] - close[i-1]), fabs(low[i] - close[i-1])))

for (i = 1; i < period; ++i) {
atr += TR(i);
}
atr /= period;

TI_REAL HP = high[0];
int HP_idx = 0;
TI_REAL LP = low[0];
int LP_idx = 0;
int trail;

/* performance reasons */
const TI_REAL smth = (period - 1) / period;
const TI_REAL per = 1. / period;

for (i = period; i < size; ++i) {
atr = atr * smth + TR(i) * per;

TI_REAL val;
if (HP <= (val = high[i])) {
HP = val;
HP_idx = i;
} else if (HP_idx < (trail = i - period + 1)) {
HP = high[trail];
HP_idx = i;
int j;
for (j = trail+1; j <= i; ++j) {
if (HP <= (val = high[j])) {
HP = val;
HP_idx = j;
}
}
}
if (LP >= (val = low[i])) {
LP = val;
LP_idx = i;
} else if (LP_idx < (trail = i - period + 1)) {
LP = low[trail];
LP_idx = i;
int j;
for (j = trail+1; j <= i; ++j) {
if (LP >= (val = low[j])) {
LP = val;
LP_idx = j;
}
}
}

*ce_high++ = HP - coef * atr;
*ce_low++ = LP + coef * atr;
}

#undef TR
#undef MAX3

return TI_OKAY;
}
9 changes: 9 additions & 0 deletions tests/extra.txt
Expand Up @@ -57,6 +57,15 @@ bop
{81.59,81.06,82.87,83,83.61,83.15,82.84,83.99,84.55,84.36,85.53,86.54}
{-0.3023,-0.112,0.7674,0.1385,0.6538,-0.3291,0.1548,0.645,0.5072,0.1236,0.8021,0.916}

ce 22 3
# Data: AAPL NASDAQ, 3m
# Baseline: Excel implementation
{160.89,156.83,150.73,146.83,157.17,156.15,156.23,157.74,157.92,142.19,148.26,147.93,150.75,153.31,153.8,152.29,150.0,153.07,154.94,155.86,156.82,153.3,153.92,152.7,157.76,156.3,154.68,165.25,166.44,166.52,171.25,174.18,174.24,170.94,170.41,169.43,170.89,170.18,170.8,170.42,170.93,172.03,171.06,172.97,174.23,174.33,174.87,173.15,174.97,175.85,175.53,174.52,172.5,172.91,178.9,180.91,181.71,183.73,186.12,188.02,186.53}
{167.45,162.11,158.16,151.55,157.23,156.77,158.52,159.36,158.85,145.72,148.5499,148.83,151.82,154.53,153.97,153.7,151.27,153.39,155.88,157.66,157.88,156.73,155.14,154.48,158.13,156.33,158.13,166.15,169.0,168.98,171.655,175.08,175.57,173.94,170.66,171.21,171.0,172.48,171.2615,171.7,171.44,173.32,172.37,173.0,175.87,175.3,175.0,174.91,175.15,177.75,176.0,175.49,174.44,173.07,179.12,182.67,183.3,184.1,187.33,188.39,188.99}
{159.09,155.3,149.63,146.59,146.72,150.07,154.55,156.48,154.23,142.0,143.8,145.9,148.52,149.63,150.86,151.51,149.22,150.05,153.0,153.26,155.9806,152.62,151.7,151.74,154.32,153.66,154.11,160.23,164.56,165.93,167.28,172.3501,172.8531,170.34,168.42,169.25,169.7,169.92,169.38,169.75,169.49,170.99,170.3,171.38,173.95,173.1732,172.73,172.92,172.89,173.97,174.54,173.94,172.02,169.5,175.35,179.37,180.92,182.56,183.74,185.79,185.92}
{157.576,157.541,157.049,157.646,157.310,163.608,165.244,165.917,170.469,173.116,173.801,173.979,174.196,173.860,174.001,173.861,174.057,173.924,174.010,173.718,174.052,173.872,173.628,174.146,174.621,174.573,174.666,175.187,175.573,175.720,175.654,175.707,177.588,179.912,181.174,183.296,185.470,187.386,187.584}
{155.484,154.859,155.351,154.754,155.090,156.122,155.676,155.083,155.261,155.544,154.919,154.741,154.524,154.860,154.719,154.859,154.663,154.796,154.710,155.002,154.668,154.848,155.092,156.514,156.579,158.427,158.434,158.793,158.407,158.260,158.326,158.273,159.442,159.128,158.666,158.564,158.780,158.764,158.566}

ceil
{0,.5,1}
{0,1,1}
Expand Down

0 comments on commit 206e638

Please sign in to comment.