Description of the strategy.
The «Grid Tendence V2» strategy is based on the classic grid strategy, but in the buying and selling mode in favor of the trend. Unlike Version 1, this includes both directions and the choice of limit or market orders. It allows you to take advantage of large directional movements to maximize profits. Just like with our trend-following grid strategies, you can enter and exit with a controlled risk balance, as the distance between each grid acts as natural and adaptable stop loss and take profit levels.
As mentioned, in this version of the script, entries and exits can be chosen as market [0] or limit [1], and are based on the percentage gain or loss of the current position, not on the percentage change in price. The user will notice that the strategy settings are controlled by risk, as it risks 5% on each trade, has a fairly standard commission, and a modest initial capital, all in order to protect the user from unrealistic outcomes. As with all strategies, it is highly recommended to optimize the parameters without bias so that the strategy is effective for each asset and time frame.
Explanation of the code.
Block 6-8: Just like with Version 1 of this strategy, we define the two input variables, one for the percentage distance between each grid and the other to store the price of the most recent operation; additionally, we add a sort of condition that acts as a button for the user to choose whether they want the strategy to execute limit orders or market orders.
Block 10-37: Similarly, in this block we define the functions that will be invoked in the main body of the algorithm; however, the user may notice that in this case we do need four functions, as the conditions are different in each function; furthermore, one may realize that there are internal conditionals, depending on whether the user chose limit orders or market orders. Although it is true that the code is extensive and that the functions could have been defined in the body of the algorithm, functions have been chosen, as recycling repetitive operations is one of the ways in which functions can help optimize the algorithm.
Block 39-50: The body of the algorithm in this block is practically the same as in Version 1, as the logical conditions are established and the functions that will form the control center of the strategy are invoked.
Block 52: Finally, we add a brief statement so that the most recent exit prices of the trades made by the grid are visually formed in the main chart. Indeed, it is about visually tracking the exact prices that make up the operations of the strategy.
This strategy is available for free use in the TradingView® community library of indicators and strategies.