Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters