Pinecoder回测引擎外部信号
Pinecoder Backtesting Engine External Signal
Picture of Oscillator Malfunction
我进去并从 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")
由于第 73 行的 Stops
变量
,振荡器的值跳到“意外”数字
Stops = EnterLong ? FudgeStop__1 : EnterShort ? -FudgeStop__2 : 0
FudgeStop
函数计算止损值,它接近价格,因此取决于图表上加载的代码。 Stops
变量变异为该函数的结果之一 - FudgeStop__1
用于多头交易,FudgeStop__2
用于空头交易。如果您绘制属于 Signal
变量的所有变量并在数据 window 中查看它们,您可以看到结果,如下图所示
如果您不需要将止损值包含到 Signal
变量中 - 删除第 78 行中的三元条件元素(在您的代码中,上面的屏幕截图中没有绘制变量),这样它看起来像这个:
Signal = IncludeExits and Exits != 0 ? Exits :
IncludeEntries and Entries != 0 ? Entries :
IncludeFilter and Filter != 0 ? Filter : na
Picture of Oscillator Malfunction 我进去并从 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")
由于第 73 行的 Stops
变量
Stops = EnterLong ? FudgeStop__1 : EnterShort ? -FudgeStop__2 : 0
FudgeStop
函数计算止损值,它接近价格,因此取决于图表上加载的代码。 Stops
变量变异为该函数的结果之一 - FudgeStop__1
用于多头交易,FudgeStop__2
用于空头交易。如果您绘制属于 Signal
变量的所有变量并在数据 window 中查看它们,您可以看到结果,如下图所示
如果您不需要将止损值包含到 Signal
变量中 - 删除第 78 行中的三元条件元素(在您的代码中,上面的屏幕截图中没有绘制变量),这样它看起来像这个:
Signal = IncludeExits and Exits != 0 ? Exits :
IncludeEntries and Entries != 0 ? Entries :
IncludeFilter and Filter != 0 ? Filter : na