xmd79

Omega Ratio

May 21st, 2023
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. // © Julien_Eche
  2.  
  3. //@version=4
  4. var maxLines = 500
  5. study("️Omega Ratio","Ω Ratio", max_lines_count=maxLines, max_bars_back=5000)
  6.  
  7. bgcolor(#000000c0)
  8. group_1 = "Color Settings"
  9. t = tickerid(syminfo.prefix, syminfo.ticker)
  10. sec1in = input( "SPX500USD", "Low-Risk Security", input.symbol, tooltip ="A low risk security used to generate Minimal Acceptable Return (MAR) levels for Omega calculations" , confirm=true)
  11. period = input( 252, "Omega Period", input.integer, minval = 1)
  12. lineCount = input( 50, "Increments", input.integer, tooltip ="Number of MARs the calculation is carried on", minval=1, maxval=maxLines, step=5)
  13. sec2in = input("QQQ", "  Other Symbol", input.symbol, tooltip ="A comparison security for performance evaluation")
  14. c1 = input(#FF22FFff, "On Chart Symbol", input.color, inline ="On Chart Symbol", group = group_1)
  15. c2 = input(#0080FFff, "Other Symbol", input.color, inline ="On Chart Symbol", group = group_1)
  16.  
  17. // Low-Risk Security and Comparison Security data retrieval
  18. var maxMinusOne = lineCount - 1
  19. var periodMinusOne = period - 1
  20. var OTHER_SYMBOL = sec2in
  21. var LOW_RISK_SECURITY = sec1in
  22. var TICKER_ID_SYMBOL = syminfo.prefix + ":" + syminfo.ticker
  23. var TICKER_ID_OTHER = sec2in
  24.  
  25. lrs = security(LOW_RISK_SECURITY, timeframe.period, close)
  26. otherSym = security( OTHER_SYMBOL, timeframe.period, close)
  27.  
  28. lastLRS = nz(lrs[1], lrs)
  29. lowRiskReturn = (lrs - lastLRS) / lastLRS * 100.0
  30. mar = stdev(lowRiskReturn, period) * 3.0 * sqrt(period/(period-1))
  31.  
  32. src = close
  33. src2 = otherSym
  34. r1 = log(src / nz(src [1],src )) * 100
  35. r2 = log(src2 / nz(src2[1],src2)) * 100
  36.  
  37. // Calculation of Omega Ratio
  38. var aOmega = array.new_float(lineCount+1)
  39. var bOmega = array.new_float(lineCount+1)
  40. var aLines = array.new_line (lineCount )
  41. var bLines = array.new_line (lineCount )
  42. var Mari = array.new_float(lineCount+1)
  43. var rvalue = array.new_label(lineCount )
  44. var linesAreNA = true
  45.  
  46. Omg(array, return) =>
  47. if barstate.islast
  48. for i=0 to lineCount
  49. Ps=0.0, Ns=0.0
  50. for j=0 to periodMinusOne
  51. ex = return[j] - (mar[j] * i / lineCount)
  52. Ps += ex > 0 ? ex : 0.0
  53. Ns -= ex < 0 ? ex : 0.0
  54. array.set(array, i, Ps / Ns)
  55.  
  56. Omg(aOmega,r1)
  57. Omg(bOmega,r2)
  58.  
  59. // Draw lines and labels
  60. drawLine(X1, Y1, X2, Y2 , LineStyle, Color, LineWidth) =>
  61. var line Line = na
  62. if barstate.islast
  63. if na(Line)
  64. Line := line.new(X1, Y1, X2, Y2, xloc.bar_index, style = LineStyle, color = Color, width = LineWidth)
  65. else
  66. line.set_xy1(Line,X1,Y1)
  67. line.set_xy2(Line,X2,Y2)
  68. Line
  69.  
  70. drawlabel(X, Y, T, labelstyle, TColor, labelsize) =>
  71. var label _label = na
  72. if barstate.islast
  73. if na(_label)
  74. _label := label.new(X, Y, T, color=color.new(color.black,99), textcolor=TColor, style=labelstyle, size = labelsize)
  75. else
  76. label.set_xy(_label, X, Y)
  77. _label
  78.  
  79. // Draw Omega lines and labels
  80. if barstate.islast
  81. if linesAreNA
  82. for i=0 to maxMinusOne
  83. barIndexMinusI = bar_index - maxMinusOne + i
  84. array.set(aLines, i, line.new(barIndexMinusI-1, array.get(aOmega, i), barIndexMinusI, array.get(aOmega, i+1), color=c1, width=2))
  85. array.set(bLines, i, line.new(barIndexMinusI-1, array.get(bOmega, i), barIndexMinusI, array.get(bOmega, i+1), color=c2, width=2))
  86. for j=0 to maxMinusOne by 3
  87. barIndexMinusj = bar_index - maxMinusOne + j - 1
  88. array.set(rvalue, j, label.new(barIndexMinusj, -0.005, text="⃓\n" + tostring(round(array.get(Mari,j), 3)), color=#00000000, textcolor=#C0C0C0ff, style=label.style_label_center, size=size.small))
  89. linesAreNA := false
  90. else
  91. for i=0 to maxMinusOne
  92. barIndexMinusI = bar_index - maxMinusOne + i
  93. lineID1 = array.get(aLines, i)
  94. lineID2 = array.get(bLines, i)
  95. line.set_xy1(lineID1, barIndexMinusI-1, array.get(aOmega, i ))
  96. line.set_xy2(lineID1, barIndexMinusI , array.get(aOmega, i+1))
  97. line.set_xy1(lineID2, barIndexMinusI-1, array.get(bOmega, i ))
  98. line.set_xy2(lineID2, barIndexMinusI , array.get(bOmega, i+1))
  99. for j=0 to maxMinusOne by 3
  100. label1 = array.get(rvalue, j)
  101. barIndexMinusj = bar_index - maxMinusOne + j - 1
  102. label.set_x(label1, barIndexMinusj)
  103. na
  104. maxh = max(array.get(aOmega,0), array.get(bOmega,0))
  105. barIndexMinusLineCount = bar_index - lineCount
  106. drawLine(barIndexMinusLineCount, 0.0, bar_index , 0.0, line.style_arrow_right, #DDDDDDff,1)
  107. drawLine(barIndexMinusLineCount, 0.0, barIndexMinusLineCount, maxh * 1.1, line.style_arrow_right, #DDDDDDff, 1)
  108. drawlabel(bar_index , 0, "𝒓 %" , label.style_label_left , #FFFFFFff,size.large)
  109. drawlabel(barIndexMinusLineCount, maxh * 1.1, "Ω" , label.style_label_down , #FFFFFFff,size.large)
  110. drawlabel(barIndexMinusLineCount, array.get(aOmega, 1), TICKER_ID_SYMBOL, label.style_label_up , c1,size.normal)
  111. drawlabel(barIndexMinusLineCount, array.get(bOmega, 1), TICKER_ID_OTHER , label.style_label_right, c2,size.normal)
  112.  
  113. hline(1.0, color=#C0C000ff, linestyle=hline.style_dotted)
  114. hline(-0.1, color=#00000000)
  115.  
Advertisement
Add Comment
Please, Sign In to add comment