### Installation Commands Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/guide.md Install the package using npm, pnpm, or JSR. ```bash # npm npm install oakscriptjs # pnpm pnpm add oakscriptjs # JSR npx jsr add oakscriptjs ``` -------------------------------- ### Polylines Example Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/polyline.new.md A complete example demonstrating how to collect user-defined points and render them as a polyline on the chart. ```oakscript //@version=6 indicator("Polylines example", overlay = true) //@variable If `true`, connects all points in the polyline with curved line segments. bool curvedInput = input.bool(false, "Curve Polyline") //@variable If `true`, connects the first point in the polyline to the last point. bool closedInput = input.bool(true, "Close Polyline") //@variable The color of the space filled by the polyline. color fillcolor = input.color(color.new(color.blue, 90), "Fill Color") // Time and price inputs for the polyline's points. p1x = input.time(0,  "p1", confirm = true, inline = "p1") p1y = input.price(0, "  ", confirm = true, inline = "p1") p2x = input.time(0,  "p2", confirm = true, inline = "p2") p2y = input.price(0, "  ", confirm = true, inline = "p2") p3x = input.time(0,  "p3", confirm = true, inline = "p3") p3y = input.price(0, "  ", confirm = true, inline = "p3") p4x = input.time(0,  "p4", confirm = true, inline = "p4") p4y = input.price(0, "  ", confirm = true, inline = "p4") p5x = input.time(0,  "p5", confirm = true, inline = "p5") p5y = input.price(0, "  ", confirm = true, inline = "p5") if barstate.islastconfirmedhistory     //@variable An array of `chart.point` objects for the new polyline.     var points = array.new()     // Push new `chart.point` instances into the `points` array.     points.push(chart.point.from_time(p1x, p1y))     points.push(chart.point.from_time(p2x, p2y))     points.push(chart.point.from_time(p3x, p3y))     points.push(chart.point.from_time(p4x, p4y))     points.push(chart.point.from_time(p5x, p5y))     // Add labels for each `chart.point` in `points`.     l1p1 = label.new(points.get(0), text = "p1", xloc = xloc.bar_time, color = na)     l1p2 = label.new(points.get(1), text = "p2", xloc = xloc.bar_time, color = na)     l2p1 = label.new(points.get(2), text = "p3", xloc = xloc.bar_time, color = na)     l2p2 = label.new(points.get(3), text = "p4", xloc = xloc.bar_time, color = na)     // Create a new polyline that connects each `chart.point` in the `points` array, starting from the first.     polyline.new(points, curved = curvedInput, closed = closedInput, fill_color = fillcolor, xloc = xloc.bar_time) ``` -------------------------------- ### request.quandl() Usage Example Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/request.quandl.md An example demonstrating the usage of the deprecated request.quandl function. ```oakscript //@version=6 indicator("request.quandl") f = request.quandl("CFTC/SB_FO_ALL", barmerge.gaps_off, 0) plot(f) ``` -------------------------------- ### Example usage of request.currency_rate() Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/request.currency_rate.md Demonstrates converting the current symbol's price to British Pounds. ```oakscript //@version=6 indicator("Close in British Pounds") rate = request.currency_rate(syminfo.currency, "GBP") plot(close * rate) ``` -------------------------------- ### Example usage of request.seed() Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/request.seed.md Demonstrates fetching development activity data from a repository and plotting the results. ```pine //@version=6 indicator("BTC Development Activity") [devAct, devActSMA] = request.seed("seed_crypto_santiment", "BTC_DEV_ACTIVITY", [close, ta.sma(close, 10)]) plot(devAct, "BTC Development Activity") plot(devActSMA, "BTC Development Activity SMA10", color = color.yellow) ``` -------------------------------- ### Displaying Analyst Recommendations in a Table Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/variables/syminfo.recommendations_buy.md This example demonstrates how to use syminfo.recommendations_buy and related properties to populate a table with analyst recommendation data. ```OakScript //@version=6 indicator("syminfo recommendations", overlay = true) //@variable A table containing information about analyst recommendations. var table ratings = table.new(position.top_right, 8, 2, frame_color = #000000) if barstate.islastconfirmedhistory     //@variable The time value one year from the date of the last analyst recommendations.     int YTD = syminfo.target_price_date + timeframe.in_seconds("12M") * 1000     // Add header cells.     table.cell(ratings, 0, 0, "Start Date", bgcolor = color.gray, text_color = #000000, text_size = size.large)     table.cell(ratings, 1, 0, "End Date", bgcolor = color.gray, text_color = #000000, text_size = size.large)     table.cell(ratings, 2, 0, "Buy", bgcolor = color.teal, text_color = #000000, text_size = size.large)     table.cell(ratings, 3, 0, "Strong Buy", bgcolor = color.lime, text_color = #000000, text_size = size.large)     table.cell(ratings, 4, 0, "Sell", bgcolor = color.maroon, text_color = #000000, text_size = size.large)     table.cell(ratings, 5, 0, "Strong Sell", bgcolor = color.red, text_color = #000000, text_size = size.large)     table.cell(ratings, 6, 0, "Hold", bgcolor = color.orange, text_color = #000000, text_size = size.large)     table.cell(ratings, 7, 0, "Total", bgcolor = color.silver, text_color = #000000, text_size = size.large)     // Recommendation strings     string startDate         = str.format_time(syminfo.recommendations_date, "yyyy-MM-dd")     string endDate           = str.format_time(YTD, "yyyy-MM-dd")     string buyRatings        = str.tostring(syminfo.recommendations_buy)     string strongBuyRatings  = str.tostring(syminfo.recommendations_buy_strong)     string sellRatings       = str.tostring(syminfo.recommendations_sell)     string strongSellRatings = str.tostring(syminfo.recommendations_sell_strong)     string holdRatings       = str.tostring(syminfo.recommendations_hold)     string totalRatings      = str.tostring(syminfo.recommendations_total)     // Add value cells     table.cell(ratings, 0, 1, startDate, bgcolor = color.gray, text_color = #000000, text_size = size.large)     table.cell(ratings, 1, 1, endDate, bgcolor = color.gray, text_color = #000000, text_size = size.large)     table.cell(ratings, 2, 1, buyRatings, bgcolor = color.teal, text_color = #000000, text_size = size.large)     table.cell(ratings, 3, 1, strongBuyRatings, bgcolor = color.lime, text_color = #000000, text_size = size.large)     table.cell(ratings, 4, 1, sellRatings, bgcolor = color.maroon, text_color = #000000, text_size = size.large) ``` -------------------------------- ### Calculate Matrix Trace Example Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/matrix.trace.md Demonstrates creating a 2x2 matrix, populating it with values, and calculating its trace using matrix.trace(). ```oakscript //@version=6 indicator("`matrix.trace()` Example") // For efficiency, execute this code only once. if barstate.islastconfirmedhistory     // Create a 2x2 matrix.     var m1 = matrix.new(2, 2, na)     // Fill the matrix with values.     matrix.set(m1, 0, 0, 1)     matrix.set(m1, 0, 1, 2)     matrix.set(m1, 1, 0, 3)     matrix.set(m1, 1, 1, 4)     // Get the trace of the matrix.     tr = matrix.trace(m1)     // Display matrix elements.     var t = table.new(position.top_right, 2, 2, color.green)     table.cell(t, 0, 0, "Matrix elements:")     table.cell(t, 0, 1, str.tostring(m1))     table.cell(t, 1, 0, "Trace of the matrix:")     table.cell(t, 1, 1, str.tostring(tr)) ``` -------------------------------- ### Example usage of var keyword Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/keywords/var.md Demonstrates how variables initialized with var maintain state across bar updates in a script. ```OakScript //@version=6 indicator("Var keyword example") var a = close var b = 0.0 var c = 0.0 var green_bars_count = 0 if close > open     var x = close     b := x     green_bars_count := green_bars_count + 1     if green_bars_count >= 10         var y = close         c := y plot(a) plot(b) plot(c) ``` -------------------------------- ### request.earnings() Usage Example Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/request.earnings.md Demonstrates basic and advanced usage of request.earnings() with different merge strategies. ```oakscript //@version=6 indicator("request.earnings") s1 = request.earnings("NASDAQ:BELFA") plot(s1) s2 = request.earnings("NASDAQ:BELFA", earnings.actual, gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_on) plot(s2) ``` -------------------------------- ### Oakscript Input Example Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/input.md This example demonstrates how to use the input() function to create various types of inputs, including boolean, integer, float, color, and string inputs, and then utilize them in a script. ```oakscript //@version=6 indicator("input",overlay=true) i_switch=input(true,"On/Off") plot(i_switch?open:na) i_len=input(7,"Length") i_src=input(close,"Source") plot(ta.sma(i_src,i_len)) i_border=input(142.50,"PriceBorder") hline(i_border) bgcolor(close>i_border?color.green:color.red) i_col=input(color.red,"PlotColor") plot(close,color=i_col) i_text=input("Hello!","Message") l=label.new(bar_index,high,text=i_text) label.delete(l[1]) ``` -------------------------------- ### matrix.reverse() Usage Example Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/matrix.reverse.md Demonstrates creating a matrix, copying it, and reversing the copy to display both states in a table. ```oakscript //@version=6 indicator("`matrix.reverse()` Example") // For efficiency, execute this code only once. if barstate.islastconfirmedhistory     // Copy the matrix to a new one.     var m1 = matrix.new(2, 2, na)     // Fill the matrix with values.     matrix.set(m1, 0, 0, 1)     matrix.set(m1, 0, 1, 2)     matrix.set(m1, 1, 0, 3)     matrix.set(m1, 1, 1, 4)     // Copy matrix elements to a new matrix.     var m2 = matrix.copy(m1)     // Reverse the `m2` copy of the original matrix.     matrix.reverse(m2)     // Display using a table.     var t = table.new(position.top_right, 2, 2, color.green)     table.cell(t, 0, 0, "Original matrix:")     table.cell(t, 0, 1, str.tostring(m1))     table.cell(t, 1, 0, "Reversed matrix:")     table.cell(t, 1, 1, str.tostring(m2)) ``` -------------------------------- ### request.security_lower_tf() Usage Example Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/request.security_lower_tf.md Demonstrates retrieving closing prices from a 60-minute timeframe on a higher timeframe chart and displaying them in a label. ```pine //@version=6 indicator("`request.security_lower_tf()` Example", overlay = true) // If the current chart timeframe is set to 120 minutes, then the `arrayClose` array will contain two 'close' values from the 60 minute timeframe for each bar. arrClose = request.security_lower_tf(syminfo.tickerid, "60", close) if bar_index == last_bar_index - 1     label.new(bar_index, high, str.tostring(arrClose)) ``` -------------------------------- ### Example usage of table.cell_set_text Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/table.cell_set_text.md Demonstrates initializing a table and updating the text of a specific cell. ```oakscript //@version=6 indicator("TABLE example") var tLog = table.new(position = position.top_left, rows = 1, columns = 2, bgcolor = color.yellow, border_width=1) table.cell(tLog, row = 0, column = 0, text = "sometext", text_color = color.blue) table.cell_set_text(tLog, row = 0, column = 0, text = "sometext") ``` -------------------------------- ### Request Financial Data Example Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/request.financial.md Demonstrates how to retrieve the annual accounts payable for Microsoft and plot the resulting series. ```OakScript //@version=6 indicator("request.financial") f = request.financial("NASDAQ:MSFT", "ACCOUNTS_PAYABLE", "FY") plot(f) ``` -------------------------------- ### Retrieve line start coordinate with line.get_x1() Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/line.get_x1.md Demonstrates creating a line object and retrieving its starting x-coordinate using line.get_x1(). ```pinescript //@version=6 indicator("line.get_x1") my_line = line.new(time, open, time + 60 * 60 * 24, close, xloc=xloc.bar_time) a = line.get_x1(my_line) plot(time - line.get_x1(my_line)) //draws zero plot ``` -------------------------------- ### Close All Positions Example Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/strategy.close_all.md Demonstrates using strategy.close_all() to exit positions every 500 bars in a multi-entry strategy. ```pine //@version=6 strategy("Multi-entry close strategy") // Calculate a 14-bar and 28-bar moving average of `close` prices. float sma14 = ta.sma(close, 14) float sma28 = ta.sma(close, 28) // Place a market order to enter a long trade every time `sma14` crosses over `sma28`. if ta.crossover(sma14, sma28)     strategy.order("My Long Entry ID " + str.tostring(strategy.opentrades), strategy.long) // Place a market order to close the entire position every 500 bars. if bar_index % 500 == 0     strategy.close_all() // Plot the position size. plot(strategy.position_size) ``` -------------------------------- ### Delete All Lines with line.all Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/variables/line.all.md This example demonstrates how to retrieve all existing lines using line.all and then delete them. Ensure that the script version is compatible. ```javascript //@version=6 indicator("line.all") //delete all lines line.new(bar_index - 10, close, bar_index, close) a_allLines = line.all if array.size(a_allLines) > 0     for i = 0 to array.size(a_allLines) - 1         line.delete(array.get(a_allLines, i)) ``` -------------------------------- ### Example Usage of plotcandle() Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/plotcandle.md A basic implementation showing how to plot candles with conditional coloring based on price movement. ```oakscript //@version=6 indicator("plotcandle example", overlay=true) plotcandle(open, high, low, close, title='Title', color = open < close ? color.green : color.red, wickcolor=color.black) ``` -------------------------------- ### Define a symbol input Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/input.symbol.md Example demonstrating how to add a symbol input field and use the selected value to request security data. ```OakScript //@version=6 indicator("input.symbol", overlay=true) i_sym = input.symbol("DELL", "Symbol") s = request.security(i_sym, 'D', close) plot(s) ``` -------------------------------- ### barcolor() Usage Example Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/barcolor.md A basic implementation of barcolor() that colors bars based on whether the close price is lower than the open price. ```oakscript //@version=6 indicator("barcolor example", overlay=true) barcolor(close < open ? color.black : color.white) ``` -------------------------------- ### Example Usage of plotbar Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/plotbar.md A basic implementation showing how to plot bars with conditional coloring based on price movement. ```oakscript //@version=6 indicator("plotbar example", overlay=true) plotbar(open, high, low, close, title='Title', color = open < close ? color.green : color.red) ``` -------------------------------- ### Initialize a new map in OakScript Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/map.newtypetype.md Creates a map with string keys and integer values, then demonstrates basic put and get operations. ```oakscript //@version=6 indicator("map.new example") a = map.new() a.put("example", 1) label.new(bar_index, close, str.tostring(a.get("example"))) ``` -------------------------------- ### Extract Substring using str.substring() Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/str.substring.md Use str.substring() to get a part of a string. The start position is inclusive, and the end position is exclusive. If end_pos is omitted, it defaults to the string's length. Indexing starts at 0. ```javascript //@version=6 indicator("str.substring", overlay = true) sym= input.symbol("NASDAQ:AAPL") pos = str.pos(sym, ":") // Get position of ":" character  tk r=  str.substring(sym, pos+1) // "AAPL" if  barstate.islastconfirmedhistory           label.new(bar_index, high, text = tkr) ``` -------------------------------- ### Get Array Element by Index in Oakscript Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/array.get.md Use array.get() to retrieve an element from an array at a specific index. Indices can be positive (from start) or negative (from end). ```javascript //@version=6 indicator("array.get example") a = array.new_float(0) for i = 0 to 9     array.push(a, close[i] - open[i]) plot(array.get(a, 9)) ``` -------------------------------- ### Basic 'for' Loop Example Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/keywords/for.md Calculates the number of bars where the close price is above the current close price within a specified length. Requires no special setup. ```oakscript //@version=6 indicator("Basic `for` loop") //@function Calculates the number of bars in the last `length` bars that have their `close` above the current `close`. //@param length The number of bars used in the calculation. greaterCloseCount(length) =>     int result = 0     for i = 1 to length         if close[i] > close             result += 1     result plot(greaterCloseCount(14)) ``` -------------------------------- ### Strategy Entry and Exit Example Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/strategy.md Demonstrates a basic strategy with an entry condition and an exit bracket order. Ensure the strategy function is called once per script. ```javascript //@version=6 strategy("My strategy", overlay = true) // Enter long by market if current open is greater than previous high. if open > high[1] strategy.entry("Long", strategy.long, 1) // Generate a full exit bracket (profit 10 points, loss 5 points per contract) from the entry named "Long". strategy.exit("Exit", "Long", profit = 10, loss = 5) ``` -------------------------------- ### Define Strategy with strategy.cash Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/constants/strategy.cash.md This example demonstrates how to declare a strategy using `strategy.cash` for the `default_qty_type`. It shows how trades are entered using a default quantity of cash when 'qty' is not specified in `strategy.entry`. ```javascript //@version=6 strategy("strategy.cash", overlay = true, default_qty_value = 50, default_qty_type = strategy.cash, initial_capital = 1000000) if bar_index == 0 // As ‘qty’ is not defined, the previously defined values for the `default_qty_type` and `default_qty_value` parameters are used to enter trades, namely 50 units of cash in the currency of `strategy.account_currency`. // `qty` is calculated as (default_qty_value)/(close price). If current price is $5, then qty = 50/5 = 10. strategy.entry("EN", strategy.long) if bar_index == 2 strategy.close("EN") ``` -------------------------------- ### Get Array Size with array.size() Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/array.size.md Use array.size() to retrieve the number of elements in an array. This example demonstrates its use by plotting the size of both an original array and a modified slice of it. ```javascript //@version=6 indicator("array.size example") a = array.new_float(0) for i = 0 to 9     array.push(a, close[i]) // note that changes in slice also modify original array slice = array.slice(a, 0, 5) array.push(slice, open) // size was changed in slice and in original array plot(array.size(a)) plot(array.size(slice)) ``` -------------------------------- ### Get Open Trade Entry ID Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/strategy.opentrades.entry_id.md This example demonstrates how to use strategy.opentrades.entry_id() to display the ID of the last opened position. It requires the strategy to have entered at least one trade. ```pinescript //@version=6 strategy("`strategy.opentrades.entry_id` Example", overlay = true) // We enter a long position when 14 period sma crosses over 28 period sma. // We enter a short position when 14 period sma crosses under 28 period sma. longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28)) shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28)) // Strategy calls to enter a long or short position when the corresponding condition is met. if longCondition     strategy.entry("Long entry at bar #" + str.tostring(bar_index), strategy.long) if shortCondition     strategy.entry("Short entry at bar #" + str.tostring(bar_index), strategy.short) // Display ID of the latest open position. if barstate.islastconfirmedhistory     label.new(bar_index, high + (2 * ta.tr), "Last opened position is \n " + strategy.opentrades.entry_id(strategy.opentrades - 1)) ``` -------------------------------- ### Get the 3rd Highest Value in an Array Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/array.max.md This example demonstrates how to find the third highest value in an array using array.max(). Ensure the array is initialized before use. The 'nth' argument is zero-indexed. ```javascript //@version=6 indicator("array.max") a = array.from(5, -2, 0, 9, 1) thirdHighest = array.max(a, 2) // 1 plot(thirdHighest) ``` -------------------------------- ### Detect New Day with timeframe.change() Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/timeframe.change.md Use timeframe.change("1D") to detect the start of a new day. This example highlights the first bar of each new day with a semi-transparent green background. ```javascript //@version=6 // Run this script on an intraday chart. indicator("New day started", overlay = true) // Highlights the first bar of the new day. isNewDay = timeframe.change("1D") bgcolor(isNewDay ? color.new(color.green, 80) : na) ``` -------------------------------- ### Retrieve Open Trade Entry Comment - OakscriptJS Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/strategy.opentrades.entry_comment.md Use strategy.opentrades.entry_comment() to get the comment of an open trade. The trade number is zero-indexed. This example demonstrates its usage within a strategy script to display trade statistics. ```javascript //@version=6 strategy("`strategy.opentrades.entry_comment()` Example", overlay = true) stopPrice = open * 1.01 longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28)) if (longCondition)     strategy.entry("Long", strategy.long, stop = stopPrice, comment = str.tostring(stopPrice, "#.####")) var testTable = table.new(position.top_right, 1, 3, color.orange, border_width = 1) if barstate.islastconfirmedhistory or barstate.isrealtime     table.cell(testTable, 0, 0, 'Last entry stats')     table.cell(testTable, 0, 1, "Order stop price value: " + strategy.opentrades.entry_comment(strategy.opentrades - 1))     table.cell(testTable, 0, 2, "Actual Entry Price: " + str.tostring(strategy.opentrades.entry_price(strategy.opentrades - 1))) ``` -------------------------------- ### Get Matrix Element - OakscriptJS Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/matrix.get.md Use this snippet to retrieve a specific element from a matrix using its ID, row, and column index. Ensure that the row and column indices are within the matrix bounds, as indexing starts at zero. ```javascript //@version=6 indicator("`matrix.get()` Example", "", true) // Create a 2x3 "float" matrix from the `hl2` values. m = matrix.new(2, 3, hl2) // Return the value of the element at index [0, 0] of matrix `m`. x = matrix.get(m, 0, 0) plot(x) ``` -------------------------------- ### Extracting a Submatrix with matrix.submatrix() Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/matrix.submatrix.md This example demonstrates how to create a matrix, fill it with values, and then extract a submatrix using matrix.submatrix(). The extracted submatrix and the original matrix are then displayed using a table. ```javascript //@version=6 indicator("`matrix.submatrix()` Example") // For efficiency, execute this code only once. if barstate.islastconfirmedhistory     // Create a 2x3 matrix matrix with values `0`.     var m1 = matrix.new(2, 3, 0)     // Fill the matrix with values.     matrix.set(m1, 0, 0, 1)     matrix.set(m1, 0, 1, 2)     matrix.set(m1, 0, 2, 3)     matrix.set(m1, 1, 0, 4)     matrix.set(m1, 1, 1, 5)     matrix.set(m1, 1, 2, 6)     // Create a 2x2 submatrix of the `m1` matrix.     var m2 = matrix.submatrix(m1, 0, 2, 1, 3)     // Display using a table.     var t = table.new(position.top_right, 2, 2, color.green)     table.cell(t, 0, 0, "Original Matrix:")     table.cell(t, 0, 1, str.tostring(m1))     table.cell(t, 1, 0, "Submatrix:")     table.cell(t, 1, 1, str.tostring(m2)) ``` -------------------------------- ### Create a New Label with Specific Properties Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/label.new.md This example demonstrates creating a new label using label.new() with a circle style, setting its position, text, color, and size. It also shows how to modify the label's properties after creation using label.set_x(), label.set_xloc(), label.set_color(), and label.set_size(). ```javascript //@version=6 indicator("label.new") var label1 = label.new(bar_index, low, text="Hello, world!", style=label.style_circle) label.set_x(label1, 0) label.set_xloc(label1, time, xloc.bar_time) label.set_color(label1, color.red) label.set_size(label1, size.large) ``` -------------------------------- ### Basic varip Initialization Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/keywords/varip.md Demonstrates the basic usage of 'varip' for initializing an integer variable that increments on each bar. This variable will retain its value across script executions on the same bar. ```oakscript //@version=6 indicator("varip") varip int v = -1 v := v + 1 plot(v) ``` -------------------------------- ### time_close() Usage Example Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/time_close.md Example demonstrating the use of time_close() to highlight a specific session on the chart. ```pine //@version=6 indicator("Time", overlay=true) t1 = time_close(timeframe.period, "1200-1300", "America/New_York") bgcolor(not na(t1) ? color.new(color.blue, 90) : na) ``` -------------------------------- ### Basic str.format() Usage Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/str.format.md Demonstrates basic usage of str.format() to include dynamic values like bar index and close price in a label. Ensure the format string correctly maps placeholders to arguments. ```javascript //@version=6 indicator("Simple `str.format()` demo") //@variable A formatted string that includes representations of the current `bar_index` and `close` values. //          The placeholder `{0}` refers to the first argument after the formatting string (`bar_index`), and  //          `{1}` refers to the second (`close`). string labelText = str.format("Current bar index: {0}\nCurrent bar close: {1}", bar_index, close) // Draw a label to display the `labelText` string at the current bar's `high` price.  label.new(bar_index, high, labelText) ``` -------------------------------- ### Swap Columns in a Matrix Copy Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/matrix.swap_columns.md This example demonstrates how to create a matrix, copy it, and then swap two columns in the copied matrix using `matrix.swap_columns()`. The original and modified matrices are then displayed in a table. ```javascript //@version=6 indicator("`matrix.swap_columns()` Example") // For efficiency, execute this code only once. if barstate.islastconfirmedhistory     // Create a 2x2 matrix with ‘na’ values.     var m1 = matrix.new(2, 2, na)     // Fill the matrix with values.     matrix.set(m1, 0, 0, 1)     matrix.set(m1, 0, 1, 2)     matrix.set(m1, 1, 0, 3)     matrix.set(m1, 1, 1, 4)     // Copy the matrix to a new one.     var m2 = matrix.copy(m1)     // Swap the first and second columns of the matrix copy.     matrix.swap_columns(m2, 0, 1)     // Display using a table.     var t = table.new(position.top_right, 2, 2, color.green)     table.cell(t, 0, 0, "Original matrix:")     table.cell(t, 0, 1, str.tostring(m1))     table.cell(t, 1, 0, "Swapped columns in copy:")     table.cell(t, 1, 1, str.tostring(m2)) ``` -------------------------------- ### Identify Pivot Highs Example Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/ta.pivothigh.md Example usage of ta.pivothigh() to plot pivot high points on a chart. ```pine //@version=6 indicator("PivotHigh", overlay=true) leftBars = input(2) rightBars=input(2) ph = ta.pivothigh(leftBars, rightBars) plot(ph, style=plot.style_cross, linewidth=3, color= color.red, offset=-rightBars) ``` -------------------------------- ### Weekly Pivots Indicator Example Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/ta.pivot_point_levels.md Example usage of ta.pivot_point_levels to draw weekly pivot lines on a chart. ```oakscript //@version=6 indicator("Weekly Pivots", max_lines_count=500, overlay=true) timeframe = "1W" typeInput = input.string("Traditional", "Type", options=["Traditional", "Fibonacci", "Woodie", "Classic", "DM", "Camarilla"]) weekChange = timeframe.change(timeframe) pivotPointsArray = ta.pivot_point_levels(typeInput, weekChange) if weekChange     for pivotLevel in pivotPointsArray         line.new(time, pivotLevel, time + timeframe.in_seconds(timeframe) * 1000, pivotLevel, xloc=xloc.bar_time) ``` -------------------------------- ### Install oakscriptjs Source: https://github.com/deepentropy/oakscriptjs/blob/main/README.md Install the oakscriptjs library using npm. This command is used to add the package to your project's dependencies. ```bash npm install oakscriptjs ``` -------------------------------- ### Insert value at array start using array.unshift() Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/array.unshift.md Demonstrates initializing a float array and prepending the current open price to the beginning of the array. ```OakScript //@version=6 indicator("array.unshift example") a = array.new_float(5, 0) array.unshift(a, open) plot(array.get(a, 0)) ``` -------------------------------- ### Retrieve line start price with line.get_y1() Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/line.get_y1.md Use this function to access the y-coordinate (price) of the starting point of a line object. ```text line.get_y1(id) → series float ``` -------------------------------- ### Using session.isfirstbar_regular in a strategy Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/variables/session.isfirstbar_regular.md Demonstrates using session.isfirstbar and session.islastbar_regular to manage strategy entries and exits. ```OakScript //@version=6 strategy("`session.isfirstbar_regular` Example", overlay = true) longCondition = year >= 2022 // Place a long order at the `close` of the trading session's first bar. if session.isfirstbar and longCondition     strategy.entry("Long", strategy.long) // Close the long position at the `close` of the trading session's last bar. if session.islastbar_regular and barstate.isconfirmed     strategy.close("Long", immediately = true) ``` -------------------------------- ### Cancel All Orders Demo Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/strategy.cancel_all.md This example demonstrates how to use strategy.cancel_all() to cancel all pending limit orders when a specific condition is met. Ensure orders are placed using strategy.entry() with limit prices. ```javascript //@version=6 strategy(title = "Cancel all orders demo") conditionForBuy1 = open > high[1] if conditionForBuy1     strategy.entry("Long entry 1", strategy.long, 1, limit = low) // Enter long using a limit order if `conditionForBuy1` is `true`.  conditionForBuy2 = conditionForBuy1 and open[1] > high[2] float lowest2 = ta.lowest(low, 2)  if conditionForBuy2     strategy.entry("Long entry 2", strategy.long, 1, limit = lowest2) // Enter long using a limit order if `conditionForBuy2` is `true`.  conditionForStopTrading = open < lowest2 if conditionForStopTrading     strategy.cancel_all() // Cancel both limit orders if `conditionForStopTrading` is `true`. ``` -------------------------------- ### Get All Values from a Map Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/map.values.md Use map.values() to get an array of all values from a map. The returned array is a copy and does not affect the original map. Elements are in insertion order. ```javascript //@version=6 indicator("map.values example") a = map.new() a.put("open", open) a.put("high", high) a.put("low", low) a.put("close", close) values = map.values(a) ohlc = 0.0 for value in values     ohlc += value plot(ohlc/4) ``` -------------------------------- ### Get Open Trade Profit Percentage Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/strategy.opentrades.profit_percent.md Use this function to get the profit/loss percentage of an open trade. Specify the trade number as an argument. The first trade is numbered zero. ```javascript strategy.opentrades.profit_percent(trade_num) ``` -------------------------------- ### Create an array using array.from() Source: https://github.com/deepentropy/oakscriptjs/blob/main/docs/official/language-reference/functions/array.from.md Demonstrates initializing an array of strings using the array.from() function. ```oakscript //@version=6 indicator("array.from_example", overlay = false) arr = array.from("Hello", "World!") // arr (array) will contain 2 elements: {Hello}, {World!}. plot(close) ```