Buy or sell using o...
 
Notifications
Clear all

Buy or sell using opening Price

6 Posts
2 Users
0 Likes
1,264 Views
ZaQ
 ZaQ
(@rhanz)
Posts: 15
Active Member User
Topic starter
 

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?

 

 

 
Posted : 22/04/2021 11:46 pm
snoperg
(@snoperg)
Posts: 15
Admin Admin
 

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)

1619139397-Capture.jpg
 
Posted : 23/04/2021 8:52 am
ZaQ
 ZaQ
(@rhanz)
Posts: 15
Active Member User
Topic starter
 
Posted by: @snoperg

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)

1619139397-Capture.jpg

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

 

 

1619153933-REF.jpg
 
Posted : 23/04/2021 12:57 pm
snoperg
(@snoperg)
Posts: 15
Admin Admin
 

#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.

 
Posted : 23/04/2021 10:25 pm
ZaQ
 ZaQ
(@rhanz)
Posts: 15
Active Member User
Topic starter
 
Posted by: @snoperg

#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)

1619363302-buy_open_script.jpg
 
Posted : 25/04/2021 11:08 pm
snoperg
(@snoperg)
Posts: 15
Admin Admin
 

Sorry for confusion. If you just want to buy it in next day using Open price, just code your script as is (no special condition) but in backtesting, make sure to click NEXT CANDLE BUY/SELL OPEN PRICE.

1619393378-Capture.jpg
 
Posted : 26/04/2021 7:29 am
Share: