Welcome to the BEST forum for traders

Did you know we're sharing tons of exclusive content here?
Our community is growing quickly. Take a look around and say "Hello".

  • Forums
  • Pinescript
  • How to imitate the Long Position tool in tradingview using Pine script?

How to imitate the Long Position tool in tradingview using Pine script?

Mar 2, 2022 - 8:59 AM

Viewed 6598 times

https://best-trading-indicator.com/community/bti/forums/4180/topics/55976 COPY
  • My goal in writing the code is to backtest my trading strategy using the Ichi. When the conditions to go long have appeared an entry position will be opened. Immediately the stop loss is the cloud support level underneath that candle while the profit is 2x the risk as described in the picture. Since the cloud levels (leadLine 1 and leadLine2) are continuously changing I cannot set a fixed level which results in the trade being closed almost immediately after it opened. I don't know how to make it fixed on that position. I've tried several simple logics but so far any attempts have failed. Any idea how to make this will be highly appreciated.

    Code: //@version=4
    strategy(title=Ichimoku Cloud, shorttitle=RISK REWARD Ichimoku, overlay=true, initial_capital=1000, default_qty_value=100, default_qty_type=strategy.percent_of_equity)
    conversionPeriods = input(20, minval=1, title=Conversion Line Length)
    basePeriods = input(60, minval=1, title=Base Line Length)
    laggingSpan2Periods = input(120, minval=1, title=Leading Span B Length)
    displacement = input(30, minval=1, title=Displacement)
    Stoploss = input(1, minval=0.1, title=Stoploss (% below cloud))
    donchian(len) => avg(lowest(len), highest(len))
    conversionLine = donchian(conversionPeriods)
    baseLine = donchian(basePeriods)
    leadLine1 = avg(conversionLine, baseLine)
    leadLine2 = donchian(laggingSpan2Periods)
    plot(conversionLine, color=#2962FF, title=Conversion Line)
    plot(baseLine, color=#B71C1C, title=Base Line)
    plot(close, offset = -displacement + 1, color=#43A047, title=Lagging Span)

    p1 = plot(leadLine1, offset = displacement - 1, color=#A5D6A7,
    title=Leading Span A)
    p2 = plot(leadLine2, offset = displacement - 1, color=#EF9A9A,
    title=Leading Span B)
    fill(p1, p2, color = leadLine1 > leadLine2 ? color.rgb(67, 160, 71, 90) : color.rgb(244, 67, 54, 90))

    bool TKcross = conversionLine > baseLine
    bool aboveCloud = close > leadLine1 and close > leadLine2
    bool greenCloud = leadLine1 > leadLine2
    bool lagLong = close > leadLine1[2*displacement+1] and close > leadLine2[2*displacement+1] and close > close[displacement]
    bool longCondition = false
    bool close_trade = crossover(leadLine1[displacement], close) or crossover (leadLine2[displacement], close) or close < close[displacement] or crossover(baseLine, close)
    var position_count = 0
    var risk = 0
    var reward= 0
    if (TKcross and aboveCloud and greenCloud and lagLong and position_count==0)
    strategy.entry(id=buy, long=true)
    if (leadLine1[displacement] > leadLine2 [displacement])
    risk = leadLine2[displacement]
    reward = strategy.position_avg_price*(1+(1-risk/strategy.position_avg_price))
    if (leadLine2[displacement] > leadLine1 [displacement])
    risk = leadLine1[displacement]
    reward = strategy.position_avg_price*(1+(1-risk/strategy.position_avg_price))
    position_count = 1

    if (close >= reward or close <= risk)
    strategy.close_all()
    position_count = 0

    plot(risk, color=#1462FF, title=Risk)
    plot(reward, color=#2342FF, title=Reward)

    Screenshot 2022-03-02 145741.jpg

    0
  • you need learn mq4 the pro script c impelling

    0
  • Hi

    You have an example here: https://www.tradingview.com/script/IHVPG6TS-Stop-loss-and-Take-Profit-in-example/

    Dave

    0
  • Hi, I tried your script in TradingView today, but it does not run! Error message: Script could not be be translated from: null Where is the problem? Please help me.
    Regards Mario

    0
CONTENTS