在交易视图上自动交易购买

时间:2021-03-02 03:37:51

标签: pine-script

我在 TradingView 上使用了 Matt De Long 的 MOMO 策略,我想实现交易自动化。我的 Gemini 账户与我的交易视图账户相关联。

根据 MOMO 代码,我如何根据收到的警报自动进行买入或卖出?

(退出多头/进入空头=卖出) (退出空头/进入多头=买入)

我是否必须为每个时间帧创建新代码?在这种情况下,我只想使用 1 小时图。

我复制了下面的 MOMO 代码:

//@version=4
//author = https://www.tradingview.com/u/MattDeLong/

study("Trend Following MOMO", overlay=true)
//lh3On = input(title="Buy/Long Signal", type=input.bool, defval=true)
//hl3On = input(title="Sell/Short Signal", type=input.bool, defval=true)
lh3On = true
hl3On = true
emaOn = input(title="105ema / 30min", type=input.bool, defval=true)
assistantOn = input(title="Assistant", type=input.bool, defval=true)
textOn = input(title="Text", type=input.bool, defval=true)

threeHigherLows() =>
    low[0] >= low[1] and low[1] >= low[2]

threeLowerHighs() =>
    high[2] >= high[1] and high[1] >= high[0]

breakHigher() =>
    padding = timeframe.isintraday ? .02 : .1
    high >= high[1] + padding

breakLower() =>
    padding = timeframe.isintraday ? .02 : .1
    low <= low[1] - padding

lh3 = threeLowerHighs() and lh3On
lh3bh = lh3[1] and breakHigher() and lh3On

hl3 = threeHigherLows() and hl3On
hl3bl = hl3[1] and breakLower() and hl3On

ema8 = ema(close, 8)
ema21 = ema(close, 21)

isUptrend = ema8 >= ema21
isDowntrend = ema8 <= ema21
trendChanging = cross(ema8,ema21)

buySignal = lh3bh and lh3[2] and lh3[3] and isUptrend and timeframe.isintraday
sellSignal = hl3bl and hl3[2] and hl3[3] and isDowntrend and timeframe.isintraday

goingDown = hl3 and isDowntrend and timeframe.isintraday
goingUp = lh3 and isUptrend and timeframe.isintraday

//plotshape(goingDown and goingDown[1], style=shape.triangledown, location=location.abovebar, color=color.red, size=size.tiny)
//plotshape(goingUp and goingUp[1], style=shape.triangleup, location=location.belowbar, color=color.green, size=size.tiny)

plotshape(lh3, style=shape.circle, location=location.abovebar, color=color.red, size=size.auto)
plotshape(hl3, style=shape.circle, location=location.belowbar, color=color.green, size=size.auto)
plotshape(trendChanging and isUptrend and assistantOn, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small, text='Exit Short\nEnter Long')
plotshape(trendChanging and isDowntrend and assistantOn, style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small, text='Exit Long\nEnter Short')
plotchar(trendChanging and isUptrend and close<open and assistantOn, char='!', location=location.abovebar, color=color.green, size=size.small)
//plotchar(trendChanging and isDowntrend and open<close and assistantOn, char='!', location=location.abovebar, color=color.red, size=size.small)


//RLT 105ema / 30-min chart
ema105 = security(syminfo.tickerid, '30', ema(close, 105))
ema205 = security(syminfo.tickerid, '30', ema(close, 20))
plot(emaOn ? ema105 : na, linewidth=4, color=color.purple, editable=true)
plot(emaOn ? ema205 : na, linewidth=2, color=color.purple, editable=true)

aa = plot(ema8, linewidth=3, color=color.green, editable=true)
bb = plot(ema21,linewidth=3, color=color.red, editable=true)
fill(aa, bb, color=isUptrend ? color.green : color.red)
buyZone =  isUptrend and lh3 and high < ema21 and timeframe.isintraday
sellZone = isDowntrend and hl3 and low > ema21 and timeframe.isintraday


// === ALERT === 

alertcondition(trendChanging, title="Trend Reversing", message="Trend Changing")
alertcondition(buyZone, title="Bullish Trend Following", message="BUY Zone, Perfect")
alertcondition(sellZone, title="Bearish Trend Following", message="SELL Zone, Perfect")
alertcondition(buySignal, title="Long Alert", message="Long")
alertcondition(sellSignal, title="Short Alert", message="Short")

1 个答案:

答案 0 :(得分:0)

如果您希望它在 1 小时内运行,请使用下面的代码并将其部署在时间范围为 1 小时的图表上

我变了 security(syminfo.tickerid, "30", ....)security(syminfo.tickerid, "60",...

我对上面的代码做同样的事情。希望能帮助到你。如果它不起作用,请随时寻求帮助

//@version=4
//author = https://www.tradingview.com/u/MattDeLong/

study("Trend Following MOMO", overlay=true)
//lh3On = input(title="Buy/Long Signal", type=input.bool, defval=true)
//hl3On = input(title="Sell/Short Signal", type=input.bool, defval=true)
lh3On = true
hl3On = true
emaOn = input(title="105ema / 60 min", type=input.bool, defval=true)   //Change made here, this doesn't have impact on code, just the input screen
assistantOn = input(title="Assistant", type=input.bool, defval=true)
textOn = input(title="Text", type=input.bool, defval=true)

threeHigherLows() =>
    low[0] >= low[1] and low[1] >= low[2]

threeLowerHighs() =>
    high[2] >= high[1] and high[1] >= high[0]

breakHigher() =>
    padding = timeframe.isintraday ? .02 : .1
    high >= high[1] + padding

breakLower() =>
    padding = timeframe.isintraday ? .02 : .1
    low <= low[1] - padding

lh3 = threeLowerHighs() and lh3On
lh3bh = lh3[1] and breakHigher() and lh3On

hl3 = threeHigherLows() and hl3On
hl3bl = hl3[1] and breakLower() and hl3On

ema8 = ema(close, 8)
ema21 = ema(close, 21)

isUptrend = ema8 >= ema21
isDowntrend = ema8 <= ema21
trendChanging = cross(ema8,ema21)

buySignal = lh3bh and lh3[2] and lh3[3] and isUptrend and timeframe.isintraday
sellSignal = hl3bl and hl3[2] and hl3[3] and isDowntrend and timeframe.isintraday

goingDown = hl3 and isDowntrend and timeframe.isintraday
goingUp = lh3 and isUptrend and timeframe.isintraday

//plotshape(goingDown and goingDown[1], style=shape.triangledown, location=location.abovebar, color=color.red, size=size.tiny)
//plotshape(goingUp and goingUp[1], style=shape.triangleup, location=location.belowbar, color=color.green, size=size.tiny)

plotshape(lh3, style=shape.circle, location=location.abovebar, color=color.red, size=size.auto)
plotshape(hl3, style=shape.circle, location=location.belowbar, color=color.green, size=size.auto)
plotshape(trendChanging and isUptrend and assistantOn, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small, text='Exit Short\nEnter Long')
plotshape(trendChanging and isDowntrend and assistantOn, style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small, text='Exit Long\nEnter Short')
plotchar(trendChanging and isUptrend and close<open and assistantOn, char='!', location=location.abovebar, color=color.green, size=size.small)
//plotchar(trendChanging and isDowntrend and open<close and assistantOn, char='!', location=location.abovebar, color=color.red, size=size.small)


//RLT 105ema / 30-min chart
ema105 = security(syminfo.tickerid, '60', ema(close, 105))       //Change made here
ema205 = security(syminfo.tickerid, '60', ema(close, 20))        //Change made here
plot(emaOn ? ema105 : na, linewidth=4, color=color.purple, editable=true)
plot(emaOn ? ema205 : na, linewidth=2, color=color.purple, editable=true)

aa = plot(ema8, linewidth=3, color=color.green, editable=true)
bb = plot(ema21,linewidth=3, color=color.red, editable=true)
fill(aa, bb, color=isUptrend ? color.green : color.red)
buyZone =  isUptrend and lh3 and high < ema21 and timeframe.isintraday
sellZone = isDowntrend and hl3 and low > ema21 and timeframe.isintraday


// === ALERT === 

alertcondition(trendChanging, title="Trend Reversing", message="Trend Changing")
alertcondition(buyZone, title="Bullish Trend Following", message="BUY Zone, Perfect")
alertcondition(sellZone, title="Bearish Trend Following", message="SELL Zone, Perfect")
alertcondition(buySignal, title="Long Alert", message="Long")
alertcondition(sellSignal, title="Short Alert", message="Short")