Pine script 更换valuewhen以遵循PineCoder的代码优化指南

Pine script 更换valuewhen以遵循PineCoder的代码优化指南,pine-script,Pine Script,我正在阅读p,更确切地说是如何优化代码。我想在以下场景中替换valuewhen。我想他们提到它是因为在创建警报时警告要重新绘制 尽可能使用一些技巧,如避免使用valuewhen() 在以下情况下,我应该如何避免valuewhen EntryPrice = valuewhen(EnterLong or EnterShort, close, 0) 在pine版本4之前,我的理解是: EntryPrice = 0, EntryPrice := nz(EntryPrice[1]) 现在应该是: va

我正在阅读p,更确切地说是如何优化代码。我想在以下场景中替换
valuewhen
。我想他们提到它是因为在创建警报时警告要重新绘制

尽可能使用一些技巧,如避免使用valuewhen()

在以下情况下,我应该如何避免
valuewhen

EntryPrice = valuewhen(EnterLong or EnterShort, close, 0)
在pine版本4之前,我的理解是:

EntryPrice = 0, EntryPrice := nz(EntryPrice[1])
现在应该是:

var float entryPrice = na
if enterLong or enterShort
    entryPrice := close
对吗

完整代码段:
/@version=3
//@author=LucF,适用于PineCoder
//回溯测试交易引擎的信号【PineCoders】
//2019年6月21日02:58
//松树编码器,所有松树编码器的工具和想法。
//此脚本引用自PineCoders常见问题解答和代码:http://www.pinecoders.com/faq_and_code/#how-我可以使用一个脚本输出作为另一个脚本的输入吗
//文件:https://www.tradingview.com/script/y4CvTwRo-Signal-for-Backtesting-Trading-Engine-PineCoders/
//此脚本是如何为PineCoders反向测试交易引擎配置信号的示例
//您可以在这里找到:https://www.tradingview.com/script/dYqL95JB-Backtesting-Trading-Engine-PineCoders/
//我们使用内置的MACD指示器作为模板。脚本的信号遵循以下协议:
//外部信号协议
//一个脚本只能连接一个外部指示器;为了最大限度地利用它,
//发动机提供将其用作和进入信号、进入/退出信号或过滤器的选项。
//当用作进入信号时,您也可以使用该信号提供进入停止。这就是它的工作原理。
//-对于过滤器状态:对于牛市(允许长输入)供应+1.0,对于熊市(允许短输入)供应-1.0。
//-对于进入信号:长供电+2.0,短供电-2.0。
//-对于出口信号:长距离出口为+3.0,短距离出口为-3.0。
//-发送带有进入信号的进入停止电平:发送长时间进入的正停止电平
//(例如,103.33表示进入长时间段,止损点为103.33),负止损水平表示进入短时间段
//(例如-103.33以进入一个在103.33处停止的短路)。如果您使用此功能,
//您的指示器必须检查1.0、2.0或3.0的精确停止水平及其负对应值,
//为了避免与协议中的其他信号混淆,用勾号对其进行篡改。
//请注意,每次使用都必须在脚本设置/输入的相应部分中确认。
//警报的配置
//进入和事件警报通常配置为每酒吧关闭触发一次。
//退出警报可以配置为每关一次或每关一次触发,具体取决于触发速度
//你想要它们。每小节一次表示警报将在第一次遇到该情况时触发
//在实时酒吧,而不是在酒吧的关闭。
//进入和退出警报将在图表上显示相关标记的同一条上触发。
//事件警报在图表上相应标记出现后在条形图上触发。
研究(“反向测试交易引擎[PineCoders]的信号”)
//允许选择信号内容。
IncludeFilter=输入(真,“包含过滤器”)
IncludeEntries=输入(真,“包含条目”)
IncludeStops=输入(真,“包含带有条目的停止”)
IncludeExits=输入(true,“包含退出”)
// ——————————————————————————————————————————————————————————————————————————————————————
// ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
//这一部分是您放置自己的指示器并定义条件的部分。
//----电视内置MACD代码。
fastLength=输入(12)
慢长=输入(26)
MACDLength=输入(9)
MACD=ema(闭合,快速长度)-ema(闭合,慢速长度)
AMCD=ema(MACD,MACDLength)
//----过滤器。
FilterLong=MACD>0
FilterShort=MACD 0
ExitShort=交叉(MACD、AMCD)和MACD<0
// ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲
// ——————————————————————————————————————————————————————————————————————————————————————
EntryPrice=valuewhen(EnterLong或EnterShort,close,0)
//-----此函数确保不发送等于协议保留值之一的停止值,因此不会将其误解为信号。
//避税的方向是交易风险降低一个刻度,因此多头加,空头减。
fudgetop(_stop,_fudgedir)=>_stop==1.0或_stop==2.0或_stop==3.0_停止+syminfo.mintick*\u fudgedir:\u停止
//----构建与状态对应的所需值。
过滤器=过滤器长?1:过滤港-1 : 0
条目=EnterLong?2:短-2 : 0
停止=长?FudgeStop(StopLong,1):输入short-FudgeStop(StopShort,-1):0
出口=出口长?3:出口短-3 : 0
//-----我们必须决定在多个值不同于0的情况下发送哪个值,因为每个条只能发送一个值。
//出口优先,过滤器状态排在最后。
信号=包括退出和退出!=0 ? 出口:IncludeStops
//@version=3
//@author=LucF, for PineCoders

// Signal for Backtesting-Trading Engine [PineCoders]
//  v1.3, 2019.06.21 02:58

// PineCoders, Tools and ideas for all Pine coders.
// This script is referenced from the PineCoders FAQ & Code here: http://www.pinecoders.com/faq_and_code/#how-can-i-use-one-scripts-output-as-an-input-into-another
// Documentation: https://www.tradingview.com/script/y4CvTwRo-Signal-for-Backtesting-Trading-Engine-PineCoders/

// This script is an example of how to configure a signal for the PineCoders Backtesting-Trading Engine
// which you will find here: https://www.tradingview.com/script/dYqL95JB-Backtesting-Trading-Engine-PineCoders/
// We used the built-in MACD indicator as a template. The script's signal adheres to the following protocol:

// EXTERNAL SIGNAL PROTOCOL
//      Only one external indicator can be connected to a script; in order to leverage its use to the fullest,
//      the engine provides options to use it as either and entry signal, an entry/exit signal or a filter.
//      When used as an entry signal, you can also use the signal to provide the entry stop. Here’s how this works.
//        - For filter state: supply +1.0 for bull (long entries allowed), -1.0 for bear (short entries allowed).
//        - For entry signals: supply +2.0 for long, -2.0 for short.
//        - For exit signals: supply +3.0 for exit from long, -3.0 for exit from short.
//        - To send an entry stop level with entry signal: Send positive stop level for long entry
//          (e.g. 103.33 to enter a long with a stop at 103.33), negative stop level for short entry
//          (e.g. -103.33 to enter a short with a stop at 103.33). If you use this feature,
//          your indicator will have to check for exact stop levels of 1.0, 2.0 or 3.0 and their negative counterparts,
//          and fudge them with a tick in order to avoid confusion with other signals in the protocol.
//      Note that each use must be confirmed in the corresponding section of the script's Settings/Inputs.

// CONFIGURATION OF ALERTS
//      Entry and Event alerts will generally be configured to trigger Once Per Bar Close.
//      Exit alerts can be configured to trigger Once Per Bar Close or Once Per Bar, depending on how fast
//      you want them. Once Per Bar means the alert will trigger the moment the condition is first encountered
//      during the realtime bar, instead of at the bar's close.
//      Entry and Exit alerts trigger at the same bar where the relevant marker will appear on the chart.
//      Event alerts trigger on the bar following the appearance of their respective marker on the chart.

study("Signal for Backtesting-Trading Engine [PineCoders]")

// Allow selection of signal content.
IncludeFilter   = input(true, "Include Filter")
IncludeEntries  = input(true, "Include Entries")
IncludeStops    = input(true, "Include Stops with Entries")
IncludeExits    = input(true, "Include Exits")


// ——————————————————————————————————————————————————————————————————————————————————————
// ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
// This part is the section where you would put your own indicator and define conditions.

// ————— TV built-in MACD code.
fastLength = input(12)
slowlength = input(26)
MACDLength = input(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

// ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲
// ——————————————————————————————————————————————————————————————————————————————————————

EntryPrice = valuewhen(EnterLong or EnterShort, close, 0)

// ————— This function ensures that no stop value equal to one of the protocol's reserved values is sent, so it isn't misinterpreted as a signal.
// The fudge is in the direction where the trade's risk is decreased by a tick, so added for longs and subtracted for shorts.
FudgeStop( _stop, _fudgedir) => _stop == 1.0 or _stop == 2.0 or _stop == 3.0 ? _stop + syminfo.mintick * _fudgedir : _stop

// ————— Build required values corresponding to states.
Filter = FilterLong ? 1 : FilterShort ? -1 : 0
Entries = EnterLong ? 2 : EnterShort ? -2 : 0
Stops = EnterLong ? FudgeStop(StopLong, 1) : EnterShort ? -FudgeStop(StopShort, -1) : 0
Exits = ExitLong ? 3 : ExitShort ? -3 : 0

// ————— We must decide which value will be sent in case more than one is different than 0, since only one value can be sent at each bar.
// Priority is given to exits, with filter states coming last.
Signal = IncludeExits and Exits != 0 ? Exits : IncludeStops and Stops != 0 ? Stops : IncludeEntries and Entries != 0 ? Entries : IncludeFilter and Filter != 0 ? Filter : na

// ————— Plot signal which is to be connected to the Engine through the External Indicator field at the very bottom of the Engine's Settings/Inputs.
plot( Signal, "Signal")


// ————— Plots markers.
plotshape(IncludeEntries and EnterLong,     "Enter Long",   style=shape.triangleup,     location=location.bottom,   color=green,    size=size.small,    text="Enter\nLong")
plotshape(IncludeEntries and EnterShort,    "Enter Short",  style=shape.triangledown,   location=location.top,      color=red,      size=size.small,    text="Enter\nShort")
plotshape(IncludeExits and ExitLong,        "Exit Long",    style=shape.triangledown,   location=location.top,      color=green,    size=size.tiny,     text="Exit\nLong")
plotshape(IncludeExits and ExitShort,       "Exit Short",   style=shape.triangleup,     location=location.bottom,   color=red,      size=size.tiny,     text="Exit\nShort")

bgcolor(color = IncludeFilter and FilterLong ? lime : IncludeFilter and FilterShort ? red : na, title = "Filter Background")

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