// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © MUQWISHI //@version=5 indicator("Wide Range Bar", overlay = true, max_lines_count = 500, max_boxes_count = 500, max_labels_count = 500) // |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++| // | INPUT | // |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++| // ++++++++++++++++++++++ {WRB Criteria} var G0 = "Wide Range Bar Criteria" wrb_clos = input.float(75, "WRB Closes Above its Range by (%)              ", 0, inline = "1", group = G0, display = display.none)/100 wrb_minC = input.bool(true, "Min  (%) | WRB Wider Than Barback", inline = "2", group = G0, display = display.none) wrb_minV = input.float(35, "", inline = "2", group = G0, display = display.none)/100 wrb_maxC = input.bool(true, "Max (%) | WRB Wider Than Barback", inline = "3", group = G0, display = display.none) wrb_maxV = input.float(175, "", inline = "3", group = G0, display = display.none)/100 cal_mode = input.string("Wide Range", "Calculation Mode           ", ["ATR", "Wide Range"], inline = "4", group = G0, display = display.none) num_bars = input.int(3, "", 1, inline = "4", group = G0, display = display.none) atr_len = input.int(14, "   ➤ ATR Length           ", 1, inline = "5", group = G0, display = display.none, tooltip = "Applicable with {Calculation Mode = ATR}") // User input for WRB Colors wrb_cBul = input.color(#55aef7, "         WRB Color    |    Bullish & Bearish            ", inline = "6-G0", display = display.none) wrb_cBer = input.color(#ff944d, "", inline = "6-G0", display = display.none) wrb_fltC = input.bool(true, "Oversized WRB(%)", inline = "7-G0", display = display.none) wrb_fltV = input.float(100, "", inline = "7-G0", display = display.none)/100 wrb_fBul = input.color(#0a6fc2, "", inline = "7-G0", display = display.none) wrb_fBer = input.color(#b34700, "", inline = "7-G0", display = display.none) // ++++++++++++++++++++++ {Strategy Settings} var G1 = "Strategy Settings" lng_chk = input.bool(true, "Enable Long Trades", group = G1) shr_chk = input.bool(true, "Enable Short Trades", group = G1) fde_chk = input.bool(false, "Fade The WRB", group = G1) // Prymiding max_trd = input.int(3, "Maximum Simultaneous Open Trades", 1, inline = "G1-1", group = G1, display = display.none) // Session timzon = input.string("America/Los_Angeles", "Timezone", display = display.none) ses_chk = input.bool(false, "Specify Intraday Trading Session", inline = "G1-2", display = display.none) ses_ent = input.session("0700-1145", "   Entry       ", inline = "G1-3", display = display.none) ses_ext = input.session("1250-1300", "   Exit          ", inline = "G1-4", display = display.none) // Period prd_chk = input.bool(false, "Specify a Trading Window", tooltip = "Note: If the datetime of the first candle on the chart is after the entered 'From' datetime, " + "the calculation will start from the first candle on the chart.") sTime = input.time(timestamp("02 JAN 2020"), "   From", display = display.none) eTime = input.time(timestamp("01 DEC 2050"), "   To ", display = display.none) // ++++++++++++++++++++++ {Entry Settings} var G2 = "Entry Trade Settings" ent_shrO = input.string("Dollar Risk", "Position Sizer        ", ["Dollar Risk", "Dollar Size"], inline = "1", group = G2, display = display.none) ent_shrV = input.float(10000, "", 0, inline = "1", group = G2, display = display.none, tooltip = "Dollar Risk: The amount of money is willing to lose if the trade does not go as planned." + "\n\nDollar Size: The total monetary value willing to apply in a single trade.") ent_ordr = input.float(75, "Place a Limit Order at WRB (%)          ", inline = "2", group = G2, display= display.none, tooltip = "For Long Direction:\n 0% → Low of WRB | 100% → High of WRB\n\n" + "For Short Direction:\n 0% → High of WRB | 100% → Low of WRB")/100 ent_can1 = input.bool(true, "Entry Within Bars                            ", inline = "4", group = G2, display = display.none) ent_bar = input.int(4, "", 1, inline = "4", group = G2, display = display.none) ent_can2 = input.bool(true, "All Bars Must Close Inside WRB", group = G2, display = display.none) // ++++++++++++++++++++++ {Exit Settings} var G3 = "Exit Trade Settings" stp_ordr = input.float(25, "Place a Stop-Loss at WRB (%)               ", inline = "1", group = G3, display = display.none, tooltip = "For Long Direction:\n 0% → Low of WRB | 100% → High of WRB\n\n" + "For Short Direction:\n 0% → High of WRB | 100% → Low of WRB")/100 tgt_chk = input.bool(true, "Target (R)                                             ", inline = "2", group = G3, display = display.none) tgt_ordr = input.float(05, "", 0, inline = "2", group = G3, display = display.none) trl_chk = input.bool(true, "Trailing starts at                               ", inline = "3", group = G3, display = display.none) typ_trl = input.string("Touch", "", ["Touch", "Close Over"], inline = "3", group = G3, display = display.none) srt_trl = input.int(3, "          ... of (R) Level                                     ", 0, inline = "4", group = G3, display = display.none) dis_trl = input.float(2, "          ... with a Distance of (R)              ", 0, inline = "5", group = G3, display = display.none) brk_chk = input.bool(true, "Enable Breakeven Stop at (R)", inline = "6", group = G3, display = display.none) brk_R = input.float(1.5, "  ", 0, inline = "6", group = G3, display = display.none) ext_tim = input.bool(true, "Close All Open Trades at End of Session", tooltip = "It must the 'Specify a Trading Session' be enabled.") // ++++++++++++++++++++++ {Styling Settings} var G4 = "Styling Drawings" ent_sty = input.string("________", "Entry             ", ["________", "-----------", "..........."], group = G4, inline = "1", display = display.none) ent_siz = input.int(2, "", 1, group = G4, inline = "1", display = display.none) ent_col = input.color(color.blue, "", group = G4, inline = "1") stp_sty = input.string("________", "StopLoss      ", ["________", "-----------", "..........."], group = G4, inline = "2", display = display.none) stp_siz = input.int(2, "", 1, group = G4, inline = "2", display = display.none) stp_col = input.color(color.red, "", group = G4, inline = "2") tgt_sty = input.string("________", "Target           ", ["________", "-----------", "..........."], group = G4, inline = "3", display = display.none) tgt_siz = input.int(1, "", 1, group = G4, inline = "3", display = display.none) tgt_col = input.color(color.green, "", group = G4, inline = "3") trl_sty = input.string("⦵⦵⦵⦵", "Trailing        ", ["________", "〇〇〇〇", "⦵⦵⦵⦵"], group = G4, inline = "4", display = display.none) trl_siz = input.int(3, "", 1, group = G4, inline = "4", display = display.none) trl_col = input.color(color.fuchsia, "", group = G4, inline = "4") lab_typ = input.string("Level & Tooltipped Price", "Labels           ", ["Level Ø Price", "Price", "Level", "Level & Tooltipped Price"], inline = "G4-5", display = display.none) lab_siz = input.string("Normal", "", ["Auto", "Huge", "Large", "Normal", "Small", "Tiny"], inline = "G4-5", display = display.none) brk_lin = input.bool(true, "Show Breakeven R Level", inline = "G4-6") brk_col = input.color(#ff9800, "", inline = "G4-6") trd_chr = input.bool(true, "Show ▲▼ Indicating Signal Trading", inline = "G4-7") lgt_chr = input.color(#5e5e5e, "", inline = "G4-7") sht_chr = input.color(#5e5e5e, "", inline = "G4-7") shr_chr = input.bool(true, "Show Number of Shares Tooltipped in ♦", inline = "G4-8") shr_col = input.color(color.fuchsia, "         ", inline = "G4-8") cnl_chr = input.bool(true, "Show ❌ When Entry Order is Canceled") ovr_trd = input.bool(true, "Shadow Open Trades ", inline = "G4-9") col_ovr = input.color(color.new(color.gray, 75), "", inline = "G4-9") // |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++| // | CALCULATION | // |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++| // ++++++++++++ Trading Period trade_window() => prd_chk ? sTime <= time and time <= eTime : true // ++++++++++++ Trading Session entry_session() => ses_chk ? not na(time("", ses_ent, timzon)) : true exit_session() => ses_chk ? not na(time("", ses_ext, timzon)) : false // ++++++++++++ Calculate ATR and Wide Range atr = ta.rma(ta.tr, atr_len) wide = high - low // ++++++++++++ ATR or WRB (previous bars) bar_range = cal_mode == "ATR" ? atr : wide // ++++++++++++ Detect WRB wrb_signal = int(na) wrb_ovrSiz = int(na) if ((close > open and close > low + (high-low) * wrb_clos) or (close < open and close < high - (high-low) * wrb_clos)) max_prev_range = float(na) for i = 1 to num_bars max_prev_range := na(max_prev_range) ? bar_range[1] : math.max(max_prev_range, bar_range[i]) if (wrb_minC ? max_prev_range + max_prev_range * wrb_minV <= bar_range : true) and (wrb_maxC ? max_prev_range + max_prev_range * wrb_maxV >= bar_range : true) wrb_signal := close > open ? 1 : -1 if wrb_fltC and max_prev_range + max_prev_range * wrb_fltV <= bar_range wrb_ovrSiz := close > open ? 2 : -2 // Color Bars signal = (fde_chk ? -1 : 1) * (not na(wrb_ovrSiz) ? wrb_ovrSiz : not na(wrb_signal) ? wrb_signal : na) barCol = signal == 2 ? wrb_fBul : signal == -2 ? wrb_fBer : signal == 1 ? wrb_cBul : signal == -1 ? wrb_cBer : na barcolor(barCol) // |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++| // | MATRICES & DRAWING FUNCTIONS | // |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++| // +++++++++++ Calculate Amount of Shares qtyShares(e, s) => out = float(na) if ent_shrO == "Dollar Risk" out := switch syminfo.type "forex" => ent_shrV * e / (math.abs(e-s)) => ent_shrV / (math.abs(e-s) * syminfo.pointvalue) else if ent_shrO == "Dollar Size" out := switch syminfo.type "forex" => ent_shrV * e => ent_shrV / e str.tostring(out > 1 ? math.round(out, 3) : out, "0.00######") // ++++++++++++ Craete Matrices // {Side-OrderType-OrderBarIndex-EntryLimit-Stoploss-Target-High (Low -> Short)-OpenEntry-OpenBarIndex-Shares} var trade_mtx = matrix.new(na, 9, na) var level_mtx = matrix.new (na, int((brk_lin and brk_chk ? 1 : 0) + math.ceil(2 + tgt_ordr)), na) var label_mtx = matrix.new