Please, copy the code below

  • Instructions

    1. Highlight the entire code below

    2. Copy The Code (Ctrl-C, Cmd-C)

    3. Open TradingView

    4. Open Pine Editor

    5. Paste The Code

    6. Click Save

    7. Click “Update” on chart

//@version=5
indicator("Gold Trend Trader", shorttitle="Gold Trader", overlay=true)

// User-defined input for moving averages
shortMA = input.int(10, minval=1, title="Short MA Period")
longMA = input.int(100, minval=1, title="Long MA Period")
target_1 = input.int(5, minval=1, title="Target_1")
target_2 = input.int(10, minval=1, title="Target_2")

// User-defined input for the start and end times with default values
startTimeInput = input.int(12, title="Start Time for  Session(UTC, in hours)", minval=0, maxval=23)
endTimeInput = input.int(17, title="End Time Session (UTC, in hours)", minval=0, maxval=23)
// Convert the input hours to minutes from midnight
startTime = startTimeInput * 60 
endTime = endTimeInput * 60  

// Function to convert the current exchange time to UTC time in minutes
toUTCTime(exchangeTime) =>
    exchangeTimeInMinutes = exchangeTime / 60000
    // Adjust for UTC time
    utcTime = exchangeTimeInMinutes % 1440
    utcTime

// Get the current time in UTC in minutes from midnight
utcTime = toUTCTime(time)

// Check if the current UTC time is within the allowed timeframe
isAllowedTime = (utcTime >= startTime and utcTime < endTime)
// Apply yellow background color during the allowed time range
bgcolor(isAllowedTime ? color.new(color.yellow, 90) : na)

// Calculating moving averages
shortMAValue = ta.sma(close, shortMA)
longMAValue = ta.sma(close, longMA)

// Plotting the MAs
plot(shortMAValue, title="Short MA", color=color.blue)
plot(longMAValue, title="Long MA", color=color.red)

// Tracking buy and sell signals
var float buyEntryPrice_1 = na
var float buyEntryPrice_2 = na
var float sellEntryPrice_1 = na
var float sellEntryPrice_2 = na

// Logic for Buy and Sell signals
buySignal = ta.crossover(shortMAValue, longMAValue) and isAllowedTime
sellSignal = ta.crossunder(shortMAValue, longMAValue) and isAllowedTime

// Update last buy and sell signal values
if (buySignal)
    buyEntryPrice_1 := close
    buyEntryPrice_2 := close
else if (sellSignal)
    sellEntryPrice_1 := close
    sellEntryPrice_2 := close
    
// Apply background color for entry candles
barcolor(buySignal or sellSignal ? color.yellow : na)

/// Creating buy and sell labels
if (buySignal)
    label.new(bar_index, low, text="BUY", style=label.style_label_up, color=color.green, textcolor=color.white, yloc=yloc.belowbar)

if (sellSignal)
    label.new(bar_index, high, text="SELL", style=label.style_label_down, color=color.red, textcolor=color.white, yloc=yloc.abovebar)

// Creating labels for 100-point movement
if (not na(buyEntryPrice_1) and close >= buyEntryPrice_1 + target_1)
    label.new(bar_index, high, text=str.tostring(target_1), style=label.style_label_down, color=color.green, textcolor=color.white, yloc=yloc.abovebar)
    buyEntryPrice_1 := na // Reset after label is created

if (not na(buyEntryPrice_2) and close >= buyEntryPrice_2 + target_2)
    label.new(bar_index, high, text=str.tostring(target_2), style=label.style_label_down, color=color.green, textcolor=color.white, yloc=yloc.abovebar)
    buyEntryPrice_2 := na // Reset after label is created

if (not na(sellEntryPrice_1) and close <= sellEntryPrice_1 - target_1)
    label.new(bar_index, low, text=str.tostring(target_1), style=label.style_label_up, color=color.red, textcolor=color.white, yloc=yloc.belowbar)
    sellEntryPrice_1 := na // Reset after label is created

if (not na(sellEntryPrice_2) and close <= sellEntryPrice_2 - target_2)
    label.new(bar_index, low, text=str.tostring(target_2), style=label.style_label_up, color=color.red, textcolor=color.white, yloc=yloc.belowbar)
    sellEntryPrice_2 := na // Reset after label is created