admin管理员组

文章数量:1376572

I am currently developing a grid strategy for the spot market on TradingView. In contrast to conventional approaches, the grids in my strategy are not placed in a fixed range, horizontally. Instead, each grid is calculated using the formula:
G(k) = ma ± atr * k, where k is a nonnegative integer. The asset is purchased from the grid immediately below the price and sold from the grid immediately above it, with each trade executed in units of one.

There are two key aspects to consider with this type of grid strategy. First, unlike other strategies that wait for the closing price, grid trading executes transactions as soon as the price touches a grid. To achieve this, I have set up limit buy orders for grids below the current price and limit sell orders for grids above it. Moreover, I have synchronized the id field in the strategy.entry function with the grid number, enabling the automatic update of order levels at the close of each bar.

Second, it is crucial to prevent multiple transactions from being executed on the same grid due to price fluctuations around that grid. To address this, I implemented a lock mechanism using a Boolean array, initially all elements are set to false. When the price touches a grid, the corresponding element in the array is set to true, and it is reset to false once the position is closed.

However, when I introduced the lock mechanism, I observed that the limit order entry and exit levels no longer update automatically. Instead, the orders seem to retain their initial grid values. I suspect that the lock mechanism may be incorrectly implemented, or perhaps I need to incorporate a method to cancel previously placed orders when no transaction is executed. As I am not very experienced with Pine Script, I am uncertain how to resolve this issue.

I would greatly appreciate any assistance or insights you might offer. Thank you for your time and efforts.

Note: The grids visible in the image are not included in the code below to avoid cluttering the strategy script due to the inability to run the plot function locally; they are instead displayed on the screen via a separate indicator. However, the calculation method and values are exactly the same.

Executed Trades, Image

// @version=6

strategy("Exp. Volume Bot | Maker β", "Maker β", true, pyramiding = 100, initial_capital = 850000, default_qty_type = strategy.fixed, default_qty_value = 1)

maLen = 200
atrLen = 500

maVal   = ta.sma(close, maLen)
atrVal  = ta.atr(atrLen)

int[] mults = array.from(-20, -16, -12, -8, -4, 0, 4, 8, 12, 16, 20) 
var float[] grids  = array.new_float(array.size(mults), maVal)
var bool[]  lock    = array.new_bool(array.size(mults), false)

// Order Logic ----
if bar_index >= math.max(maLen, atrLen)
    for i = 0 to array.size(mults) - 1
        array.set(grids, i, maVal + atrVal * array.get(mults, i))
        buy_id = "Grid " + str.tostring(i)
        sell_id = "Grid " + str.tostring(i-1)
        // Entry Logic ---
        if array.get(grids, i) < close and not array.get(lock, i)
            strategy.entry(buy_id, strategy.long, stop = array.get(grids, i), limit = array.get(grids, i))
            array.set(lock, i, true)
        
        // Exit Logic ---
        else if array.get(grids, i) > close and i != 0 and array.get(lock, i - 1)
            strategy.exit(buy_id, limit = array.get(grids, i))
            array.set(lock, i, false)

本文标签: tradingview apiCannot Implement a Lock Mechanism to my Grid Strategy in Pine ScriptStack Overflow