Im trying to make a grid hedging strategy on trading view, how can I enable hadging?
this is my code:
//@version=5
strategy(“Grid Hedging Trading Algorithm with Hedging”, overlay=true)
// Inputs
grid_multiplier = input(5, title=”ATR Multiplier for Grid Spread”)
// Calculate the ATR
atr_length = 14
atr = ta.atr(atr_length)
// Calculate the grid spread
grid_spread = atr * grid_multiplier
// Initialize variables
var float grid_start_price = na
if (na(grid_start_price))
grid_start_price := close
// Calculate the current grid level
grid_level = math.round((close – grid_start_price) / grid_spread)
// Track positions
var float last_buy_price = na
var float last_sell_price = na
// Entry conditions
new_grid_crossed = not na(grid_level[1]) and grid_level != grid_level[1]
// Take profit conditions
take_profit_buy = not na(last_buy_price) and close > last_buy_price + grid_spread
take_profit_sell = not na(last_sell_price) and close < last_sell_price – grid_spread
// Buy and Sell signals at each grid level crossing
if (new_grid_crossed)
strategy.entry("Buy", strategy.long)
strategy.entry("Sell", strategy.short)
last_buy_price := close
last_sell_price := close
// Take profit execution
if (take_profit_buy)
strategy.close("Buy")
last_buy_price := na
if (take_profit_sell)
strategy.close("Sell")
last_sell_price := na
// Close all positions at the end of each day (assume the market closes at 16:00)
market_close_hour = 16
market_close_minute = 0
if (hour == market_close_hour and minute == market_close_minute)
strategy.close_all()
// Plot the grid lines for visualization
grid_line_color = color.new(color.blue, 50)
var line[] grid_lines = array.new_line(0)
for i = -10 to 10
float hline_price = grid_start_price + i * grid_spread
if (bar_index == 0)
array.push(grid_lines, line.new(x1=bar_index, y1=hline_price, x2=bar_index + 1, y2=hline_price, color=grid_line_color))
else
line.set_xy1(array.get(grid_lines, i + 10), x=bar_index, y=hline_price)
line.set_xy2(array.get(grid_lines, i + 10), x=bar_index + 1, y=hline_price)
// Explanatory comments:
// 1. Define the strategy and its overlay on the chart.
// 2. Inputs: grid multiplier to determine the grid spread.
// 3. Calculate the Average True Range (ATR) over a period to determine market volatility.
// 4. Determine the grid spread by multiplying the ATR by the grid multiplier.
// 5. Initialize variables to store the grid start price.
// 6. Calculate the current grid level based on the grid start price and the grid spread.
// 7. Track the last buy and sell prices to manage take profit conditions.
// 8. Define conditions for placing buy and sell orders at each grid level crossing.
// 9. Place buy and sell orders when a new grid level is crossed.
// 10. Define take profit conditions for buy and sell orders.
// 11. Close buy and sell orders when take profit conditions are met and reset the price levels.
// 12. Close all positions at the end of each day (assuming the market closes at 16:00).
// 13. Plot the grid lines on the chart for visualization purposes using the line.new
function.
this is made by chatGPT so pls dont kill me if this code is shitty
נועם פוזיילוב is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.