// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © SpeculationLab
//@version=5
strategy("Vegas Stragety2.0")
// 定义EMA的周期
emaPeriod1 = input.int(144, "EMA Period 1")
emaPeriod2 = input.int(169, "EMA Period 2")
ecMult=input.float(2,"Engulfing Multiplier")
rr=input.float(1,"Risk Reward Ratio")
maLength=input.int(50,"MA Length")
trigger1=input.float(2,"Ratio Trigger1")
trigger2=input.float(5,"Ratio Trigger2")
color1=input.color(color.aqua,"Ratio Color1")
color2=input.color(color.yellow,"Ratio Color2")
//获取量能指标相关值
ma=ta.sma(volume,maLength)
ratio=volume/ma
alert1=ratio>trigger1
alert2=ratio>trigger2
volColor=alert2? color2:(alert1? color1:(close>open? color.green:color.red))
volumeFilter=alert1 or alert2
// 计算两条EMA
ema1 = ta.ema(close, emaPeriod1)
ema2 = ta.ema(close, emaPeriod2)
//识别有效的吞没形态
bullishEC=close[1]<open[1] and open<=close[1] and close>open[1]
bearishEC=close[1]>open[1] and open>=close[1] and close<open[1]
//识别入场信号
longFilter=ta.crossover(close,ema1)
shortFilter=ta.crossunder(close,ema1)
longSignal=bullishEC and longFilter and ema1 > ema2 and volumeFilter
shortSignal= bearishEC and shortFilter and ema1< ema2 and volumeFilter
//设置止盈/止损点位
longStop=ema2
shortStop=ema2
longDistance=close-ema2
shortDistance=ema2-close
longTarget=close+(longDistance*rr)
shortTarget=close-(shortDistance*rr)
//设置储存变量并进入交易
var t_stop=0.0
var t_target=0.0
if longSignal and strategy.position_size==0 and barstate.isconfirmed
strategy.entry(id="LONG",direction=strategy.long)
t_stop:=longStop
t_target:=longTarget
if shortSignal and strategy.position_size==0 and barstate.isconfirmed
strategy.entry(id="SHORT",direction=strategy.short)
t_stop:=shortStop
t_target:=longTarget
//退出交易
if strategy.position_size>0
strategy.exit("LONG EXIT","LONG",limit=t_target,stop=t_stop)
if strategy.position_size<0
strategy.exit("SHORT EXIT","SHORT",limit=t_target,stop=t_stop)
// 绘制EMA线
plot(volume,"Volume",style=plot.style_columns,color=volColor,force_overlay=false)
plot(ma,title="MA",color=color.white,force_overlay=false)
plot(ema1, title="EMA 144", color=color.blue, linewidth=2,force_overlay=true)
plot(ema2, title="EMA 169", color=color.red, linewidth=2,force_overlay=true)
plotshape(longSignal,"BUY Signal",shape.triangleup,location.abovebar,color.green,force_overlay=true)
plotshape(shortSignal,"SELL Signal",shape.triangledown,location.belowbar,color.red,force_overlay=true)
plot(strategy.position_size!=0? t_stop:na, style=plot.style_linebr,force_overlay=true)
plot(strategy.position_size!=0? t_target:na, style=plot.style_linebr,force_overlay=true)