Module: Cotcube::Helpers::Candlestick_Recognition
- Defined in:
- lib/cotcube-helpers/recognition.rb
Constant Summary collapse
- SUPERFLUOUS =
i[wap datetime prev_slope upper lower body_size lower_wick upper_wick ranges vranges vavg trades type bullish bearish doji contract]
- COMMON =
[:symbol, :timestamp, :size, :rth, :time_end, :open, :high, :vhigh, :vlow, :low, :close, :interval, :offset, :appeared, :datetime, :volume, :dist, :vperd, :vol_i, :contract, :date, :bar_size, :upper_wick, :lower_wick, :body_size, :upper, :lower, :slope, :rel, :tr, :atr, :ranges, :vranges, :vavg]
Instance Method Summary collapse
- #candles(candles, debug: false, sym:) ⇒ Object
- #comparebars(prev, curr) ⇒ Object
- #patterns(candles, debug: false, size: 5, contract:, ticksize: nil) ⇒ Object
- #print_bar(bar:, format:, power:, short: false) ⇒ Object
-
#recognize(contract:, interval: :quarters, short: true, base:, return_as_string: false, sym:) ⇒ Object
recognize serves as interface.
-
#slopescore(pprev, prev, bar, debug = false) ⇒ Object
SLOPE SCORE.
Instance Method Details
#candles(candles, debug: false, sym:) ⇒ Object
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 |
# File 'lib/cotcube-helpers/recognition.rb', line 75 def candles(candles, debug: false, sym: ) ticksize = sym[:ticksize] candles.each_with_index do |, i| # rel simply sets a grace limit based on the full height of the bar, so we won't need to use the hard limit of zero begin rel = (([:high] - [:low]) * 0.05).round(8) rel = 2 * ticksize if rel < 2 * ticksize rescue puts "Warning, found inappropriate bar".light_white + " #{bar}" raise end [:rel] = rel [:dist] ||= (([:high] - [:low])/ticksize).round(8) [:upper] = [[:open], [:close]].max [:lower] = [[:open], [:close]].min [:bar_size] = ([:high] - [:low]) [:body_size] = ([:open] - [:close]).abs [:lower_wick] = ([:lower] - [:low]) [:upper_wick] = ([:high] - [:upper]) .each{|k,v| [k] = v.round(8) if v.is_a? Float} # a doji's open and close are same (or only differ by rel) [:doji] = true if [:body_size] <= rel and [:dist] >= 3 [:tiny] = true if [:dist] <= 5 next if [:tiny] [:bullish] = true if not [:doji] and [:close] > [:open] [:bearish] = true if not [:doji] and [:close] < [:open] [:spinning_top] = true if [:body_size] <= [:bar_size] / 4 and [:lower_wick] >= [:bar_size] / 4 and [:upper_wick] >= [:bar_size] / 4 # a marubozu open at high or low and closes at low or high [:marubozu] = true if [:upper_wick] < rel and [:lower_wick] < rel # a bar is considered bearish if it has at least a dist of 5 ticks and it's close it near high (low resp) [:bullish_close] = true if ([:high] - [:close]) <= rel and not [:marubozu] [:bearish_close] = true if ([:close] - [:low]) <= rel and not [:marubozu] # the distribution of main volume is shown in 5 segments, like [0|0|0|4|5] shows that most volume concentrated at the bottom, [0|0|3|0|0] is heavily centered # TODO end candles end |
#comparebars(prev, curr) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/cotcube-helpers/recognition.rb', line 125 def (prev, curr) bullishscore = 0 bearishscore = 0 bullishscore += 1 if prev[:high] <= curr[:high] bullishscore += 1 if prev[:low] <= curr[:low] bullishscore += 1 if prev[:close] <= curr[:close] bearishscore += 1 if prev[:close] >= curr[:close] bearishscore += 1 if prev[:low] >= curr[:low] bearishscore += 1 if prev[:high] >= curr[:high] r = {} r[:bullish] = true if bullishscore >= 2 r[:bearish] = true if bearishscore >= 2 return r end |
#patterns(candles, debug: false, size: 5, contract:, ticksize: nil) ⇒ Object
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 |
# File 'lib/cotcube-helpers/recognition.rb', line 141 def patterns(candles, debug: false, size: 5, contract:, ticksize: nil ) candles.each_with_index do |, i| preceeding = candles.select{|x| x[:datetime] <= [:datetime] } if i.zero? [:slope] = 0 next end ppprev= candles[i-3] pprev= candles[i-2] prev = candles[i-1] succ = candles[i+1] [:huge] = true if [:true_range] >= [:atr] * 1.5 [:small] = true if [:true_range] <= [:atr5] * 0.6666 [:vavg] = ([:vranges].reduce(:+) / [:vranges].size.to_f).round if [:vranges] and [:vranges].compact.size > 0 [:vranges] = prev[:vranges].nil? ? [ [:volume] ] : prev[:vranges] + [ [:volume] ] [:vranges].shift while [:vranges].size > size [:vavg] ||= ([:vranges].reduce(:+) / [:vranges].size.to_f).round if [:vranges] and [:vranges].compact.size > 0 # VOLUME if [:volume] > [:vavg] * 1.3 [:BREAKN_volume] = true elsif [:volume] >= [:vavg] * 1.1 [:RISING_volume] = true elsif [:volume] < [:vavg] * 0.7 [:FAINTN_volume] = true elsif [:volume] <= [:vavg] * 0.9 [:FALLIN_volume] = true else [:STABLE_volume] = true end # GAPS [:bodygap] = true if [:lower] > prev[:upper] or [:upper] < prev[:lower] [:gap] = true if [:low] > prev[:high] or [:high] < prev[:low] [:slope] = slopescore(pprev, prev, , debug) [:prev_slope] = prev[:slope] # UPPER_PIVOTs define by having higher highs and higher lows than their neighor [:UPPER_ISOPIVOT] = true if succ and prev[:high] < [:high] and prev[:low] <= [:low] and succ[:high] < [:high] and succ[:low] <= [:low] and [:lower] >= [prev[:upper], succ[:upper]].max [:LOWER_ISOPIVOT] = true if succ and prev[:high] >= [:high] and prev[:low] > [:low] and succ[:high] >= [:high] and succ[:low] > [:low] and [:upper] <= [prev[:lower], succ[:lower]].min [:UPPER_PIVOT] = true if succ and prev[:high] < [:high] and prev[:low] <= [:low] and succ[:high] < [:high] and succ[:low] <= [:low] and not [:UPPER_ISOPIVOT] [:LOWER_PIVOT] = true if succ and prev[:high] >= [:high] and prev[:low] > [:low] and succ[:high] >= [:high] and succ[:low] > [:low] and not [:LOWER_ISOPIVOT] # stopping volume is defined as high volume candle during downtrend then closes above mid candle (i.e. lower_wick > body_size) [:stopping_volume] = true if ([:BREAKN_volume] or [:RISING_volume]) and prev[:slope] < -5 and [:lower_wick] >= [:body_size] [:stopping_volume] = true if ([:BREAKN_volume] or [:RISING_volume]) and prev[:slope] > 5 and [:upper_wick] >= [:body_size] [:volume_lower_wick] = true if [:vhigh] and ([:vol_i].nil? or [:vol_i] >= 2) and [:vhigh] <= [:lower] and not [:FAINTN_volume] and not [:FALLIN_volume] [:volume_upper_wick] = true if [:vlow] and ([:vol_i].nil? or [:vol_i] >= 2) and [:vlow] >= [:upper] and not [:FAINTN_volume] and not [:FALLIN_volume] ################################### # SINGLE CANDLE PATTERNS ################################### # a hammer is a bar, whose open or close is at the high and whose body is lte 1/3 of the size, found on falling slope, preferrably gapping away [:HAMMER] = true if [:upper_wick] <= [:rel] and [:body_size] <= [:bar_size] / 3 and [ :slope] <= -6 # same shape, but found at a raising slope without the need to gap away [:HANGING_MAN] = true if [:upper_wick] <= [:rel] and [:body_size] <= [:bar_size] / 3 and prev[:slope] >= 6 # a shooting star is the inverse of the hammer, while the inverted hammer is the inverse of the hanging man [:SHOOTING_STAR] = true if [:lower_wick] <= [:rel] and [:body_size] <= [:bar_size] / 2.5 and [ :slope] >= 6 [:INVERTED_HAMMER] = true if [:lower_wick] <= [:rel] and [:body_size] <= [:bar_size] / 3 and prev[:slope] <= -6 # a star is simply gapping away the preceding slope if (([:lower] > prev[:upper] and [:slope] >= 6 and [:high] >= prev[:high]) or ([:upper] < prev[:lower] and [:slope] <= -6) and [:low] <= prev[:low]) [:doji] ? [:DOJI_STAR] = true : [:STAR] = true end # a belthold is has a gap in the open, but reverses strong [:BULLISH_BELTHOLD] = true if [:lower_wick] <= [:rel] and [:body_size] >= [:bar_size] / 2 and prev[:slope] <= -4 and [:lower] <= prev[:low ] and [:bullish] and not prev[:bullish] and [:bar_size] >= prev[:bar_size] [:BEARISH_BELTHOLD] = true if [:upper_wick] <= [:rel] and [:body_size] >= [:bar_size] / 2 and prev[:slope] >= -4 and [:upper] <= prev[:high] and [:bearish] and not prev[:bearish] and [:bar_size] >= prev[:bar_size] ################################### # DUAL CANDLE PATTERNS ################################### # ENGULFINGS [:BULLISH_ENGULFING] = true if [:bullish] and prev[:bearish] and [:lower] <= prev[:lower] and [:upper] > prev[:upper] and prev[:slope] <= -6 [:BEARISH_ENGULFING] = true if [:bearish] and prev[:bullish] and [:lower] < prev[:lower] and [:upper] >= prev[:upper] and prev[:slope] >= 6 # DARK-CLOUD-COVER / PIERCING-LINE (on-neck / in-neck / thrusting / piercing / PDF pg 63) [:DARK_CLOUD_COVER] = true if [:slope] > 5 and prev[:bullish] and [:open] > prev[:high] and [:close] < prev[:upper] - prev[:body_size] * 0.5 and not [:BEARISH_ENGULFING] [:PIERCING_LINE] = true if [:slope] < -5 and prev[:bearish] and [:open] < prev[:low ] and [:close] > prev[:lower] + prev[:body_size] * 0.5 and not [:BULLISH_ENGULFING] [:SMALL_CLOUD_COVER] = true if [:slope] > 5 and prev[:bullish] and [:open] > prev[:high] and [:close] < prev[:upper] - prev[:body_size] * 0.25 and not [:BEARISH_ENGULFING] and not [:DARK_CLOUD_COVER] [:THRUSTING_LINE] = true if [:slope] < -5 and prev[:bearish] and [:open] < prev[:low ] and [:close] > prev[:lower] + prev[:body_size] * 0.25 and not [:BULLISH_ENGULFING] and not [:PIERCING_LINE] # COUNTER ATTACKS are like piercings / cloud covers, but insist on a large reverse while only reaching the preceding close [:BULLISH_COUNTERATTACK] = true if [:slope] < 6 and prev[:bearish] and [:bar_size] > [:atr] * 0.66 and ([:close] - prev[:close]).abs < 2 * [:rel] and [:body_size] >= [:bar_size] * 0.5 and [:bullish] [:BEARISH_COUNTERATTACK] = true if [:slope] > 6 and prev[:bullish] and [:bar_size] > [:atr] * 0.66 and ([:close] - prev[:close]).abs < 2 * [:rel] and [:body_size] >= [:bar_size] * 0.5 and [:bearish] # HARAMIs are an unusual long body embedding the following small body [:HARAMI] = true if [:body_size] < prev[:body_size] / 2.5 and prev[:bar_size] >= [:atr] and prev[:upper] > [:upper] and prev[:lower] < [:lower] and not [:doji] [:HARAMI_CROSS] = true if [:body_size] < prev[:body_size] / 2.5 and prev[:bar_size] >= [:atr] and prev[:upper] > [:upper] and prev[:lower] < [:lower] and [:doji] if [:HARAMI] or [:HARAMI_CROSS] puts [ :date, :open, :high, :low, :close, :upper, :lower ].map{|x| prev[x]}.join("\t") if debug puts [ :date, :open, :high, :low, :close, :upper, :lower ].map{|x| [ x]}.join("\t") if debug puts "" if debug end # TODO TWEEZER_TOP and TWEEZER_BOTTOM # actually being a double top / bottom, this dual candle pattern has to be unfolded. It is valid on daily or weekly charts, # and valid if # 1 it has an according ################################### # TRIPLE CANDLE PATTERNS ################################### # morning star, morning doji star next unless prev and pprev [:MORNING_STAR] = true if prev[:STAR] and [:bullish] and [:close] >= pprev[:lower] and prev[:slope] < -6 [:MORNING_DOJI_STAR] = true if prev[:DOJI_STAR] and [:bullish] and [:close] >= pprev[:lower] and prev[:slope] < -6 [:EVENING_STAR] = true if prev[:STAR] and [:bearish] and [:close] <= pprev[:upper] and prev[:slope] > 6 [:EVENING_DOJI_STAR] = true if prev[:DOJI_STAR] and [:bearish] and [:close] <= pprev[:upper] and prev[:slope] > 6 # the abandoned baby escalates above stars by gapping the inner star candle to both framing it [:ABANDONED_BABY] = true if ([:MORNING_STAR] or [:MORNING_DOJI_STAR]) and prev[:high] <= [ pprev[:low ], [:low ] ].min [:ABANDONED_BABY] = true if ([:EVENING_STAR] or [:EVENING_DOJI_STAR]) and prev[:low ] >= [ pprev[:high], [:high] ].max # UPSIDEGAP_TWO_CROWS [:UPSIDEGAP_TWO_CROWS] = true if (prev[:STAR] or prev[:DOJI_STAR]) and prev[:slope] > 4 and [:bearish] and prev[:bearish] and [:close] > pprev[:close] [:DOWNGAP_TWO_RIVERS] = true if (prev[:STAR] or prev[:DOJI_STAR]) and prev[:slope] < 4 and [:bullish] and prev[:bullish] and [:close] < pprev[:close] # THREE BLACK CROWS / THREE WHITE SOLDIERS [:THREE_BLACK_CROWS] = true if [ , prev, pprev ].map{|x| x[:bearish] and x[:bar_size] > 0.5 * [:atr] }.reduce(:&) and pprev[:close] - prev[ :close] > [:atr] * 0.2 and prev[ :close] - [ :close] > [:atr] * 0.2 [:THREE_WHITE_SOLDIERS] = true if [ , prev, pprev ].map{|x| x[:bullish] and x[:bar_size] > 0.5 * [:atr] }.reduce(:&) and prev[:close] - pprev[:close] > [:atr] * 0.2 and [ :close] - prev[ :close] > [:atr] * 0.2 #### MARKTTECHNIK #### # Umkehrstäbe # Ein Umkehrstab bullish ist ein Candle, der zumindest einen Downtrend aus 3 Kerzen beenden könnte. # dazu muss der stab selber ein niedrigers tief haben als seine beiden vorgänger, # der TR muss above average sein und der vorgänger muss einen slopescore von < -5 haben # if pprev[:low] > prev[:low] and prev[:low] > [:low] and ( [:tr] > [:atr] * 1.25 or [:BREAKN_volume] ) and prev[:slope] <= -5 and [:close] - [:low] >= ([:bar_size]) * 0.7 #bar[:close] >= (bar[:high] + bar[:low]) / 2.0 [:UMKEHRSTAB_BULLISH] = true [:CLASSIC] = true end if pprev[:high] < prev[:high] and prev[:high] < [:high] and ( [:tr] > [:atr] * 1.25 or [:BREAKN_volume] ) and prev[:slope] >= 5 and [:high] - [:close] >= ([:bar_size]) * 0.7 #bar[:close] <= (bar[:high] + bar[:low]) / 2.0 [:UMKEHRSTAB_BEARISH] = true [:CLASSIC] = true end # TINY REVERSALS only work for short periods of time (i.e. lte 5 min) if [:datetime] and [:datetime].respond_to?(:-) and [:datetime] - prev[:datetime] <= 5*60 if pprev[:low] > prev[:low] and prev[:low] > [:low] and ((not [:tiny] and [:bar_size] >= 0.75 * [:atr]) or [:BREAKN_volume]) and [:high] - [:close] < [:bar_size] / 2.5 [:UMKEHRSTAB_BULLISH] = true [:TINY] = true end if pprev[:high] < prev[:high] and prev[:high] < [:high] and ((not [:tiny] and [:bar_size] >= 0.75 * [:atr]) or [:BREAKN_volume]) and [:close] - [:low] < [:bar_size] / 2.5 [:UMKEHRSTAB_BEARISH] = true [:TINY] = true end end # GEM reversals are just another set of criteria if prev[:low] > [:low] and prev[:high] > [:high] and ([:bar_size] >= [:atr] or [:BREAKN_volume]) and [:high] - [:close] < [:bar_size] / 4.0 [:UMKEHRSTAB_BULLISH] = true [:GEM] = true end if prev[:low] < [:low] and prev[:high] < [:high] and ([:bar_size] >= [:atr] or [:BREAKN_volume]) and [:close] - [:low] < [:bar_size] / 4.0 [:UMKEHRSTAB_BEARISH] = true [:GEM] = true end # DOUBLEBAR reversals are reversals, that span over 2 bars instead of one unless ppprev.nil? or pprev[:slope].nil? pot = { open: prev[:open], high: [prev[:high], [:high]].max, low: [prev[:low], [:low]].min, close: [:close] } pot[:size] = (pot[:high] - pot[:low]).round(8) if (pprev[:slope] >=8 or (prev[:low] > pprev[:low] and pprev[:low] > ppprev[:low])) and pot[:high] > pprev[:high] and pot[:high] - pot[:close] >= pot[:size] * 0.7 [:UMKEHRSTAB_BEARISH] = true [:DOUBLE] = true end if (pprev[:slope] <=-8 or (prev[:high] < pprev[:high] and pprev[:high] < ppprev[:high])) and pot[:low] < pprev[:low] and pot[:close] - pot[:low] >= pot[:size] * 0.7 [:UMKEHRSTAB_BULLISH] = true [:DOUBLE] = true end end # BULLISH_MOVE und BEARISH_MOVE unless pprev[:atr].nil? if pprev[:high] > prev[:high] and prev[:high] > [:high] and pprev[:low] > prev[:low] and prev[:low] > [:low] and [:bar_size] >= pprev[:atr] and pprev[:bar_size] >= pprev[:atr] and prev[:bar_size] >= pprev[:atr] and [:close] - [:low] < [:bar_size] / 3 [:BEARISH_MOVE] = prev[:BEARISH_MOVE].nil? ? (pprev[:BEARISH_MOVE].nil? ? 1 : pprev[:BEARISH_MOVE] + 2) : prev[:BEARISH_MOVE] + 1 end if pprev[:high] < prev[:high] and prev[:high] < [:high] and pprev[:low] < prev[:low] and prev[:low] < [:low] and [:bar_size] >= pprev[:atr] and pprev[:bar_size] >= pprev[:atr] and prev[:bar_size] >= pprev[:atr] and [:high] - [:close] < [:bar_size] / 3 [:BULLISH_MOVE] = prev[:BULLISH_MOVE].nil? ? (pprev[:BULLISH_MOVE].nil? ? 1 : pprev[:BULLISH_MOVE] + 2) : prev[:BULLISH_MOVE] + 1 end end # support and resistance end end |
#print_bar(bar:, format:, power:, short: false) ⇒ Object
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 |
# File 'lib/cotcube-helpers/recognition.rb', line 26 def (bar:, format:, power:, short: false) x = .dup dir = x[:bullish] ? :bullish : x[:bearish] ? :bearish : x[:doji] ? :doji : :none contract = [:contract] SUPERFLUOUS.map{|s| x.delete(s)} i[ UPPER_PIVOT LOWER_PIVOT UPPER_ISOPIVOT LOWER_ISOPIVOT ].map{|s| x.delete(s)} vol = x.keys.select{|x| x.to_s =~ /_volume/}[0] x.delete vol vol = vol.to_s.split("_")[0] col = case dir when :bullish :light_green when :bearish :light_red when :doji :light_blue else:light_black end special = lambda do |s| case s when *i[ THRUSTING_LINE SHOOTING_STAR HANGING_MAN EVENING_STAR EVENING_DOJI_STAR UPSIDEGAP_TWO_CROWS UMKEHRSTAB_BEARISH ] s.to_s.colorize(:light_red) when *i[ PIERCING_LINE INVERTED_HAMMER HAMMER MORNING_STAR MORNING_DOJI_STAR DOWNGAP_TWO_RIVERS UMKEHRSTAB_BULLISH ] s.to_s.colorize(:light_green) else s.to_s.colorize(:light_cyan) end end "#{ format '%12s', (format % x[:open ]) }" + " #{format '%12s', (format % x[:high ])}".colorize( (x.keys.include?(:UPPER_PIVOT) or x.keys.include?(:UPPER_ISOPIVOT)) ? :light_blue : :white ) + " #{format '%12s', (format % x[:low ])}".colorize( (x.keys.include?(:LOWER_PIVOT) or x.keys.include?(:LOWER_ISOPIVOT)) ? :light_blue : :white ) + " #{format '%12s', ( format % x[:close ])}" + (short ? "" : " #{"%10s" % ("[ % -4.1f ]" % x[:slope])}".colorize( if x[:slope].abs < 3; :yellow; elsif x[:slope] >= 3; :green; else; :red; end)) + (short ? "" : " #{"% 8d" % x[:volume]}") + " #{vol}".colorize( case vol; when *["BREAKN", "FAINTN"]; :light_blue; when *["RISING","FALLIN"]; :cyan; else; :white; end) + (short ? "" : " D:#{"%5d" % x[:dist]}" ) + (short ? "" : " P:#{"%10.2f" % (x[:dist] * power)} ".cyan ) + format('%10s', dir.to_s).colorize(col) + (short ? "" : format('%-22s', " >>> #{x.keys.map{|v| (COMMON.include?(v) or v.upcase == v)? nil : v}.compact.join(" ")}").colorize( col )) + "\t#{x.keys.map{|v| (COMMON.include?(v) or v.upcase != v)? nil : special.call(v)}.compact.join(" ")}" end |
#recognize(contract:, interval: :quarters, short: true, base:, return_as_string: false, sym:) ⇒ Object
recognize serves as interface
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/cotcube-helpers/recognition.rb', line 12 def recognize(contract:, interval: :quarters, short: true, base:, return_as_string: false, sym: ) s = bas CR::candles s, ticksize: sym[:ticksize] CR::patterns s, contract: contract, ticksize: sym[:ticksize] s.map{|x| x[:datetime] += 7.hours } if s and %w[ GG DY ].include?(contract[..1]) and interval == :quarters s.map{|x| x[:datetime] += 1.hour } if s and %w[ GC PA PL SI HG NG HO RB CL ].include?(contract[..1]) and interval == :quarters make_string = lambda {|c| "#{contract }\t#{c[:datetime].strftime( interval == :quarters ? '%Y-%m-%d %H:%M' : '%Y-%m-%d' )}".colorize(:light_white) + "\t#{print_bar(bar: c, format: sym[:format], power: sym[:power], short: short) } #{"\n\n" if c[:datetime].wday == 5 and interval == :days}" } return_as_string ? s[-count..].map{|candle| make_string.call(candle) }.join("\n") : s end |
#slopescore(pprev, prev, bar, debug = false) ⇒ Object
SLOPE SCORE
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 492 493 494 495 496 497 498 499 500 501 502 503 |
# File 'lib/cotcube-helpers/recognition.rb', line 398 def slopescore(pprev, prev, , debug = false) # the slope between to bars is considered bullish, if 2 of three points match # - higher high # - higher close # - higher low # the opposite counts for bearish # # this comparison is done between the current bar and previous bar # - if it confirms the score of the previous bar, the new slope score is prev + curr # - otherwise the is compared to score of the pprevious bar # - if it confirms there, the new slope score is pprev + curr # - otherwise the trend is destroyed and tne new score is solely curr if [:bullish] curr = 1 curr += 1 if [:bullish_close] elsif [:bearish] curr = -1 curr -= 1 if [:bearish_close] else curr = 0 end puts "curr set to #{curr} @ #{bar[:date]}".yellow if debug if prev.nil? puts "no prev found, score == curr: #{curr}" if debug score = curr else comp = (prev, ) puts prev.select{|k,v| [:high,:low,:close,:score].include?(k)} if debug puts if debug puts "COMPARISON 1: #{comp}" if debug if prev[:slope] >= 0 and comp[:bullish] # bullish slope confirmed score = prev[:slope] score += curr if curr > 0 [ :gap, :bodygap ] .each {|x| score += 0.5 if [x] } score += 1 if [:RISING_volume] score += 2 if [:BREAKN_volume] puts "found bullish slope confirmed, new score #{score}" if debug elsif prev[:slope] <= 0 and comp[:bearish] # bearish slope confirmed score = prev[:slope] score += curr if curr < 0 [ :gap, :bodygap ] .each {|x| score -= 0.5 if [x] } score -= 1 if [:RISING_volume] score -= 2 if [:BREAKN_volume] puts "found bearish slope confirmed, new score #{score} (including #{curr} and #{bar[:bodygap]} and #{bar[:gap]}" if debug else #if prev[:slope] > 0 # slopes failed puts "confirmation failed: " if debug if pprev.nil? score = curr else comp2 = (pprev, ) puts "\t\tCOMPARISON 2: #{comp2}" if debug if pprev[:slope] >= 0 and comp2[:bullish] # bullish slope confirmed on pprev score = pprev[:slope] score += curr if curr > 0 [ :gap, :bodygap ] .each {|x| score += 0.5 if [x] } puts "\t\tfound bullish slope confirmed, new score #{score}" if debug score += 1 if [:RISING_volume] score += 2 if [:BREAKN_volume] elsif pprev[:slope] <= 0 and comp2[:bearish] # bearish slope confirmed score = pprev[:slope] score += curr if curr < 0 [ :gap, :bodygap ] .each {|x| score -= 0.5 if [x] } score -= 1 if [:RISING_volume] score -= 2 if [:BREAKN_volume] puts "\t\tfound bearish slope confirmed, new score #{score}" if debug else #slope confirmation finally failed comp3 = (pprev, prev) if prev[:slope] > 0 # was bullish, turning bearish now score = curr score -= 1 if comp3[:bearish] score -= 1 if comp[:bearish] score -= 1 if prev[:bearish] score -= 1 if prev[:RISING_volume] and comp3[:bearish] score -= 2 if prev[:BREAKN_volume] and comp3[:bearish] score -= 1 if [:RISING_volume] and comp[:bearish] score -= 2 if [:BREAKN_volume] and comp[:bearish] score -= 1 if [:RISING_volume] and comp[:bearish] score -= 2 if [:BREAKN_volume] and comp[:bearish] [ :gap, :bodygap ] .each {|x| score += 0.5 if [x] } puts "\t\tfinally gave up, turning bearish now, new score #{score}" if debug elsif prev[:slope] < 0 score = curr score += 1 if comp3[:bullish] score += 1 if comp[:bullish] score += 1 if prev[:bullish] score += 1 if prev[:RISING_volume] and comp3[:bullish] score += 2 if prev[:BREAKN_volume] and comp3[:bullish] score += 1 if [:RISING_volume] and comp[:bullish] score += 2 if [:BREAKN_volume] and comp[:bullish] score += 1 if [:RISING_volume] and comp[:bullish] score += 2 if [:BREAKN_volume] and comp[:bullish] [ :gap, :bodygap ] .each {|x| score -= 0.5 if [x] } if curr < 0 puts "\t\tfinally gave up, turning bullish now, new score #{score}" if debug else score = 0 end end end end end puts "" if debug score end |