How to buy/sell using the opening price?
How to buy/sell using the opening price after a day or selected day of the crossover condition?
set Buy = Your Condition on Buy
//Ref(Buy,1) value of Buy 1 day ago
set BuyPrice = if(Ref(Buy,1),Open,0)
To store your buy price you can use the blockref.
Please also note that backtester has already a built in option to buy and sell using next day Open price without changing your script (Next Candle Buy/Sell - Open Price)
set Buy = Your Condition on Buy
//Ref(Buy,1) value of Buy 1 day ago
set BuyPrice = if(Ref(Buy,1),Open,0)
To store your buy price you can use the blockref.
Please also note that backtester has already a built in option to buy and sell using next day Open price without changing your script (Next Candle Buy/Sell - Open Price)
Since I can not buy at EOD, I screen at night. Testing how effective to buy at opening and sell at before close. Also want to test selling the next day.
The code you showed gave me an issue, see below code and error.
// Set Buy and Sell
Set nBuy = crossover(MACD_Line, MACD_Signal)
Set Buy = if(REF(nBuy,-1),Open,0)
Set Sell = Crossover(MACD_Signal,MACD_Line)
Error Below
#Moving Average Convergence/Divergence (MACD)
Set MACD_Line = MACD(12, 26, 9, Exponential)
Set MACD_Signal = MACDSIGNAL(12, 26, 9, Exponential)
Set HISTOGRAM = MACD_Line - MACD_Signal
// Set Buy and Sell
Set nBuy = crossover(MACD_Line, MACD_Signal)
set nBuy1 = REF(nBuy,1)
Set Buy = if(nBuy1 ,Open,0)
Set Sell = Crossover(MACD_Signal,MACD_Line)
Plot(Buy , Bar ,Any, nBuy)
Plot(HISTOGRAM , Bar ,Any, MACD)
Plot(MACD_Line, Line,Any, MACD)
Plot(MACD_Signal, Line,Any, MACD)
Sorry for my previous example. Set REF(nBuy,1) to variable before passing to IF. There is problem with the parsing. "If" cannot read parameters properly if Ref put inside directly.
#Moving Average Convergence/Divergence (MACD)
Set MACD_Line = MACD(12, 26, 9, Exponential)
Set MACD_Signal = MACDSIGNAL(12, 26, 9, Exponential)
Set HISTOGRAM = MACD_Line - MACD_Signal// Set Buy and Sell
Set nBuy = crossover(MACD_Line, MACD_Signal)
set nBuy1 = REF(nBuy,1)
Set Buy = if(nBuy1 ,Open,0)
Set Sell = Crossover(MACD_Signal,MACD_Line)Plot(Buy , Bar ,Any, nBuy)
Plot(HISTOGRAM , Bar ,Any, MACD)
Plot(MACD_Line, Line,Any, MACD)
Plot(MACD_Signal, Line,Any, MACD)
Sorry for my previous example. Set REF(nBuy,1) to variable before passing to IF. There is problem with the parsing. "If" cannot read parameters properly if Ref put inside directly.
Sorry, still giving me the wrong result, please see screen shot and script.
Set MACD_line = MACD(12,26,9, exponential)
Set MACD_signal = MACDSIGNAL(12,26,9, exponential)
Set Hist = MACD_line - MACD_signal
Set nBuy = crossover(MACD_Line, MACD_Signal)
set nBuy1 = REF(nBuy,-2)
Set Buy = if(nBuy1 ,Open,0)
Set Sell = Ref(Buy,-1)
Set MACDhistup = if(hist > 0, hist ,0)
Set MACDhistdown = if(hist < 0, hist, 0)
plot(MACD_line, line, Blue, MACD)
plot(MACD_signal, line, Red, MACD)
plot(MACDhistup, stackedbar, green, MACD)
plot(MACDhistdown, stackedbar, red, MACD)
plotbuysell (buy, sell, green, red, main)
signals(Buy,Sell)



