alzahrania2
Pine Script Rookie
Pine Script Rookie
Posts: 1
Joined: January 7th, 2024

Pine Script Issue Troubleshooting and Fixing tradingview

PINESCRIPT ISSUE DESCRIPTION

BACKGROUND OF THE SCRIPT:

The script is a trading strategy, written as an indicator, that performs all the computations of a typical pinescript strategy script. These computations include trading signal generation from the core strategy logic, handling/tracking orders (pending and canceled orders) and trades (active and closed orders), as well as the tracking and visualization of performance metrics. The script contains 3000+ lines of code performing all these tasks; fairly large, yes, but also optimized to avoid Pinescript’s time limit and maximum token errors.

The choice to develop an indicator rather than a strategy is for more control over pending orders; ability to delay, modify, filter and cancel specific orders based on different criteria.

For simulating real time order fills in historical bars, the script imports and iterates through lower timeframe data on each bar to handle order fills (basically the bar magnifier feature in tradingview strategy). [check the attached code snippet for the implementation]. Yes, this implies that the script runs the logic a couple of times on each bar, hence might be computationally expensive. Nonetheless, we have implemented proper data structures and algorithms that fit the problem, and improves time efficiency.

``` pinescript
for [index, bar] in _array
if (...) and (i_use_time_limit and (bar._time > _limit))
break

// Loop through orders
array_pending_orders.manage_pending(bar, cond_allow_entry)
_pnl += array_active_orders.manage_active(bar, cond_allow_exit)
if i_use_safety_orders
_pnl += array_active_orders.manage_safety(bar)
f_special_exits(bar)

_array : Array containing lower timeframe candlesticks data
```


ISSUE AND ATTEMPTED SOLUTIONS:
When an order is filled, an alert is sent and a label is plotted at the entry price. However, the label experiences a delay before it appears, and consequently appears the same time the bar closes, during tests using the replay feature.

So far, these are the proffered solutions that we have implemented to avoid this:

1. When iterating through historical lower timeframe bars, we omit the last bar in the array to avoid order fills, using that bar. This was proposed to avoid order fills and hence alerts appearing only on the close of the bar.
2. There is a time limit setting for the maximum time limit within the bar, for order fills to be monitored. For example, on a Daily timeframe/bar, the time limit could be set to the first 10 hours. Any orders whose prices get filled after this limit will simply be ignored.
Both of these monitor the time of the lower timeframe bars for their implementation.
3. Alert frequency is set to `alert.freq_once_per_bar ` so, the alert can be sent anytime within the bar, and doesn’t wait for the bar to close.

Let us know what insights/ideas you may have, or if you need more information about how a feature works.

Return to “Pine Script Q&A”