Indicator · Pine Script v4 · Own pane

Test-021-Sudo

by ricardosantos79· code credited to RicardoSantos

This indicator is an experimental Sudoku helper that runs in a separate pane.

View on GitHub

GitHub stars261on the repository
Pine versionv4older, still runs
Lines of code154
Settings11inputs
LicenceNone statedfile: MPL-2.0
Last update4 years ago13 Nov 2021

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

154 lines of Pine Script v4 from beta/arrays/test-021-sudo.pinescript, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

test-021-sudo.pinescript
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RicardoSantos

//@version=4
study("test-021-sudo")

//| -------------------------------------------------------------------------------|
var float[] seed = array.new_float(size=1, initial_value=42.0)                  //-|
f_prng(_range)=>                                                              //---|
//| pseudo random function:                                                 // O O |
    var float _return = 1.0                                                 //- V -|
    float _seed = array.get(id=seed, index=0)                                //NNNN|
    float _reseed =  271.828                                                  // A |
    _return := ((3.1416 * _return % _seed) * _reseed) % _range                // V |
    array.set(id=seed, index=0, value=(1 + (_seed * 1.618) % _range) + _return) //||
    _return                                                                    // ||
//| -------------------------------------------------------------------------------|

// C# https://rosettacode.org/wiki/Sudoku
// extra: https://dev.to/christinamcmahon/use-backtracking-algorithm-to-solve-sudoku-270

// public bool checkValidity(int val, int x, int y)
//     {
//         for (int i = 0; i < 9; i++)
//         {
//             if (grid[y * 9 + i] == val || grid[i * 9 + x] == val)
//                 return false;
//         }
        // // int startX = (x / 3) * 3;
        // // int startY = (y / 3) * 3;
        // for (int i = startY; i < startY + 3; i++)
        // {
        //     for (int j = startX; j < startX + 3; j++)
        //     {
        //         if (grid[i * 9 + j] == val)
        //             return false;
        //     }
        // }
    //     return true;
    // }
f_sudoku_check_cell_validity(_T, _value, _x, _y)=>
//| Check if a input value is valid on a Sudoku array(81 elements)
    bool _is_size_ok = array.size(id=_T) == 81
    bool _is_lines_valid = true
    bool _is_box_valid = true
    string _msg = ''
    if _is_size_ok
        for _i = 0 to 8
            bool _is_value_in_row = array.get(id=_T, index=(_y * 9) + _i) == _value
            bool _is_value_in_col = array.get(id=_T, index=(_i * 9) + _x) == _value
            if _is_value_in_row 
                _is_lines_valid := false
                _msg := 'Value ' + tostring(_value, '#') + ' present in row at cell: ' + tostring((_y * 9) + _i, '#')
                break
            if _is_value_in_col
                _is_lines_valid := false
                _msg := 'Value ' + tostring(_value, '#') + ' present in col at cell: ' + tostring((_i * 9) + _x, '#')
                break
        if _is_lines_valid
            int _startX = _x//(_x / 3) * 3
            int _startY = _y//(_y / 3) * 3
            for _i = _startY to _startY + 3
                for _j = _startX to _startX + 3
                    bool _is_value_in_box = array.get(id=_T, index=(_i * 9) + _j) == _value
                    if _is_value_in_box
                        _is_box_valid := false
                        _msg := 'Value ' + tostring(_value, '#') + ' present in box group.'
                        break
    [_is_size_ok and _is_lines_valid and _is_box_valid, _msg]


//  create array to keep track of numbers added and another to store original values.
var int[] S = array.new_int(size=81, initial_value=0)
var int[] C = array.copy(id=S)

//  initial number population inputs
int number_of_initial_population = input(defval=12, minval=0, maxval=80)
int population_seed = input(defval=0)

//  populate the sudoku table
if bar_index == population_seed
    array.set(id=seed, index=0, value=1 + bar_index)
    for _i = 0 to max(0, min(80, number_of_initial_population))
        array.set(id=S, index=int(f_prng(80)), value=int(1+f_prng(8)))
    //C := array.copy(id=S)
    //if uncomment the above line the values on the original array S are reseted to 0

int row_01 = input(defval=100001001, minval=0, maxval=999999999)
int row_02 = input(defval=000000000, minval=0, maxval=999999999)
int row_03 = input(defval=000000000, minval=0, maxval=999999999)
int row_04 = input(defval=000000000, minval=0, maxval=999999999)
int row_05 = input(defval=000000000, minval=0, maxval=999999999)
int row_06 = input(defval=000000000, minval=0, maxval=999999999)
int row_07 = input(defval=000000000, minval=0, maxval=999999999)
int row_08 = input(defval=000000000, minval=0, maxval=999999999)
int row_09 = input(defval=000000000, minval=0, maxval=999999999)

f_row_to_digits(_row)=>
//| Return a array containing each digit as a element from a integer value
//|     (ex: 01025 --> [0,1,0,2,5])
    int[] _R = array.new_int(size=0, initial_value=0)
    int _value = _row
    for _i = 8 to 0
        _value := int((_row / pow(10, _i) % 10))
        array.push(id=_R, value=_value)
    _R

update_row(_T, _n_row, _row)=>
//| Update a row in a array containing Sudoku table
    int[] _R = f_row_to_digits(_row)
    string _msg = ''
    for _i = 8 to 0
        int _value = array.get(id=_R, index=_i)
        [_ok, _m] = f_sudoku_check_cell_validity(S, _value, _i, _n_row)
        if _ok
            array.set(id=_T, index=(_n_row * 9) + _i, value=_value)
            _msg
        else
            _msg := _msg + '\n' + _m
    _msg

    

//plot(close)
string t = ''
for _i = 0 to 80
    t := t + tostring(array.get(id=S, index=_i))
    if _i % 9 == 8 and _i != 80
        t := t + '\n'
        if _i == 26 or _i == 53
            t := t + '\n―――――――――\n'
    else if _i % 3 == 2 and _i != 80
        t := t + ' | '
    else
        t := t

// t_v = input(0), t_x = input(0), t_y = input(0)
// [test, _m] = f_sudoku_check_cell_validity(S, t_v, t_x, t_y)
// plot(test?1:0)

_m =  update_row(S, 0, row_01)
_m := _m + update_row(S, 1, row_02)
_m := _m + update_row(S, 2, row_03)
_m := _m + update_row(S, 3, row_04)
_m := _m + update_row(S, 4, row_05)
_m := _m + update_row(S, 5, row_06)
_m := _m + update_row(S, 6, row_07)
_m := _m + update_row(S, 7, row_08)
_m := _m + update_row(S, 8, row_09)


var label debug = label.new(bar_index, 0.0, '')
label.set_xy(id=debug, x=bar_index, y=0.0)
label.set_text(id=debug, text=t+'\n'+_m)

Overview

Test-021-Sudo stores an 81-cell grid in arrays, seeds a custom pseudo-random function, and checks whether a proposed value already appears in the same row, column, or box. Inputs set the initial population size and seed and supply nine integer row values. The provided code does not show any plot, alert, entry, or exit logic, and the script ends before the population routine is finished.

Details

Published by

ricardosantos79 on GitHub

Code credit

RicardoSantos, as written in the script's header

Type

Indicator, Pine Script v4

Draws

Separate pane below the chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
number_of_initial_populationint12number_of_initial_population
population_seedint0population_seed
row_01int100001001row_01
row_02int000000000row_02
row_03int000000000row_03
row_04int000000000row_04
row_05int000000000row_05
row_06int000000000row_06
row_07int000000000row_07
row_08int000000000row_08
row_09int000000000row_09
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

Asymmetrical RSI

by everget
948 stars

Asymmetrical RSI is an indicator that computes a custom relative strength index from the rate of change of a user-selected source. It plots the resulting ARSI line in a separate pane with color coding that turns green above the overbought level and red below t

Indicator v4 Oscillators & momentum
Updated 10 days ago · GPL-3.0 GitHub repository of Asymmetrical RSI

This indicator calculates momentum setups based on the Relative Strength Index (RSI) and plots in a separate pane. It offers multiple scanning modes such as Continuous Break, Continuous Retracement, Flip Break, Flip Retracement, and both regular and hidden div

Indicator v6 Oscillators & momentumDivergence Repaints
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup - RSI Directional Momentum Indicator
549 stars

Range Volume Change compares volume inside a chosen session window with the same window on the previous day and plots that change as a percentage. It accumulates volume during the window and, when enabled, volume outside it, drawing growing columns in a separa

Indicator v5 VolumeSessions & time Repaints
Updated 10 months ago · None stated GitHub repository of Range Volume Change

Beep Boop

by magic8bot
409 stars

This indicator calculates a modified MACD oscillator and signal line alongside a trend EMA filter, plotting the output as a stylized column histogram in a separate pane. Instead of plotting true MACD distance, it quantifies positive and negative histogram mome

Indicator v4 Oscillators & momentumTrend
Updated 3 years ago · MIT GitHub repository of Beep Boop

Mojito v3 Strategy

by namuan
318 stars

The Mojito v3 Strategy indicator evaluates volatility term structure by calculating the ratio between front-month and second-month VIX futures contracts. It smooths this ratio using a configurable median filter and compares the result against user-defined uppe

Indicator v5 Volatility & bands
Updated 2 months ago · MIT GitHub repository of Mojito v3 Strategy

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers