I have been trying to research how I can trigger LONG and EXIT from both crossover and price above security in my backtesting logic without luck and ask now for assistance with my backtesting pine editor code.
I will include some parts of my code so you see what I trying to do.
// add trading pair outside chart data
btc_price = security("BINANCE:BTCUSDT", "D", close)
// compute the indicators ( main chart ETH/USDT )
smaInput = input(title="SMA", type=input.integer, defval=1)
indicator1 = sma(close,smaInput)
// compute the indicators BTC
btcsmaInput = input(title="SMABTC", type=input.integer, defval=1)
indicatorbtc = sma(btc_price,btcsmaInput)
// plot the indicators
plot(indicator1, title="Indicator1", color=color.red, linewidth=2)
plot(indicatorbtc, title="Indicator1", color=color.blue, linewidth=2)
What I trying to achieve in pseudo-code: Go Long IF close IS ABOVE indicator1(ETH/USDT) and BTC/USDT price IS ABOVE indicatorbtc Go Exit IF close IS BELOW indicator1(ETH/USDT) or BTC/USDT price close IS BELOW indicatorbtc
Here is my code that I need help with, the code below does not work as I describe it above in the pseudo-code.
// Backtesting Logic
EnterLong = crossover(close,indicator1) and crossover(btc_price, indicatorbtc)//
ExitLong = crossunder(close,indicator1) or crossunder(btc_price, indicatorbtc) //
Please advice.
crossover
and crossunder
will be true
for one bar only and that is when the crossing action becomes true
. Your AND
condition will fail unless both crossovers happen at the same time.
Here is another idea, use actual values of those variables. So, if btc_price
is greater than indicatorbtc
, you know that this crossover has already happened. And to trigger your entry function only once, you can use crossover()
and the value comparison together.
EnterLong = (crossover(close,indicator1) or crossover(btc_price, indicatorbtc)) and ((close > indicator1) and (btc_price > indicatorbtc))
So, the first part (crossover(close,indicator1) or crossover(btc_price, indicatorbtc))
will only be true
when one of those crossovers happen. Then you can look at the actual values to figure out if the other crossover has happened in the past. That is the ((close > indicator1) and (btc_price > indicatorbtc))
part.