Backtest Strategy Optimizer Adapter
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © DinGrogu

//@version=5

library('backtest')

// ############################################################
// # FUNCTIONS
// ############################################################

f_backtest_table_margin(int margin) =>
    margin_string = ''
    for i = 1 to margin
        margin_string := margin_string + ' '
    margin_string

// @function - Get the take profit price (only used externally).
f_take_profit_price(_entry_price, _take_profit_percentage, _atr_length=14) =>
    atr_enabled = _atr_length > 0
    atr = atr_enabled ? ta.sma(ta.tr, _atr_length) : 0
    _take_profit_percentage > 0 ? atr_enabled ? _entry_price + (atr * _take_profit_percentage) : _entry_price + (_entry_price * (_take_profit_percentage / 100)) : na

// @function - Get the stop loss price (only used externally).
f_stop_loss_price(_entry_price, _stop_loss_percentage, _atr_length=14) =>
    atr_enabled = _atr_length > 0
    atr = atr_enabled ? ta.sma(ta.tr, _atr_length) : 0
    _stop_loss_percentage > 0 ? atr_enabled ? _entry_price - (atr * _stop_loss_percentage) : _entry_price - (_entry_price * (_stop_loss_percentage / 100)) : na

// @function - Backtesting statistics table
f_backtest_table(_initial_capital, _profit_and_loss, _open_balance, _winrate, _entries, _exits, _wins, _losses, _table_position='RIGHT', _table_margin=15, _table_transparency=20, _table_cell_color=color.purple, _table_title_cell_color=color.gray, _table_text_color=color.white) =>
    var green = #26A69A
    var red = #FF5252

    var table_width = 0
    var table_margin_right = _table_margin > 0 ? str.tostring(f_backtest_table_margin(_table_margin)) : ''
    var table_position = _table_position == 'LEFT' ? position.bottom_left : position.bottom_right

    var table_titles = _table_position == 'LEFT' ? 1 : 0
    var table_values = _table_position == 'LEFT' ? 2 : 1
    var table_offset = _table_position == 'LEFT' ? 0 : 2

    backtesting_table = table.new(table_position, columns=3, rows=7, border_width=3)
    profit_color = _open_balance > _initial_capital ? green : red

    table.cell(backtesting_table, table_titles, 0, text='    Initial Balance    ', bgcolor=_table_title_cell_color, text_color=_table_text_color,  text_halign=text.align_left, text_size=size.small)
    table.cell(backtesting_table, table_titles, 1, text='    Close Balance    ', bgcolor=_table_title_cell_color, text_color=_table_text_color, text_halign=text.align_left, text_size=size.small)
    table.cell(backtesting_table, table_titles, 2, text='    Profit / Loss ($)    ', bgcolor=_table_title_cell_color, text_color=_table_text_color, text_halign=text.align_left, text_size=size.small)
    table.cell(backtesting_table, table_titles, 3, text='    Profit / Loss (%)    ', bgcolor=_table_title_cell_color, text_color=_table_text_color, text_halign=text.align_left, text_size=size.small)
    table.cell(backtesting_table, table_titles, 4, text='    Entries / Exits    ', bgcolor=_table_title_cell_color, text_color=_table_text_color, text_halign=text.align_left, text_size=size.small)
    table.cell(backtesting_table, table_titles, 5, text='    Wins / Losses   ', bgcolor=_table_title_cell_color, text_color=_table_text_color, text_halign=text.align_left, text_size=size.small)
    table.cell(backtesting_table, table_titles, 6, text='    Winrate (%)    ', bgcolor=_table_title_cell_color, text_color=_table_text_color, text_halign=text.align_left, text_size=size.small)

    table.cell(backtesting_table, table_values, 0, text='    ' + str.tostring(_initial_capital, '###,###.##') + '   ', width=table_width, bgcolor=color.new(_table_cell_color, _table_transparency), text_color=color.new(color.white, 0), text_halign=text.align_right, text_size=size.small)
    table.cell(backtesting_table, table_values, 1, text='    ' + str.tostring(_open_balance, '###,###.##') + '   ', width=table_width, bgcolor=color.new(profit_color, _table_transparency), text_color=color.new(color.white, 0), text_halign=text.align_right, text_size=size.small)
    table.cell(backtesting_table, table_values, 2, text='    ' + str.tostring(_open_balance - _initial_capital, '###,###.##') + '   ', width=table_width, bgcolor=color.new(profit_color, _table_transparency), text_color=color.new(color.white, 0), text_halign=text.align_right, text_size=size.small)
    table.cell(backtesting_table, table_values, 3, text='    ' + str.tostring(_profit_and_loss, format.percent) + '   ', width=table_width, bgcolor=color.new(_open_balance - _initial_capital >= 0 ? green : red, _table_transparency), text_color=color.new(color.white, 0), text_halign=text.align_right, text_size=size.small)
    table.cell(backtesting_table, table_values, 4, text='    ' + str.tostring(_entries, '###,###.##') + ' / ' + str.tostring(_exits, '###,###.##') + '   ', width=table_width, bgcolor=color.new(_table_cell_color, _table_transparency), text_color=color.new(color.white, 0), text_halign=text.align_right, text_size=size.small)
    table.cell(backtesting_table, table_values, 5, text='    ' + str.tostring(_wins, '###,###.##') + ' / ' + str.tostring(_losses, '###,###.##') + '   ', width=table_width, bgcolor=color.new(_table_cell_color, _table_transparency), text_color=color.new(color.white, 0), text_halign=text.align_right, text_size=size.small)
    table.cell(backtesting_table, table_values, 6, text='    ' + str.tostring(_winrate, '#.##') + '%' + '   ', width=table_width, bgcolor=color.new(_table_cell_color, _table_transparency), text_color=color.new(color.white, 0), text_halign=text.align_right, text_size=size.small)

    table.cell(backtesting_table, table_offset, 0, table_margin_right, bgcolor=color.new(color.white, 100), text_color=color.white, text_size=size.small)

// @function - Main backtesting function.
f_backtest_main(_date_start, _date_end, _entry_signal, _exit_signal, _take_profit_percentage=0.0, _stop_loss_percentage=0.0, _atr_length=0, _initial_capital=1000.00, _order_size=100, _commission=0.0, _on_bar_close=false, _high=high, _low=low, _src=close, _chart=false) =>
    var open_balance = _initial_capital

    var initial_capital = _initial_capital
    var order_size = _order_size
    var commission = _commission

    var entries = 0
    var exits = 0

    var wins = 0
    var losses = 0

    var float cumulative_profit = 0.0
    var float profit_factor = 0.0
    var float total_purchased = 0.0

    var float entry_commission = 0.0
    var float exit_commission = 0.0
    var float total_commission = 0.0

    var float trade_profit = 0.0
    var float gross = 0.0
    var float net = 0.0
    var float winrate = 0.0

    var float closing_balance = 0.0
    var float profit_and_loss = 0.0

    var open_trade = false

    // Here, we store the 'last_bar_index' value that is known from the beginning of the script's calculation.
    // The 'last_bar_index' will change when new real-time bars appear, so we declare 'last_bar' with the 'var' keyword.
    var last_bar = last_bar_index

    // Check if the current bar_index is '_date_start' removed from the last bar on the chart, or the chart is traded in real-time.
    // Or if the _date start is bigger than 476928000 it's probably a date and we use the date time.
    date_in_range = _date_start < 476928000 ? (last_bar - bar_index <= _date_start) : (time >= _date_start and time < _date_end)
    if (date_in_range)
        var line_src = 0.0
        var label_position = label.style_label_down

        var last_entry_signal_bar_index = 0
        var last_exit_signal_bar_index = 0

        var entry_price = 0.0
        var exit_price = 0.0
        var exit_profit = 0.0
        var exit_comment = ''

        var take_profit_price = 0.0
        var stop_loss_price = 0.0

        var take_profit_enabled = false
        var take_profit_condition = false
        var take_profit_condition_cross = false
        var take_profit_condition_close = false

        var stop_loss_enabled = false
        var stop_loss_condition = false
        var stop_loss_condition_cross = false
        var stop_loss_condition_close = false

        take_profit_enabled := _take_profit_percentage > 0
        take_profit_percentage = take_profit_enabled ? (_take_profit_percentage / 100) : 9999

        stop_loss_enabled := _stop_loss_percentage > 0
        stop_loss_percentage = stop_loss_enabled ? (_stop_loss_percentage / 100) : 9999

        atr_enabled = _atr_length > 0
        tr = math.max(_high - _low, math.abs(_high - _src[1]), math.abs(_low - _src[1]))
        atr = atr_enabled ? ta.sma(tr, _atr_length) : 0

        entry_signal = _entry_signal
        entry_condition = not open_trade and entry_signal
        entry_signal_value = ta.valuewhen(entry_condition, _src, 0)
        if (entry_condition)
            open_trade := true
            entry_price := _src

            // Only show labels on chart when using the full backtest function.
            if (_chart)
                last_entry_signal_bar_index := bar_index

                entry_label_bar_index = _on_bar_close ? last_entry_signal_bar_index : last_entry_signal_bar_index + 1
                entry_label_text = str.tostring(math.round_to_mintick(entry_price))
                label.new(entry_label_bar_index, na, entry_label_text, color=color.green, textcolor=color.white, size=size.normal, style=label.style_label_up, yloc=yloc.belowbar)

            cumulative_profit := open_balance * (order_size / 100)
            entry_commission := (cumulative_profit / 100) * commission
            profit_factor := cumulative_profit - entry_commission
            total_purchased := profit_factor / entry_price

            // Increase the entries count.
            entries += 1

            // Reset the exit price
            exit_price := na

        exit_signal = _exit_signal

        // Take Profit Conditions
        if (open_trade and take_profit_enabled)
            take_profit_price := atr_enabled ? entry_price + (atr * (take_profit_percentage * 100)) : entry_price + (entry_price * take_profit_percentage)
            take_profit_condition_cross := ta.crossover(_high, take_profit_price) or ta.crossunder(_high, take_profit_price) or ta.cross(_src, take_profit_price)
            take_profit_condition_close := _src >= take_profit_price
            take_profit_condition := ta.crossover(_high, take_profit_price) or take_profit_condition_close

            exit_signal := exit_signal or take_profit_condition

        // Stop Loss Conditions
        if (open_trade and stop_loss_enabled)
            stop_loss_price := atr_enabled ? entry_price - (atr * (stop_loss_percentage * 100)) : entry_price - (entry_price * stop_loss_percentage)
            stop_loss_condition_cross := ta.crossunder(_low, stop_loss_price) or ta.crossunder(_low, stop_loss_price) or ta.cross(_src, stop_loss_price)
            stop_loss_condition_close := _src <= stop_loss_price
            stop_loss_condition := stop_loss_condition_cross or stop_loss_condition_close

            exit_signal := exit_signal or stop_loss_condition

        exit_condition = open_trade and exit_signal
        exit_signal_value = ta.valuewhen(exit_condition, _src, 0)
        if (exit_condition)
            open_trade := false
            exit_price := _src

            // Only show labels on chart when using the full backtest function.
            if (_chart)
                if (stop_loss_enabled and stop_loss_condition)
                    exit_price := stop_loss_price
                    exit_comment := 'SL @ '

                if (take_profit_enabled and take_profit_condition)
                    exit_price := take_profit_price
                    exit_comment := 'TP @ '

                //if (last_entry_signal_bar_index != bar_index)
                line_profit_color = entry_price < exit_price ? color.green : color.red
                label_profit_color = entry_price < exit_price ? color.blue : color.red

                // Draw a line between exit and entry.
                line_bar_index = _on_bar_close ? last_entry_signal_bar_index : last_entry_signal_bar_index + 1
                line.new(line_bar_index, exit_price, bar_index, exit_price, extend=extend.none, color=color.new(line_profit_color, 20))
                //line_bar_price = line.get_x2(order_line)

                exit_profit := (((exit_price - entry_price) / entry_price) * 100)

                // The exit label.
                bar_index_exit_label = _on_bar_close ? bar_index : bar_index + 1
                label.new(bar_index_exit_label, na, exit_comment + str.tostring(math.round_to_mintick(exit_price)) + ' (' + str.tostring(exit_profit, '#.##') + '%)', color=label_profit_color, textcolor=color.white, size=size.normal, style=label.style_label_down, yloc=yloc.abovebar)

                // Reset the bar_index of the entry_signal.
                last_entry_signal_bar_index := 0

            // Calculate the total Wins / Losses and Winrate (%)
            if (exit_price > entry_price)
                wins += 1
            else
                losses += 1

            winrate := nz(math.round((wins / (wins + losses) * 100), 2))

            // Reset the entry price.
            entry_price := na
            exit_profit := 0

            // Reset the TP/SL conditions
            take_profit_condition := false
            take_profit_condition_cross := false
            take_profit_condition_close := false

            stop_loss_condition := false
            stop_loss_condition_cross := false
            stop_loss_condition_close := false

            // Cum. Profit % = Profit / (Initial Capital + Cumulative Profit of the previous trade) * 100%
            // Calculate the profit and loss.
            gross := total_purchased * exit_signal_value
            net := gross - profit_factor
            exit_commission := (gross / 100) * commission
            total_commission := (entry_commission + exit_commission)
            trade_profit := net - total_commission
            closing_balance := open_balance + trade_profit
            open_balance := closing_balance
            profit_and_loss := ((open_balance - initial_capital) / initial_capital) * 100

            // Increase the exits count.
            exits += 1

    [profit_and_loss, open_balance, winrate, entries, exits, wins, losses]

// @function - Show full backtesting results.
f_backtest(_date_start, _date_end, _entry_signal, _exit_signal, _take_profit_percentage=0.0, _stop_loss_percentage=0.0, _atr_length=0, _initial_capital=1000.00, _order_size=100, _commission=0.0, _on_bar_close=true, _high=high, _low=low, _src=close, _chart=true) =>
    [profit_and_loss, open_balance, winrate, entries, exits, wins, losses] = f_backtest_main(_date_start, _date_end, _entry_signal, _exit_signal, _take_profit_percentage, _stop_loss_percentage, _atr_length, _initial_capital, _order_size, _commission, _on_bar_close, _high, _low, _src, _chart)

// @function - Show backtesting profit.
f_backtest_profit(_date_start, _date_end, _entry_signal, _exit_signal, _take_profit_percentage=0.0, _stop_loss_percentage=0.0, _atr_length=0, _initial_capital=1000.00, _order_size=100, _commission=0.0, _on_bar_close=true, _high=high, _low=low, _src=close, _chart=false) =>
    [profit_and_loss, _, _, _, _, _, _] = f_backtest_main(_date_start, _date_end, _entry_signal, _exit_signal, _take_profit_percentage, _stop_loss_percentage, _atr_length, _initial_capital, _order_size, _commission, _on_bar_close, _high, _low, _src, _chart)
    response = profit_and_loss

// @function - Show backtesting winrate.
f_backtest_winrate(_date_start, _date_end, _entry_signal, _exit_signal, _take_profit_percentage=0.0, _stop_loss_percentage=0.0, _atr_length=0, _initial_capital=1000.00, _order_size=100, _commission=0.0, _on_bar_close=true, _high=high, _low=low, _src=close, _chart=false) =>
    [_, _, winrate, _, _, _, _] = f_backtest_main(_date_start, _date_end, _entry_signal, _exit_signal, _take_profit_percentage, _stop_loss_percentage, _atr_length, _initial_capital, _order_size, _commission, _on_bar_close, _high, _low, _src, _chart)
    response = winrate

// @function - Define random colors for plots.
// The most stupid function I've ever created. "ChatGPT".
f_color(_n) =>
    color = _n == 1 or _n == 16 or _n == 32 or _n == 48 or _n == 64 or _n == 80 or _n == 96 or _n == 112 or _n == 128 or _n == 144 or _n == 160 or _n == 176 or _n == 192 or _n == 208 or _n == 224 or _n == 240 ? color.aqua : _n == 2 or _n == 18 or _n == 34 or _n == 50 or _n == 66 or _n == 82 or _n == 98 or _n == 114 or _n == 130 or _n == 146 or _n == 162 or _n == 178 or _n == 194 or _n == 210 or _n == 226 or _n == 242 ? color.yellow : _n == 3 or _n == 19 or _n == 35 or _n == 51 or _n == 67 or _n == 83 or _n == 99 or _n == 115 or _n == 131 or _n == 147 or _n == 163 or _n == 179 or _n == 195 or _n == 211 or _n == 227 or _n == 243 ? color.fuchsia : _n == 4 or _n == 20 or _n == 36 or _n == 52 or _n == 68 or _n == 84 or _n == 100 or _n == 116 or _n == 132 or _n == 148 or _n == 164 or _n == 180 or _n == 196 or _n == 212 or _n == 228 or _n == 244 ? color.gray : _n == 5 or _n == 21 or _n == 37 or _n == 53 or _n == 69 or _n == 85 or _n == 101 or _n == 117 or _n == 133 or _n == 149 or _n == 165 or _n == 181 or _n == 197 or _n == 213 or _n == 229 or _n == 245 ? color.green : _n == 6 or _n == 22 or _n == 38 or _n == 54 or _n == 70 or _n == 86 or _n == 102 or _n == 118 or _n == 134 or _n == 150 or _n == 166 or _n == 182 or _n == 198 or _n == 214 or _n == 230 or _n == 246 ? color.lime : _n == 7 or _n == 23 or _n == 39 or _n == 55 or _n == 71 or _n == 87 or _n == 103 or _n == 119 or _n == 135 or _n == 151 or _n == 167 or _n == 183 or _n == 199 or _n == 215 or _n == 231 or _n == 247 ? color.maroon : _n == 8 or _n == 24 or _n == 40 or _n == 56 or _n == 72 or _n == 88 or _n == 104 or _n == 120 or _n == 136 or _n == 152 or _n == 168 or _n == 184 or _n == 200 or _n == 216 or _n == 232 or _n == 248 ? color.navy : _n == 9 or _n == 25 or _n == 41 or _n == 57 or _n == 73 or _n == 89 or _n == 105 or _n == 121 or _n == 137 or _n == 153 or _n == 169 or _n == 185 or _n == 201 or _n == 217 or _n == 233 or _n == 249 ? color.olive : _n == 10 or _n == 26 or _n == 42 or _n == 58 or _n == 74 or _n == 90 or _n == 106 or _n == 122 or _n == 138 or _n == 154 or _n == 170 or _n == 186 or _n == 202 or _n == 218 or _n == 234 or _n == 250 ? color.orange : _n == 11 or _n == 27 or _n == 43 or _n == 59 or _n == 75 or _n == 91 or _n == 107 or _n == 123 or _n == 139 or _n == 155 or _n == 171 or _n == 187 or _n == 203 or _n == 219 or _n == 235 or _n == 251 ? color.purple : _n == 12 or _n == 28 or _n == 44 or _n == 60 or _n == 76 or _n == 92 or _n == 108 or _n == 124 or _n == 140 or _n == 156 or _n == 172 or _n == 188 or _n == 204 or _n == 220 or _n == 236 or _n == 252 ? color.red : _n == 13 or _n == 29 or _n == 45 or _n == 61 or _n == 77 or _n == 93 or _n == 109 or _n == 125 or _n == 141 or _n == 157 or _n == 173 or _n == 189 or _n == 205 or _n == 221 or _n == 237 or _n == 253 ? color.silver : _n == 14 or _n == 30 or _n == 46 or _n == 62 or _n == 78 or _n == 94 or _n == 110 or _n == 126 or _n == 142 or _n == 158 or _n == 174 or _n == 190 or _n == 206 or _n == 222 or _n == 238 or _n == 254 ? color.teal : _n == 15 or _n == 31 or _n == 47 or _n == 63 or _n == 79 or _n == 95 or _n == 111 or _n == 127 or _n == 143 or _n == 159 or _n == 175 or _n == 191 or _n == 207 or _n == 223 or _n == 239 or _n == 255 ? color.blue : color.blue
    color_transparency = _n < 15 ? 0 : _n > 15 and _n < 30 ? 5 : _n > 30 and _n < 45 ? 8 : _n > 45 and _n < 60 ? 11 : _n > 60 and _n < 75 ? 14 : _n > 75 and _n < 90 ? 17 : _n > 90 and _n < 105 ? 20 : _n > 105 and _n < 120 ? 23 : _n > 120 and _n < 135 ? 26 : _n > 135 and _n < 150 ? 29 : _n > 150 and _n < 165 ? 32 : _n > 165 and _n < 180 ? 35 : _n > 180 and _n < 195 ? 38 : _n > 195 and _n < 210 ? 41 : _n > 210 and _n < 225 ? 44 : _n > 225 and _n < 240 ? 47 : _n > 240 and _n < 255 ? 50 : _n > 255 and _n < 270 ? 53 : _n > 270 and _n < 285 ? 56 : _n > 285 and _n < 300 ? 59 : _n > 300 and _n < 315 ? 62 : _n > 315 and _n < 330 ? 65 : _n > 330 and _n < 345 ? 68 : _n > 345 and _n < 360 ? 71 : _n > 360 and _n < 375 ? 74 : _n > 375 and _n < 390 ? 77 : _n > 390 and _n < 405 ? 80 : _n > 405 and _n < 420 ? 83 : _n > 420 and _n < 435 ? 86 : _n > 435 and _n < 450 ? 89 : _n > 450 and _n < 465 ? 92 : _n > 465 and _n < 480 ? 95 : _n > 480 and _n < 495 ? 98 : _n > 495 and _n < 510 ? 101 : 0
    color.new(color, color_transparency)

// ############################################################
// # EXPORT FUNCTIONS
// ############################################################

// @function - Get a random color, great for using many plots.
// @param _n <int> A random number.
export color(int _n=1) =>
    f_color(_n)

// @function - Shortcut to get the take profit price.
// @param _entry_price <series float> The entry price.
// @param _take_profit_percentage <float> The take profit percentage / atr multiplier.
// @param _atr_length <int> The ATR Length.
export take_profit_price(series float _entry_price=close, float _take_profit_percentage=0.0, int _atr_length=14) =>
    f_take_profit_price(_entry_price, _take_profit_percentage, _atr_length)

// @function - Shortcut to get the take profit price.
// @param _entry_price <series float> The entry price.
// @param _stop_loss_percentage <float> The stop loss percentage / atr multiplier.
// @param _atr_length <int> The ATR Length.
export stop_loss_price(series float _entry_price=close, float _stop_loss_percentage=0.0, int _atr_length=14) =>
    f_stop_loss_price(_entry_price, _stop_loss_percentage, _atr_length)

// @function - Show a table of the backtest results.
// @param _initial_capital <simple float> Initial Capital.
// @param _profit_and_loss <simple float> Profit and Loss.
// @param _open_balance <simple float> Open Balance.
// @param _winrate <float> Winrate.
// @param _entries <int> Number of entries.
// @param _wins <int> Number of wins.
// @param _losses <int> Number of losses.
// @param _table_position <string> The position of the table.
// @param _table_margin <string> The margin of the table.
// @param _table_transparency <string> The transparency of the table.
// @param _table_cell_color <string> The cell color of the table.
// @param _table_title_cell_color <string> The title cell color of the table.
// @param _table_text_color <string> The text color.
export table(float _initial_capital, float _profit_and_loss, float _open_balance, float _winrate, int _entries, int _exits, int _wins, int _losses, string _table_position='RIGHT', int _table_margin=15, int _table_transparency=20, color _table_cell_color=color.purple, color _table_title_cell_color=color.gray, color _table_text_color=color.white) =>
    f_backtest_table(_initial_capital, _profit_and_loss, _open_balance, _winrate, _entries, _exits, _wins, _losses, _table_position, _table_margin, _table_transparency, _table_cell_color, _table_title_cell_color, _table_text_color)

// @function - Show a table of the backtest results.
// @param _date_start <int> Start Date (When using a value below 5000 instead of date it will use bars back as starting point).
// @param _date_end <int> End Date.
// @param _entry_signal <bool> The Entry Signal.
// @param _exit_signal <int> The Exit Signal.
// @param _take_profit_percentage <simple float> The take profit percentage / atr multiplier.
// @param _stop_loss_percentage <simple float> The stop loss percentage / atr multiplier.
// @param _atr_length <simple int> The ATR length for TP/SL.
// @param _initial_capital <float> Initial Capital.
// @param _order_size <float> The order size.
// @param _commission <float> The commission.
// @param _on_bar_close <string> On Bar Close toggle (It's recommended to not change this value).
// @param _high <series float> High source.
// @param _low <series float> Low source.
// @param _src <series float> Calculation source.
// @param _chart <bool> Enable / Disable visuals, labels of the backtesting results.
export run(int _date_start, int _date_end, bool _entry_signal, bool _exit_signal, simple float _take_profit_percentage=0.0, simple float _stop_loss_percentage=0.0, simple int _atr_length=0, float _initial_capital=1000.00, float _order_size=100, float _commission=0.0, bool _on_bar_close=true, series float _high=high, series float _low=low, series float _src=close, simple bool _chart=true) =>
    [profit_and_loss, open_balance, winrate, entries, exits, wins, losses] = f_backtest_main(_date_start, _date_end, _entry_signal, _exit_signal, _take_profit_percentage, _stop_loss_percentage, _atr_length, _initial_capital, _order_size, _commission, _on_bar_close, _high, _low, _src, _chart)

export results(int _date_start, int _date_end, bool _entry_signal, bool _exit_signal, simple float _take_profit_percentage=0.0, simple float _stop_loss_percentage=0.0, simple int _atr_length=0, float _initial_capital=1000.00, float _order_size=100, float _commission=0.0, bool _on_bar_close=true, series float _high=high, series float _low=low, series float _src=close, simple bool _chart=true) =>
    [profit_and_loss, open_balance, winrate, entries, exits, wins, losses] = f_backtest_main(_date_start, _date_end, _entry_signal, _exit_signal, _take_profit_percentage, _stop_loss_percentage, _atr_length, _initial_capital, _order_size, _commission, _on_bar_close, _high, _low, _src, _chart)

// @function - Only show the profit.
export profit(int _date_start, int _date_end, bool _entry_signal, bool _exit_signal, simple float _take_profit_percentage=0.0, simple float _stop_loss_percentage=0.0, simple int _atr_length=0, float _initial_capital=1000.00, float _order_size=100, float _commission=0.0, bool _on_bar_close=true, series float _high=high, series float _low=low, series float _src=close, simple bool _chart=false) =>
    [profit_and_loss, _, _, _, _, _, _] = f_backtest_main(_date_start, _date_end, _entry_signal, _exit_signal, _take_profit_percentage, _stop_loss_percentage, _atr_length, _initial_capital, _order_size, _commission, _on_bar_close, _high, _low, _src, _chart)
    response = profit_and_loss

// @function - Only show the winrate.
export winrate(int _date_start, int _date_end, bool _entry_signal, bool _exit_signal, simple float _take_profit_percentage=0.0, simple float _stop_loss_percentage=0.0, simple int _atr_length=0, float _initial_capital=1000.00, float _order_size=100, float _commission=0.0, bool _on_bar_close=true, series float _high=high, series float _low=low, series float _src=close, simple bool _chart=false) =>
    [_, _, winrate, _, _, _, _] = f_backtest_main(_date_start, _date_end, _entry_signal, _exit_signal, _take_profit_percentage, _stop_loss_percentage, _atr_length, _initial_capital, _order_size, _commission, _on_bar_close, _high, _low, _src, _chart)
    response = winrate
