Levels Plotter

s7s avatar
s7s
JavaScript
09/02/2025 8:31 AM
10.2 KB
14
Indexable
Never

indicator("Algoflow's Levels Plotter", overlay = true)

// Define user inputs for ES and NQ OVN/RTH/Weekly levels
var string esOVNinput = input.text_area(defval = "", title = "ES OVN Input", display=display.none)
var string nqOVNinput = input.text_area(defval = "", title = "NQ OVN Input", display=display.none)
var string esRTHinput = input.text_area(defval = "", title = "ES RTH Input", display=display.none)
var string nqRTHinput = input.text_area(defval = "", title = "NQ RTH Input", display=display.none)
var string esWKinput = input.text_area(defval = "Bulls: \nInflection Point: \nBears: \n", title = "ES Weekly Input", tooltip = "Enter all levels, separated by commas", display=display.none)
var string nqWKinput = input.text_area(defval = "Bulls: \nInflection Point: \nBears: \n", title = "NQ Weekly Input", tooltip = "Enter all levels, separated by commas", display=display.none)

// Define line and lable graphic properties
var TextSizeOptions = input.string("small", title = "Text size for all labels", options = ["auto", "tiny", "small", "normal", "large", "huge"], display=display.none)
TextSize = switch TextSizeOptions
    "auto"   => size.auto
    "tiny"   => size.tiny
    "small"  => size.small
    "normal" => size.normal
    "large"  => size.large
    "huge"   => size.huge

var IPgrp = "Inflection Point Colors"
var color IPcolor = input.color(color.purple,      title = "Lines",   inline = "1", group = IPgrp, display=display.none)
var color IPtextColor = input.color(color.white,   title = "Texts",   inline = "1", group = IPgrp, display=display.none)

var OVNgrp = "OVN Colors: Lines | Texts"
var color OVNbullColor = input.color(color.yellow, title = "Bulls:",  inline = "2", group = OVNgrp, display=display.none)
var color OVNbullText = input.color(color.blue,    title = "|",       inline = "2", group = OVNgrp, display=display.none)
var color OVNbearColor = input.color(color.blue,   title = "Bears:",  inline = "2", group = OVNgrp, display=display.none)
var color OVNbearText = input.color(color.yellow,  title = "|",       inline = "2", group = OVNgrp, display=display.none)

var RTHgrp = "RTH Colors: Lines | Texts"
var color RTHbullColor = input.color(color.red,    title = "Bulls:",  inline = "3", group = RTHgrp, display=display.none)
var color RTHbullText = input.color(color.white,   title = "|",       inline = "3", group = RTHgrp, display=display.none)
var color RTHbearColor = input.color(color.green,  title = "Bears:",  inline = "3", group = RTHgrp, display=display.none)
var color RTHbearText = input.color(color.white,   title ="|",        inline = "3", group = RTHgrp, display=display.none)

var WKgrp = "Weekly Colors: Lines | Texts"
var color WKbullColor = input.color(color.white,   title = "Bulls:",  inline = "4", group = WKgrp, display=display.none)
var color WKbullText = input.color(color.black,    title ="|",        inline = "4", group = WKgrp, display=display.none)
var color WKbearColor = input.color(color.silver,  title = "Bears:",  inline = "4", group = WKgrp, display=display.none)
var color WKbearText = input.color(color.black,    title ="|",        inline = "4", group = WKgrp, display=display.none)

// Define shift/placement for OVN/RTH/Weekly labels
OVNlabelAlign = input.int(2, "OVN Label Align (-: left, +: right)", minval=-100, maxval=100, step=2, display=display.none)
RTHlabelAlign = input.int(8, "RTH Label Align (-: left, +: right)", minval=-100, maxval=100, step=2, display=display.none)
WKlabelAlign = input.int(-8, "Weekly Label Align (-: left, +: right)", minval=-100, maxval=100, step=2, display=display.none)

// Determine which input to use based on the symbol. Allows user to input data for both in 1 chart
var string OVNinput = str.startswith(syminfo.ticker, "MES") ? esOVNinput : str.startswith(syminfo.ticker, "ES") ? esOVNinput : str.startswith(syminfo.ticker, "MNQ") ? nqOVNinput : str.startswith(syminfo.ticker, "NQ") ? nqOVNinput : ""
var string RTHinput = str.startswith(syminfo.ticker, "MES") ? esRTHinput :str.startswith(syminfo.ticker, "ES") ? esRTHinput : str.startswith(syminfo.ticker, "MNQ") ? nqRTHinput : str.startswith(syminfo.ticker, "NQ") ? nqRTHinput : ""
var string WKinput = str.startswith(syminfo.ticker, "MES") ? esWKinput : str.startswith(syminfo.ticker, "ES") ? esWKinput : str.startswith(syminfo.ticker, "MNQ") ? nqWKinput : str.startswith(syminfo.ticker, "NQ") ? nqWKinput : ""

// Define lines and labels' arrays, and variables
var LevelLineArray = array.new_line()
var LevelLabelArray = array.new_label()

// User function to be called to plot levels from parsed values
PlotLineArray(LineArray) =>
    // Plots levels based on last update of current bar or new bar
    if barstate.isconfirmed
        arraySize = array.size(LineArray)
        var line Line = na
        if arraySize > 0
            for i = 0 to arraySize - 1
                // Retrieve the arrays' first elements to plot and then remove from array to free memory
                Line := array.shift(LineArray)

                // Delete the previous plot for clean updates of lines and labels' movement over time
                line.delete(Line)
    array.clear(LineArray)

// User function to be called to plot levels' labels from parsed values
PlotLabelArray(LabelArray) =>
    // Plots levels based on last update of current bar or new bar
    if barstate.isconfirmed  
        arraySize = array.size(LabelArray)
        var label Label = na
        if arraySize > 0
            for i = 0 to arraySize - 1
                // Retrieve the arrays' first elements to plot and then remove from array to free memory
                Label := array.shift(LabelArray)

                // Delete the previous plot for clean updates of lines and labels' movement over time
                label.delete(Label)
    array.clear(LabelArray)

// User functioin to process input lines and the values
ProcessInputs(session, lines, bullColor, bullTextColor, bearColor, bearTextColor, labelAlign) =>
    for line in lines
        // Ignore blank lines
        if line != ""
            // Check if the line contains the primary directional inflection point
            if str.contains(str.lower(line), str.lower("Primary directional inflection point:"))

                var inflectionPoint = array.last(str.split(line, ":"))

                y = str.tonumber(inflectionPoint)
                // Append each level and label to respective arrays for later plotting
                array.push(LevelLineArray, line.new(bar_index, y, bar_index + 1, y, xloc = xloc.bar_index, color = IPcolor, extend=extend.both))
                array.push(LevelLabelArray, label.new(bar_index + labelAlign, y, text = session + " IP " + str.tostring(y), xloc=xloc.bar_index, style=labelAlign>=0?label.style_label_left:label.style_label_right, color = IPcolor, textcolor = color.new(IPtextColor, 0), size = TextSize))

                // Call user functions to plot levels and labels with new data, then empty array to save memory
                PlotLineArray(LevelLineArray)
                PlotLabelArray(LevelLabelArray)

            // Check if the line contains levels for Bulls
            if str.contains(str.lower(line), str.lower("For Bulls:"))
                // Extract and split the resistance levels for Bulls to an array
                var bullText = array.last(str.split(line, ":"))
                var bullValues = str.split(str.replace_all(bullText, " ", ""), ",")

                // Sorts the levels in an order for labeling. ie R1..R4
                array.sort(bullValues, order.ascending)

                i = 0
                for bullValue in bullValues

                    y = str.tonumber(bullValue)
                    // Append each level and label to respective arrays for later plotting
                    array.push(LevelLineArray, line.new(bar_index, y, bar_index + 1, y, xloc = xloc.bar_index, color = bullColor, extend=extend.both))
                    array.push(LevelLabelArray, label.new(bar_index + labelAlign, y, text = session + " R" + str.tostring(i + 1) + " " + str.tostring(y), xloc=xloc.bar_index, style=labelAlign>=0?label.style_label_left:label.style_label_right, color = bullColor, textcolor = color.new(bullTextColor, 0), size = TextSize))

                    // Call user function to plot levels and labels with new data, then empty array to save memory
                    PlotLineArray(LevelLineArray)
                    PlotLabelArray(LevelLabelArray)

                    i := i + 1

            // Check if the line contains support levels for Bears to an array
            if str.contains(str.lower(line), str.lower("For Bears:"))
                // Extract and split the levels for Bears
                var bearText = array.last(str.split(line, ":"))
                var bearValues = str.split(str.replace_all(bearText, " ", ""), ",")

                // Sorts the levels in an order for labeling. ie S1...S4
                array.sort(bearValues, order.descending)

                i = 0
                for bearValue in bearValues

                    y = str.tonumber(bearValue)
                    // Append each level and label to respective arrays for later plotting
                    array.push(LevelLineArray, line.new(bar_index, y, bar_index + 1, y, xloc = xloc.bar_index, color = bearColor, extend=extend.both))
                    array.push(LevelLabelArray, label.new(bar_index + labelAlign, y, text = session + " S" + str.tostring(i + 1) + " " + str.tostring(y), xloc=xloc.bar_index, style=labelAlign>=0?label.style_label_left:label.style_label_right, color = bearColor, textcolor = color.new(bearTextColor, 0), size = TextSize))

                    // Call user function to plot levels and labels with new data, then empty array to save memory
                    PlotLineArray(LevelLineArray)
                    PlotLabelArray(LevelLabelArray)

                    i := i + 1

// Split input into lines
if OVNinput != ""
    var OVNlines = str.split(OVNinput, "\n")
    ProcessInputs("OVN", OVNlines, OVNbullColor, OVNbullText, OVNbearColor, OVNbearText, OVNlabelAlign)

if RTHinput != ""
    var RTHlines = str.split(RTHinput, "\n")
    ProcessInputs("RTH", RTHlines, RTHbullColor, RTHbullText, RTHbearColor, RTHbearText, RTHlabelAlign)

if WKinput != ""
    var WKlines = str.split(WKinput, "\n")
    ProcessInputs("WK", WKlines, WKbullColor, WKbullText, WKbearColor, WKbearText, WKlabelAlign) 
Editor is loading...