Editors' picksPINE LIBRARY

TradingToolsLibrary

Library "TradingToolsLibrary"
Easily create advanced entries, exits, filters and qualifiers to simulate strategies. Supports DCA (Dollar Cost Averaging) Lines, Stop Losses, Take Profits (with trailing or without) & ATR.

method deepCopy(this)
  This creates a deep copy instead of a shallow copy of an entry_position. This does NOT deep copy the self_pyramiding_positions array reference, since only the master entry_position needs this to track the rest of its copies for efficiency reasons. This is to prevent a feedback loop.
  Namespace types: entry_position
  Parameters:
    this (entry_position)
  Returns: entry_position

method precision_fix(this, precision)
  Convert a floating point number to a precise floating point number with digit precision to avoid floating point errors in quantity calculations.
  Namespace types: series float, simple float, input float, const float
  Parameters:
    this (float)
    precision (int)
  Returns: float

xSellBuyMidInterpolation(_x, _high, _low, _sellRange, _buyRange)
  Creates an interpolation for a sell range and buy range but with an emphasis on reaching the _low the closer to the middle of the _sell and _buy range you go.
  Parameters:
    _x (float): is the value you want to use to control interpolation bewteen the _high and _low value. This will return the lowest percentage at the mid between high and low and highest percentage at the _high and _low.
    _high (float)
    _low (float)
    _sellRange (float)
    _buyRange (float)
  Returns: an interpolated float between the _high and _low supplied.

xSellBuyInterpolation(_x, _high, _low, _sellRange, _buyRange)
  Creates an interpolation a sell range and buy range
  Parameters:
    _x (float): is the value you want to use to control interpolation bewteen the _high and _low value.
    _high (float)
    _low (float)
    _sellRange (float)
    _buyRange (float)
  Returns: an interpolated float between the _high and _low supplied.

activate_entries_and_exits(_entries, _exits, _filters, _qualifiers, _equity)
  Determines activation for entries or exits. Does not place the actual orders.
  Parameters:
    _entries (entry_position[])
    _exits (exit_position[])
    _filters (filter[])
    _qualifiers (qualifier[])
    _equity (equity_management)
  Returns: void

create_entries_and_exits(_entries, _exits, _equity)
  Creates actual entry and exit orders if activated
  Parameters:
    _entries (entry_position[])
    _exits (exit_position[])
    _equity (equity_management)
  Returns: void

filter
  Fields:
    disabled (series__bool)
    filter_for_entries_or_exits (series__string)
    filter_for_groups (series__string)
    condition (series__bool)
    dynamic_condition (series__bool)
    use_dynamic_condition (series__bool)
    use_override_default_condition (series__bool)
    dynamic_condition_operator (series__string)
    dynamic_condition_source (series__float)
    dynamic_compare_source (series__float)
    dynamic_condition_source_prior (series__float)
    dynamic_compare_source_prior (series__float)
    use_dynamic_compare_source (series__bool)
    dynamic_condition_activate_value (series__string)
    expire_condition_activate_value (series__string)
    expire_condition_source (series__float)
    expire_condition_source_prior (series__float)
    expire_compare_source (series__float)
    expire_compare_source_prior (series__float)
    use_expire_compare_source (series__bool)
    expire_condition_operator (series__string)

qualifier
  Fields:
    disabled (series__bool)
    qualify_for_entries_or_exits (series__string)
    qualify_for_groups (series__string)
    disqualify (series__bool)
    condition (series__bool)
    dynamic_condition (series__bool)
    use_dynamic_condition (series__bool)
    use_override_default_condition (series__bool)
    dynamic_condition_operator (series__string)
    dynamic_condition_source (series__float)
    dynamic_compare_source (series__float)
    dynamic_condition_source_prior (series__float)
    dynamic_compare_source_prior (series__float)
    use_dynamic_compare_source (series__bool)
    dynamic_condition_activate_value (series__string)
    expire_after_x_bars (series__integer)
    use_expire_after_x_bars (series__bool)
    use_expire_condition (series__bool)
    use_override_expire_condition (series__bool)
    expire_condition_operator (series__string)
    expire_condition_source (series__float)
    expire_compare_source (series__float)
    expire_condition_source_prior (series__float)
    expire_compare_source_prior (series__float)
    use_expire_compare_source (series__bool)
    expire_condition_activate_value (series__string)
    active (series__bool)
    expire_after_bars_bar_index (series__integer)
    expire_after_bars_bar_index_prior (series__integer)
    expire_bar_count (series__integer)
    expire_bar_changed (series__bool)

entry_position
  Fields:
    disabled (series__bool)
    activate (series__bool)
    active (series__bool)
    override_occured (series__bool)
    passDebug (array__bool)
    initial_activation_price (series__float)
    dca_done (series__bool)
    condition (series__bool)
    dynamic_condition (series__bool)
    use_dynamic_condition (series__bool)
    use_override_default_condition (series__bool)
    dynamic_condition_operator (series__string)
    dynamic_condition_source (series__float)
    dynamic_compare_source (series__float)
    dynamic_condition_source_prior (series__float)
    dynamic_compare_source_prior (series__float)
    use_dynamic_compare_source (series__bool)
    dynamic_condition_activate_value (series__string)
    use_cash (series__bool)
    use_percent_equity (series__bool)
    percent_equity_amount (series__float)
    cash_amount (series__float)
    position_size (series__float)
    total_position_size (series__float)
    prior_total_position_size (series__float)
    equity_remaining (series__float)
    prior_equity_remaining (series__float)
    initial_equity (series__float)
    use_martingale (series__bool)
    martingale_win_ratio (series__float)
    martingale_lose_ratio (series__float)
    martingale_win_limit (series__integer)
    martingale_lose_limit (series__integer)
    martingale_limit_reset_mode (series__string)
    use_dynamic_percent_equity (series__bool)
    dynamic_percent_equity_amount (series__float)
    initial_dynamic_percent_equity_amount (series__float)
    dynamic_percent_equity_source (series__float)
    dynamic_percent_equity_min (series__float)
    dynamic_percent_equity_max (series__float)
    dynamic_percent_equity_source_sell_range (series__float)
    dynamic_percent_equity_source_buy_range (series__float)
    dynamic_equity_interpolation_method (series__string)
    total_bars (series__integer)
    bar_index_at_activate (series__integer)
    bars_since_active (series__integer)
    time_at_activate (series__integer)
    time_since_active (series__integer)
    bar_index_at_activated (series__integer)
    bar_index_at_pyramid_change (series__integer)
    name (series__string)
    id (series__string)
    group (series__string)
    pyramiding_limit (series__integer)
    self_pyramiding_limit (series__integer)
    self_pyramiding_positions (array__|entry_position|#OBJ)
    new_pyramid_cancels_dca (series__bool)
    num_active_long_positions (series__integer)
    num_active_short_positions (series__integer)
    num_active_positions (series__integer)
    position_remaining (series__float)
    prior_position_remaining (series__float)
    direction (series__string)
    allow_flip_position (series__bool)
    flip_occurred (series__bool)
    ignore_flip (series__bool)
    use_dca (series__bool)
    dca_use_limit (series__bool)
    dca_num_positions (series__integer)
    dca_positions (array__float)
    dca_deviation_percentage (series__float)
    dca_scale (series__float)
    dca_percentages (series__string)
    dca_close_cancels (series__bool)
    dca_active_positions (series__integer)
    use_atr_deviation (series__bool)
    dca_atr_length (series__integer)
    dca_atr_mult (series__float)
    dca_atr_updates_dca_positions (series__bool)
    close_price_at_order (series__float)
    dca_use_deviation_atr_min (series__bool)
    dca_position_quantities (array__float)
    use_dca_dynamic_percent_equity (series__bool)
    dca_in_use (array__bool)
    dca_activated (array__bool)
    dca_money_used (array__float)
    dca_lines (array__line)
    dca_color (series__color)
    show_dca_lines (series__bool)
    atr_value (series__float)
    atr_value_at_activation (series__float)
    use_cooldown_bars (series__bool)
    cooldown_bars (series__integer)
    cooldown_bar_changed (series__bool)
    cooldown_bar_index (series__integer)
    cooldown_bar_index_prior (series__integer)
    cooldown_bar_change_count (series__integer)
    expire_condition_activate_value (series__string)
    expire_condition_source (series__float)
    expire_condition_source_prior (series__float)
    expire_compare_source (series__float)
    expire_compare_source_prior (series__float)
    use_expire_compare_source (series__bool)
    expire_condition_operator (series__string)

exit_position
  Fields:
    disabled (series__bool)
    id (series__string)
    group (series__string)
    exit_for_entries (series__string)
    exit_for_groups (series__string)
    total_bars (series__integer)
    name (series__string)
    condition (series__bool)
    dynamic_condition (series__bool)
    use_dynamic_condition (series__bool)
    use_override_default_condition (series__bool)
    dynamic_condition_operator (series__string)
    dynamic_condition_source (series__float)
    dynamic_compare_source (series__float)
    dynamic_condition_source_prior (series__float)
    dynamic_compare_source_prior (series__float)
    use_dynamic_compare_source (series__bool)
    dynamic_condition_activate_value (series__string)
    activate (series__bool)
    active (series__bool)
    reset_equity (series__bool)
    use_limit (series__bool)
    use_alerts (series__bool)
    reset_entry_cooldowns (series__bool)
    prevent_new_entries_on_partial_close (series__bool)
    show_activation_zone (series__bool)
    use_average_position (series__bool)
    source_value (series__float)
    trigger_x_times (series__integer)
    amount_of_times_triggered (series__integer)
    quantity_percent (series__float)
    trade_qty (series__float)
    exit_amount (series__float)
    entries_exiting_for (array__|entry_position|#OBJ)
    atr_value (series__float)
    update_atr (series__bool)
    use_activate_after_bars (series__bool)
    show_activate_after_bars (series__bool)
    activate_after_bars (series__integer)
    activate_after_bars_bar_changed (series__bool)
    activate_after_bars_bar_index (series__integer)
    activate_after_bars_bar_index_prior (series__integer)
    activate_after_bars_bar_change_count (series__integer)
    all_conditions_pass (series__bool)
    use_close_if_profit_only (series__bool)
    profit_value (series__float)
    exit_type (series__string)
    exit_modifier (series__string)
    update_atr_with_new_pyramid (series__bool)
    percentage (series__float)
    activation_percentage (series__float)
    atr_multiplier (series__float)
    use_cancel_if_percent (series__bool)
    cancel_if_percent (series__float)
    activation_value (series__float)
    activation_value_crossed (series__bool)
    exit_value (series__float)
    hypo_long_exit_value (series__float)
    hypo_short_exit_value (series__float)
    close_exit_value (series__float)
    debug (series__float)
    expire_condition_activate_value (series__string)
    expire_condition_source (series__float)
    expire_condition_source_prior (series__float)
    expire_compare_source (series__float)
    expire_compare_source_prior (series__float)
    use_expire_compare_source (series__bool)
    expire_condition_operator (series__string)

equity_management
  Fields:
    equity (series__float)
    prior_equity (series__float)
    position_used (series__float)
    prior_position_used (series__float)
    prevent_future_entries (series__bool)
    minimum_order_size (series__float)
    decimal_rounding_precision (series__integer)
    direction (series__string)
    show_order_info_in_comments (series__bool)
    show_order_info_in_labels (series__bool)
    allow_longs (series__bool)
    allow_shorts (series__bool)
    override_occured (series__bool)
    flip_occured (series__bool)
    num_concurrent_wins (series__integer)
    num_concurrent_losses (series__integer)
    first_entry (|entry_position|#OBJ)
    num_win_trades (series__integer)
    num_losing_trades (series__integer)
strategies

Pine library

In true TradingView spirit, the author has published this Pine code as an open-source library so that other Pine programmers from our community can reuse it. Cheers to the author! You may use this library privately or in other open-source publications, but reuse of this code in a publication is governed by House rules.


-=Gentleman Goat=- Download the TradingView Input Optimizer at tradingtools.software/optimizer
Discord: discord.gg/pGHHRczpbu
Also on:

Disclaimer