-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_expert_advisor_signals.pine
299 lines (249 loc) · 12.6 KB
/
my_expert_advisor_signals.pine
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//@version = 4
study(title='MY:Expert Advisor Signals', overlay=true)
// EA ID 5608726102199
///////////////////////////////////////////////
// User Input
///////////////////////////////////////////////
// Metatrader Expert Advisor API Request Data
EAuid = input('', title='EA UID', group = "Pine Connector Info", type=input.string)
EAtickerId = input('', title='EA Ticker ID', group = "Pine Connector Info", type=input.string)
EAvolume = input(0.0, title='EA Volume', group = "Pine Connector Info", type=input.float)
EAtakeProfit = input(0, title='EA Take Profit Pips', group = "Pine Connector Info", type=input.integer)
stopLoss = input(0, title='EA Stop Loss Pips (0 = auto)', group = "Pine Connector Info", type=input.integer)
// Enable or Disable a Signal
weekendSignals = input(false, title='Enable Week End Signals', group = "Signal E/D", type=input.bool)
enableChandelierSignal = input(false, title='Enable Chandelier Signal', group = "Signal E/D", type=input.bool)
maxMinPriceCancel = input(0, title='Block Chandelier if gap', group = "Signal E/D", type=input.integer)
enableTdiStochSignal = input(false, title='Enable TDI & STOCH Signal', group = "Signal E/D", type=input.bool)
forceDisableSignals = weekendSignals == false and (dayofweek == dayofweek.sunday or dayofweek == dayofweek.saturday)
// Enable / Disable visual plotting on the chart
showIndicators = input(true, title='Display Indicators on the Chart', group = "Other", type=input.bool)
playbackMode = input(true, title='Playback Mode', group = "Other", type=input.bool)
var table panel = table.new("bottom_right", 10, 20, border_width = 1)
//////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////
// STRATEGY 1 -- Chandelier Strategy
///////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////
// STRATEGY 1 -- Indicators Configuration
///////////////////////////////////////////////
//
// Bollinger Band Config
//
bollingerBandLength = 20
bollingerBandMultiplier = 2
bollingerBandBasis = sma(close, bollingerBandLength)
//
// Average True Range Config
//
averageTrueRangeLength = 22
averageTrueRangeMultiplier = 4.8 * atr(averageTrueRangeLength)
//
// Support of a bullish momentum. If the price is coming back down to this support, it's likely going to
// trigger a Chandelier Short Signal
//
longStop = highest(close, averageTrueRangeLength) - averageTrueRangeMultiplier
longStopPrev = nz(longStop[1], longStop)
longStop := close[1] > longStopPrev ? max(longStop, longStopPrev) : longStop
//
// Resistance of a bearish momentum. If the price is coming back up to this resistance, it's likely going to
// trigger a Chandelier Buy Signal
//
shortStop = lowest(close, averageTrueRangeLength) + averageTrueRangeMultiplier
shortStopPrev = nz(shortStop[1], shortStop)
shortStop := close[1] < shortStopPrev ? min(shortStop, shortStopPrev) : shortStop
//
// Definition of the direction, 1 = bullish / -1 = bearish
//
var int dir = 1
dir := close > shortStopPrev ? 1 : close < longStopPrev ? -1 : dir
///////////////////////////////////////////////
// STRATEGY 1 -- Alert Configuration
///////////////////////////////////////////////
//
// We define whether we are on a buy or sell setup
//
maxMinPriceCancelV = if (maxMinPriceCancel == 0)
5000
else
maxMinPriceCancel
triggerChandelierBuy = dir == 1 and dir[1] == -1 and close < shortStop + maxMinPriceCancelV
triggerChandelierSell = dir == -1 and dir[1] == 1 and close > longStop - maxMinPriceCancelV
// To avoid triggering multiple identical signals (avoid multi orders)
isChandellierBuyInProgress = false
isChandelierSellInProgress = false
// Will contain the type of order, buy or sell
orderType = ''
if (triggerChandelierBuy and isChandellierBuyInProgress == false)
isChandellierBuyInProgress := true
isChandelierSellInProgress := false
orderType := 'buy'
if (triggerChandelierSell and isChandelierSellInProgress == false)
isChandellierBuyInProgress := false
isChandelierSellInProgress := true
orderType := 'sell'
//
// Alert configuration and trigger for the Chandelier Signal
//
chandelierAlertMessage = ''
if (triggerChandelierSell or triggerChandelierBuy)
// Metatrader Expert Advisor Stop Loss update
// This stop loss is specific to the Chandelier Signal
EAStopLoss = if (stopLoss == 0)
gap = floor(close - bollingerBandBasis)
if (gap < 0)
gap * -10 // Stop Loss at the Bollinger Band Basis
else
gap * 10
else
stopLoss
chandelierAlertMessage := tostring(EAuid) + ','
chandelierAlertMessage := chandelierAlertMessage + tostring(orderType) + ','
chandelierAlertMessage := chandelierAlertMessage + tostring(EAtickerId) + ','
chandelierAlertMessage := chandelierAlertMessage + 'sl=' + tostring(EAStopLoss) + ','
chandelierAlertMessage := chandelierAlertMessage + 'tp=' + tostring(EAtakeProfit) + ','
chandelierAlertMessage := chandelierAlertMessage + 'risk=' + tostring(EAvolume)
if (enableChandelierSignal and forceDisableSignals == false)
alert(tostring(chandelierAlertMessage), alert.freq_once_per_bar_close)
/////////////////////////////////////////////////////////////
// STRATEGY 1 -- Indicators Plot on the Chart - VISUAL ONLY
/////////////////////////////////////////////////////////////
longPlot = plot(showIndicators and dir == 1 ? longStop : na, display=display.none, editable=false)
shortPlot = plot(showIndicators and dir == -1 ? shortStop : na, display=display.none, editable=false)
midPricePlot = plot(showIndicators ? ohlc4 : na, display=display.none, editable=false)
fill(midPricePlot, longPlot, color=color.new(color.green, 90))
fill(midPricePlot, shortPlot, color=color.new(color.red, 90))
plot(bollingerBandBasis, "Basis", color=color.new(#FF6D00, 90))
/////////////////////////////////////////////////////////////
// STRATEGY 1 -- Playback Mode
/////////////////////////////////////////////////////////////
if (playbackMode and (triggerChandelierSell or triggerChandelierBuy))
table.cell(panel, 0, 0, tostring(orderType), bgcolor = color.gray, text_size=size.auto)
table.cell(panel, 1, 0, 'Chandelier alert', bgcolor = color.gray, text_size=size.auto)
table.cell(panel, 2, 0, tostring(chandelierAlertMessage), bgcolor = color.gray, text_size=size.auto)
///////////////////////////////////////////////
// STRATEGY 1 -- Chandelier Strategy - END //
///////////////////////////////////////////////
// --------------------------------------------------------------------------------------------------------------- //
//////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////
// STRATEGY 2 -- TDI & STOCH Strategy
///////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////
// STRATEGY 2 -- TDI Indicator Configuration
///////////////////////////////////////////////
rsiPeriod = 14
rsiOversold = 30
rsiOverbought = 72
// The RSI is moving inside/outside a band defined by a upper band and a lower band
// We check its state on the previous candle to avoid a setup where
// the signal is triggered early in the new candle and the candle keeps pushing
// in the same direction
rsiValue = rsi(close[1], rsiPeriod)
rsiMovingAverage = sma(rsiValue, 31)
offs = (1.6185 * stdev(rsiValue, 31))
upperBand = rsiMovingAverage + offs
lowerBand = rsiMovingAverage - offs
mid = (upperBand + lowerBand) / 2
rsiFastMA = sma(rsiValue, 1)
rsiSlowMA = sma(rsiValue, 9)
// Based on the TDI indicator, a signal is triggered when the
// RSI is over sold and below the lower band, or over bought and above the upper band
tdiBuySignal = rsiFastMA <= lowerBand and rsiFastMA <= rsiOversold
tdiSellSignal = rsiFastMA >= upperBand and rsiFastMA >= rsiOverbought
////////////////////////////////////////////////////
// STRATEGY 2 -- STOCHASTIC Indicator Configuration
////////////////////////////////////////////////////
highValue = 82
lowValue = 18
smoothingK = 5
smoothingD = 3
getStoch(stochPeriod) =>
values = array.new_float()
k = sma(stoch(close, high, low, stochPeriod), smoothingD)
array.push(values, k)
array.push(values, sma(k, smoothingD))
values
//
// Stoch Period 1 - SMA 15
//
sotch1 = getStoch(15)
stoch1k = array.get(sotch1, 0)
stoch1d = array.get(sotch1, 1)
//
// Stoch Period 2 - SMA 32
//
sotch2 = getStoch(32)
stoch2k = array.get(sotch2, 0)
stoch2d = array.get(sotch2, 1)
//
// Stoch Period 3 - SMA 50
//
sotch3 = getStoch(50)
stoch3k = array.get(sotch3, 0)
stoch3d = array.get(sotch3, 1)
// Based on the TRIPPLE STOCHASTIC indicator, a signal is triggered when the
// 3 Periods are above or below a certain value at the same time
stochBuySignal = stoch1k <= lowValue and stoch1d <= lowValue and stoch2k <= lowValue and stoch2d <= lowValue and stoch3k <= lowValue and stoch3d <= lowValue
stochSellSignal = stoch1k >= highValue and stoch1d >= highValue and stoch2k >= highValue and stoch2d >= highValue and stoch3k >= highValue and stoch3d >= highValue
////////////////////////////////////////////////////
// STRATEGY 2 -- Signals Configuration
////////////////////////////////////////////////////
isTdiStochBuy = tdiBuySignal and stochBuySignal
isTdiStochSell = tdiSellSignal and stochSellSignal
// to avoid multiple signals, we block any new signal if it the same signal
// happens less than 10 candles forward
var buyCountLock = 0
if (buyCountLock > 0)
buyCountLock := buyCountLock + 1
if (isTdiStochBuy and buyCountLock == 0)
buyCountLock := 1
if (buyCountLock > 10)
buyCountLock := 0
var sellCountLock = 0
if (sellCountLock > 0)
sellCountLock := sellCountLock + 1
if (isTdiStochSell and sellCountLock == 0)
sellCountLock := 1
if (sellCountLock > 10)
sellCountLock := 0
triggerTdiStochBuy = buyCountLock == 1 and isTdiStochBuy
triggerTdiStochSell = sellCountLock == 1 and isTdiStochSell
//
// Alert configuration and trigger for the TDI & STOCHASTIC Signal
//
tdiStochAlertMessage = ''
if (triggerTdiStochSell or triggerTdiStochBuy)
// Metatrader Expert Advisor Stop Loss update
// This stop loss is specific to the TDI & STOCHASTIC Signal
// 30000 is equals to 3000$ BTC price change
EAStopLoss = stopLoss
orderType := if (triggerTdiStochSell)
'sell'
else if (triggerTdiStochBuy)
'buy'
tdiStochAlertMessage := tostring(EAuid) + ','
tdiStochAlertMessage := tdiStochAlertMessage + tostring(orderType) + ','
tdiStochAlertMessage := tdiStochAlertMessage + tostring(EAtickerId) + ','
tdiStochAlertMessage := tdiStochAlertMessage + 'sl=' + tostring(EAStopLoss) + ','
tdiStochAlertMessage := tdiStochAlertMessage + 'tp=' + tostring(EAtakeProfit) + ','
tdiStochAlertMessage := tdiStochAlertMessage + 'risk=' + tostring(EAvolume)
if (enableTdiStochSignal and forceDisableSignals == false)
alert(tostring(tdiStochAlertMessage), alert.freq_once_per_bar_close)
/////////////////////////////////////////////////////////////
// STRATEGY 2 -- Indicators Plot on the Chart - VISUAL ONLY
/////////////////////////////////////////////////////////////
plotshape(triggerTdiStochBuy and showIndicators, style=shape.labelup,size=size.small, text='B', color=color.green, textcolor=color.white, location=location.belowbar)
plotshape(triggerTdiStochSell and showIndicators, style=shape.labeldown,size=size.small, text='S', color=color.red, textcolor=color.white, location=location.abovebar)
/////////////////////////////////////////////////////////////
// STRATEGY 2 -- Playback Mode
/////////////////////////////////////////////////////////////
if (playbackMode and (triggerTdiStochSell or triggerTdiStochBuy))
table.cell(panel, 0, 1, tostring(orderType), bgcolor = color.gray, text_size=size.auto)
table.cell(panel, 1, 1, 'TDI / STOCH alert', bgcolor = color.gray, text_size=size.auto)
table.cell(panel, 2, 1, tostring(tdiStochAlertMessage), bgcolor = color.gray, text_size=size.auto)
///////////////////////////////////////////////
// STRATEGY 2 -- TDI & STOCH Strategy - END //
///////////////////////////////////////////////