Skip to content
The Chat Archive
gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)

code me pine script SMA moving average cross buy and sell signals for trading view

  1. User

    code me pine script SMA moving average cross buy and sell signals for trading view

  2. Assistant

    Here's the Pine Script for SMA moving average cross buy and sell signals:

    //@version=4
    study(title="SMA Cross Buy and Sell Signals", overlay=true)
    
    // Define Inputs
    fastLength = input(9, title="Fast Length")
    slowLength = input(21, title="Slow Length")
    
    // Calculate Moving Averages
    fastSMA = sma(close, fastLength)
    slowSMA = sma(close, slowLength)
    
    // Plot Moving Averages
    plot(fastSMA, color=color.blue, title="Fast SMA")
    plot(slowSMA, color=color.red, title="Slow SMA")
    
    // Generate Buy and Sell Signals
    buySignal = crossover(fastSMA, slowSMA)
    sellSignal = crossunder(fastSMA, slowSMA)
    
    // Plot Buy and Sell Signals
    plotshape(buySignal, color=color.green, style=shape.triangleup, size=size.small, location=location.belowbar, title="Buy Signal")
    plotshape(sellSignal, color=color.red, style=shape.triangledown, size=size.small, location=location.abovebar, title="Sell Signal")
    

    This Pine Script calculates the simple moving averages for the fast and slow lengths specified by the user using the sma() function. It then plots these moving averages and generates buy and sell signals based on the crossover and crossunder of the fast and slow moving averages using the crossover() and crossunder() functions. Finally, it plots these buy and sell signals using the plotshape() function.

  3. User

    code me pine script hull moving average cross buy and sell signals for trading view

  4. Assistant

    Here's the Pine Script for Hull Moving Average (HMA) cross buy and sell signals:

    //@version=4
    study(title="Hull MA Cross Buy and Sell Signals", overlay=true)
    
    // Define inputs
    len = input(title="HMA Length", type=input.integer, defval=9)
    src = input(title="Source", type=input.source, defval=close)
    
    // Calculate Hull Moving Average
    wma1 = wma(2*wma(src, len/2)-wma(src,len), round(sqrt(len)))
    hma = wma(2*wma1 - wma1[round(len/2)], round(sqrt(len)))
    
    // Calculate Signal Line
    signal = ema(hma, round(len/4))
    
    // Plot HMA and Signal Line
    plot(hma, color=color.blue, title="Hull MA")
    plot(signal, color=color.red, title="Signal Line")
    
    // Generate Buy and Sell Signals
    buySignal = crossover(hma, signal)
    sellSignal = crossunder(hma, signal)
    
    // Plot Buy and Sell Signals
    plotshape(buySignal, color=color.green, style=shape.triangleup, size=size.small, location=location.belowbar, title="Buy Signal")
    plotshape(sellSignal, color=color.red, style=shape.triangledown, size=size.small, location=location.abovebar, title="Sell Signal")
    

    This Pine Script calculates the Hull Moving Average (HMA) using the Weighted Moving Average (WMA) and the Exponential Moving Average (EMA) functions. It also calculates a signal line using the ema() function. It then plots the HMA and Signal Line using the plot() function. The buy and sell signals are generated using the crossover() and crossunder() functions and plotted using the plotshape() function.