# Confluence script

783 1
This is modified version of Dale Legan's "Confluence" indicator written by Gary Fritz.
================================================================
Here is Gary`s commentary:
* I moved the core Confluence computations into a Confluence function.
Since the Confluence indicator returned several "states" (bull, bear, grey, and zero),
he modified the return value a bit:
-9 to -1 = Bearish
-0.9 to 0.9 = "grey" (and zero)
1 to 9 = Bullish
The "grey" range corresponds to the "grey" values plotted by Dale's indicator, but
they're divided by 10.
So -0.4 is equivalent to "grey -4" in Dale's indicator.
* I got rid of a bit of extra computation in the function. I didn't try to do a hard-core
Pierre-style optimization :-), but I noticed several significant chunks of calculation were
being done several times each bar, and I commented them out and replaced them with an intermediate
variable. It still calls sine/cosine a dozen times on each bar, which accounts for the bulk of the
processing time, but I think it's a bit easier to understand what the code is doing this way. (It also
seems to work better -- see below.) For the most part I didn't try to use mnemonic names for these
intermediate variables, because I don't understand exactly what the values represent!!
* I'm appending a simplified Confluence indicator using the function.
* I've also appended a simple Confluence system. This system sets an entry stop above/below the current
bar if Confluence goes into bull/bear mode, and similarly sets an exit stop below/above the bar where it
exits bull/bear mode. There's also an optional "aggressive" stop mode that tightens the stops if the market
moves in your direction; for example, if the high is 1000 and your "Trigger" offset is 2, the initial stop
is set at 1002. If the next bar has a high of 997, the stop is tightened to 997+2=999.
Interestingly, when I first wrote this system, I ran into a strange MaxBarsBack problem. The Confluence
indicator worked just fine with a MaxBarsBack setting of "Auto-Detect." But systems don't have a setting
like that -- you have to specify a fixed value. But NO fixed value (up to the maximum of 999) worked for
either the system OR the indicator! And I couldn't see anywhere that it was looking back that many bars.
Then, when I did the optimization on the Confluence code, the MaxBarsBack problem mysteriously disappeared.
Sometimes TradeStation is just spooky... Any ideas what happened?
I've appended a sample system report for the system on SPX             , using the default parameters. The system actually
does pretty well. It probably won't make anyone rich, but I thought some folks might enjoy playing with it.
There are some other things you could do with it -- e.g. it might be interesting to change it to look for
long opportunities when Confluence hits -9, and short when it hits 9.
```////////////////////////////////////////////////////////////
//  Copyright by HPotter v1.0 25/04/2014
// This is modified version of Dale Legan's "Confluence" indicator written by Gary Fritz.
// ================================================================
// Here is Gary`s commentary:
// * I moved the core Confluence computations into a Confluence function.
// Since the Confluence indicator returned several "states" (bull, bear, grey, and zero),
// he modified the return value a bit:
// -9 to -1 = Bearish
// -0.9 to 0.9 = "grey" (and zero)
// 1 to 9 = Bullish
// The "grey" range corresponds to the "grey" values plotted by Dale's indicator, but
// they're divided by 10.
// So -0.4 is equivalent to "grey -4" in Dale's indicator.
// * I got rid of a bit of extra computation in the function. I didn't try to do a hard-core
// Pierre-style optimization :-), but I noticed several significant chunks of calculation were
// being done several times each bar, and I commented them out and replaced them with an intermediate
// variable. It still calls sine/cosine a dozen times on each bar, which accounts for the bulk of the
// processing time, but I think it's a bit easier to understand what the code is doing this way. (It also
// seems to work better -- see below.) For the most part I didn't try to use mnemonic names for these
// intermediate variables, because I don't understand exactly what the values represent!!
// * I'm appending a simplified Confluence indicator using the function.
// * I've also appended a simple Confluence system. This system sets an entry stop above/below the current
// bar if Confluence goes into bull/bear mode, and similarly sets an exit stop below/above the bar where it
// exits bull/bear mode. There's also an optional "aggressive" stop mode that tightens the stops if the market
// moves in your direction; for example, if the high is 1000 and your "Trigger" offset is 2, the initial stop
// is set at 1002. If the next bar has a high of 997, the stop is tightened to 997+2=999.
// Interestingly, when I first wrote this system, I ran into a strange MaxBarsBack problem. The Confluence
// indicator worked just fine with a MaxBarsBack setting of "Auto-Detect." But systems don't have a setting
// like that -- you have to specify a fixed value. But NO fixed value (up to the maximum of 999) worked for
// either the system OR the indicator! And I couldn't see anywhere that it was looking back that many bars.
// Then, when I did the optimization on the Confluence code, the MaxBarsBack problem mysteriously disappeared.
// Sometimes TradeStation is just spooky... Any ideas what happened?
// I've appended a sample system report for the system on SPX, using the default parameters. The system actually
// does pretty well. It probably won't make anyone rich, but I thought some folks might enjoy playing with it.
// There are some other things you could do with it -- e.g. it might be interesting to change it to look for
// long opportunities when Confluence hits -9, and short when it hits 9. If I get a chance to throw that together,
// I'll post it to the list.
// Have fun,
// Gary
////////////////////////////////////////////////////////////

study(title="Confluence", shorttitle="Confluence")
Harmonic = input(10, minval=1)
Price = close

STL = round((Harmonic * 2) - 1 - 0.5)
ITL = round((STL * 2) - 1 - 0.5)
LTL = round((ITL * 2) - 1 - 0.5)
HOFF = round(Harmonic / 2 - 0.5)
SOFF = round(STL / 2 - 0.5)
IOFF = round(ITL / 2 - 0.5)

xHavg = sma(Price, Harmonic)
xSavg = sma(Price, STL)
xIavg = sma(Price, ITL)
xLavg = sma(Price, LTL)

xvalue2 = xSavg - xHavg[HOFF]
xvalue3 = xIavg - xSavg[SOFF]
xvalue12 = xLavg - xIavg[IOFF]

xmomsig = xvalue2 + xvalue3 + xvalue12

xLavgOHLC = sma(ohlc4, LTL - 1)

xH2 = sma(Price, Harmonic - 1)
xS2 = sma(Price, STL - 1)
xI2 = sma(Price, ITL - 1)
xL2 = sma(Price, LTL - 1)

DerivH = (xHavg * 2) - xHavg[1]
DerivS = (xSavg * 2) - xSavg[1]
DerivI = (xIavg * 2) - xIavg[1]
DerivL = (xLavg * 2) - xLavg[1]
SumDH = Harmonic * DerivH
SumDS = STL * DerivS
SumDI = ITL * DerivI
SumDL = LTL * DerivL

LengH = Harmonic - 1
LengS = STL - 1
LengI = ITL - 1
LengL = LTL - 1

N1H = xH2 * LengH
N1S = xS2 * LengS
N1I = xI2 * LengI
N1L = xL2 * LengL

DRH = SumDH - N1H
DRS = SumDS - N1S
DRI = SumDI - N1I
DRL = SumDL - N1L

SumH = xH2 * (Harmonic - 1)
SumS = xS2 * (STL - 1)
SumI = xI2 * (ITL - 1)
SumL = xLavgOHLC * (LTL - 1)

xvalue5 = (SumH + DRH) / Harmonic
xvalue6 = (SumS + DRS) / STL
xvalue7 = (SumI + DRI) / ITL
xvalue13 = (SumL + DRL) / LTL

value9 = xvalue6 - xvalue5[HOFF]
value10 = xvalue7 - xvalue6[SOFF]
value14 = xvalue13 - xvalue7[IOFF]
xmom = value9 + value10 + value14

HT = sin(xvalue5 * 2 * 3.14 / 360) + cos(xvalue5 * 2 * 3.14 / 360)
HTA = sin(xHavg * 2 * 3.14 / 360) + cos(xHavg * 2 * 3.14 / 360)
ST = sin(xvalue6 * 2 * 3.14 / 360) + cos(xvalue6 * 2 * 3.14 / 360)
STA = sin(xSavg * 2 * 3.14 / 360) + cos(xSavg * 2 * 3.14 / 360)
IT = sin(xvalue7 * 2 * 3.14 / 360) + cos(xvalue7 * 2 * 3.14 / 360)
ITA = sin(xIavg * 2 * 3.14 / 360) + cos(xIavg * 2 * 3.14 / 360)

xSum = HT + ST + IT
xErr = HTA + STA + ITA

Condition2 = (((xSum > xSum[SOFF]) and (xHavg < xHavg[SOFF])) or ((xSum < xSum[SOFF]) and (xHavg > xHavg[SOFF])))
Phase = iff(Condition2 , -1 , 1)
xErrSum = (xSum - xErr) * Phase
xErrSig = sma(xErrSum, SOFF)

xvalue70 = xvalue5 - xvalue13
xvalue71 = sma(xvalue70, Harmonic)

ErrNum = iff (xErrSum > 0 and xErrSum < xErrSum[1] and xErrSum < xErrSig, 1,
iff (xErrSum > 0 and xErrSum < xErrSum[1] and xErrSum > xErrSig, 2,
iff (xErrSum > 0 and xErrSum > xErrSum[1] and xErrSum < xErrSig, 2,
iff (xErrSum > 0 and xErrSum > xErrSum[1] and xErrSum > xErrSig, 3,
iff (xErrSum < 0 and xErrSum > xErrSum[1] and xErrSum > xErrSig, -1,
iff (xErrSum < 0 and xErrSum < xErrSum[1] and xErrSum > xErrSig, -2,
iff (xErrSum < 0 and xErrSum > xErrSum[1] and xErrSum < xErrSig, -2,
iff (xErrSum < 0 and xErrSum < xErrSum[1] and xErrSum < xErrSig, -3, 0))))))))

momNum = iff (xmom > 0 and xmom < xmom[1] and xmom < xmomsig , 1,
iff (xmom > 0 and xmom < xmom[1] and xmom > xmomsig, 2,
iff (xmom > 0 and xmom > xmom[1] and xmom < xmomsig, 2,
iff (xmom > 0 and xmom > xmom[1] and xmom > xmomsig, 3,
iff (xmom < 0 and xmom > xmom[1] and xmom > xmomsig, -1,
iff (xmom < 0 and xmom < xmom[1] and xmom > xmomsig, -2,
iff (xmom < 0 and xmom > xmom[1] and xmom < xmomsig, -2,
iff (xmom < 0 and xmom < xmom[1] and xmom < xmomsig, -3, 0))))))))

TCNum = iff (xvalue70 > 0 and xvalue70 < xvalue70[1] and xvalue70 < xvalue71, 1,
iff (xvalue70 > 0 and xvalue70 < xvalue70[1] and xvalue70 > xvalue71, 2,
iff (xvalue70 > 0 and xvalue70 > xvalue70[1] and xvalue70 < xvalue71, 2,
iff (xvalue70 > 0 and xvalue70 > xvalue70[1] and xvalue70 > xvalue71, 3,
iff (xvalue70 < 0 and xvalue70 > xvalue70[1] and xvalue70 > xvalue71, -1,
iff (xvalue70 < 0 and xvalue70 < xvalue70[1] and xvalue70 > xvalue71, -2,
iff (xvalue70 < 0 and xvalue70 > xvalue70[1] and xvalue70 < xvalue71, -2,
iff (xvalue70 < 0 and xvalue70 < xvalue70[1] and xvalue70 < xvalue71, -3,0))))))))

value42 = ErrNum + momNum + TCNum
Confluence = iff (value42 > 0 and xvalue70 > 0, value42,
iff (value42 < 0 and xvalue70 < 0, value42,
iff ((value42 > 0 and xvalue70 < 0) or (value42 < 0 and xvalue70 > 0), value42 / 10, 0)))
Res1 = iff (Confluence >= 1, Confluence, 0)
Res2 = iff (Confluence <= -1, Confluence, 0)
Res3 = iff (Confluence == 0, 0, iff (Confluence > -1 and Confluence < 1, 10 * Confluence, 0))
plot(Res1, color=green, title="Confluence", linewidth=3, style = histogram)
plot(Res2, color=red, title="Confluence", linewidth=3, style = histogram)
plot(Res3, color=gray, title="Confluence",  linewidth=3, style = histogram)

```
DONATE/TIP

BTC: 3FKWwtaYrf5NHZzaCi1fyAfQj7XSgtyCqe
In the Confluence source code, you can change line "Price = close" on the any series from next : open high low close hl2 hlc3 ohlc4, it can be more helpful for work.
EN English
EN English (UK)
EN English (IN)
DE Deutsch
FR Français
ES Español
IT Italiano
PL Polski
SV Svenska
TR Türkçe
RU Русский
PT Português
ID Bahasa Indonesia
MS Bahasa Melayu
TH ภาษาไทย
VI Tiếng Việt
JA 日本語
KO 한국어
ZH 简体中文
ZH 繁體中文
AR العربية
Home Stock Screener Forex Signal Finder Cryptocurrency Signal Finder Economic Calendar How It Works Chart Features House Rules Moderators Website & Broker Solutions Widgets Stock Charting Library Feature Request Blog & News FAQ Help & Wiki Twitter