-
Notifications
You must be signed in to change notification settings - Fork 5
/
4 - MACD.pine
105 lines (78 loc) · 3.56 KB
/
4 - MACD.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
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © stmd
// @version=4
//-------------------------------------
// Release: 2020-08-05
// Author: stmd
// Email: pine@stmd.nl
// Profile: https://www.tradingview.com/u/stmd/
//-------------------------------------
// PURPOSE
//
//-------------------------------------
// FEATURES
//
//-------------------------------------
// NOTES
//
//-------------------------------------
// INDICATOR PARAMETERS
vVersion = "V1.1"
vTitle = "Moving Average Convergence Divergence"
vShort = "MACD"
vOverlay = false
vScale = scale.right
vPrecision = 0
study( title = vTitle+" ["+vVersion+"]", shorttitle = vShort, overlay = vOverlay, precision = vPrecision, resolution = "" )
//-------------------------------------
// INPUTS
vSource = input( defval = close, type = input.source, title = "Source" )
vFastLength = input( defval = 12, minval = 1, maxval = 1024, title = "Fast Length" )
vSlowLength = input( defval = 26, minval = 1, maxval = 1024, title = "Slow Length" )
vSignalLength = input( defval = 9, minval = 1, maxval = 1024, title = "Signal Length" )
vSmoothMacD = input( defval = 1, minval = 1, maxval = 1024, title = "Smooth MacD" )
vSmoothSignal = input( defval = 1, minval = 1, maxval = 1024, title = "Smooth Signal" )
vOffSet = input( defval = 0, minval = 0, maxval = 1024, title = "Offset" )
//-------------------------------------
// FUNCTIONS
fMa( n, s, le ) =>
r = float( 0 )
for i = n + 0 to ( n + ( le - 1 ) )
r := ( r + s[ i ] )
a = r / le
fEma( n, s, le ) =>
ma = fMa( n, s, le )
r = float( 0 )
r := ( na( r[ 1 ] ) ? ma : ( r[ 1 ] + ( 2 / ( le + 1 ) ) * ( s[ n ] - r[ 1 ] ) ) )
fMacd( n, s, lf, ls, li ) =>
emaf = fEma( n, s, lf )
emas = fEma( n, s, ls )
macd = ( emaf - emas )
r = macd
fSignal( macd, li ) =>
signal = fEma( 0, macd, li )
r = signal
fHistogram( macd, signal ) =>
histogram = ( macd - signal )
r = histogram
//-------------------------------------
// COLORS
fColorMacd( n, s ) =>
r = ( s[ n + 1 ] < s[ n ] ) ? #26A69A : #EF5350
r
fColorSignal( n, s ) =>
r = ( s[ n + 1 ] < s[ n ] ) ? #999999 : #999999
r
fColorHistogram( n, s ) =>
r = ( s[ n ] >= 0 ) ? ( s[ n + 1 ] < s[ n ] ) ? #26A69A : #B2DFDB : ( s[ n + 1 ] > s[ n ] ) ? #EF5350 : #FFCDD2
r
//-------------------------------------
// PLOT
vLineMedian = hline( 0, color = #666666, linestyle = hline.style_dashed, linewidth = 1, title = "Median", editable = true )
vMacd = fMa( 0, fMacd( vOffSet, vSource, vFastLength, vSlowLength, vSignalLength ), vSmoothMacD )
vPlotMacd = plot( vMacd, style = plot.style_line, color = fColorMacd( 0, vMacd ), title = "MacD" )
vSignal = fMa( 0, fSignal( vMacd, vSignalLength ), vSmoothSignal )
vPlotSignal = plot( vSignal, style = plot.style_line, color = fColorSignal( 0, vSignal ), title = "Signal" )
vHistogram = fHistogram( vMacd, vSignal )
vPlotHistogram = plot( vHistogram, style = plot.style_columns, color = fColorHistogram( 0, vHistogram ), title = "Histogram" )
//-------------------------------------