Advertisement
fabgonber

Koncorde Plus

Oct 11th, 2022 (edited)
1,087
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.33 KB | Source Code | 0 0
  1. #// This source code is subject to the terms of the Mozilla Public License 2.0
  2. #https://www.tradingview.com/script/mAEzn9Im-Koncorde-Plus/
  3. #// © OskarGallard
  4. #indicator("Koncorde Plus", "Koncorde [+]")
  5. # Converted by Sam4Cok@Samer800 - 10/2022 - not typical
  6. # added option to show the plot as histogram or area
  7. declare lower;
  8. input timeOfPhaseProfile = {Default Day, Week, Month, Quarter, Year};
  9. def na = Double.NaN;
  10. def h = high;
  11. def c = close;
  12. def l = low;
  13. def o = open;
  14. def v = volume;
  15.  
  16. script nz {
  17.     input data  = close;
  18.     input repl  = 0;
  19.     def ret_val = if isNaN(data) then repl else data;
  20.     plot return = ret_val;
  21. }
  22. #f_htf_ohlc(_htf) =>
  23. script f_htf_ohlc {
  24. input _htf = AggregationPeriod.DAY;
  25.     def time = close(period = _htf);
  26.     def htf_cx;
  27.     if (time != time[1]) {
  28.         htf_cx = time[1];
  29.     } else {
  30.         htf_cx = htf_cx[1];
  31. }
  32.     plot cx = htf_cx;
  33. }
  34. input ShowHist = no;
  35. input MirrorPattern  = yes;   # "Mirror Pattern"
  36. input BearHugPattern = no;    # "Bear Hug Pattern"
  37. input CrossingPattern = no;   # "Crossing Pattern"
  38. input SpringPattern  = no;    # "Spring Pattern"
  39. input ZeroPattern    = yes;   # "Zero Pattern"
  40. input HarpoonPattern = yes;   # "Harpoon Pattern",
  41. input Label          = yes;   # "Statistic Panel of Performance (Change)"
  42.  
  43. input TrendSrc    = ohlc4;    # "RSI, BB & Stoch Source"
  44. input srcMfi      = hlc3;     # "MFI Source"
  45. input len_mfi_rsi = 14;       # "MFI & RSI Length"
  46. input length_bb   = 25;       # "BB Length"
  47. input mult_bb     = 2.0;      # "BB Multiplier"
  48. input length_ma  = 15;        # "MA Length"
  49. input type_ma    = {"SMA",Default "EMA", "Wilder", "WMA", "VWMA", "ALMA", "LSMA", "HMA", "VAMA", "JMA"};
  50. #// Volume Type
  51. input voltype = {Default "Default", "Tick"};
  52. #// ALMA Offset and Sigma
  53. input almaOffset  = 0.85;     # "Offset (if ALMA)"
  54. input sigma = 6;              # "Sigma (if ALMA)"
  55. #// JMA
  56. input phaseJ = 50;            # "Phase (if JMA)"
  57. input powerJ = 2;             # "Power (if JMA)"
  58. #// LSMA Offset
  59. input loff = 0;               # "Offset (if LSMA)"
  60.  
  61. input m_pvi_nvi   = 15;       # "PVI & NVI Length"
  62. input longitudPVI = 90;       # "Min-Max Period [PVI]"
  63. input longitudNVI = 90;       # "Min-Max Period [NVI]"
  64.  
  65. input show_delta = no;        # "Show Volume Delta Line"
  66. input fast_len   = 5;         # "Fast MA"
  67. input type_fast  = {"SMA", "EMA",Default "Wilder", "WMA", "ALMA", "LSMA", "HMA", "JMA"};
  68. input slow_len   = 21;        # "Slow MA"
  69. input type_slow  = {"SMA",Default "EMA", "Wilder", "WMA", "ALMA", "LSMA", "HMA", "JMA"};
  70.  
  71. #jma(src, length, power, phase) =>
  72. script jma {
  73. input src = hlc3;
  74. input length = 10;
  75. input power = 0;
  76. input phase = 0;
  77.     def phaseRatio = if phase < -100 then 0.5 else
  78.                      if phase > 100 then 2.5 else phase / 100 + 1.5;
  79.     def beta = 0.45 * (length - 1) / (0.45 * (length - 1) + 2);
  80.     def alpha = power(beta, power);
  81.     def Jma;
  82.     def e0;
  83.     def e1;
  84.     def e2;
  85.     e0 = (1 - alpha) * src + alpha * nz(e0[1]);
  86.     e1 = (src - e0) * (1 - beta) + beta * nz(e1[1]);
  87.     e2 = (e0 + phaseRatio * e1 - nz(Jma[1])) * power(1 - alpha, 2) + power(alpha, 2) * nz(e2[1]);
  88.     Jma = e2 + nz(Jma[1]);
  89. plot return = Jma;
  90. }
  91. #enhanced_alma(_series, _length, _offset, _sigma) =>
  92. script enhanced_alma {
  93. input _series = close;
  94. input _length = 10;
  95. input _offset = 0;
  96. input _sigma = 0;
  97.     def length = _length;
  98.     def numerator;
  99.     def denominator;
  100.     def m = _offset * (length - 1);
  101.     def s = length / _sigma;
  102.     numerator = fold i=0 to length-1 with p do
  103.             numerator[1] + (exp(-((i-m)*(i-m)) / (2 * s * s))) * GetValue(_series,length - 1 - i);
  104.  
  105.     denominator = fold j=0 to length-1 with q do
  106.             denominator[1] + (exp(-((j-m)*(j-m)) / (2 * s * s)));
  107.     def alma = numerator / denominator;
  108. plot return = alma;
  109. }
  110. #// Tick Volume
  111. def tick     = TickValue();
  112. def rng      = c - o;
  113. def tickrng  = if isNaN(tickrng[1]) then tick else
  114.                if AbsValue(rng) < tick then nz(tickrng[1]) else rng;
  115. def tickvol  = AbsValue(tickrng)/tick;
  116. def volumen  = if nz(v) == 0 then tickvol else v;
  117.  
  118. #enhanced_vwma(_series, _length, voltype) =>
  119. script enhanced_vwma {
  120. input _series = close;
  121. input _length = 10;
  122. input voltype = "Default";
  123. def tick     = TickValue();
  124. def rng      = close - open;
  125. def tickrng  = if isNaN(tickrng[1]) then tick else
  126.                if AbsValue(rng) < tick then nz(tickrng[1]) else rng;
  127. def tickvol  = AbsValue(tickrng)/tick;
  128. def volumen  = if nz(volume) == 0 then tickvol else volume;
  129.     def vol  = if voltype == "Default" then tickvol else volumen;
  130.     def vmp  = _series * vol;
  131.     def VWMA = sum(vmp, _length) / sum(vol, _length);
  132.    plot return =  VWMA;
  133. }
  134. #ma(_type, _source, _length, voltype, offs,sigma, powerJ, phaseJ, loff ) =>
  135. script ma{
  136. input _type = "SMA";
  137. input _source = hlc3;
  138. input _length = 14;
  139. input voltype = 0;
  140. input offs = 0.85;
  141. input sigma = 6;
  142. input powerJ = 2;
  143. input phaseJ = 50;
  144. input loff = 0;
  145.  
  146. def ma =
  147.     if _type == "SMA"  then SimpleMovingAvg(_source, _length) else
  148.     if _type == "EMA"  then ExpAverage(_source, _length) else
  149.     if _type == "WMA"  then wma(_source, _length) else
  150.     if _type == "VWMA" then enhanced_vwma(_source, _length, voltype) else
  151.     if _type == "LSMA" then inertia(_source[-loff], _length) else
  152.     if _type == "ALMA" then enhanced_alma(_source, _length, offs, sigma) else
  153.     if _type == "HMA"  then HullMovingAvg(_source, _length)else
  154.     if _type == "VAMA" then VariableMA(_source, _length)else
  155.     if _type == "JMA"  then jma(_source, _length, powerJ, phaseJ) else
  156.     if _type == "Wilder" then WildersAverage(_source, _length) else double.NaN;
  157.  
  158. plot return = ma;
  159. }
  160. #pine_mfi(src, length) =>
  161. script mfi {
  162. input src = hlc3;
  163. input length = 14;
  164.     def v = volume;
  165.     def upper = sum(v * (if (src-src[1]) <= 0.0 then 0.0 else src), length);
  166.     def lower = sum(v * (if (src-src[1]) >= 0.0 then 0.0 else src), length);
  167.     def mfi = 100.0 - (100.0 / (1.0 + upper / lower));
  168.   plot return =  mfi;
  169. }
  170. #f_bb(source, length, mult_bb) =>
  171. script f_bb {
  172. input source= hlc3;
  173. input length = 20;
  174. input mult_bb = 2;
  175.     def basis = SimpleMovingAvg(source, length);
  176.     def dev   = mult_bb * stdev(source, length);
  177.     def upper = basis + dev; # // Upper Band
  178.     def lower = basis - dev; # // Lower Band
  179.     def avg_up_low = (upper + lower) / 2;
  180.     def diff_up_low = upper - lower;
  181.   def f_bb =(source - avg_up_low) / diff_up_low * 100;
  182. plot return = f_bb;
  183. }
  184. def ll = lowest(l, 21);
  185. def hh = highest(h, 21);
  186. def k = 100 * (TrendSrc - ll) / (hh - ll);
  187. def f_stoc = SimpleMovingAvg(k, 3);
  188. #-----
  189.     def pvi;
  190.     def prevPvi = if nz(pvi[1]) == 0 then  1 else pvi[1];
  191.     if c == 0 or c[1] == 0 {
  192.         pvi = prevPvi;
  193.     } else {
  194.         pvi = if (v > nz(v[1], 0.0)) then
  195.                  prevPvi + ((c - c[1]) / c[1]) * prevPvi else prevPvi;}
  196.     def nvi;
  197.     def prevNvi = if(nz(nvi[1], 0.0) == 0.0) then 1.0 else nvi[1];
  198.     if c == 0 or c[1] == 0 {
  199.         nvi = prevNvi;
  200.     } else {
  201.         nvi = if (v < nz(v[1], 0.0)) then
  202.              prevNvi + ((c - c[1]) / c[1]) * prevNvi else prevNvi;}
  203.  
  204. def pvi_media = ExpAverage(pvi, m_pvi_nvi);
  205. def pvi_max   = highest(pvi_media, longitudPVI);
  206. def pvi_min   = lowest(pvi_media, longitudPVI);
  207. def osc_pos   = (pvi - pvi_media)*100 / (pvi_max - pvi_min);
  208.  
  209. def nvi_media = ExpAverage(nvi, m_pvi_nvi);
  210. def nvi_max   = highest(nvi_media, longitudNVI);
  211. def nvi_min   = lowest(nvi_media, longitudNVI);
  212. def osc_neg   = (nvi - nvi_media)*100 / (nvi_max - nvi_min);
  213. #----------------------------
  214.  
  215. def rsi_valor = rsi(Price = TrendSrc, Length = len_mfi_rsi);
  216. def mfi_valor = mfi(srcMfi, len_mfi_rsi);
  217. def osc_bb    = f_bb(TrendSrc,length_bb, mult_bb);
  218. def stoc      = f_stoc;
  219.  
  220. def tendencia = (rsi_valor + mfi_valor + osc_bb + stoc/3) / 2;
  221. def tiburones = SimpleMovingAvg(osc_neg, 1);
  222. def pececillos = tendencia + osc_pos;
  223.  
  224. def ma_trend = ma(type_ma, tendencia, length_ma,voltype, almaOffset,sigma, powerJ, phaseJ, loff);
  225.  
  226. def patron_espejo = (pececillos < 0 and tiburones > 0);
  227. def patron_oso = (pececillos > 0 and tiburones < 0) and pececillos >= tendencia;
  228. def patron_cero  = (tendencia crosses above 0);
  229.  
  230. def cross_up_trend = (tendencia crosses above ma_trend);
  231. def cross_dn_trend = (tendencia crosses below ma_trend);
  232.  
  233. def patron_primavera = ((tendencia crosses above ma_trend) and pececillos > tendencia);
  234.  
  235. def cross_up_shark = (tiburones crosses above ma_trend) and (pececillos < 0 and tiburones > 0);
  236. def cross_dn_shark = (tiburones crosses below 0) and pececillos > tendencia;
  237.  
  238. plot MaTrend = ma_trend;    # "MA of Trend"
  239. MaTrend.SetDefaultColor(CreateColor(255,0,0));
  240. MaTrend.SetLineWeight(2);
  241.  
  242. plot Trend = tendencia;    # "Trend"
  243. Trend.AssignValueColor(if tendencia > tendencia[1] then CreateColor(141,73,37) else CreateColor(50,0,0));
  244.  
  245. plot Shark = tiburones;       # "Shark"
  246. #Shark.SetLineWeight(1);
  247. Shark.SetPaintingStrategy(if ShowHist then PaintingStrategy.SQUARED_HISTOGRAM else PaintingStrategy.LINE);
  248. Shark.AssignValueColor( if tiburones > 0 then CreateColor(0,176,246) else CreateColor(0,55,133));
  249.  
  250. plot Minnows = pececillos;    # "Minnows"
  251. #Minnows.SetLineWeight(1);
  252. Minnows.SetPaintingStrategy(if ShowHist then PaintingStrategy.SQUARED_HISTOGRAM else PaintingStrategy.LINE);
  253. Minnows.AssignValueColor( if pececillos > 0 then CreateColor(5,255,104) else CreateColor(0,153,41));
  254.  
  255. plot Trend1 = tendencia;    # "Trend"
  256. #Trend1.SetLineWeight(1);
  257. Trend1.SetPaintingStrategy(if ShowHist then PaintingStrategy.SQUARED_HISTOGRAM else PaintingStrategy.LINE);
  258. Trend1.AssignValueColor(if tendencia > tendencia[1] then CreateColor(141,73,37) else CreateColor(50,0,0));
  259.  
  260. #--------------------------
  261. AddCloud(Minnows,0,CreateColor(76,175,80),CreateColor(76,175,80));
  262.  
  263. AddCloud(Trend1, 0,CreateColor(255,204,153),CreateColor(255,204,153));
  264.  
  265. AddCloud(Shark, 0 ,CreateColor(0,216,212),CreateColor(0,216,212));
  266. #---------------------------
  267.  
  268. plot Crossover = if CrossingPattern then if cross_up_trend then -80 else na else na;
  269. Crossover.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
  270. Crossover.SetDefaultColor(CreateColor(0,128,128));
  271.  
  272. plot Crossunder = if CrossingPattern then if cross_dn_trend then 80 else na else na;
  273. Crossunder.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
  274. Crossunder.SetDefaultColor(Color.RED);
  275.  
  276. plot Zero_Pattern = if ZeroPattern then if patron_cero then -80 else na else na;
  277. Zero_Pattern.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
  278. Zero_Pattern.SetDefaultColor(CreateColor(255,204,153));
  279.  
  280. plot Spring_Pattern = if SpringPattern then if patron_primavera then -80 else na else na;
  281. Spring_Pattern.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
  282. Spring_Pattern.SetDefaultColor(Color.GREEN);
  283.  
  284. plot Harpoon_Pattern = if HarpoonPattern then if cross_up_shark then -80 else na else na;
  285. Harpoon_Pattern.SetPaintingStrategy(PaintingStrategy.SQUARES);
  286. Harpoon_Pattern.SetDefaultColor(Color.CYAN);
  287.  
  288. plot ExitHarpPat =  if HarpoonPattern then if cross_dn_shark then MaTrend else na else na;
  289. ExitHarpPat.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
  290. ExitHarpPat.SetDefaultColor(Color.RED);
  291.  
  292. AddCloud(if MirrorPattern and patron_espejo then Double.POSITIVE_INFINITY else na, Double.NEGATIVE_INFINITY, Color.CYAN);    # "Mirror Pattern"
  293. AddCloud(if BearHugPattern and patron_oso then Double.POSITIVE_INFINITY else na,Double.NEGATIVE_INFINITY,Color.DARK_RED);    # "Bear Hug Pattern
  294.  
  295. #// Delta Arrows
  296.  
  297. #f_rate(cond) =>
  298. script f_rate {
  299. input cond = yes;
  300. def o = open; def c = close; def l = low; def h = high;
  301. def tw = h - max(o, c);
  302. def bw = min(o, c) - l;
  303. def body = AbsValue(c - o);
  304.     def ret = 0.5 * (tw + bw + (if cond then 2 * body else 0)) / (tw + bw + body);
  305.     def ret1 = if nz(ret) == 0 then 0.5 else ret;
  306.   plot return = ret1;
  307. }
  308.    
  309. def delta_up   = v * f_rate(o <= c);
  310. def delta_down = v * f_rate(o > c);
  311. def delta = if c >= o then delta_up else -delta_down;
  312. def cum_delta = TotalSum(delta);
  313. def ma_fast   = ma(type_fast, cum_delta, fast_len) ;
  314. def ma_slow   = ma(type_slow, cum_delta, slow_len) ;
  315. def delta_osc = ma_fast - ma_slow;
  316.  
  317. def sc1 = delta_osc >= 0;
  318. def sc2 = delta_osc < 0;
  319. def sc3 = delta_osc >= delta_osc[1];
  320. def sc4 = delta_osc < delta_osc[1];
  321. def col_delta_up = if sc1 and sc3 then 1 else #8080FF
  322.                    if sc1 and sc4 then -1 else 0; #40407F : color.silver
  323. def col_delta_dn = if sc2 and sc4 then 1 else  #FF8080
  324.                    if sc2 and sc3 then -1 else 0; #7F4040 : color.silver
  325.  
  326. plot UpDelta = if show_delta and delta_osc >= 0 then 150 else na;    #"Up Delta"
  327. UpDelta.SetPaintingStrategy(PAintingStrategy.POINTS);
  328. upDelta.AssignValueColor(if col_delta_up > 0 then CreateColor(128,128,255) else
  329.                          if col_delta_up < 0 then CreateColor(64,64,127) else Color.GRAY);
  330.  
  331. plot DownDelta = if show_delta and delta_osc <  0 then 150 else na;   #"Down Delta"
  332. DownDelta.SetPaintingStrategy(PAintingStrategy.POINTS);
  333. DownDelta.AssignValueColor(if col_delta_dn > 0 then CreateColor(255,128,128) else
  334.                            if col_delta_up < 0 then CreateColor(127,64,64) else Color.GRAY);
  335.  
  336. #// Performance Table
  337. def Cd  = f_htf_ohlc(timeOfPhaseProfile);
  338.  
  339. def Perform = ROUND((c / Cd - 1) * 100,2);
  340.  
  341. addlabel(Label,timeOfPhaseProfile +"(" + Perform +"%)",if Perform > 0 then color.GREEN else color.RED);
  342. #____
  343. #// Inputs
  344. input show_DVDI = no;#"????? Show Dual Volume Divergence Index ?????")
  345.  
  346. #// Tick Volume Toggle
  347. input usetick = no;#(false, "Use Tick Volume", inline="extra2")
  348.  
  349. input show_bar_dvdi = no;#(false, "Show Bars Color", inline="extra2")
  350.  
  351. #// Source
  352. input src = close;#, "Source")
  353.  
  354. #// Sampling Period
  355. input per = 55;#, "Sampling Period", 1)
  356.  
  357. input SmoothingPeriod = 1;#(1, "Smoothing Period", 1)
  358.  
  359. #
  360. #// Dual Volume Divergence Index
  361. #dvdi(x, t1, t2, v)=>
  362. script dvdi {
  363. input x = close;
  364. input t1 = 0;
  365. input t2 = 0;
  366. input v = volume;
  367.    
  368.     def ROC_x = if x[1] != 0 then (x-x[1]) / x[1] * 100 else 0;
  369. #    def roc_x = RateOfChange(Price = x, Length = 1);
  370.     def pvi;
  371.     pvi   = if isNaN(pvi[1]) then x else
  372.             if v > v[1] then nz(pvi[1]) + roc_x else nz(pvi[1]);
  373.     def psig  = ExpAverage(pvi, t1);
  374.     def PDIV1  = ExpAverage(pvi - psig, t2);
  375.     def nvi;
  376.     nvi   = if isNaN(nvi[1]) then x else
  377.             if v < v[1] then nz(nvi[1]) - roc_x else nz(nvi[1]);
  378.     def nsig  = ExpAverage(nvi, t1);
  379.     def NDIV1 = ExpAverage(nvi - nsig, t2);
  380. plot pdiv = PDIV1;
  381. plot ndiv = NDIV1;
  382. }
  383.  
  384. def vol = if usetick then tickvol else v;
  385. def pdiv = dvdi(src, per, SmoothingPeriod, vol).pdiv;
  386. def ndiv = dvdi(src, per, SmoothingPeriod, vol).ndiv;
  387.  
  388. #// Dual Index Plots
  389. plot pplot = if show_DVDI then pdiv else na;#, "PVI Divergence", #05FFA6)
  390. pplot.SetDefaultColor(Color.GREEN);
  391. plot nplot = if show_DVDI then ndiv else na;#, "NVI Divergence", #FF0A70)
  392. nplot.SetDefaultColor(Color.RED);
  393.  
  394. #// Bar Colors
  395. Def Barcolor = if (pdiv > ndiv) and (pdiv > 0) then 1 else  #05ffa6
  396.                if (pdiv > ndiv) and (pdiv <= 0) then 2 else  #00945f
  397.                if (ndiv > pdiv) and (ndiv > 0)then -1 else #ff0a70
  398.                if (ndiv > pdiv) and (ndiv <= 0) then -2  else 0;#990040: #cccccc
  399.  
  400.  
  401. AssignPriceColor(if show_bar_dvdi then
  402.                  if Barcolor == 1 then CreateColor(5,255,166) else
  403.                  if Barcolor == 2 then CreateColor(0,148,95) else
  404.                  if Barcolor == -1 then CreateColor(255,10,112) else
  405.                  if Barcolor == -2 then CreateColor(153,0,64) else Color.GRAY else Color.CURRENT);
  406.  
  407. ### END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement