// © pAulseperformance // Rev 98 // 1. Script Structure: { Must Begin with `//@version` directive, //@version=5 // - organize with `indicator()`, `strategy()`, or `library()` declarations, and include an output statement. indicator("[pAulseperformance][PracticalPine][Crash] Syntax, Structure, Stuff", overlay = true, max_labels_count = 500) // } // 2. Variable Declaration: { Pine Script is dynamically typed; assign values without explicit type declarations. // - Pay attention to variable types to avoid errors. // - You can use proper variable type declarations (`var`, `int`, `float`, `bool`, `string`) // to enhance code clarity and maintainability, but they are not needed. // There are 10 Types and unlimited User Defined Types we will explore later. // Fundamental types: “int”, “float”, “bool”, “color” and “string” // The following special types: “line”, “linefill”, “label”, “box”, “table” // User-defined types (UDTs) // - Variables must be unique and are CaseSensitive. They can contain numbers, but cannot start with a number. // } // 3. Plotting: { Use `plot()` to visualize indicator values on the chart, customize with options like color and line style. // Comments (hotkeys): Utilize comments (`//`) and hotkeys for faster code documentation and uncommenting. (Win: ctrl + /) (Mac: CMD + /) // myVar = ta.rsi(close, 14) // mybool = true // plot(mybool ? 1 : 0, 'myVar') // plot(bar_index, 'Bar Index') // plot(firstIndex, 'First Index') // plot(myInput, 'MyInput') // plot(myVar + MyVar, 'Myvar +', style = plot.style_histogram, color = color.purple) // } // 4. Line Wrapping: { Wrap long lines for better readability by indenting each line. // myVar = 1 // MyVar = 1 // plot(series = myVar + MyVar, // title = 'Myvar +', // color = color.purple, // style = plot.style_histogram, // trackprice = false, // offset = 0 // ) // } // 5. Operators: { Utilize arithmetic, comparison, and logical operators for expressions. // a = 1 // b = 2. // c = a / b //>= a and not a == b or a != 1 // d = (a > 0) and (b <= 5) // plot(d ? 1 : 0, 'd') // } // 6. Conditional Statements: { Execute code based on conditions using `if else`, `?:`, and `switch`. // var d = 0 // d := close > open ? d + 1 : close == open ? 0 : d - 1 // if close > open // d += 1 // else if close == open // d := 0 // else // d -= 1 // plot(d, 'd') // --> Syntax Highlighting: Identify syntax errors and improve code readability with color-coded keywords, functions, and variables. // Can plot bools using these // plotshape(d) // plotchar(d) // Or Use Ternary Operator `?:` // e = false // plot(e ?1 : 0) // Using Switch // i_type = input.string('SMA', 'MA Type', ['SMA', 'EMA','WMA']) // i_src = input.source(close, 'Ma source') // i_length = input.int(20, 'MA Length') // ma = switch i_type // 'SMA' => ta.sma(i_src, i_length) // 'EMA' => 10.0 // 'WMA' => 123 // => na // plot(ma, 'MA') // } // 7. Loops and Arrays: { Iterate with `for` and `while` loops, store collections in arrays, and work with tabular data using matrices. // myVar = 0 // for i=0 to 10 // myVar := i * 2 // // plot(i) // Syntax Error - Cannot use plot functions in local scope // // Label Debugging: Plot labels on the chart to debug code regardless of the scope. // // label.new(bar_index[i], close[i], text=str.tostring(myVar)) // // plot(myVar) // // Arrays { // // Creating Arrays // A1 = array.from(1,2,3,4,5) // A2 = array.new_float(na) // A2.push(1) // A2.push(2) // A2.push(3) // A2.push(4) // A2.push(5) // // Plotting Arrays // if barstate.islastconfirmedhistory // label.new(bar_index, close, str.tostring(A1)) // plot(A2.avg()) // plot(A2.variance()) // plot(A2.stdev()) // // Accessing Arrays // // plot(A2.get(6), 'Array Index 0') // // plot(A2.get(1), 'Array Index 1') // // plot(A2.get(2), 'Array Index 2') // // plot(A2.get(3), 'Array Index 3') // // plot(A2.get(4), 'Array Index 4') // // Looping through an array // // for element in A2 // // label.new(bar_index, close, text=str.tostring(element)) // for i= 0 to A2.size() - 1 // label.new(bar_index, close, str.tostring(A2.get(i))) // // if barstate.islastconfirmedhistory // // label.new(bar_index, high, str.tostring(A2)) // // // } // // // Matrices { // // Create a 2x3 (2 rows x 3 columns) "float" matrix with values zero. // var m = matrix.new(2, 3, close) // // Display the contents of the Matrix // if barstate.islastconfirmedhistory // label.new(bar_index, high, str.tostring(m)) // // Looping Through a Matrix and displaying each value // for arrayElement in m // for [index,element] in arrayElement // label.new(bar_index[index], high[index], str.tostring(element)) // // } // // } // // 8. Functions, Objects, and Methods: { Define custom functions, create objects with properties and methods, and invoke object-specific methods. // // Functions { // // - Good for reusable Code // // - Variables inside are locally owned, meaning they cannot be called outside of the function // // - Its possible to use variables from outside the function, as long as they are defined BEFORE the function // // However, this is bad practice and should be avoided. // // Example 1: Simple { // myFunction() => // _a = 1 // _b = 2 // _a + _b // // - Functions return the last line in the function // functionResult = myFunction() // plot(functionResult, 'Function Result 1') // myFunction() // myFunction() // myFunction() // // } End Example 1 // Example 2: Parameters { // myCoolFunction(_param1, _param2) => // _param1 * _param2 // plot(myCoolFunction(input(20), input(10)), 'Function Result 2') // } End Example 2 // // Example 3: Cannot Modify Globals { var globalVar = 0 // count() => // globalVar += 1 // instead count() => globalVar + 1 globalVar := count() plot(globalVar) // // } End Example 3 // // } // Objects { // fun(open, close, high, low, true, ) // Initialize myBar = myBar.new() plot(myBar.C, 'Object Close') // passAnObjectToAFunction(myBar _bar) => // _bar.isUp := _bar.C > _bar.O // myBar // myBar := passAnObjectToAFunction(myBar) // getAnObjectFromAFunction()=> // _bar = myBar.new() // // } // // Methods { // // Example 1: Too Simple // method myCountingMethod(float _a, float _b) => // _a + _b // aFloat = 0.0 // aFloat.myCountingMethod(1) // Doesn't work that well as it needs to be reassigned // plot(aFloat, 'aFloat') // // } End Example 1 // // Example 2: Working { // type types // int INT // float FLOAT // bool BOOL // color COLOR // string STRING // line LINE // linefill LINEFILL // label LABEL // box BOX // table TABLE // method showOnChart(types this) => // var _message = '' // var _myLabel = label.new(time, high, na, xloc.bar_time, text_font_family = font.family_monospace) // if this.INT or this.FLOAT // _message := this.INT ? str.tostring(this.INT) : str.tostring(this.FLOAT) // else if this.BOOL // _message := this.BOOL ? 'True' : 'False' // else if not na(this.COLOR) // _myLabel.set_color(this.COLOR) // _message := "it's a color" // else if not na(this.STRING) // _message := this.STRING // else // _message := "It's a Line, Linefill, label, Box, or Table idk 🤷🏻‍♂️" // oneTime = time - time[1] // if barstate.islastconfirmedhistory // // _myLabel.setx (time + (10*oneTime), high, str.tostring(this), xloc=xloc.bar_time) // _myLabel.set_xy(time + (10*oneTime), high) // _myLabel.set_text(_message) // someRandomVariableYouWantToCheck = close+open/ta.rsi(close, 12) // itsAFloat = types.new(FLOAT = someRandomVariableYouWantToCheck) // // showOnChart(itsAFloat) // itsAFloat.showOnChart() // // Example 3: Overloading { // method show(int _a) => // if barstate.islastconfirmedhistory // var _myLabel = label.new(time, high, str.tostring(_a), xloc.bar_time, text_font_family = font.family_monospace) // method show(float _a) => // if barstate.islastconfirmedhistory // var _myLabel = label.new(time, high, str.tostring(_a), xloc.bar_time, text_font_family = font.family_monospace) // method show(bool _a) => // if barstate.islastconfirmedhistory // var _myLabel = label.new(time, high, str.tostring(_a), xloc.bar_time, text_font_family = font.family_monospace) // method show(color _a) => // if barstate.islastconfirmedhistory // var _myLabel = label.new(time, high, 'Its a color', xloc.bar_time, text_font_family = font.family_monospace, color = _a) // method show(string _a) => // if barstate.islastconfirmedhistory // var _myLabel = label.new(time, high, _a, xloc.bar_time, text_font_family = font.family_monospace) // method show(array _a) => // if barstate.islastconfirmedhistory // var _myLabel = label.new(time, high, str.tostring(_a), xloc.bar_time, text_font_family = font.family_monospace) // method show(array _a) => // if barstate.islastconfirmedhistory // var _myLabel = label.new(time, high, str.tostring(_a), xloc.bar_time, text_font_family = font.family_monospace) // method show(array _a) => // if barstate.islastconfirmedhistory // var _myLabel = label.new(time, high, str.tostring(_a), xloc.bar_time, text_font_family = font.family_monospace) // randomVar = array.from('hello', 'world') // randomVar.show() // } // } // } // 9. Option Key Power // - Multiple Cursors // - Quick Navigaton // - Line Shifting and duplication // 11. Plot Debugging (Data Window): { Visualize variables and values using `plot()` and view them in the Data Window. // } // 12. `runtime.error` Debugging: { Display custom error messages using `runtime.error` to troubleshoot complex code. // } // Debug Resources { // [Conquer Debugging with These Links] // - [Debugging](https://www.tradingview.com/pine-script-docs/en/v5/writing/Debugging.html) // - [Common Error Messages](https://www.tradingview.com/pine-script-docs/en/v5/Error_messages.html#error-messages) // - [Pine Coders](https://www.pinecoders.com/faq_and_code/#debugging) // }