【问题标题】:A tradingview strategy need some fixes - pinescript交易视图策略需要一些修复 - pinescript
【发布时间】:2021-08-05 20:58:54
【问题描述】:

亲爱的,

我是 Pine Script 的新手,正在寻求您的帮助。

我有这个初学者级别的脚本,当快速 MA 越过慢速 MA 并在 3 天内保持高于交叉价格时给出(买入信号),我称之为确认柱。当慢速 MA 穿过具有相同确认柱的快速 MA 时,它也会发出卖出信号。

其实我不想用这个卖出信号策略,下面我需要的东西

当 X% 产生时我需要 TP 信号。例如,如果买入价为 100,则为 110(如果 x=10%)

当价格下跌特定百分比时与止损相同。

//@version=4
strategy("MovingAvg2Line Cross", overlay=true)
fastLength = input(30)
slowLength = input(50)
confirmBars = input(3)
price = close
mafast = sma(price, fastLength)
maslow = sma(price, slowLength)

bcond = price > mafast
bcount = 0
bcount := bcond ? nz(bcount[1]) + 1 : 0
 
if (crossover(mafast, maslow)) and (bcount == confirmBars)
    strategy.entry("MA2CrossLE", strategy.long, comment="MA2CrossLE")
if (crossunder(mafast, maslow))
    strategy.entry("MA2CrossSE", strategy.short, comment="MA2CrossSE")
//plot(strategy.equity, title="equity", color=color.red, linewidth=2, style=plot.style_areabr)

【问题讨论】:

    标签: pine-script tradingview-api


    【解决方案1】:

    我也给了它一些视觉效果,根据你的喜好改变它。不确定您使用它的时间范围,因此没有使用已确认的柱,但我认为需要调整条件

    // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
    // © SafetyHammer
    
    //@version=4
    strategy("MovingAvg2Line Cross", overlay=true)
    fastLength = input(30)
    slowLength = input(50)
    confirmBars = input(3)
    bcount_OnOff = input(true, title = "Enable Bar Count Filter")
    
    
    price = close
    mafast = sma(price, fastLength)
    maslow = sma(price, slowLength)
    
    bcond = price > mafast
    bcount = 0
    bcount := bcond ? nz(bcount[1]) + 1 : 0
    plot(bcount, title="bcount") // for debug
    
    buy = crossover(mafast, maslow) and (bcount == confirmBars or bcount_OnOff == false)
    sell = crossunder(mafast, maslow)
    
    TP1 = input(title="Take Profit 1 %", type=input.float, defval=2.5, step=0.1, group="TP & SL")
    TP2 = input(title="Take Profit 2 %", type=input.float, defval=5, step=0.1, group="TP & SL")
    TP3 = input(title="Take Profit 3 %", type=input.float, defval=12, step=0.1, group="TP & SL")
    stop_percent = input(title="Stop loss %", type=input.float, defval=2.5, step=0.1, group="TP & SL")
    
    TP1_Ratio = input(title="Take Profit 1 Ratio %", type=input.float, defval=25, step=1, group="TP & SL")/100
    TP2_Ratio = input(title="Take Profit 2 Ratio %", type=input.float, defval=25, step=1, group="TP & SL")/100
    
    
    percentAsPoints(pcnt) =>
        strategy.position_size != 0 ? round(pcnt / 100.0 * strategy.position_avg_price / syminfo.mintick) : float(na)
    
    percentAsPrice(pcnt) =>
        strategy.position_size != 0 ? ((pcnt / 100.0) + 1.0) * strategy.position_avg_price : float(na)
    
    
    
    TP_1 = strategy.position_avg_price + percentAsPoints(TP1) * syminfo.mintick * strategy.position_size / abs(strategy.position_size)
    TP_2 = strategy.position_avg_price + percentAsPoints(TP2) * syminfo.mintick * strategy.position_size / abs(strategy.position_size)
    TP_3 = strategy.position_avg_price + percentAsPoints(TP3) * syminfo.mintick * strategy.position_size / abs(strategy.position_size)
    
    Long_SL  = strategy.position_avg_price - percentAsPoints(stop_percent) * syminfo.mintick * strategy.position_size / abs(strategy.position_size)
    Short_SL = strategy.position_avg_price - percentAsPoints(stop_percent) * syminfo.mintick * strategy.position_size / abs(strategy.position_size)
    
    
    
    current_position_size = abs(strategy.position_size)
    initial_position_size = abs(valuewhen(strategy.position_size[1] == 0.0, strategy.position_size, 0))
    
    
     
    if (buy) 
        strategy.entry(id="Long", long=true)    
     
    
    if (sell) 
        strategy.entry(id="Short", long=false)
        
            
    
    if strategy.position_size > 0 
        strategy.exit("L_TP1", from_entry="Long", qty = initial_position_size * TP1_Ratio, limit = TP_1, stop = Long_SL)
        strategy.exit("L_Tp2", from_entry="Long", qty = initial_position_size * TP2_Ratio, limit = TP_2, stop = Long_SL)
        strategy.exit("L_TP3", from_entry="Long", limit = TP_3, stop = Long_SL) 
    
    
    if strategy.position_size < 0 
        strategy.exit("S_TP1", from_entry="Short", qty = initial_position_size * TP1_Ratio, limit = TP_1, stop = Short_SL)
        strategy.exit("S_TP2", from_entry="Short", qty = initial_position_size * TP2_Ratio, limit = TP_2, stop = Short_SL)
        strategy.exit("S_TP3", from_entry="Short", limit = TP_3, stop = Short_SL)   
    
    
    //plot(strategy.equity, title="equity", color=color.red, linewidth=2, style=plot.style_areabr)
    plot(mafast, title="mafast", color=color.blue)
    plot(maslow, title="mafast", color=color.red)
    
    
    TP_SL_Lines = input(true, title = "Show Take Profit/SL Lines", group="TP & SL") 
    
    plot(series=strategy.position_size>0 and TP_SL_Lines ? Long_SL  : na , title="SL Long", style=plot.style_linebr, color=color.red)
    plot(series=strategy.position_size>0 and TP_SL_Lines  ? strategy.position_avg_price : na , title="Entry Long", style=plot.style_circles, color=color.blue)
    plot(series=strategy.position_size>0 and TP_SL_Lines  ? TP_1 : na , title="TP1 Long", style=plot.style_circles, color=color.green)
    plot(series=strategy.position_size>0 and TP_SL_Lines  ? TP_2 : na , title="TP2 Long", style=plot.style_circles, color=color.green)
    plot(series=strategy.position_size>0 and TP_SL_Lines  ? TP_3 : na , title="TP3 Long", style=plot.style_circles, color=color.green)
    
    
    plot(series=strategy.position_size<0 and TP_SL_Lines  ? Short_SL  : na , title="SL Short", style=plot.style_linebr, color=color.red)
    plot(series=strategy.position_size<0 and TP_SL_Lines  ? strategy.position_avg_price : na , title="Entry Short", style=plot.style_circles, color=color.blue)
    plot(series=strategy.position_size<0 and TP_SL_Lines  ? TP_1 : na , title="TP1 Short", style=plot.style_circles, color=color.green)
    plot(series=strategy.position_size<0 and TP_SL_Lines  ? TP_2 : na , title="TP2 Short", style=plot.style_circles, color=color.green)
    plot(series=strategy.position_size<0 and TP_SL_Lines  ? TP_3 : na , title="TP3 Short", style=plot.style_circles, color=color.green)
    

    【讨论】:

      猜你喜欢
      • 2018-09-24
      • 2023-02-04
      • 2023-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-05
      • 1970-01-01
      • 2020-08-03
      相关资源
      最近更新 更多