Hallo there,
Please help me.
indicator1 = Average[8](close)
indicator2 = Average[24](close)
c1 = (indicator1 CROSSES OVER indicator2)
I want to calculate the Close of the candle (c1 above) when the cross-over occurred (or just after the cross-over occurred). I want to enter a trade once a candle following the cross-over closes above the close of the cross-over candle.
You need here to store the Close value when your condition c1 occurred:
c1 = (indicator1 CROSSES OVER indicator2)
if c1 then
val = Close
endif
Then you can test if the next Closes will cross over this value. But it’s also possible that the price is already above this level .. so you have to deal with this too.
c2 = Close crosses over val
Thank you Nic, you’ve put me on the path to success.
I amended my code but now don’t get any Long entries. Can you possibly see what is wrong with my code?
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Conditions to enter long positions
indicator1 = Average[8](close)
indicator2 = Average[24](close)
c1 = (indicator1 CROSSES OVER indicator2)
if c1 then
val = Close
endif
c2 = Close crosses over val
c3 = Average[8](close) > Average[24](close)
IF c1 THEN
WHILE c3
IF c2 THEN
BUY 1 CONTRACT AT MARKET
ENDIF
WEND
ENDIF
// Conditions to exit long positions
indicator3 = Average[24](close)
c4 = (close < indicator3)
IF c4 THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
indicator4 = Average[8](close)
indicator5 = Average[24](close)
c5 = (indicator4 CROSSES UNDER indicator5)
IF c5 THEN
SELLSHORT 1 CONTRACT AT MARKET
ENDIF
// Conditions to exit short positions
indicator6 = Average[24](close)
c6 = (close > indicator6)
IF c6 THEN
EXITSHORT AT MARKET
ENDIF
// Stops and targets
SET STOP %LOSS 2
You should GRAPH you new “val” variable and see if it correctly stored the good information, also with Close to test the cross over “visually”
GRAPH val coloured(255,0,0)
GRAPH close coloured(0,200,20)
I haven’t tried it, but isn’t there a “DO” missing in line 18 for the “While… Do… Wend”?
Hi, me again,
I amended the code but it still doesn’t create Long positions. After the initial cross-over, I wait for confirmation (c2 and c3), which could be a few candles down the line, before I enter a trade (post confirmation). Does my code reflect this?
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Conditions to enter long positions
indicator1 = Average[8](close)
indicator2 = Average[24](close)
c1 = (indicator1 CROSSES OVER indicator2)
if c1 then
val = Close
endif
c2 = Close crosses over val
c3 = Average[8](close) > Average[24](close)
IF c1 THEN
WHILE c3 DO
IF c2 THEN
BUY AT MARKET
ENDIF
WEND
ENDIF
GRAPH val coloured(255,0,0)
GRAPH close coloured(0,200,20)
> For clarity of messages on ProRealCode’s forums, please use the “insert code PRT” button to separate the text of the code part! Thank you! <<
Did you learn something while debugging with GRAPH? Do the “val” variable correctly stored? Do the Close cross above it sometimes? ..
Hi Nic,
I will comply with the “insert code PRT” requirement. Thanks for pointing that out.
The “val” variable and Close variables are correctly stored and displayed on the graph. If I look at my chart, there are many instances where a candle following the cross-over candle closes above “val” but the code doesn’t trigger the respective entries so I can’t do proper debugging. Please see attached screen dump.
Thanks