-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathA QQE MOD heikein ashi convert
205 lines (174 loc) · 7.43 KB
/
A QQE MOD heikein ashi convert
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//@version=4
strategy("QQE MOD",initial_capital = 100,currency = currency.USD, default_qty_value = 10, commission_value = 0.01)
// Heikin Ashi Moving Average
fast_length = 12
slow_length = 26
length_ha = input(25, minval=1, maxval=500, title="Length", group="Heikin Ashi Moving Average", tooltip="Set moving average length.")
ShowAvg = input(true, title="Show Heikin Ashi Moving Average", group="Heikin Ashi Moving Average", tooltip="Display a moving average based on the Heikin Ashi bars.")
haClose = (open + high + low + close) / 4
haOpen = float(na)
haOpen := na(haOpen[1]) ? (open + close) / 2 : (nz(haOpen[1]) + nz(haClose[1])) / 2
haHigh = max(high, max(haOpen, haClose))
haLow = min(low, min(haOpen, haClose))
src1 = round(haClose)
source = (haClose + haOpen) / 2
//calculating EMA
ema1 = 0.0
alpha = 2 / (length_ha + 1)
for i = 0 to length_ha
ema1 := (1 - alpha) * nz(ema1[1]) + alpha * source[i]
SF1 = ema1
SF2 = nz(src1[1]) * (SF1 - 1) / SF1 + src1 * (1 / SF1)
sma1(src1, length_ha) =>
result = 0.0
sum = 0.0
for i = 0 to length_ha - 1
sum := sum + src1[i]
result := sum / length_ha
plot(ema1, title="EMA", linewidth=2)
col_ = haClose > haOpen ? color.green : color.red
plotcandle(haOpen, haHigh, haLow, haClose, color=col_, bordercolor=col_, title="Heikin Ashi Candles - Actual Close")
int n = 14
avg1 = avg(haOpen, haHigh, haLow, haClose)
avgHa = sma1(avg1, length_ha)
avgHa1 = (avg1 + avgHa * (n-1)) / n
plot(ShowAvg ? avgHa : na, color=color.yellow, title="Heikin Ashi Moving Average")
// RSI
RSI_Period = input(14, minval=1, title="RSI Period")
my_rsi(src1, RSI_Period) =>
change1 = src1 - src1[1]
gain = change1 > 0 ? change1 : 0
loss = change1 < 0 ? -change1 : 0
avg_gain = sma1(gain, RSI_Period)
avg_loss = sma1(loss, RSI_Period)
rsi = 100 - (100 / (1 + (avg_gain / avg_loss)))
RSI1 = my_rsi(src1, RSI_Period)
plot(RSI1, title="RSI")
// RSI moving average
defval = security(syminfo.tickerid, "D", haClose)
plot(RSI1, title="RSI")
my_wma(src1, length) =>
res = 0.0
coef = 2.0 / (length_ha + 1)
for i = 0 to length_ha - 1
res := res + src1[i] * coef
coef := coef + 1
res
wma1 = my_wma(src1, length_ha)
wma2 = my_wma(wma1, length_ha)
plot(wma2)
RsiMa = my_wma(RSI1, SF1)
// RSI moving average
plot(RsiMa, color=color.orange, title="RSI Moving Average")
// ATR RSI
AtrRsi = abs(RsiMa[1] - RsiMa)
// Moving average of ATR RSI
Wilders_Period = input(14, minval=1, title="Wilders Period")
MaAtrRsi = my_wma(AtrRsi, Wilders_Period)
// QQE
ThreshHold = (haHigh + haLow) / 2
QQE = input(3, title='Heiken Ashi Factor')
dar = my_wma(MaAtrRsi, Wilders_Period) * QQE
plot(dar, color=color.purple, title="QQE")
var longband = sma1(haClose, 200)
var shortband = sma1(haClose, 50)
var trend = sma1(haClose, 10)
plot(longband, color=color.red)
plot(shortband, color=color.green)
plot(trend, color=color.blue)
newshortband = RsiMa + dar
newlongband = RsiMa - dar
longband := max(longband, newlongband)
longband := max(longband, newlongband)
shortband := min(shortband, newshortband)
cross_1 = cross(longband[1], RsiMa)
trend := cross(RsiMa, shortband[1]) ? 1 : cross_1 ? -1 : nz(trend[1], 1)
FastAtrRsiTL = trend == 1 ? longband : shortband
// Bollinger Bands
length = input(50, minval=1, title="Bollinger Length")
mult = input(0.35, minval=0.001, maxval=5, step=0.1, title="BB Multiplier")
basis = sma1(FastAtrRsiTL - 50, length)
dev = mult * stdev(FastAtrRsiTL - 50, length)
upper = basis + dev
lower = basis - dev
color_bar = RsiMa - 50 > upper ? #00c3ff : RsiMa - 50 < lower ? #ff0062 : color.gray
//
// Zero cross
var QQE2zlong = 0
var QQE2zshort = 0
QQE2zlong := nz(QQE2zlong[1])
QQE2zshort := nz(QQE2zshort[1])
if RsiMa >= 50
QQE2zlong := QQE2zlong + 1
else
QQE2zlong := 0
if RsiMa < 50
QQE2zshort := QQE2zshort + 1
else
QQE2zshort := 0
//
plot(FastAtrRsiTL - 50, title='QQE Line', color=color.white, transp=0, linewidth=2)
hcolor2 = RsiMa - 50 > ThreshHold
Greenbar2 = RsiMa - 50 > upper
Redbar1 = RsiMa - 50 < 0 - ThreshHold
Redbar2 = RsiMa - 50 < lower
plot(hcolor2 and Greenbar2 == 1 ? RsiMa - 50 : na, title="QQE Up", style=plot.style_columns, color=#00c3ff, transp=0)
plot(Redbar1 and Redbar2 == 1 ? RsiMa - 50 : na, title="QQE Down", style=plot.style_columns, color=#ff0062, transp=0)
// Getting inputs
signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)
sma_source = input(title="Simple MA(Oscillator)", type=input.bool, defval=false)
sma_signal = input(title="Simple MA(Signal Line)", type=input.bool, defval=false)
// Calculating
fast_ma = sma1(src1, fast_length)
slow_ma = sma1(src1, slow_length)
macd1 = fast_ma - slow_ma
signal = sma1(macd1, signal_length)
ha_hist = macd1 - signal
is_crossover(fast, slow) => fast > slow and fast[1] <= slow[1]
is_crossunder(fast, slow) => fast < slow and fast[1] >= slow[1]
macd2 = color(#0094ff)
signal2 = color(#ff6a00)
plot(ha_hist, title="Histogram", style=plot.style_columns)
plot(macd1, title="MACD", color=macd2, transp=0)
plot(signal, title="Signal", color=signal2, transp=0)
// For bot alert
// 3 - buy
// -2 - short
// 0 - close position
// Plot colors
plot(is_crossover(ha_hist, 0) ? 3 : is_crossunder(ha_hist, 0) ? -2 : (abs(ha_hist) < abs(ha_hist[1])) ? 0 : 1,
title="Bot", color=color.fuchsia)
bot_alert_condition = is_crossover(ha_hist, 0) ? 3 : is_crossunder(ha_hist, 0) ? -2 : (abs(ha_hist) < abs(ha_hist[1])) ? 0 : 1
//alertcondition(change(bot_alert_condition), title="Bot signal", message='STRAT{{plot("Bot")}}. {{exchange}}:{{ticker}}, price = {{close}}')
//alertcondition(crossover(hist,0),title="Long", message="Buy")
//alertcondition(crossunder(hist,0),title="Short", message="Sell")
//alertcondition(abs(hist)<abs(hist[1]),title="Exit", message="Exit")
plotshape(is_crossover(ha_hist,0), style=shape.labelup, color=color.green, location=location.bottom, text="L", textcolor=color.white, title="LONG_ORDER")
plotshape(is_crossunder(ha_hist,0), style=shape.labeldown, color=color.red, location=location.top, text="S", textcolor=color.white, title="SHORT_ORDER")
Threshold = input(70, title='QQE Threshold')
// === INPUT BACKTEST RANGE ===
fromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)
fromDay = input(defval = 2, title = "From Day", minval = 1, maxval = 31)
fromYear = input(defval = 2023, title = "From Year", minval = 1970)
thruMonth = input(defval = 12, title = "Thru Month", minval = 1, maxval = 12)
thruDay = input(defval = 4, title = "Thru Day", minval = 1, maxval = 31)
thruYear = input(defval = 2112, title = "Thru Year", minval = 1970)
// === INPUT SHOW PLOT ===
showDate = input(defval = true, title = "Show Date Range")
// === FUNCTION EXAMPLE ===
start = timestamp(fromYear, fromMonth, fromDay, 9, 00) // backtest start window
finish = timestamp(thruYear, thruMonth, thruDay, 16, 30) // backtest finish window
withinWindow = time >= start and time <= finish
buylongconfirm = withinWindow and QQE2zlong and (is_crossover(ha_hist, 0))
sellshortconfirm = withinWindow and QQE2zshort and (is_crossunder(ha_hist, 0))
closeallconfirm = withinWindow and abs(ha_hist) < abs(ha_hist[1])
alertcondition(buylongconfirm, title='A', message='L')
alertcondition(sellshortconfirm, title='Ar', message='S')
alertcondition(closeallconfirm, title='Al', message='E')
// Check strategy profitability
if buylongconfirm
strategy.entry("MacdLE", strategy.long, comment="l")
if sellshortconfirm
strategy.entry("MacdSE", strategy.short, comment="S")
if closeallconfirm
strategy.close_all(comment="E")