Description of the strategy.
The «Grid TLong V1» strategy is based on the classic grid strategy, but in the buying and selling mode in favor of the trend and only in long positions. This allows for taking advantage of large bullish movements to maximize profits in trending markets. For this reason, excessively sideways or bearish markets may not be very conducive to this strategy. Like our trend-favoring grid strategies, one can enter and exit with the total or partial balance and with controlled risk, as the distance between each grid acts as a natural and adaptable stop loss and take profit.
The difference with bidirectional strategies is that in short it uses a minimal amount of tracking, so the percentage distance between the grids is maintained. In this version of the script, entries and exits can be chosen as market [0] or limit [1], and are based on the profit or loss of the current position, not on the percentage change of the price.
The user can also observe that the risk in the strategy configuration is controlled, as they risk 5% on each trade, have a fairly standard commission, and a modest initial capital, all in order to protect the user from unrealistic outcome strategies. As with all strategies, it is strongly recommended to optimize the parameters so that the strategy is effective for each asset and each time frame.
Explanation of the code.
Block 6-8: In this block, the input variables are defined: the percentage distance between each grid, the type of order to be chosen (market [0] or limit [1]), and the exit price of the most recent transaction, which will be used to recalculate the size of the grid each time one enters or exits the market.
Block 10-37: Just as with other versions of our grid strategies, in this section or block of code, the functions that will later be invoked in the main body of the script are written. The user can observe that four functions are required, as the logical conditions that invoke them cannot be simplified to fewer than four; the user can also notice that the amount risked in short is 0.0001, while in long it enters with the predefined size at the beginning. Of course, this is done because almost no broker would accept transactions of infinitesimal sizes, except perhaps in cryptocurrencies, as the purpose of that amount is simply to keep track, not to enter the market. These limitations are due to the capabilities of the programming language itself, as in Python, one could resort to more solutions without the need for this workaround.
Block 39-50: This block expresses the main body of the algorithm and is where the logical rules that the strategy must follow are explicitly defined; let us remember that, to simplify and optimize it, functions have been invoked instead of writing the functions themselves in each code block. Note that the resource «strategy.position_size» is still used to determine whether the position is empty (first transaction), is long, or is short, which is the core of the strategy. Once the side of the position is determined, an additional conditional is added: «strategy.opentrades.profit_percent(strategy.opentrades - 1)», which serves to know if the position in question is in profit or loss at the moment it reaches the next level of the grid.
Block 52: Finally, we use the plot() function to display on the current price chart the levels where our entry variable has experienced a quantitative change, thus visually establishing the grid levels.
This strategy is available for free use in the TradingView® community library of indicators and strategies.