请求将Pine Script策略转换为Study并添加买卖止损警报
需求:将Pine Script策略代码转换为Study指标
- 本人无编程基础,现有一段Pine Script策略代码,希望将其转换为Study指标
- 转换后需支持设置买入、止损相关警报
- 愿意支付报酬寻求技术协助
待转换的策略代码
if long strategy.entry("L_1", strategy.long, when=ACT_BT and testPeriod) if short strategy.entry("S_1", strategy.short, when=ACT_BT and testPeriod) curProfitInPts() => if strategy.position_size > 0 (high - strategy.position_avg_price) / syminfo.mintick else if strategy.position_size < 0 (strategy.position_avg_price - low) / syminfo.mintick else 0 calcStopLossPrice(OffsetPts) => if strategy.position_size > 0 strategy.position_avg_price - OffsetPts * syminfo.mintick else if strategy.position_size < 0 strategy.position_avg_price + OffsetPts * syminfo.mintick else na calcProfitTrgtPrice(OffsetPts) => calcStopLossPrice(-OffsetPts) getCurrentStage() => var stage = 0 if strategy.position_size == 0 stage := 0 if stage == 0 and strategy.position_size != 0 stage := 1 else if stage == 1 and curProfitInPts() >= tp //tp1123 //or tp stage := 2 else if stage == 2 and curProfitInPts() >= tp2 //tp2123 //or tp2 stage := 3 stage calcTrailingAmountLevel(points) => var float level = na level := calcProfitTrgtPrice(points) if not na(level) if strategy.position_size > 0 if not na(level[1]) level := max(level[1], level) if not na(level) level := max(high, level) else if strategy.position_size < 0 if not na(level[1]) level := min(level[1], level) if not na(level) level := min(low, level) calcTrailingOffsetLevel(points, offset) => float result = na amountLevel = calcTrailingAmountLevel(points) if strategy.position_size > 0 trailActiveDiff = amountLevel - calcProfitTrgtPrice(points) if trailActiveDiff > 0 result := trailActiveDiff + calcProfitTrgtPrice(offset) else if strategy.position_size < 0 trailActiveDiff = calcProfitTrgtPrice(points) - amountLevel if trailActiveDiff > 0 result := calcProfitTrgtPrice(offset) - trailActiveDiff result float stopLevel = na float trailOffsetLevel = na float profitLevel = activateTrailingOnThirdStep ? calcTrailingAmountLevel(tp3123) : calcProfitTrgtPrice(tp3123) // note: calcTrailingOffsetLevel uses calcTrailingAmountLevel and last one has a state (level). // therefor we needs calculate it on every bar for correct result. // if we inline it the Pine compiler give us warning "The function '***' should be called on each calculation for consistency. It is recommended to extract the call from this scope." trailOffsetLevelTmp = calcTrailingOffsetLevel(tp3123, tp2) //(tp3123, tp2123) // based on current stage set up exit // note: we use same exit ids ("x") consciously, for MODIFY the exit's parameters curStage = getCurrentStage() if curStage == 1 stopLevel := calcStopLossPrice(sl) //stopLevel := calcStopLossPrice(sl123) strategy.exit("x", loss = sl, profit = tp3123, comment = "sl123 or tp3123") //strategy.exit("x", loss = sl123, profit = tp3123, comment = "sl123 or tp3123") else if curStage == 2 stopLevel := calcStopLossPrice(0) strategy.exit("x", stop = stopLevel, profit = tp3123, comment = "breakeven or tp3123") else if curStage == 3 stopLevel := calcStopLossPrice(-tp) // stopLevel := calcStopLossPrice(-tp1123) if activateTrailingOnThirdStep trailOffsetLevel := trailOffsetLevelTmp strategy.exit("x", stop = stopLevel, trail_points = tp3123, trail_offset = tp3123-tp2, comment = "stop tp1 or trailing tp3 with offset tp2") // strategy.exit("x", stop = stopLevel, trail_points = tp3123, trail_offset = tp3123-tp2123, comment = "stop tp1 or trailing tp3 with offset tp2") else strategy.exit("x", stop = stopLevel, profit = tp3123, comment = "tp1123 or tp3123") else strategy.cancel("x") // this is debug plots for visulalize TP & SL levels plot(stopLevel, style = plot.style_linebr, color = color.red) plotshape(long and stopLevel, text='stop') //plotshape(stopLevel, title = "stopLevel", style = shape.labelup, location = location.belowbar, text = " stopLevel ", color=bearcolor, textcolor=text, size=size.tiny) alertcondition(stopLevel, title="LONG BUY_BINANCE_USDC-ETH_4000 ", message = "LONG BUY_BINANCE_USDC-ETH_4000 {{exchange}}:{{ticker}}, price = {{close}}, volume = {{volume}} {{timenow}}") plot(profitLevel, style = plot.style_linebr, color = color.blue) //plotshape(profitLevel, title = "profitLevel", style = shape.labelup, location = location.belowbar, text = " profitLevel ", color=bearcolor, textcolor=text, size=size.tiny) plot(trailOffsetLevel, style = plot.style_linebr, color = color.green) //plotshape(trailOffsetLevel, title = "trailOffsetLevel", style = shape.labelup, location = location.belowbar, text = " trailOffsetLevel ", color=bearcolor, textcolor=text, size=size.tiny)
内容的提问来源于stack exchange,提问作者Brians
相关产品推荐
相关产品推荐

