0
votes

Picture of Oscillator Malfunction I went in and took an example from tradingview that was designed for Pinecoders Backtesting engine where you design a external input signal that is typically -3 to positive 3 to be recognized by a strategy as a binary long or short signal. Well if you compile mine you see the Signal plot jumps up to the actual literal market price of a moving crossover or cross under and not the oscillator value I'm trying to assign to it, big problem since I can't get a strategy to read an unknowable value that isn't a -2 or 2 or -1 or 1 etc. I've tried a lot of things and it keeps reading through and plotting the actual crossover or crossunder of the moving average. Any ideas?

//@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")
1
Could you be more specific whats the issue with the code? Your snippet output varies from 3 to -3, isn't it an expected output? - e2e4
You'll have to compile it, and if you look at it in the underside at the plot the blue line plot for "Signal" it varys between 0, 1 or -1 , 3 or -3, then spikes abruptly to like 400 dollars on ethereum, the lliteral market price is being passed through to the signal oscilator. I want it to pass a value "when" a crossover happens as an oscillator value, it is passing the literal market value. I will upload a photo. - Styles Grant
Ok, got it. See my answer below in a few. - e2e4

1 Answers

0
votes

The oscillator's value jump to the "unexpected" numbers because of the Stops variable at line 73

Stops = EnterLong ? FudgeStop__1 : EnterShort ? -FudgeStop__2 : 0

FudgeStop function calculate the stoploss value, it is close to the price so depends on the ticker loaded on your chart. Stops variable mutate to one of the results of this function - FudgeStop__1 for long trades and FudgeStop__2 for short trades. You can see the result if you'll plot all variables that are parts of your Signal variable and see them in the data window as in the picture below enter image description here

If you don't need to include the stoploss value to the Signal variable - remove ternary conditional element in line 78(of your code, without plotted variables on my screenshot above) so it will look like this:

Signal = IncludeExits and Exits != 0 ? Exits : 
  IncludeEntries and Entries != 0 ? Entries : 
  IncludeFilter and Filter != 0 ? Filter : na