-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTradingConfidenceIndicator
151 lines (123 loc) · 6.06 KB
/
TradingConfidenceIndicator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//@version=5
indicator("Phoenix Confidence Indicator (Enhanced)", overlay=true)
// Inputs
phx_master_period = input.int(9, title="Phoenix Master Period") // For TCI and TSI calculations
willy_smooth_period = input.int(6, title="Willy Smoothing Period") // For Willy calculation
money_flow_period = input.int(3, title="Money Flow Period") // For MF calculation
lsma_length = input.int(32, title="LSMA Length") // For LSMA calculation
lsma_offset = input.int(0, title="LSMA Offset") // LSMA Offset (usually 0)
rsi_length = input.int(14, title="RSI Length for CSI")
// Stochastic RSI Inputs
enable_stochastic_rsi = input.bool(false, title="Enable Stochastic RSI")
stochastic_rsi_length = input.int(14, title="Stochastic RSI Length", minval=1)
stochastic_k_smoothing = input.int(3, title="Stochastic %K Smoothing", minval=1)
stochastic_d_smoothing = input.int(3, title="Stochastic %D Smoothing", minval=1)
// Functions
// Trend Confidence Index (TCI) Function
calculate_tci(src, period) =>
ema_fast = ta.ema(src, period)
ema_dev = ta.ema(math.abs(src - ema_fast), period)
tci = ta.ema((src - ema_fast) / (0.025 * ema_dev + 1e-10), period) + 50
tci
// Money Flow (MF) Function
calculate_money_flow(src, period) =>
volume_up = volume * (ta.change(src) > 0 ? src : 0)
volume_down = volume * (ta.change(src) < 0 ? src : 0)
sum_up = ta.sma(volume_up, period)
sum_down = ta.sma(volume_down, period)
mf = 100 * sum_up / (sum_up + sum_down + 1e-10)
mf
// Williams %R (Willy) Function
calculate_willy(src, period) =>
highest_high = ta.highest(src, period)
lowest_low = ta.lowest(src, period)
willy = 60 * (src - highest_high) / (highest_high - lowest_low + 1e-10) + 80
willy
// Composite Strength Index (CSI) Function
calculate_csi(src, rsi_length) =>
rsi_val = ta.rsi(src, rsi_length)
tsi_val = ta.tsi(src, rsi_length, rsi_length) * 50 + 50
csi = (rsi_val + tsi_val) / 2
csi
// Tradition Indicator
calculate_tradition_indicator(src) =>
tci_value = calculate_tci(src, phx_master_period)
mf_value = calculate_money_flow(src, money_flow_period)
rsi_value = ta.rsi(src, money_flow_period)
tradition_indicator = (tci_value + mf_value + rsi_value) / 3
tradition_indicator
// Stochastic RSI Calculation
calculate_stochastic_rsi(src, rsi_length, stoch_length, k_smoothing, d_smoothing) =>
rsi_val = ta.rsi(src, rsi_length)
k_line = ta.sma(ta.stoch(rsi_val, rsi_val, rsi_val, stoch_length), k_smoothing) * 100
d_line = ta.sma(k_line, d_smoothing)
[k_line, d_line]
// Calculations
// Tradition Indicator
tradition_indicator = calculate_tradition_indicator(hlc3)
// Smoothed Tradition Indicator
smoothed_tradition_indicator = ta.sma(tradition_indicator, willy_smooth_period)
// LSMA of the Tradition Indicator
lsma_tradition = ta.linreg(tradition_indicator, lsma_length, lsma_offset)
// Energy Line
energy_line = ta.ema((tradition_indicator - smoothed_tradition_indicator) * 2 + 50, money_flow_period)
// Stochastic RSI
[stochastic_k, stochastic_d] = calculate_stochastic_rsi(close, rsi_length, stochastic_rsi_length, stochastic_k_smoothing, stochastic_d_smoothing)
// Detect Peaks and Swings in Energy Line
energy_swing_threshold = 15
upside_peak = energy_line[1] > energy_line[2] and energy_line[1] > energy_line
downside_peak = energy_line[1] < energy_line[2] and energy_line[1] < energy_line
energy_swing_up = downside_peak and math.abs(energy_line - energy_line[1]) > energy_swing_threshold
energy_swing_down = upside_peak and math.abs(energy_line - energy_line[1]) > energy_swing_threshold
// Conditions
bullish_crossover = ta.crossover(tradition_indicator, smoothed_tradition_indicator)
bearish_crossover = ta.crossunder(tradition_indicator, smoothed_tradition_indicator)
tradition_oversold = tradition_indicator <= 20
tradition_overbought = tradition_indicator >= 80
stochastic_oversold = enable_stochastic_rsi and stochastic_k <= 20
stochastic_overbought = enable_stochastic_rsi and stochastic_k >= 80
// Confidence Score Calculation
var float confidence_score = 50.0
confidence_score := 50.0
if bullish_crossover
confidence_score += 25
if bearish_crossover
confidence_score -= 25
if tradition_oversold
confidence_score += 10
if tradition_overbought
confidence_score -= 10
if downside_peak
confidence_score += 5
if upside_peak
confidence_score -= 5
if energy_swing_up
confidence_score += 5
if energy_swing_down
confidence_score -= 5
if stochastic_oversold
confidence_score += 5
if stochastic_overbought
confidence_score -= 5
// Ensure Confidence Score is between 0 and 100
confidence_score := math.max(math.min(confidence_score, 100), 0)
// Define Color Based on Confidence Score
//confidence_color = confidence_score >= 70 ? color.green : confidence_score <= 30 ? color.red : color.yellow
// Plot Confidence Score
//plot(confidence_score, title="Confidence Score", color=confidence_color, linewidth=2)
//hline(70, title="Buy Threshold", color=color.gray, linestyle=hline.style_dashed)
//hline(30, title="Sell Threshold", color=color.gray, linestyle=hline.style_dashed)
// Buy and Sell Signals
buy_signal = confidence_score >= 85
sell_signal = confidence_score <= 15
// Plotting Signals on Chart
plotshape(buy_signal, title="Buy Signal", style=shape.arrowup, location=location.belowbar, color=color.green, size=size.small)
plotshape(sell_signal, title="Sell Signal", style=shape.arrowdown, location=location.abovebar, color=color.red, size=size.small)
// Plot Tradition Indicator and Energy Line
//plot(tradition_indicator, title="Tradition Indicator", color=color.green, linewidth=2)
//plot(smoothed_tradition_indicator, title="Smoothed Tradition", color=color.red, linewidth=2)
//plot(lsma_tradition, title="LSMA Tradition", color=color.blue, linewidth=2)
//plot(energy_line, title="Energy Line", color=color.white, style=plot.style_area, transp=65, histbase=50)
// Plot Stochastic RSI (Optional)
//plot(enable_stochastic_rsi ? stochastic_k : na, title="Stochastic %K", color=color.yellow)
//plot(enable_stochastic_rsi ? stochastic_d : na, title="Stochastic %D", color=color.magenta)