我从tradingview中拿了一个例子,这个例子是为Pinecoders反向测试引擎设计的,在这个引擎中,你设计了一个外部输入信号,通常是-3到正3,以被策略识别为二进制的长信号或短信号。如果你编译我的,你会看到信号图跳到了移动的交叉点或交叉点的实际市场价格,而不是我试图分配给它的振荡器值,这是一个大问题,因为我不能得到一个策略来读取一个不是-2,2,-1或1等等的未知值。我尝试了很多方法,它一直在读取并绘制实际的交叉点或交叉点的移动平均线。有什么想法吗?
//@version=4
// Original Author - Rajandran R
// Updated to Pinescript version 3 by Cosmic_Coin
study(title="TestRiskExternalInput", shorttitle="TestRiskExternalInput")
////////////////////////////////////////////////////////////////////////////////
// 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)
quickMa = sma(close, fastLength)
slowMa = sma(close, slowlength)
// ————— Exits.
//SourceTypeL = 0//crossover(quickMa, slowMa)//[idx]
//SourceTypeS = 0 //crossunder(quickMa, slowMa)//[idx]
tempcharL = 0
tempcharS = 0
if crossover(quickMa, slowMa)
tempcharL := 2
tempcharS := 0
if crossunder(quickMa, slowMa)
tempcharS := -2
tempcharL := 0
// ————— Filter.
FilterLong = MACD > 0
FilterShort = MACD < 0
// ————— Entries.
EnterLong = tempcharL
EnterShort = tempcharS
// ————— 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
// ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲
// ——————————————————————————————————————————————————————————————————————————————————————
// ————— 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 = tempcharL == 2 ? 2 : tempcharS == -2 ? -2 : 0
FudgeStop__1 = FudgeStop(StopLong, 1)
FudgeStop__2 = FudgeStop(StopShort, -1)
Stops = EnterLong ? FudgeStop__1 : EnterShort ? -FudgeStop__2 : 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")发布于 2020-11-11 02:12:26
由于第73行的Stops变量,振荡器的值会跳到“意外”数字
Stops = EnterLong ? FudgeStop__1 : EnterShort ? -FudgeStop__2 : 0FudgeStop函数计算非点数的值,它接近于价格,因此取决于图表上加载的滚动条。Stops变量变为此函数的结果之一- FudgeStop__1表示多头交易,FudgeStop__2表示空头交易。如果绘制属于Signal变量的所有变量,并在数据窗口中查看它们,则可以看到结果,如下图所示

如果您不需要在Signal变量中包含非索引值-删除第78行中的三元条件元素(代码的第78行,上面的屏幕截图中没有绘制变量),那么它将如下所示:
Signal = IncludeExits and Exits != 0 ? Exits :
IncludeEntries and Entries != 0 ? Entries :
IncludeFilter and Filter != 0 ? Filter : nahttps://stackoverflow.com/questions/64761605
复制相似问题