Pinescript with ATR strategy

Discussion in 'Strategy Building' started by MrAgi1, Nov 17, 2023.

  1. MrAgi1

    MrAgi1

    • Write a pinescript strategy that places the following orders at 12: 00 AM GMT every working day on GBPUSD.

    1. 1 lot of GBPUSD buy stop order at ATR value+current candle open price.

    2. 1 lot of GBPUSD buy limit order at (-ATR value/3) +current candle open price.

    3. 1 lot of GBPUSD sell stop order at (-ATR value)+current candle open price.

    4. 1 lot of GBPUSD sell limit order at ATR value/3 +current candle open price.

    If trade 1 is activated, then traded 2 is deleted and vice versa. If trade 3 is activated, then trade 4 is deleted and vice versa.
     
  2. MrAgi1

    MrAgi1

    Not good with coding at all, so I tried to use some help online and arrived at the logic below. The problem is that I get the error message “your strategy did not generate any trade orders” in tradingview. Please who can help me solve the problem?


    //@version=5
    strategy("GBPUSD Orders", overlay=true)

    // Function to convert time to GMT
    to_GMT(_time) =>
    sydney = 'GMT+0'
    nz_time = _time
    new_time = request.security(sydney, "D", nz_time)
    new_time

    // Check if it's 12:00 AM GMT
    isLondonOpen = hour == 0 and minute == 0

    // Calculate ATR
    atrPeriod = input(14, title="ATR Period")
    atrValue = ta.atr(atrPeriod)

    // Calculate buy and sell levels
    buyStop = isLondonOpen ? close + atrValue : na
    buyLimit = isLondonOpen ? close - atrValue / 3 : na
    sellStop = isLondonOpen ? close - atrValue : na
    sellLimit = isLondonOpen ? close + atrValue / 3 : na

    // Plotting orders
    plotshape(series=buyStop, title="Buy Stop", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
    plotshape(series=buyLimit, title="Buy Limit", location=location.abovebar, color=color.green, style=shape.triangledown, size=size.small)
    plotshape(series=sellStop, title="Sell Stop", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small)
    plotshape(series=sellLimit, title="Sell Limit", location=location.belowbar, color=color.red, style=shape.triangleup, size=size.small)
     
  3. MarkBrown

    MarkBrown

    chat gpt professional edition.. i have not verified this code m

    //@version=5
    indicator("GBPUSD Orders", overlay = true)

    // Define session start time (12:00 AM GMT)
    sessionStartHour = input(0, title = "Session Start Hour (GMT)")
    sessionStartMinute = input(0, title = "Session Start Minute (GMT)")

    // Define ATR length
    atrLength = input(14, title = "ATR Length")

    // Calculate ATR value
    atrValue = ta.atr(atrLength)

    // Calculate entry prices for buy and sell orders
    buyStopPrice = atrValue + ta.open
    buyLimitPrice = (-atrValue / 3) + ta.open
    sellStopPrice = (-atrValue) + ta.open
    sellLimitPrice = (atrValue / 3) + ta.open

    // Calculate session time
    sessionTime = ta.hour * 60 + ta.minute

    // Initialize order variables
    var order1 = na
    var order2 = na
    var order3 = na
    var order4 = na

    // Condition to check if it's a working day and the session time is 12:00 AM GMT
    if ta.dayofweek >= 1 and ta.dayofweek <= 5 and sessionTime == sessionStartHour * 60 + sessionStartMinute
    // Check if order 1 is not active and order 2 is not active
    if na(order1) and na(order2)
    // Place buy stop order
    order1 := ta.buy_stop("Buy Stop", stop = buyStopPrice, qty = 1)

    // Check if order 3 is not active and order 4 is not active
    if na(order3) and na(order4)
    // Place sell stop order
    order3 := ta.sell_stop("Sell Stop", stop = sellStopPrice, qty = 1)

    // Check for trade activations and deletions
    if not na(order1) and ta.crossover(ta.close, buyLimitPrice)
    // Delete order 2 if it's active
    if not na(order2)
    ta.cancel(order2)
    // Delete order 3 if it's active
    if not na(order3)
    ta.cancel(order3)
    // Delete order 4 if it's active
    if not na(order4)
    ta.cancel(order4)

    if not na(order3) and ta.crossunder(ta.close, sellLimitPrice)
    // Delete order 1 if it's active
    if not na(order1)
    ta.cancel(order1)
    // Delete order 2 if it's active
    if not na(order2)
    ta.cancel(order2)
    // Delete order 4 if it's active
    if not na(order4)
    ta.cancel(order4)

    // Plot entry prices on the chart
    plotshape(series = buyStopPrice, title = "Buy Stop", location = location.belowbar, color = color.green, style = shape.triangleup, size = size.small)
    plotshape(series = buyLimitPrice, title = "Buy Limit", location = location.belowbar, color = color.green, style = shape.triangleup, size = size.small)
    plotshape(series = sellStopPrice, title = "Sell Stop", location = location.abovebar, color = color.red, style = shape.triangledown, size = size.small)
    plotshape(series = sellLimitPrice, title = "Sell Limit", location = location.abovebar, color = color.red, style = shape.triangledown, size = size.small)
     
    MrAgi1 likes this.