### Initialize Buy and Hold Return Calculation Source: https://github.com/tradelexx/trade_lexx/blob/main/Zendog V3 backtest DCA bot 3commas.txt Sets the starting price and time for a buy and hold return calculation when a specific date range is entered and the calculation hasn't started. ```pinescript //stats for buy and hold return if in_date_range and bh_calculation_started == false bh_start_price := open bh_start_time := time bh_calculation_started := true bh_end_price := close bh_end_time := time_close ``` -------------------------------- ### Pine Script: Base Order and Deal Initialization Logic Source: https://github.com/tradelexx/trade_lexx/blob/main/3C QFL Mean reversal.txt Handles the logic for initiating a base order and the first deal in the strategy. It checks for approval, signals, and ensures no deal is currently active. Upon starting the first deal, it initializes various strategy-related variables and safety order statistics. ```pinescript //condition for base order if timeFilterApproval and signal and not is_deal_started() and was_deal_marked_as_finished() //stats for the first deal if firstdeal_started == false firstdeal_started := true firstdeal_bar_index := bar_index firstdeal_start_price := close firstdeal_start_time := time init_stats_array_safety_orders() c_still_in_deal := true stats_deals_started := stats_deals_started + 1 c_dealstart_bar_index := bar_index c_dealstart_bar_time := time ``` -------------------------------- ### Display Max Deviation from Deal Start Source: https://github.com/tradelexx/trade_lexx/blob/main/3C QFL Mean reversal.txt Populates a table row with the maximum deviation from the deal start price. It includes context about whether the stop loss was active and the time of the deviation, formatted with a percentage and date string. ```pinescript string _bef = valid_stop_loss() ? 'before SL' : '' table.cell(sostats, column=0, row=_row, text='Max deviation:\n(Deal start price vs worst candle ' + _bef + ')', text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_grey(_row)) table.cell(sostats, column=1, row=_row, text=str.tostring(math.round(stats_biggest_dev, 2)) + '%\n' + '(' + time_to_date_string(stats_biggest_dev_time) + ')', text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_grey(_row)) _row := _row + 1 ``` -------------------------------- ### Pine Script: Buy and Hold Statistics Update Source: https://github.com/tradelexx/trade_lexx/blob/main/3C QFL Mean reversal.txt Manages the initialization and updating of buy-and-hold strategy statistics. It captures the start and end prices and times for a buy-and-hold period, triggered by specific conditions like `timeFilterApproval` and `bh_calculation_started`. ```pinescript //stats for buy and hold return if timeFilterApproval and bh_calculation_started == false bh_start_price := open bh_start_time := time bh_calculation_started := true bh_end_price := close bh_end_time := time_close ``` ```pinescript //update stats for buy and hold as long as we're still inside date range if timeFilterApproval bh_end_price := close bh_end_time := time_close ``` -------------------------------- ### Base Order Entry Condition and Initialization Source: https://github.com/tradelexx/trade_lexx/blob/main/Zendog V3 backtest DCA bot 3commas.txt Defines the conditions for entering a base order, including being within a date range, meeting a deal start condition, and not already having a deal started. It also initializes first deal statistics and safety order arrays. ```pinescript //condition for base order if in_date_range and deal_start_condition and not is_deal_started() and was_deal_marked_as_finished() //stats for the first deal if firstdeal_started == false firstdeal_started := true firstdeal_bar_index := bar_index firstdeal_start_price := close firstdeal_start_time := time init_stats_array_safety_orders() glb_still_in_deal := true stats_deals_started := stats_deals_started + 1 glb_dealstart_bar_index := bar_index glb_dealstart_bar_time := time // Adjust glb_strategy_prev_netprofit before starting a new deal if different than the pine script strategy // This solves a bug when pine script sells at a sligthly different price than the close price. // Case was found when closing deal from external indicator if glb_strategy_prev_netprofit != strategy.netprofit glb_strategy_prev_netprofit := strategy.netprofit //how many coins glb_base_order_qty := cfg_base_order_size_usd / close //This value will be overwritten with the actual executed market price on the next candle glb_base_order_price := close // enter with market order because with limit order the open of next candle might be a bit different // than close of current candle and the deal might not start _alert_human = 'D' + str.tostring(stats_deals_started) + '-BO (' + str.tostring(syminfo.basecurrency) + '-' ``` -------------------------------- ### Generate Start Deal JSON for 3C Bot Source: https://github.com/tradelexx/trade_lexx/blob/main/Zendog V3 backtest DCA bot 3commas.txt Constructs a JSON string payload for initiating a trading deal with a 3Commas bot. It includes bot identification, API token, and the trading pair derived from `syminfo`. ```pinescript get_3cbot_startdeal_json() => _string = '{"message_type":"bot", "bot_id":"' + cfg_bot_id + '", "email_token":"' + cfg_email_token + '", "delay_seconds":0, "pair":"' + str.tostring(syminfo.currency) + '_' + str.tostring(syminfo.basecurrency) + '"}' ``` -------------------------------- ### Configure Deal Start Conditions Source: https://github.com/tradelexx/trade_lexx/blob/main/Zendog V3 backtest DCA bot 3commas.txt Sets up the conditional logic for initiating a trading deal based on RSI values or external indicator states. It handles different comparison operations like less than or equal to, and greater than or equal to, for both RSI and external indicators. ```pinescript //--------- DEAL START CONDITION ---------- //RSI-7 if deal_start_type == 'RSI-7' and rsi_start_operation == '<=' deal_start_condition := ta.rsi(close, 7) <= rsi_start_value else if deal_start_type == 'RSI-7' and rsi_start_operation == '>=' deal_start_condition := ta.rsi(close, 7) >= rsi_start_value //MAYBE FOUND A BUG --> Leave all if's on this level otherwise they do not work else if deal_start_type == 'External Indicator' and deal_start_value != -99999 and deal_start_operation == '=' and external_indicator == deal_start_value deal_start_condition := true else if deal_start_type == 'External Indicator' and deal_start_value != -99999 and deal_start_operation == '<=' and external_indicator <= deal_start_value deal_start_condition := true else if deal_start_type == 'External Indicator' and deal_start_value != -99999 and deal_start_operation == '>=' and external_indicator >= deal_start_value deal_start_condition := true else deal_start_condition := false //----------------------------------------- ``` -------------------------------- ### Pine Script 3commas Bot Control Settings Source: https://github.com/tradelexx/trade_lexx/blob/main/3C QFL Mean reversal.txt Configuration options for integrating the strategy with 3commas bots via webhook calls. This includes enabling the feature, specifying bot ID and email token, and controlling deal start execution. ```pinescript string _tooltip_bot_control = 'If this is enabled the strategy can control some bot operations via Webhook calls.\n\nTo enable Webhook calls, check this option, complete Bot id and Email Token. After the strategy is configured, create an alert on the strategy, select Order fills only, and in the message field simply input {{strategy.order.alert_message}}.' bool i_enable_bot_control = input.bool(title='Enable Bot Control Via Webhook', defval=false, group='3commas Bot Settings', tooltip=_tooltip_bot_control) string i_bot_id = input.string(title='Bot id', defval='', group='3commas Bot Settings') string i_email_token = input.string(title='Email token', defval='', group='3commas Bot Settings') bool i_exec_deal_start = input.bool(title='Deal start', defval=true, group='3commas Bot Settings') ``` -------------------------------- ### Execute Initial Trade Entry Source: https://github.com/tradelexx/trade_lexx/blob/main/Zendog V3 backtest DCA bot 3commas.txt Initiates a trading entry (long or short) based on strategy conditions. It conditionally includes bot control alerts for deal start events. The alert message is constructed using currency and order quantity/price information. ```pinescript str.tostring(syminfo.currency) + (cfg_strategy_type == 'long' ? ' LONG' : ' SHORT') + ') | ' + str.tostring(glb_base_order_qty * glb_base_order_price) + ' ' + str.tostring(syminfo.currency) if cfg_enable_bot_control and cfg_exec_deal_start _alert_json = get_3cbot_startdeal_json() strategy.entry(id='D' + str.tostring(stats_deals_started) + '-BO', direction=IS_LONG ? strategy.long : strategy.short, qty=glb_base_order_qty, alert_message=_alert_json) alert(_alert_human, alert.freq_once_per_bar_close) else strategy.entry(id='D' + str.tostring(stats_deals_started) + '-BO', direction=IS_LONG ? strategy.long : strategy.short, qty=glb_base_order_qty) alert(_alert_human, alert.freq_once_per_bar_close) ``` -------------------------------- ### Pine Script: Bot Communication JSON Generation Source: https://github.com/tradelexx/trade_lexx/blob/main/3C QFL Mean reversal.txt Generates JSON strings for interacting with a trading bot. Functions include starting a deal, stopping all deals, and adding funds to a quote. These require bot-specific identifiers like `i_bot_id` and `i_email_token`. ```pinescript get_3cbot_startdeal_json() => _string = '{"message_type":"bot", "bot_id":"' + i_bot_id + '", "email_token":"' + i_email_token + '", "delay_seconds":0}' ``` ```pinescript get_3cbot_stopdeal_json() => _string = '{"action": "close_at_market_price_all", "message_type":"bot", "bot_id":"' + i_bot_id + '", "email_token":"' + i_email_token + '", "delay_seconds":0}' ``` ```pinescript get_3cbot_addfundsinquote_json(add_volume = 0) => _string = '{"action": "add_funds_in_quote", "message_type":"bot", "bot_id":"' + i_bot_id + '", "email_token":"' + i_email_token + '", "delay_seconds":0, "volume":"' + str.tostring(add_volume) + '"}' ``` -------------------------------- ### Populate Trading Statistics Table Source: https://github.com/tradelexx/trade_lexx/blob/main/3C QFL Mean reversal.txt This snippet demonstrates how to populate a table (`sostats`) with various trading performance metrics. It calculates and displays statistics such as winning/losing deal PNL, average deal duration, backtest period, required capital, and overall profit, often using conditional formatting for visual cues. ```pinescript _text5 := _text5 + str.tostring(stats_deals_take_profit_finished) _text5 := _text5 + ' (' + str.tostring(math.round(array.avg(statsarray_winning_deals_pnl), i_decimals)) _text5 := _text5 + ' ' + str.tostring(syminfo.currency) + ' on avg)' table.cell(sostats, column=1, row=_row, text=_text5, text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_green()) _row := _row + 1 table.cell(sostats, column=0, row=_row, text='Losing deals:', text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_grey(_row)) if array.size(statsarray_losing_deals_pnl) == 0 table.cell(sostats, column=1, row=_row, text=str.tostring(array.size(statsarray_losing_deals_pnl)), text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_green()) else _text6 = '' _text6 := _text6 + str.tostring(array.size(statsarray_losing_deals_pnl)) _text6 := _text6 + ' (' + str.tostring(math.round(array.avg(statsarray_losing_deals_pnl), i_decimals)) _text6 := _text6 + ' ' + str.tostring(syminfo.currency) + ' on avg)' table.cell(sostats, column=1, row=_row, text=_text6, text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_red()) _row := _row + 1 table.cell(sostats, column=0, row=_row, text='Total time ( Max | Avg time in deal ):', text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_grey(_row)) _text4 = '' + str.tostring(get_timespan_string(bh_start_time, bh_end_time)) + ' ( ' _text4 := _text4 + '' + get_timestring_from_days(array.max(statsarray_no_of_days)) + ' | ' _text4 := _text4 + '' + get_timestring_from_days(array.avg(statsarray_no_of_days)) + ' )' table.cell(sostats, column=1, row=_row, text=_text4, text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_grey(_row)) _row := _row + 1 table.cell(sostats, column=0, row=_row, text='Total backtest:\n' + str.tostring(time_to_date_string(bh_start_time)) + '\n' + str.tostring(time_to_date_string(bh_end_time)), text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_grey(_row)) table.cell(sostats, column=1, row=_row, text='Max days in deal:\n' + str.tostring(time_to_date_string(stats_max_days_in_deal_start_time)) + '\n' + str.tostring(time_to_date_string(stats_max_days_in_deal_close_time)), text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_grey(_row)) _row := _row + 1 table.cell(sostats, column=0, row=_row, text='Required capital:', text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_grey(_row)) table.cell(sostats, column=1, row=_row, text=str.tostring(c_required_capital) + ' ' + str.tostring(syminfo.currency), text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_grey(_row)) _row := _row + 1 table.cell(sostats, column=0, row=_row, text='Profit:\n(after commision)', text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_grey(_row)) _text1 = str.tostring(math.round(get_final_pnl_new(), i_decimals)) + ' ' + str.tostring(syminfo.currency) _text1 := _text1 + ' (' + str.tostring(math.round(get_final_pnl_prct_new(), 2)) + ' %)\n' if get_days(bh_start_time, bh_end_time) >= 1 _text1 := _text1 + str.tostring(math.round(get_final_pnl_prct_new() / get_days(bh_start_time, bh_end_time), 2)) + '% / day' else _text1 := _text1 + str.tostring(math.round(get_final_pnl_prct_new(), 2)) + '% / day' table.cell(sostats, column=1, row=_row, text=_text1, text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_grey(_row)) _row := _row + 1 //if date range is limited calculate between start and end date _bh_equity = c_required_capital / bh_start_price * bh_end_price - c_required_capital _bh_commision = get_commission_for_volume(c_required_capital + c_required_capital / bh_start_price * bh_end_price) _bh_equity := _bh_equity - _bh_commision _bh_prct_total = math.round(_bh_equity * 100 / c_required_capital, 2) table.cell(sostats, column=0, row=_row, text='Buy & hold return:\n(after commision)', text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_grey(_row)) _text2 = str.tostring(math.round(_bh_equity, i_decimals)) + ' ' + str.tostring(syminfo.currency) _text2 := _text2 + ' (' + str.tostring(_bh_prct_total) + '%)\n' ``` -------------------------------- ### Get Grey Background Color Source: https://github.com/tradelexx/trade_lexx/blob/main/Zendog V3 backtest DCA bot 3commas.txt Returns a grey background color based on the row index. Even rows get a lighter grey (#CACACA), and odd rows get a darker grey (#E5E5E5). ```pinescript get_bg_color_grey(row) _bgcolor = row % 2 == 0 ? #CACACA : #E5E5E5 ``` -------------------------------- ### Pine Script Table for Strategy Statistics Source: https://github.com/tradelexx/trade_lexx/blob/main/3C QFL Mean reversal.txt This snippet demonstrates how to create and populate a table in Pine Script to display various statistics related to a trading strategy's performance. It includes handling deal status, open deals with profit/loss, and finished deals, using the `table` object and its `cell` method. ```pinescript table.cell(steps_amount, 6, _i + 2, "-", text_color=color.white, text_size=size.small) table.cell(steps_amount, 7, _i + 2, "-", text_color=color.white, text_size=size.small) table.cell(steps_amount, 8, _i + 2, "-", text_color=color.white, text_size=size.small) table.cell(steps_amount, 9, _i + 2, "-", text_color=color.white, text_size=size.small) table.cell(steps_amount, 10, _i + 2, str.tostring(_steps_total_volume), text_color=color.white, text_size=size.small) //---------------------------------------------------------------- //---------------------------------------------------------------- //a table with all kind of statistics about strategy results if i_show_stats_table int rowsforstats = array.size(statsarray_safety_orders) + 25 table sostats = table.new(position.top_right, columns=2, rows=rowsforstats, frame_width=1, frame_color=color.black) _row = 0 table.cell(sostats, column=0, row=_row, text='QFL Backtester ' + str.tostring(syminfo.basecurrency) + ' / ' + str.tostring(syminfo.currency), text_halign=text.align_left, text_size=size.small, text_color=color.white, bgcolor=get_bg_color_orange()) table.cell(sostats, column=1, row=_row, text='', text_size=size.small, text_color=color.white, bgcolor=get_bg_color_orange()) _row := _row + 1 table.cell(sostats, column=0, row=_row, text='Status:', text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_grey(_row)) if _deal_in_progress table.cell(sostats, column=1, row=_row, text='Deal in progress', text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_red()) else if stats_deals_started == 0 and stats_deals_finished == 0 table.cell(sostats, column=1, row=_row, text='No deals', text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_red()) else table.cell(sostats, column=1, row=_row, text='All deals closed', text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_green()) _row := _row + 1 table.cell(sostats, column=0, row=_row, text='Open deals:', text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_grey(_row)) if stats_deals_started - stats_deals_finished == 0 table.cell(sostats, column=1, row=_row, text='0', text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_green()) else _current_deal_total_value = strategy.position_size * strategy.position_avg_price _current_deal_actual_value = strategy.position_size * close float _current_deal_equity_percent = 0 if IS_LONG _current_deal_equity_percent := _current_deal_actual_value * 100 / _current_deal_total_value - 100 _current_deal_equity_percent else _current_deal_equity_percent := 100 - _current_deal_actual_value * 100 / _current_deal_total_value _text0 = str.tostring(stats_deals_started - stats_deals_finished) + ' deal\n' _text0 := _text0 + str.tostring(math.round(strategy.openprofit, i_decimals)) + ' ' + str.tostring(syminfo.currency) _text0 := _text0 + ' (' + str.tostring(math.round(_current_deal_equity_percent, 2)) + '%)\n' _text0 := _text0 + str.tostring(get_timespan_string(c_dealstart_bar_time, time_close)) + ', currently at SO ' + str.tostring(count_executed_safety_orders+1) + '\n' _text0 := _text0 + '(start: ' + str.tostring(time_to_date_string(c_dealstart_bar_time)) + ')' table.cell(sostats, column=1, row=_row, text=_text0, text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_red()) _row := _row + 1 table.cell(sostats, column=0, row=_row, text='Finished deals:', text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_grey(_row)) table.cell(sostats, column=1, row=_row, text=str.tostring(stats_deals_finished), text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_grey(_row)) _row := _row + 1 table.cell(sostats, column=0, row=_row, text='Winning deals:', text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_grey(_row)) if stats_deals_take_profit_finished == 0 table.cell(sostats, column=1, row=_row, text=str.tostring(array.size(statsarray_winning_deals_pnl)), text_halign=text.align_left, text_color=color.black, text_size=size.small, bgcolor=get_bg_color_grey(_row)) else _text5 = '' ``` -------------------------------- ### Deal Start Condition Logic Source: https://github.com/tradelexx/trade_lexx/blob/main/3Commas Visible DCA Strategy.txt Determines the condition for starting a new trading deal based on moving average crossovers. It checks if the short moving average crosses over or under the long moving average, depending on the `Triger_Type` input. ```pinescript Base_order_Condition = Triger_Type == "Over" ? ta.crossover(Short_Moving_Average_Line, Long_Moving_Average_Line) : ta.crossunder(Short_Moving_Average_Line, Long_Moving_Average_Line) // Buy when close crossing lower band ``` -------------------------------- ### Display Buying Steps and Volume Table Source: https://github.com/tradelexx/trade_lexx/blob/main/Zendog V3 backtest DCA bot 3commas.txt Generates a detailed table showing buying steps, order sizes, volumes, prices, and profit targets, similar to 3Commas interface. It initializes base order price if not set and calculates values for each step. ```pinescript if cfg_show_step_table if cfg_steps_bo_price == 0 or na(cfg_steps_bo_price) cfg_steps_bo_price := close _steps_bo_size = cfg_base_order_size_usd / cfg_steps_bo_price table steps_amount = table.new(position.bottom_left, columns=12, rows=int(cfg_max_safety_orders) + 4, frame_width=1, frame_color=color.black, border_width=1, border_color=color.black, bgcolor=color.green) table.cell(steps_amount, 0, 0, 'Order\nno', text_color=color.white, text_size=size.small) table.cell(steps_amount, 1, 0, 'Deviation\n( % )', text_color=color.white, text_size=size.small) table.cell(steps_amount, 2, 0, 'Size\n( ' + str.tostring(syminfo.basecurrency) + ' )', text_color=color.white, text_size=size.small) table.cell(steps_amount, 3, 0, 'Volume\n( ' + str.tostring(syminfo.currency) + ' )', text_color=color.white, text_size=size.small) table.cell(steps_amount, 4, 0, 'Price\n( ' + str.tostring(syminfo.currency) + ' )', text_color=color.white, text_size=size.small) table.cell(steps_amount, 5, 0, 'Avg price\n( ' + str.tostring(syminfo.currency) + ' )', text_color=color.white, text_size=size.small) table.cell(steps_amount, 6, 0, 'Price for TP\n( ' + str.tostring(syminfo.currency) + ' )', text_color=color.white, text_size=size.small) table.cell(steps_amount, 7, 0, '% change\nfor TP', text_color=color.white, text_size=size.small) table.cell(steps_amount, 8, 0, '% change\nfor BEP', text_color=color.white, text_size=size.small) table.cell(steps_amount, 9, 0, 'Total size\n( ' + str.tostring(syminfo.basecurrency) + ' )', text_color=color.white, text_size=size.small) table.cell(steps_amount, 10, 0, 'Total Vol\n( ' + str.tostring(syminfo.currency) + ' )', text_color=color.white, text_size=size.small) float _steps_total_size = _steps_bo_size float _steps_total_volume = cfg_steps_bo_price * _steps_bo_size //base order table.cell(steps_amount, 0, 1, 'BO', text_color=color.white, text_size=size.small) table.cell(steps_amount, 1, 1, '0', text_color=color.white, text_size=size.small) table.cell(steps_amount, 2, 1, str.tostring(math.round(_steps_bo_size, cfg_decimals)), text_color=color.white, text_size=size.small) table.cell(steps_amount, 3, 1, str.tostring(math.round(cfg_steps_bo_price * _steps_bo_size, cfg_decimals)), text_color=color.white, text_size=size.small) table.cell(steps_amount, 4, 1, str.tostring(math.round(cfg_steps_bo_price, cfg_decimals)), text_color=color.white, text_size=size.small) table.cell(steps_amount, 5, 1, str.tostring(math.round(_steps_total_volume / _steps_total_size, cfg_decimals)), text_color=color.white, text_size=size.small) _req_price_for_tp = steps_required_price(cfg_steps_bo_price, _steps_total_volume / _steps_total_size, _steps_total_size, cfg_take_profit_perc) ``` -------------------------------- ### Trade Step Table Generation Source: https://github.com/tradelexx/trade_lexx/blob/main/3C QFL Mean reversal.txt Generates a detailed table displaying buying steps, volumes, prices, and profit targets, similar to the 3Commas interface. It calculates and populates data for each safety order step. ```pinescript //---------------------------------------------------------------- //a table with buying steps, volumes, prices similar to 3commas //set custom values for bo entry price if i_show_step_table if i_steps_bo_price == 0 or na(i_steps_bo_price) i_steps_bo_price := close _steps_bo_size = i_base_order_size_usd / i_steps_bo_price table steps_amount = table.new(position.bottom_left, columns=12, rows=int(i_max_safety_orders) + 4, frame_width=1, frame_color=color.black, border_width=1, border_color=color.black, bgcolor=color.blue) table.cell(steps_amount, 0, 0, 'Order\nno', text_color=color.white, text_size=size.small) table.cell(steps_amount, 1, 0, 'Deviation\n( % )', text_color=color.white, text_size=size.small) table.cell(steps_amount, 2, 0, 'Size\n( ' + str.tostring(syminfo.basecurrency) + ' )', text_color=color.white, text_size=size.small) table.cell(steps_amount, 3, 0, 'Volume\n( ' + str.tostring(syminfo.currency) + ' )', text_color=color.white, text_size=size.small) table.cell(steps_amount, 4, 0, 'Price\n( ' + str.tostring(syminfo.currency) + ' )', text_color=color.white, text_size=size.small) table.cell(steps_amount, 5, 0, 'Avg price\n( ' + str.tostring(syminfo.currency) + ' )', text_color=color.white, text_size=size.small) table.cell(steps_amount, 6, 0, 'Price for TP\n( ' + str.tostring(syminfo.currency) + ' )', text_color=color.white, text_size=size.small) table.cell(steps_amount, 7, 0, '% change\nfor TP', text_color=color.white, text_size=size.small) table.cell(steps_amount, 8, 0, '% change\nfor BEP', text_color=color.white, text_size=size.small) table.cell(steps_amount, 9, 0, 'Total size\n( ' + str.tostring(syminfo.basecurrency) + ' )', text_color=color.white, text_size=size.small) table.cell(steps_amount, 10, 0, 'Total Vol\n( ' + str.tostring(syminfo.currency) + ' )', text_color=color.white, text_size=size.small) float _steps_total_size = _steps_bo_size float _steps_total_volume = i_steps_bo_price * _steps_bo_size //base order table.cell(steps_amount, 0, 1, 'BO', text_color=color.white, text_size=size.small) table.cell(steps_amount, 1, 1, '0', text_color=color.white, text_size=size.small) table.cell(steps_amount, 2, 1, str.tostring(math.round(_steps_bo_size, i_decimals)), text_color=color.white, text_size=size.small) ``` -------------------------------- ### Define RSI-7 Start Value Source: https://github.com/tradelexx/trade_lexx/blob/main/Zendog V3 backtest DCA bot 3commas.txt Defines the input value for the Relative Strength Index (RSI) indicator, used as a trigger for starting a trading deal. It specifies the default value, title, and grouping for the input field. ```pinescript rsi_start_value = input.int(defval=30, title='RSI-7 Value', group='RSI7 (if used for Deal start trigger)') ``` -------------------------------- ### Populate Trading Table with Safety Orders Source: https://github.com/tradelexx/trade_lexx/blob/main/3C QFL Mean reversal.txt This snippet details the logic for populating a trading table with key financial metrics. It calculates and displays initial trade data and then iterates through safety orders, updating cumulative size, volume, and required prices for take profit. It handles two distinct calculation paths based on whether safety orders are defined by quantity or USD value. ```pinescript table.cell(steps_amount, 3, 1, str.tostring(math.round(i_steps_bo_price * _steps_bo_size, i_decimals)), text_color=color.white, text_size=size.small) table.cell(steps_amount, 4, 1, str.tostring(math.round(i_steps_bo_price, i_decimals)), text_color=color.white, text_size=size.small) table.cell(steps_amount, 5, 1, str.tostring(math.round(_steps_total_volume / _steps_total_size, i_decimals)), text_color=color.white, text_size=size.small) _req_price_for_tp = steps_required_price(i_steps_bo_price, _steps_total_volume / _steps_total_size, _steps_total_size, i_take_profit_perc) table.cell(steps_amount, 6, 1, str.tostring(math.round(_req_price_for_tp, i_decimals)), text_color=color.white, text_size=size.small) table.cell(steps_amount, 7, 1, str.tostring(steps_required_percent(i_steps_bo_price, _req_price_for_tp)), text_color=color.white, text_size=size.small) table.cell(steps_amount, 8, 1, str.tostring(steps_required_percent(i_steps_bo_price, i_steps_bo_price)), text_color=color.white, text_size=size.small) table.cell(steps_amount, 9, 1, str.tostring(math.round(_steps_total_size, i_decimals)), text_color=color.white, text_size=size.small) table.cell(steps_amount, 10, 1, str.tostring(_steps_total_volume), text_color=color.white, text_size=size.small) float _steps_next_safety_order_price = 0 float _steps_next_safety_order_qty = 0 if i_max_safety_orders > 0 for _i = 1 to i_max_safety_orders by 1 _steps_next_safety_order_price := next_so_price(_i, i_steps_bo_price) _steps_next_safety_order_qty := next_so_qty(_i, i_steps_bo_price) _steps_total_size += _steps_next_safety_order_qty _steps_total_volume += _steps_next_safety_order_qty * _steps_next_safety_order_price table.cell(steps_amount, 0, _i + 2, str.tostring(_i), text_color=color.white, text_size=size.small) table.cell(steps_amount, 1, _i + 2, str.tostring(stepped_deviation(_i)), text_color=color.white, text_size=size.small) table.cell(steps_amount, 2, _i + 2, str.tostring(math.round(_steps_next_safety_order_qty, i_decimals)), text_color=color.white, text_size=size.small) table.cell(steps_amount, 3, _i + 2, str.tostring(math.round(_steps_next_safety_order_qty * _steps_next_safety_order_price, i_decimals)), text_color=color.white, text_size=size.small) table.cell(steps_amount, 4, _i + 2, str.tostring(math.round(_steps_next_safety_order_price, i_decimals)), text_color=color.white, text_size=size.small) table.cell(steps_amount, 5, _i + 2, str.tostring(math.round(_steps_total_volume / _steps_total_size, i_decimals)), text_color=color.white, text_size=size.small) _req_price_for_tp := steps_required_price(i_steps_bo_price, _steps_total_volume / _steps_total_size, _steps_total_size, i_take_profit_perc) table.cell(steps_amount, 6, _i + 2, str.tostring(math.round(_req_price_for_tp, i_decimals)), text_color=color.white, text_size=size.small) table.cell(steps_amount, 7, _i + 2, str.tostring(steps_required_percent(_steps_next_safety_order_price, _req_price_for_tp)), text_color=color.white, text_size=size.small) table.cell(steps_amount, 8, _i + 2, str.tostring(steps_required_percent(_steps_next_safety_order_price, _steps_total_volume / _steps_total_size)), text_color=color.white, text_size=size.small) table.cell(steps_amount, 9, _i + 2, str.tostring(math.round(_steps_total_size, i_decimals)), text_color=color.white, text_size=size.small) table.cell(steps_amount, 10, _i + 2, str.tostring(_steps_total_volume), text_color=color.white, text_size=size.small) else if i_max_safety_orders > 0 float _steps_next_safety_order_qty_usd = 0 for _i = 1 to i_max_safety_orders by 1 _steps_next_safety_order_qty_usd := next_so_size_usd(_i) _steps_total_volume += _steps_next_safety_order_qty_usd table.cell(steps_amount, 0, _i + 2, str.tostring(_i), text_color=color.white, text_size=size.small) table.cell(steps_amount, 1, _i + 2, "-", text_color=color.white, text_size=size.small) table.cell(steps_amount, 2, _i + 2, "-", text_color=color.white, text_size=size.small) table.cell(steps_amount, 3, _i + 2, str.tostring(math.round(_steps_next_safety_order_qty_usd, i_decimals)), text_color=color.white, text_size=size.small) table.cell(steps_amount, 4, _i + 2, "-", text_color=color.white, text_size=size.small) table.cell(steps_amount, 5, _i + 2, "-", text_color=color.white, text_size=size.small) ``` -------------------------------- ### Get Orange Background Color Source: https://github.com/tradelexx/trade_lexx/blob/main/Zendog V3 backtest DCA bot 3commas.txt Creates an orange background color with a specified transparency level. The base color is #FFA500. ```pinescript get_bg_color_orange(transp=0) _bgcolor = color.new(#FFA500, transp) ``` -------------------------------- ### Trade Execution and Styling Source: https://github.com/tradelexx/trade_lexx/blob/main/3C QFL Mean reversal.txt Initializes trade execution parameters and applies visual styling based on conditions. It uses conditional logic to determine background colors for visual cues. ```pinescript _col = (i_max_safety_orders == 0 or na(c_next_safety_order_price)) ? get_bg_color_red(90) : get_bg_color_red(100) fill(p2, p4, color=_col, title='Fill stop loss') ``` -------------------------------- ### Get Red Background Color Source: https://github.com/tradelexx/trade_lexx/blob/main/Zendog V3 backtest DCA bot 3commas.txt Creates a red background color with a specified transparency level. The base color is #E59B9B. ```pinescript get_bg_color_red(transp=0) _bgcolor = color.new(#E59B9B, transp) ``` -------------------------------- ### Populate Trade Table Cells Source: https://github.com/tradelexx/trade_lexx/blob/main/3Commas Bot DCA Backtester & Signals FREE.txt This snippet demonstrates populating a table with various financial metrics and settings. It uses the `table.cell` function to define the content, styling, and position of each cell within a specified table structure. Dependencies include a `dataTable` object and variables like `grossProfit`, `netProfit`, etc. ```pinescript table.cell(table_id=dataTable, column=0, row=12, text=grossProfit, height=0, text_color=color.white, text_halign=text.align_left, text_valign= text.align_center) table.cell(table_id=dataTable, column=0, row=13, text=netProfit, height=0, text_color=color.white, text_halign=text.align_left, text_valign= text.align_center) table.cell(table_id=dataTable, column=0, row=14, text=percentProfit, height=0, text_color=color.white, text_halign=text.align_left, text_valign= text.align_center) table.cell(table_id=dataTable, column=0, row=15, text=equity, height=0, text_color=color.white, text_halign=text.align_left, text_valign= text.align_center) table.cell(table_id=dataTable, column=0, row=16, text=initialCapital, height=0, text_color=color.white, text_halign=text.align_left, text_valign= text.align_center) table.cell(table_id=dataTable, column=1, row=0, text="Settings", height=0, text_color=color.white, text_halign=text.align_left, text_valign= text.align_center, bgcolor=color.navy) table.cell(table_id=dataTable, column=1, row=1, text=stoplossText, height=0, text_color=color.white, text_halign=text.align_left, text_valign= text.align_center, bgcolor=color.purple) table.cell(table_id=dataTable, column=1, row=2, text="TP " + str.tostring(takeProfit*100) + "%", height=0, text_color=color.white, text_halign=text.align_left, text_valign= text.align_center, bgcolor=color.purple) table.cell(table_id=dataTable, column=1, row=3, text="AD " + str.tostring(avgDown*100) + "%", height=0, text_color=color.white, text_halign=text.align_left, text_valign= text.align_center, bgcolor=color.purple) table.cell(table_id=dataTable, column=1, row=4, text="AD%M " + str.tostring(percentScale) + "x", height=0, text_color=color.white, text_halign=text.align_left, text_valign= text.align_center, bgcolor=color.purple) table.cell(table_id=dataTable, column=1, row=5, text="VM " + str.tostring(multiplier) + "x", height=0, text_color=color.white, text_halign=text.align_left, text_valign= text.align_center, bgcolor=color.purple) table.cell(table_id=dataTable, column=1, row=6, text="M#O " + str.tostring(maxOrders), height=0, text_color=color.white, text_halign=text.align_left, text_valign= text.align_center, bgcolor=color.purple) table.cell(table_id=dataTable, column=1, row=7, text="BO $" + str.tostring(quantity), height=0, text_color=color.white, text_halign=text.align_left, text_valign= text.align_center, bgcolor=color.purple) ``` -------------------------------- ### Get Green Background Color Source: https://github.com/tradelexx/trade_lexx/blob/main/Zendog V3 backtest DCA bot 3commas.txt Creates a green background color with a specified transparency level. The base color is #A6E59B. ```pinescript get_bg_color_green(transp=0) _bgcolor = color.new(#A6E59B, transp) ```