Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

create pinescript version 5 code for below strategy : if close price go to upper from…

  1. User

    create pinescript version 5 code for below strategy : if close price go to upper from HMA(105) buy command. if close price go to down from HMA(105) sell command. in two direction i want stop loss equals 0.7% and take profit equals to 1.4 %

  2. Assistant

    Here's the Pine script code for your strategy:

    //@version=5
    indicator("HMA Strategy", shorttitle="HS", overlay=true)
    
    // Input parameters
    hmaLength = input(105, title="HMA Length", type=input.integer)
    stopLoss = input(0.7, title="Stop Loss (%)", type=input.float) / 100
    takeProfit = input(1.4, title="Take Profit (%)", type=input.float) / 100
    
    // Calculate HMA
    hma = wma(2 * wma(close, hmaLength / 2) - wma(close, hmaLength), round(sqrt(hmaLength)))
    
    // Buy and Sell signals
    buySignal = crossover(close, hma)
    sellSignal = crossunder(close, hma)
    
    // Plot HMA
    plot(hma, title="HMA", color=color.blue, linewidth=2)
    
    // Strategy
    strategy.entry("Long", strategy.long, when=buySignal)
    strategy.entry("Short", strategy.short, when=sellSignal)
    strategy.exit("Exit Long", "Long", stop=close * (1 - stopLoss), limit=close * (1 + takeProfit))
    strategy.exit("Exit Short", "Short", stop=close * (1 + stopLoss), limit=close * (1 - takeProfit))
    
    // Plot Buy and Sell signals
    plotshape(buySignal, title="Buy", location=location.abovebar, color=color.green, style=shape.arrowup, text="Buy", textcolor=color.white)
    plotshape(sellSignal, title="Sell", location=location.belowbar, color=color.red, style=shape.arrowdown, text="Sell", textcolor=color.white)
    
    

    This code creates an indicator with an overlay on the chart, plots the HMA and marks buy and sell signals with arrows. The strategy includes a stop loss of 0.7% and a take profit equal to 1.4%.