Pine script 管理多头和空头订单,如LucF PineCoders回测交易引擎

Pine script 管理多头和空头订单,如LucF PineCoders回测交易引擎,pine-script,Pine Script,我正试图在学习模式下完成交易方向(长/短/两者),就像LucF和PineCoders所做的那样 问题: 当我选择“仅多头”时,我希望它只显示多头交易,但这不是因为缺少在多头交易中找到蜡烛范围的部分。如果你检查我上面给出的链接(反向测试和交易引擎[PineCoders]),LucF使用InLong、InShort和InTrade变量。问题是他的代码对我来说太旧,太过复杂,我不知道如何重新创建它 我说的是过度工程化,因为他的代码包括金字塔、滑动和其他现在内置在TradingView中的东西,可能

我正试图在学习模式下完成交易方向(长/短/两者),就像LucF和PineCoders所做的那样

问题:
  • 当我选择“仅多头”时,我希望它只显示多头交易,但这不是因为缺少在多头交易中找到蜡烛范围的部分。如果你检查我上面给出的链接(反向测试和交易引擎[PineCoders]),LucF使用InLong、InShort和InTrade变量。问题是他的代码对我来说太旧,太过复杂,我不知道如何重新创建它
我说的是过度工程化,因为他的代码包括金字塔、滑动和其他现在内置在TradingView中的东西,可能是因为早在2019年,PineScript就没有这样的功能

该指示器背后的实际想法是绘制警报和另一个脚本,该脚本使用该指示器作为源对其进行回溯测试

  • 条形图着色部分不起作用,并且它当前已被注释,因此代码可以编译。问题与上面的描述相同。我需要知道我是在做多还是做空,或者根本不在交易中。我不知道如何完成那部分
/@version=4
//@作者=团队交易1
研究(“未来策略”,叠加=真实,精度=6)
//-------常数{
//----输入选项
var string ON=“ON”
var string OFF=“OFF”
var string TD1=“两者”
var string TD2=“仅长”
var string TD3=“仅限短裤”
//----颜色常数
var color C#U AQUA=#0080FFff
变量颜色C#U黑色=#000000 ff
变量颜色C#U蓝=#013BCAff
var color C#U珊瑚=#FF8080ff
var color C#U GOLD=#CCCC00ff
变量颜色C#U灰色=#80FF
变量颜色C#U深绿色=#006400ff
变量颜色C#U绿色=#008000ff
变量颜色C#U石灰=#00FF00ff
var color C#U栗色=#800000 ff
变量颜色C#U橙色=#FF8000ff
变量颜色C#U粉色=#FF0080ff
变量颜色C#U深#U红=#8B0000ff
变量颜色C#U红色=#FF0000ff
变量颜色C#U紫色=#AA00FFff
变量颜色C#U黄色=#FFFF00ff
变量颜色C#U白色=#FFFFFF FF
var color C_UPTREND=color.new(color.green,80)
var color C_下降趋势=color.new(color.red,80)
var color C_HARDEXIT=color.new(color.maroon,25)
// }
//-------输入{
//----参赛作品
var string GP1=“条目”
字符串i_tradeDirection=输入(TD1,“交易方向”,选项=[TD1,TD2,TD3],组=GP1)
//----用户选择的交易方向
var bool doLongs=(i_tradeDirection==TD2或i_tradeDirection==TD1)
var bool doShorts=(i_tradeDirection==TD3或i_tradeDirection==TD1)
// }
//-------功能{
//-----函数将OHLC四舍五入到刻度精度
f_roundedToTickOHLC()=>
float _op=round(open/syminfo.mintick)*syminfo.mintick
浮动高度=圆形(高度/符号信息.mintick)*符号信息.mintick
浮点数_lo=圆形(低位/符号信息.mintick)*符号信息.mintick
浮动_cl=圆形(关闭/syminfo.mintick)*syminfo.mintick
[_op,_hi,_lo,_cl]
//----用于HTF数据重新绘制/非重新绘制版本的函数
f_安全性(_符号、_分辨率、_src、_重绘)=>
安全性(_-symbol,_-resolution,_-src[_-repaint?0:barstate.isrealtime?1:0])[_-repaint?0:barstate.isrealtime?0:1]
// }
//-------计算{
//-----四舍五入价格
[rOpen,rHigh,rLow,rClose]=f_roundedToTickOHLC()
//----电视内置MACD代码
fastLength=12
慢长=26
麦德伦=9
MACD=ema(闭合,快速长度)-ema(闭合,慢速长度)
AMCD=ema(MACD,MACDLength)
//----过滤器
filterLong=MACD>0
filterShort=MACD<0
//----参赛作品
enterLong=交叉(MACD,0)
enterShort=crossunder(MACD,0)
//----停止
atr=atr(14)
stopLong=min(最低(5),min(关闭,打开)-atr*1.5
stopShort=最大值(最高(5),最大值(关闭,打开)+atr*1.5)
//----出口
exitLong=交叉(MACD、AMCD)和MACD>0
exitShort=交叉(MACD、AMCD)和MACD<0
//-----确定我们是否已进入交易和传播状态,直到我们退出
longEntryTrigger=doLongs
shortEntryTrigger=doShorts
bool inLong=false
bool inShort=false
//stoppedCondition=((inLong和rCloseInTradeStop[shortEntryTrigger[1]?0:1]))
exitTradeCondition=(inLong和exitLong)或(inShort和exitShort)
exitCondition=exitTradeCondition//stoppedCondition或exitTradeCondition
inLong:=longEntryTrigger[1]或(inLong[1]且不存在条件[1])
inShort:=shortEntryTrigger[1]或(inShort[1]且不存在条件[1])
entryPrice=valuewhen(enterLong或enterShort,close,0)
// }
//-------情节{
//----潮流背景色
bgcolor(filterLong?C_上升趋势:filterShort?C_下降趋势:na,title=“趋势背景色”)
//入口价格颜色
绘图(entryPrice,title=“Entry Price”,color=color.blue,style=plot.style\u圆圈,线宽=2)
//硬出口颜色
绘图(stopLong,title=“硬退出价格”,color=color.orange,style=plot.style\u圆圈,线宽=2)
//----入口背景色
plotshape(enterLong,title=“Main BUY”,style=shape.triangleup,location=location.belowbar,color=color.green,size=size.large)
plotshape(enterShort,title=“Main SELL”,style=shape.triangledown,location=location.overbar,color=color.red,size=size.large)
plotshape(exitLong,title=“Close BUY”,style=shape.diamond,location=location.overbar,color=color.green,size=size.large)
plotshape(exitShort,title=“Close SELL”,style=shape.diamond,location=location.belowbar,color=color.red,size=size.large)
//进场价=石灰色
//非交易时为灰色
//深绿色
//@version=4
//@author=TeamTrading1
study("Futures Strategy", overlay = true, precision = 6)

// —————————— Constants {

// ————— Input options
var string ON  = "On"
var string OFF = "Off"

var string TD1 = "Both"
var string TD2 = "Longs Only"
var string TD3 = "Shorts Only"

// ————— Color constants
var color C_AQUA        = #0080FFff
var color C_BLACK       = #000000ff
var color C_BLUE        = #013BCAff
var color C_CORAL       = #FF8080ff
var color C_GOLD        = #CCCC00ff
var color C_GRAY        = #808080ff
var color C_DARK_GREEN  = #006400ff
var color C_GREEN       = #008000ff
var color C_LIME        = #00FF00ff
var color C_MAROON      = #800000ff
var color C_ORANGE      = #FF8000ff
var color C_PINK        = #FF0080ff
var color C_DARK_RED    = #8B0000ff
var color C_RED         = #FF0000ff
var color C_VIOLET      = #AA00FFff
var color C_YELLOW      = #FFFF00ff
var color C_WHITE       = #FFFFFFff
var color C_UPTREND     = color.new(color.green, 80)
var color C_DOWNTREND   = color.new(color.red, 80)
var color C_HARDEXIT    = color.new(color.maroon, 25)
// }

// —————————— Inputs {

// ————— Entries
var string GP1 = "Entries"
string i_tradeDirection = input(TD1, "Trade Direction", options = [TD1, TD2, TD3], group = GP1)

// ————— User-selected trade directions
var bool doLongs  = (i_tradeDirection == TD2 or i_tradeDirection == TD1)
var bool doShorts = (i_tradeDirection == TD3 or i_tradeDirection == TD1)
// }

// —————————— Functions {

// ————— Functions rounding OHLC to tick precision
f_roundedToTickOHLC() =>
    float _op = round(open  / syminfo.mintick) * syminfo.mintick
    float _hi = round(high  / syminfo.mintick) * syminfo.mintick
    float _lo = round(low   / syminfo.mintick) * syminfo.mintick
    float _cl = round(close / syminfo.mintick) * syminfo.mintick
    [_op, _hi, _lo, _cl]

// ————— Function for a repainting/non-repainting version of the HTF data
f_security(_symbol, _resolution, _src, _repaint) =>
    security(_symbol, _resolution, _src[_repaint ? 0 : barstate.isrealtime ? 1 : 0])[_repaint ? 0 : barstate.isrealtime ? 0 : 1]
// }

// —————————— Calculations {

// ————— Get rounded prices
[rOpen, rHigh, rLow, rClose] = f_roundedToTickOHLC()

// ————— TV built-in MACD code
fastLength = 12
slowlength = 26
MACDLength = 9
MACD = ema(close, fastLength) - ema(close, slowlength)
aMACD = ema(MACD, MACDLength)

// ————— Filter
filterLong = MACD > 0
filterShort = MACD < 0

// ————— Entries
enterLong = crossover(MACD, 0)
enterShort = crossunder(MACD, 0)

// ————— Stops
atr = atr(14)
stopLong  = min(lowest(5), min(close, open) - atr * 1.5)
stopShort = max(highest(5), max(close, open) + atr * 1.5)

// ————— Exits
exitLong  = crossunder(MACD, aMACD) and MACD > 0
exitShort = crossover(MACD, aMACD) and MACD < 0

// ————— Determine if we have entered a trade and propagate state until we exit
longEntryTrigger = doLongs
shortEntryTrigger = doShorts

bool inLong = false
bool inShort = false

//stoppedCondition = ((inLong and rClose < InTradeStop[longEntryTrigger[1] ? 0 : 1]) or (InShort and rClose > InTradeStop[shortEntryTrigger[1] ? 0 : 1]))
exitTradeCondition = (inLong and exitLong) or (inShort and exitShort)
exitCondition = exitTradeCondition // stoppedCondition or exitTradeCondition

inLong := longEntryTrigger[1] or (inLong[1] and not exitCondition[1])
inShort := shortEntryTrigger[1] or (inShort[1] and not exitCondition[1])

entryPrice = valuewhen(enterLong or enterShort, close, 0)

// }

// —————————— Plots {

// ————— Trend Background Colors
bgcolor(filterLong ? C_UPTREND : filterShort ? C_DOWNTREND : na, title = "Trend Background Colors")

// Entry Price Colors
plot(entryPrice, title = "Entry Price", color = color.blue, style = plot.style_circles, linewidth = 2)

// Hard Exit Colors
plot(stopLong, title = "Hard Exit Price", color = color.orange, style = plot.style_circles, linewidth = 2)

// ————— Entry Background Colors
plotshape(enterLong, title = "Main BUY", style = shape.triangleup, location = location.belowbar, color = color.green, size = size.large)
plotshape(enterShort, title = "Main SELL", style = shape.triangledown, location = location.abovebar, color = color.red, size = size.large)
plotshape(exitLong, title = "Close BUY", style = shape.diamond, location = location.abovebar, color = color.green, size = size.large)
plotshape(exitShort, title = "Close SELL", style = shape.diamond, location = location.belowbar, color = color.red, size = size.large)

// entry price = lime colored
// gray when not in a trade
// dark green when the candle close is below the entry price (in a loss)
// light green when the candle close is above the entry price (in a win position)
// dark red when the candle close is above the entry price (in a loss)
// light red when the candle close is below the entry price (in a win position)
var color c = na

// if signal == 0
//     c := c_NOT_IN_TRADE
// if signal == 1 and close < entryPrice
//     c := c_LONG_ABOVE_ENTRYPRICE
// else if signal == 1 and close > entryPrice
//     c := c_LONG_BELOW_ENTRYPRICE
// else if signal == -1 and close > entryPrice
//     c := c_SHORT_BELOW_ENTRYPRICE
// else if signal == -1 and close < entryPrice
//     c := c_SHORT_ABOVE_ENTRYPRICE

// barcolor(c, title = "Trade State Bar Coloring")

// —————————— Alerts {
alertcondition(enterLong, title = "Buy Alert", message = "Buy Alert")
alertcondition(enterShort, title = "Sell Alert", message = "Sell Alert")
alertcondition(exitLong, title = "Buy Alert", message = "Buy Alert")
alertcondition(exitShort, title = "Sell Alert", message = "Sell Alert")
// }
//@version=4
//@author=TeamTrading1
study("Futures Strategy", overlay = true, precision = 6)

// —————————— Constants {

// ————— Input options
var string ON  = "On"
var string OFF = "Off"

var string TD1 = "Both"
var string TD2 = "Longs Only"
var string TD3 = "Shorts Only"

// ————— Color constants
var color C_AQUA        = #0080FFff
var color C_BLACK       = #000000ff
var color C_BLUE        = #013BCAff
var color C_CORAL       = #FF8080ff
var color C_GOLD        = #CCCC00ff
var color C_GRAY        = #808080ff
var color C_DARK_GREEN  = #006400ff
var color C_GREEN       = #008000ff
var color C_LIME        = #00FF00ff
var color C_MAROON      = #800000ff
var color C_ORANGE      = #FF8000ff
var color C_PINK        = #FF0080ff
var color C_DARK_RED    = #8B0000ff
var color C_RED         = #FF0000ff
var color C_VIOLET      = #AA00FFff
var color C_YELLOW      = #FFFF00ff
var color C_WHITE       = #FFFFFFff
var color C_UPTREND     = color.new(color.green, 80)
var color C_DOWNTREND   = color.new(color.red, 80)
var color C_HARDEXIT    = color.new(color.maroon, 25)
// }

// —————————— Inputs {

// ————— Entries
var string GP1 = "Entries"
string i_tradeDirection = input(TD1, "Trade Direction", options = [TD1, TD2, TD3], group = GP1)

// ————— User-selected trade directions
var bool doLongs  = (i_tradeDirection == TD2 or i_tradeDirection == TD1)
var bool doShorts = (i_tradeDirection == TD3 or i_tradeDirection == TD1)
// }

// —————————— Functions {

// ————— Functions rounding OHLC to tick precision
f_roundedToTickOHLC() =>
    float _op = round(open  / syminfo.mintick) * syminfo.mintick
    float _hi = round(high  / syminfo.mintick) * syminfo.mintick
    float _lo = round(low   / syminfo.mintick) * syminfo.mintick
    float _cl = round(close / syminfo.mintick) * syminfo.mintick
    [_op, _hi, _lo, _cl]

// ————— Function for a repainting/non-repainting version of the HTF data
f_security(_symbol, _resolution, _src, _repaint) =>
    security(_symbol, _resolution, _src[_repaint ? 0 : barstate.isrealtime ? 1 : 0])[_repaint ? 0 : barstate.isrealtime ? 0 : 1]
// }

// —————————— Calculations {

// ————— Get rounded prices
[rOpen, rHigh, rLow, rClose] = f_roundedToTickOHLC()

// ————— TV built-in MACD code
fastLength = 12
slowlength = 26
MACDLength = 9
MACD = ema(close, fastLength) - ema(close, slowlength)
aMACD = ema(MACD, MACDLength)

// ————— Filter
filterLong = MACD > 0
filterShort = MACD < 0

// ————— States
var float entryPrice = na
var bool inLong = false
var bool inShort = false
bool inTrade = inLong or inShort

// ————— Entries
enterLong  = doLongs  and not inTrade and crossover(MACD, 0)
enterShort = doShorts and not inTrade and crossunder(MACD, 0)

// ————— Stops
atr = atr(14)
stopLong  = min(lowest(5), min(close, open) - atr * 1.5)
stopShort = max(highest(5), max(close, open) + atr * 1.5)

// ————— Exits
exitLong  = inLong  and ((crossunder(MACD, aMACD) and MACD > 0) or close < stopLong[1])
exitShort = inShort and ((crossover(MACD, aMACD) and MACD < 0)  or close > stopShort[1])

if enterLong
    inLong := true
    entryPrice := close
else if enterShort
    inShort := true
    entryPrice := close
else if exitLong
    inLong := false
    entryPrice := na
else if exitShort
    inShort := false
    entryPrice := na

// }



// —————————— Plots {

// ————— Trend Background Colors
bgcolor(filterLong ? C_UPTREND : filterShort ? C_DOWNTREND : na, title = "Trend Background Colors")

// Entry Price Colors
plot(entryPrice, title = "Entry Price", color = color.blue, style = plot.style_circles, linewidth = 2)

// Hard Exit Colors
plot(inLong ? stopLong : inShort ? stopShort : na, title = "Hard Exit Price", color = color.orange, style = plot.style_circles, linewidth = 2)

// ————— Entry Background Colors
plotshape(enterLong, title = "Main BUY", style = shape.triangleup, location = location.belowbar, color = color.green, size = size.large)
plotshape(enterShort, title = "Main SELL", style = shape.triangledown, location = location.abovebar, color = color.red, size = size.large)
plotshape(exitLong, title = "Close BUY", style = shape.diamond, location = location.abovebar, color = color.green, size = size.large)
plotshape(exitShort, title = "Close SELL", style = shape.diamond, location = location.belowbar, color = color.red, size = size.large)

// —————————— Alerts {
alertcondition(enterLong, title = "Buy Alert", message = "Buy Alert")
alertcondition(enterShort, title = "Sell Alert", message = "Sell Alert")
alertcondition(exitLong, title = "Buy Alert", message = "Buy Alert")
alertcondition(exitShort, title = "Sell Alert", message = "Sell Alert")
// }