-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBuySellLables.pinescript
More file actions
492 lines (409 loc) · 25.6 KB
/
BuySellLables.pinescript
File metadata and controls
492 lines (409 loc) · 25.6 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//@version=5
//=============================================================================================
// Creator: Woverine
//
// Source Code: https://github.com/RyKaj/PineScript/tree/main
//
// Purpose:
// Merge mutliple BuySell Indicators
//
//=============================================================================================
indicator('Woverine - Buy Sell Indicators', overlay=true, shorttitle='BuySell')
// Plot colors
// https://chir.ag/projects/name-that-color/#6195ED
// https://kodify.net/tradingview/colours/basic-colours/
// color colorAquaIsland = #B2DFDB
// color colorAzureRadiance = #0094ff
// color colorBlack = #363A45
// color colorBlazeOrange = #ff6a00
color colorBlue = #2196F3
// color colorBlueChill = #088A8D
// color colorBlushPink = #fd7fe6
// color colorBurntSienna = #EF5350
// color colorCandelLight = #FCD917
// color colorCoral = #FF7F50
// color colorCyan = #17FFFF
// color colorDeathCross = #e32636
// color colorFreshEggplant = #800080
// color colorFruitSalad = #4CAF50
color colorFuchsia = #E040FB
// color colorGoldenCross = #61ed77
// color colorGray = #787B86
// color colorJapaneseLaurel = #008000
color colorGreen = #4CAF50
// color colorJungleGreen = #39ff14
// color colorKeyLimePie = #BCBB26
// color colorLilacBush = #9575cd
// color colorLime = #00E676
// color colorOrange = #FF9800
// color colorParsley = #0E4813
// color colorPastelPink = #FFCDD2
// color colorPurple = #9C27B0
color colorRed = #FF5252
// color colorRobinEggBlue = #00C9C9
// color colorRoseBudCherry = #880e4f
// color colorRusticRed = #4E0404
// color colorSaratoga = #505010
// color colorSpringGreen = #00E676
// color colorStarship = #F2F131
// color colorTamarillo = #991515
// color colorTeal = #00897B
// color colorTealBlue = #055355
// color colorTrendyGreen = #86851b
color colorWhite = #FFFFFF
//
//
// //Configured signal output
// slow = 8
// fast = 5
//
//
// //
// //====================channel======================
// int SMAlength = input.int(defval = 8 , title = "Trend Change SMA", minval = 1 , maxval = 1 , step = 1, tooltip = "", inline = "", group = "Channel" , confirm = false )
// float source = input.source(defval = close, title = "Source", tooltip = "", inline = "", group = "Channel" )
// bool bShowBarChannel = input.bool(defval = false , title = "Bar Channel On/Off", tooltip = "", inline = "", group = "Channel" , confirm = false )
//
// float sma = ta.sma(source = source, length = SMAlength)
// float last8h = ta.highest( source = source, length = 13)
// float lastl8 = ta.lowest( source = source, length = 13)
// bool bearish = ta.cross( source1 = source, source2 = sma) == 1 and source[1] > source
// bool bullish = ta.cross( source1 = source, source2 = sma) == 1 and source[1] < source
//
//
// plot( bShowBarChannel ? last8h : last8h == nz( last8h[1] ) ? last8h : na, color=color.new( colorBlack, 0), linewidth=1, style=plot.style_linebr, title='Candle body resistance level top', offset=0)
// plot( bShowBarChannel ? lastl8 : lastl8 == nz( lastl8[1] ) ? lastl8 : na, color=color.new( colorBlack, 0), linewidth=1, style=plot.style_linebr, title='Candle body resistance level bottom', offset=0)
//
// //--------------------trend colour ema------------------------------------------------// ema trend direction trigger For Singals 1 & 2
// EMAlength = input.int(defval = 13 , title = "Trend Change EMA", minval = 1 , maxval = 1 , step = 1, tooltip = "", inline = "", group = "" , confirm = false )
// ema = ta.ema(source = source, length = EMAlength)
// plotColor = ema >= ema[2] ? colorLime : ema < ema[2] ? colorRed : na
// plot( ema, title='EMA', style=plot.style_line, linewidth=1, color=color.new( color= plotColor , transp = 0 ) )
//
//
// //--Modified vyacheslav.shindin-------------------------------------------------// Signal 1
// vh1 = ta.ema( source = ta.highest( source=math.avg( low , source ) , length=fast ), length =fast )
// vl1 = ta.ema( source = ta.lowest( source=math.avg( high , source ) , length=slow ), length =slow )
// e_ema1 = ta.ema( source = source, length=1 )
// e_ema2 = ta.ema( source = e_ema1, length=1 )
// e_ema3 = ta.ema( source = e_ema2, length=1 )
// tema = 1 * ( e_ema1 - e_ema2 ) + e_ema3
// e_e1 = ta.ema( source=source, length=8 )
// e_e2 = ta.ema( source=e_e1, length=5 )
//
// dema = 2 * e_e1 - e_e2
// signal = tema > dema ? math.max( vh1, vl1 ) : math.min( vh1, vl1 )
// is_call = tema > dema and signal > low and signal - signal[1] > signal[1] - signal[2]
// is_put = tema < dema and signal < high and signal[1] - signal > signal[2] - signal[1]
//
// plotshape( is_call ? 1 : na, title='BUY ARROW', color=color.new( colorGreen, 0 ), text='*BUY*', style=shape.arrowup, location=location.belowbar )
// plotshape( is_put ? -1 : na, title='SELL ARROW', color=color.new( colorRed, 0 ), text='*SELL*', style=shape.arrowdown )
//
//
// //Modified - Rajandran R Supertrend----------------------------------------------------- // Signal 2
// int iFactor = input.int(defval = 1 , title = "Trend Transition Signal", minval = 1 , maxval = 800 , step = 1, tooltip = "", inline = "", group = "" , confirm = false )
// Pd = input.int(defval = 1 , title = "Pd", minval = 1 , maxval = 800 , step = 1, tooltip = "", inline = "", group = "" , confirm = false )
// Up = hl2 - iFactor * ta.atr(Pd)
// Dn = hl2 + iFactor * ta.atr(Pd)
//
// // trendUp := close[1] > trendUp[1] ? max( Up,trendUp[1] ) : Up
// // trendDown = close[1] < trendDown[1] ? min( Dn, trendDown[1] ) : Dn
// // trend = close > trendDown[1] ? 1: close< trendUp[1]? -1: nz(trend[1],0)
// // plotarrow(trend == 1 and trend[1] == -1 ? trend : na, title="Up Entry Arrow", colorup=lime, maxheight=1000, minheight=50, transp=85)
// // plotarrow(trend == -1 and trend[1] == 1 ? trend : na, title="Down Entry Arrow", colordown=red, maxheight=1000, minheight=50, transp=85)
//
// // Moddified [RS]Support and Resistance V0
// RSLength = input.int(defval = 21 , title = "Support / Resistance length", minval = 1 , maxval = 800 , step = 1, tooltip = "", inline = "", group = "" , confirm = false )
//
// float RSTT = ta.valuewhen( condition= high >= ta.highest( high, RSLength ), source=high, occurrence=0 )
// float RSTB = ta.valuewhen( condition= low <= ta.lowest( low, RSLength), source=low, occurrence=0 )
//
// plot( series= RSTT, color=RSTT != RSTT[1] ? na : color.new( color=colorRed , transp = 0 ), linewidth=1, offset=0 )
// plot( series=RSTB, color=RSTB != RSTB[1] ? na : color.new( color=colorGreen , transp = 0 ), linewidth=1, offset=0 )
//
//
// //===============================Directional Projection=======================================//
// string sTrendProjTimeFame = input.string( defval='1', title='Tgrend Projection TF / Mins/D/W' )
// string sStyle = input.string( defval='ATR', title='Average True Range' )
//
// int P2 = input.int( defval = 13 , title = "", minval = 1 , maxval = 20 , step = 1, tooltip = "", inline = "", group = "" , confirm = false )
// int W2 = input.int( defval = 1 , title = "", minval = 1 , maxval = 20 , step = 1, tooltip = "", inline = "", group = "" , confirm = false )
//
// string pf2 = ticker.pointfigure( syminfo.tickerid, 'close', sStyle, P2, W2 )
//
// spfc2 = request.security( symbol=pf2, timeframe=sTrendProjTimeFame, expression=source )
// bool bShowProjection = input.bool( defval = false , title = "Connect Projection High/Low", tooltip = "", inline = "", group = "" , confirm = false )
//
// p22 = plot( bShowProjection ? spfc2 : spfc2 == nz( spfc2[1] ) ? spfc2 : na, color=color.new( colorBlue, 0), linewidth=2, style=plot.style_linebr, title='Directional Projection', offset=0)
// //----------------------------------------------------------------------//
//
//-------------------------------------------------------------------------------------------------------------------------
// Inputs
//-------------------------------------------------------------------------------------------------------------------------
// The HLC stands for High, Low, Close and the /3 divides the result by 3 to give you the average.
float inputSource = input.source( defval = close, title = "Calculation Source", tooltip = "", inline = "", group = "" )
string inputStrategy = input.string( defval = "EMA/SMA", title = "Strategy", options = ['EMA/SMA', 'EMA/SMA TF based', 'Fractal Regular', 'Fractal Bill Williams', 'Percentage Price Oscillator', 'Hull Trend', 'Kaufman Adaptive Moving Average'], tooltip = "", inline = "", group = "" , confirm = false )
string inputStrategyLongShort = input.string( defval='ADX', title='Strategy - Long/Short', options=['DISABLED', 'ADX', 'Ichimoku_9-26-52', 'Ichimoku_10-30-60', 'Ichimoku_20-60-120'] , tooltip = "", inline = "", group = "" , confirm = false )
string inputStyleLabelSize = input.string( defval='small', title='Label - Size', options=['auto', 'huge', 'large', 'normal', 'small', 'tiny'], tooltip = "", inline = "", group = "" , confirm = false )
string inputStyleLabelType = input.string( defval='label', title='Label - Type', options=['label', 'triangle'], tooltip = "", inline = "", group = "" , confirm = false )
string inputStyleLabelPosition = input.string( defval='default', title='Label - Position', options=['default', 'lowhigh'], tooltip = "", inline = "", group = "" , confirm = false )
bool inputShowPriceOnLabel = input.bool(defval = true , title = "Show Price on Labels", tooltip = "", inline = "", group = "" , confirm = false )
bool inputShowBuyLabel = input.bool(defval = true , title = "Show BUY Signal", tooltip = "", inline = "", group = "" , confirm = false )
bool inputShowSellLabel = input.bool(defval = true , title = "Show SELL Signal", tooltip = "", inline = "", group = "" , confirm = false )
bool inputActivateAlerts = input.bool(defval = false , title = "Activate Alerts", tooltip = "", inline = "", group = "" , confirm = false )
//-------------------------------------------------------------------------------------------------------------------------
// Variables
//-------------------------------------------------------------------------------------------------------------------------
int iPosTimeBased = time //offset -1
string sCurrentTimeFramePeriod = timeframe.period
string sPrice = close <= 0.99999 ? str.tostring( close, '#.####' ) : close <= 99.99 ? str.tostring( close, '#.###' ) : close <= 999.99 ? str.tostring( close, '#.##' ) : str.tostring( close, '#' )
string sLabelTextBuy = 'BUY' + ( inputShowPriceOnLabel == true ? ': ' + sPrice : '' )
string sLabelTextSell = 'SELL' + ( inputShowPriceOnLabel == true ? ': ' + sPrice : '' )
// Average Directional Index (ADX)
int iADXLength = 13
int iADXDirectionalIndexLength = 14
int iADXAverageTrueRange = 20
int iADXHigherLowTrend = 40
//********** Set Signals - Long/Short
bool bSignalFinalLong = false
bool bSignalFinalShort = false
//-------------------------------------------------------------------------------------------------------------------------
// Functions
//-------------------------------------------------------------------------------------------------------------------------
funcDonchian( iLength ) =>
math.avg( ta.lowest( iLength ), ta.highest( iLength ) )
funcEmaSma(fSource, sPeriod, sStrategy) =>
tresholdPositive = 88
tresholdNegative = -88
// EMA/SMA
maLengthBuy_1_Default = 14
maLengthBuy_2_Default = 3
maLengthSell_1_Default = 8
maLengthSell_2_Default = 1
// setting 1
maLengthBuy_1 = sPeriod == '1' ? 6 : sPeriod == '3' ? 5 : sPeriod == '5' ? 5 : sPeriod == '15' ? 42 : sPeriod == '30' ? 42 : sPeriod == '45' ? 16 : sPeriod == '60' ? 20 : timeframe.isdaily ? 14 : timeframe.isweekly ? 14 : timeframe.ismonthly ? 5 : 14
maLengthBuy_2 = sPeriod == '1' ? 1 : sPeriod == '3' ? 1 : sPeriod == '5' ? 1 : sPeriod == '15' ? 1 : sPeriod == '30' ? 1 : sPeriod == '45' ? 1 : sPeriod == '60' ? 2 : timeframe.isdaily ? 5 : timeframe.isweekly ? 3 : timeframe.ismonthly ? 1 : 3
maLengthSell_1 = sPeriod == '1' ? 6 : sPeriod == '3' ? 3 : sPeriod == '5' ? 3 : sPeriod == '15' ? 6 : sPeriod == '30' ? 6 : sPeriod == '45' ? 8 : sPeriod == '60' ? 20 : timeframe.isdaily ? 12 : timeframe.isweekly ? 8 : timeframe.ismonthly ? 5 : 8
maLengthSell_2 = sPeriod == '1' ? 3 : sPeriod == '3' ? 1 : sPeriod == '5' ? 2 : sPeriod == '15' ? 2 : sPeriod == '30' ? 1 : sPeriod == '45' ? 1 : sPeriod == '60' ? 1 : timeframe.isdaily ? 1 : timeframe.isweekly ? 1 : timeframe.ismonthly ? 1 : 1
// setting 2
isTimeframeBased = sStrategy == 'EMA/SMA TF based'
maLengthBuyFinal_1 = isTimeframeBased ? maLengthBuy_1 : maLengthBuy_1_Default
maLengthBuyFinal_2 = isTimeframeBased ? maLengthBuy_2 : maLengthBuy_2_Default
maLengthSellFinal_1 = isTimeframeBased ? maLengthSell_1 : maLengthSell_1_Default
maLengthSellFinal_2 = isTimeframeBased ? maLengthSell_2 : maLengthSell_2_Default
// BUY
esaBuy = ta.ema(fSource, maLengthBuyFinal_1)
sourceEmaBuy = ta.ema(math.abs(fSource - esaBuy), maLengthBuyFinal_1)
ciBuy = (fSource - esaBuy) / (0.015 * sourceEmaBuy)
trendEmaBuy = ta.ema(ciBuy, maLengthBuyFinal_2)
trendSmaBuy = ta.sma(trendEmaBuy, 4)
// SELL
esaSell = ta.ema(fSource, maLengthSellFinal_1)
sourceEmaSell = ta.ema(math.abs(fSource - esaSell), maLengthSellFinal_1)
ciSell = (fSource - esaSell) / (0.015 * sourceEmaSell)
trendEmaSell = ta.ema(ciSell, maLengthSellFinal_2)
trendSmaSell = ta.sma(trendEmaSell, 4)
// prepare signals
buyEmaSma = ta.crossover(trendEmaBuy, trendSmaBuy) and trendEmaBuy <= tresholdNegative
sellEmaSma = ta.crossunder(trendEmaSell, trendSmaSell) and trendEmaSell >= tresholdPositive
[buyEmaSma, sellEmaSma]
// Percentage Price Oscillator
PercentagePriceOscillator( fSource ) =>
int iPPOfast = 12
int iPPOslow = 26
int iPPOsmooth = 2
float fPPOfastMA = ta.ema( fSource, iPPOfast )
float fPPOslowMA = ta.ema( fSource, iPPOslow )
float fPPOmacd = fPPOfastMA - fPPOslowMA
float fPPOmacd2 = fPPOmacd / fPPOslowMA * 100
float fPPOresult = ta.sma( fPPOmacd2, iPPOsmooth )
bool bPPOoscMin = fPPOresult > fPPOresult[1] and fPPOresult[1] < fPPOresult[2]
bool bPPOoscMax = fPPOresult < fPPOresult[1] and fPPOresult[1] > fPPOresult[2]
valPPOdivergenceBull = bPPOoscMin ? fPPOresult[1] : na
valPPOdivergenceBear = bPPOoscMax ? fPPOresult[1] : na
// prepare signals
bool buyPPO = valPPOdivergenceBull != 0
bool sellPPO = valPPOdivergenceBear != 0
[buyPPO, sellPPO]
// Kaufman's Adaptive Moving Average (KAMA)
KaufmanAdaptiveMovingAverage( fSource ) =>
// settings
int iKamaFastLength = 12
int iKamaFastLengthKAMA = 2
int iKamaSlowLength = 26
int iKamaSlowLengthKAMA = 30
int iKamaSignalLength = 9
// fast
float fKamaFastLag = (iKamaFastLength - 1) / 2
float fKamaFastData = fSource + fSource - fSource[ fKamaFastLag ]
float fKamaFastZeroLagEMA = ta.ema( fKamaFastData, iKamaFastLength )
float fKamaFastEma = ta.ema( fSource, iKamaFastLength )
float fKamaFastVolatility = math.sum( math.abs( fSource - fSource[1] ), iKamaFastLength )
float fKamaFastEfficiencyRatio = fKamaFastVolatility != 0 ? math.abs( fSource - fSource[ iKamaFastLength ] ) / fKamaFastVolatility : 0
float fKamaFast = 0.0
fKamaFast := nz( fKamaFast[1] ) + math.pow( fKamaFastEfficiencyRatio * ( 2 / ( iKamaFastLengthKAMA + 1 ) - 2 / ( iKamaSlowLengthKAMA + 1 )) + 2 / ( iKamaSlowLengthKAMA + 1 ), 2) * ( fSource - nz( fKamaFast[1] ) )
// slow
float fKamaSlowLag = ( iKamaSlowLength - 1 ) / 2
float fKamaSlowData = fSource + fSource - fSource[ fKamaSlowLag ]
float fKamaSlowZeroLagEMA = ta.ema( fKamaSlowData, iKamaSlowLength )
fKamaSlowVolatility = math.sum( math.abs( fSource - fSource[1] ), iKamaSlowLength )
fKamaSlowEfficiencyRatio = fKamaSlowVolatility != 0 ? math.abs( fSource - fSource[ iKamaSlowLength ]) / fKamaSlowVolatility : 0
float fKamaSlow = 0.0
fKamaSlow := nz( fKamaSlow[1] ) + math.pow( fKamaSlowEfficiencyRatio * ( 2 / ( iKamaFastLengthKAMA + 1 ) - 2 / ( iKamaSlowLengthKAMA + 1 ) ) + 2 / ( iKamaSlowLengthKAMA + 1 ), 2 ) * ( fSource - nz( fKamaSlow[1] ) )
// Moving Average Convergence Divergence (MACD)
float fKamaMacdAverage = ( ( fKamaFastZeroLagEMA - fKamaSlowZeroLagEMA ) + ( fKamaFastEma - ta.ema( fSource, iKamaSlowLength ) ) + ( fKamaFast - fKamaSlow) ) / 3
// prepare signals
fKamaSignal = ta.ema(fKamaMacdAverage, iKamaSignalLength)
// prepare signals
bool buyKama = ta.crossover(fKamaMacdAverage, fKamaSignal)
bool sellKama = ta.crossunder(fKamaMacdAverage, fKamaSignal)
[buyKama, sellKama]
funcIchimoku(sPeriod, fSource) =>
int iConversionLinePeriod = sPeriod == 'Ichimoku_9-26-52' ? 9 : sPeriod == 'Ichimoku_10-30-60' ? 10 : sPeriod == 'Ichimoku_20-60-120' ? 20 : 9
int iBaseLinePeriod = sPeriod == 'Ichimoku_9-26-52' ? 26 : sPeriod == 'Ichimoku_10-30-60' ? 30 : sPeriod == 'Ichimoku_20-60-120' ? 60 : 26
int iLaggingSpanPeriod = sPeriod == 'Ichimoku_9-26-52' ? 52 : sPeriod == 'Ichimoku_10-30-60' ? 60 : sPeriod == 'Ichimoku_20-60-120' ? 120 : 52
// calc
float fTenkanSenConversionLine = funcDonchian(iConversionLinePeriod)
float fKijunSenBaseLine = funcDonchian(iBaseLinePeriod)
float fLeadingSpanA = math.avg( fTenkanSenConversionLine, fKijunSenBaseLine )
float fLeadingSpanB = funcDonchian(iLaggingSpanPeriod)
// RSI Settings
int iChiRsiLength = 14
int iChiRsiOverBought = 50
int iChiRsiOverSold = 100
float fChiRsiDefault = ta.rsi( fSource, iChiRsiLength )
// lines, span
float tenkanSenLine = fTenkanSenConversionLine[ iBaseLinePeriod ]
float kijunSenLine = fKijunSenBaseLine[ iBaseLinePeriod ]
float senkouSpanA = fLeadingSpanA[ iBaseLinePeriod * 2 ]
float senkouSpanB = fLeadingSpanB[ iBaseLinePeriod * 2 ]
float chikuSpan = fSource[0]
// up/down
// smallest/greatest of multiple given values
bool upTrend = chikuSpan > math.max( senkouSpanA, senkouSpanB )
bool downTrend = chikuSpan < math.min( senkouSpanA, senkouSpanB )
// bull/bear
bool bBull = ta.crossover(tenkanSenLine, kijunSenLine)
bool bBear = ta.crossunder(tenkanSenLine, kijunSenLine)
// prepare signals
bool bLongIchi = bBull and upTrend and fChiRsiDefault <= iChiRsiOverSold
bool bShortIchi = bBear and downTrend and fChiRsiDefault >= iChiRsiOverBought
[bLongIchi, bShortIchi]
funcDirectionalMovement( iLength ) =>
float fUp = ta.change( high )
float fDown = -ta.change( low )
plusDM = na( fUp ) ? na : fUp > fDown and fUp > 0 ? fUp : 0
minusDM = na( fDown ) ? na : fDown > fUp and fDown > 0 ? fDown : 0
float fTueRange = ta.rma( ta.tr, iLength )
plus = fixnan( 100 * ta.rma( plusDM, iLength ) / fTueRange )
minus = fixnan( 100 * ta.rma( minusDM, iLength ) / fTueRange )
[ plus, minus ]
funcAdx( iADXDirectionalIndexLength, iLength ) =>
[ plus, minus ] = funcDirectionalMovement( iADXDirectionalIndexLength )
sum = plus + minus
funcAdx = 100 * ta.rma( math.abs( plus - minus ) / ( sum == 0 ? 1 : sum ), iLength )
funcAdx
funcADXLowHigh( iLength, sType) =>
[ plus, minus] = funcDirectionalMovement( iLength )
return_1 = sType == 'high' ? plus : minus
return_1
funcHullMovingAverage(fSource, iLength) =>
tmpVal = math.max( 2, iLength )
return_2 = ta.wma( 2 * ta.wma( fSource, tmpVal / 2 ) - ta.wma( fSource, tmpVal ), math.round( math.sqrt( tmpVal ) ) )
return_2
funcHullMovingAverage3(fSource, iLength) =>
tmpVal = iLength / 2
ta.wma( ta.wma( fSource, tmpVal / 3 ) * 3 - ta.wma( fSource, tmpVal / 2 ) - ta.wma( fSource, tmpVal ), tmpVal )
funcHullTrend(fSource, sPeriod) =>
int iHullTrendLength = sPeriod == '1' ? 30 : sPeriod == '3' ? 29 : sPeriod == '5' ? 28 : sPeriod == '15' ? 27 : sPeriod == '30' ? 26 : sPeriod == '45' ? 24 : sPeriod == '60' ? 24 : timeframe.isdaily ? 24 : timeframe.isweekly ? 24 : timeframe.ismonthly ? 24 : 24
bHullTrendA = funcHullMovingAverage( fSource, iHullTrendLength )
bHullTrendB = funcHullMovingAverage3( fSource, iHullTrendLength )
bBuyHullTrend = bHullTrendB > bHullTrendA and bHullTrendB[1] < bHullTrendA[1]
bSellHullTrend = bHullTrendA > bHullTrendB and bHullTrendA[1] < bHullTrendB[1]
[bBuyHullTrend, bSellHullTrend]
funcFractalCalculated(sType, iVariant) =>
bRegular = iVariant == 1 ? high[4] < high[3] and high[3] < high[2] and high[2] > high[1] and high[1] > high[0] : iVariant == -1 ? low[4] > low[3] and low[3] > low[2] and low[2] < low[1] and low[1] < low[0] : false
bBillWilliams = iVariant == 1 ? high[4] < high[2] and high[3] < high[2] and high[2] > high[1] and high[2] > high[0] : iVariant == -1 ? low[4] > low[2] and low[3] > low[2] and low[2] < low[1] and low[2] < low[0] : false
return_3 = sType == 'Fractal Regular' ? bRegular : bBillWilliams
return_3
float fADXDirectionalIndexHigh = funcADXLowHigh( iADXDirectionalIndexLength, 'high' )
float fADXDirectionalIndexLow = funcADXLowHigh( iADXDirectionalIndexLength, 'low' )
bool bAdxDirectionalIndexCross = ta.cross( fADXDirectionalIndexHigh, fADXDirectionalIndexLow )
funcADXUp() =>
funcAdx(iADXDirectionalIndexLength, iADXLength) >= iADXAverageTrueRange
funcADXDirectionalIndexUp() =>
fADXDirectionalIndexHigh >= fADXDirectionalIndexLow
funcADXDirectionalIndexUps() =>
fADXDirectionalIndexHigh >= iADXHigherLowTrend
funcADXDirectionalIndexDown() =>
fADXDirectionalIndexLow > fADXDirectionalIndexHigh
funcADXDirectionalIndexDowns() =>
fADXDirectionalIndexLow > iADXHigherLowTrend
funcAdxFinal() =>
// bool
bool bADXBuyCondition = funcADXUp() and funcADXDirectionalIndexUp() and funcADXDirectionalIndexUps()
bool bADXBuyCloseCondition = bAdxDirectionalIndexCross and funcADXDirectionalIndexUp()[1]
boolAdxSellCondition = funcADXUp() and funcADXDirectionalIndexDown() and funcADXDirectionalIndexDowns()
boolAdxSellCloseCondition = bAdxDirectionalIndexCross and funcADXDirectionalIndexDown()[1]
// counter
adxCounter = 0
adxCounter := bADXBuyCloseCondition ? 0 : boolAdxSellCloseCondition ? 0 : bADXBuyCondition ? 1 : boolAdxSellCondition ? -1 : nz(adxCounter[1])
// prepare signals
longAdx = adxCounter == 1 and adxCounter[1] == 0
shortAdx = adxCounter == -1 and adxCounter[1] == 0
[longAdx, shortAdx]
//********** Styling Labels
string valFinalLabelSize = inputStyleLabelSize == 'auto' ? size.auto : inputStyleLabelSize == 'huge' ? size.huge : inputStyleLabelSize == 'large' ? size.large : inputStyleLabelSize == 'normal' ? size.normal : inputStyleLabelSize == 'small' ? size.small : inputStyleLabelSize == 'tiny' ? size.tiny : size.normal
string valFinalLabelStyleUp = inputStyleLabelType == 'label' ? label.style_label_up : inputStyleLabelType == 'triangle' ? label.style_triangleup : label.style_label_up
string valFinalLabelStyleDown = inputStyleLabelType == 'label' ? label.style_label_down : inputStyleLabelType == 'triangle' ? label.style_triangledown : label.style_label_down
//********** Set Signals - Buy/Sell
[ signalFinalBuy, signalFinalSell ] = funcEmaSma( inputSource, sCurrentTimeFramePeriod, inputStrategy )
if inputStrategy == 'Kaufman Adaptive Moving Average'
[buyKama, sellKama] = KaufmanAdaptiveMovingAverage(inputSource)
signalFinalBuy := buyKama
signalFinalSell := sellKama
signalFinalSell
else if inputStrategy == 'Percentage Price Oscillator'
[buyPPO, sellPPO] = PercentagePriceOscillator(inputSource)
signalFinalBuy := buyPPO
signalFinalSell := sellPPO
signalFinalSell
else if inputStrategy == 'Hull Trend'
[buyHullTrend, sellHullTrend] = funcHullTrend(inputSource, sCurrentTimeFramePeriod)
signalFinalBuy := buyHullTrend
signalFinalSell := sellHullTrend
signalFinalSell
else if inputStrategy == 'Fractal Regular' or inputStrategy == 'Fractal Bill Williams'
signalBuyFractal = funcFractalCalculated(inputStrategy, -1)
signalSellFractal = funcFractalCalculated(inputStrategy, 1)
signalFinalBuy := signalBuyFractal
signalFinalSell := signalSellFractal
signalFinalSell
if inputStrategyLongShort == 'Ichimoku_9-26-52' or inputStrategyLongShort == 'Ichimoku_10-30-60' or inputStrategyLongShort == 'Ichimoku_20-60-120'
[signalLongIchi, signalShortIchi] = funcIchimoku(inputStrategyLongShort, inputSource)
bSignalFinalLong := signalLongIchi
bSignalFinalShort := signalShortIchi
bSignalFinalShort
else if inputStrategyLongShort == 'ADX'
[longAdx, shortAdx] = funcAdxFinal()
bSignalFinalLong := longAdx
bSignalFinalShort := shortAdx
bSignalFinalShort
//************ Plotting
// BUY
label.new( x=signalFinalBuy and inputShowBuyLabel == true ? iPosTimeBased : na, y=inputStyleLabelPosition == 'default' ? na : low, xloc=xloc.bar_time, yloc=inputStyleLabelPosition == 'default' ? yloc.belowbar : yloc.price, text=sLabelTextBuy, color=colorGreen, textcolor=colorWhite, style=valFinalLabelStyleUp, size=valFinalLabelSize )
// SELL
label.new( x=signalFinalSell and inputShowSellLabel == true ? iPosTimeBased : na, y=inputStyleLabelPosition == 'default' ? na : high, xloc=xloc.bar_time, yloc=inputStyleLabelPosition == 'default' ? yloc.abovebar : yloc.price, text=sLabelTextSell, color=colorRed, textcolor=colorWhite, style=valFinalLabelStyleDown, size=valFinalLabelSize )
// LONG
label.new( x=bSignalFinalLong ? iPosTimeBased : na, y=low, xloc=xloc.bar_time, yloc=yloc.price, text='LONG', color=colorBlue, textcolor=colorWhite, style=valFinalLabelStyleUp, size=size.tiny )
// SHORT
label.new( x=bSignalFinalShort ? iPosTimeBased : na, y=high, xloc=xloc.bar_time, yloc=yloc.price, text='SHORT', color=colorFuchsia, textcolor=colorWhite, style=valFinalLabelStyleDown, size=size.tiny )
//************ Alerting
// BUY
alertcondition( inputActivateAlerts and signalFinalBuy, title='BUY', message='BUY' )
// SELL
alertcondition( inputActivateAlerts and signalFinalSell, title='SELL', message='SELL' )
// LONG
alertcondition( inputActivateAlerts and bSignalFinalLong, title='LONG', message='LONG' )
// SHORT
alertcondition( inputActivateAlerts and bSignalFinalShort, title='SHORT', message='SHORT' )