Hello people.
I want my bots to enter a long trade when there has been a previous cross on the green moving average lines, and the moving average lines are above the central black bollingers.
As per the screengrab, I’d like to be able to enter on August 4, i.e. “The cross condition was met on August 2. Then on August 3 the averages have eventually got higher than the bollinger. Now I want to enter on August 4”.
So I’d like the first entry rule to be committed to memory until such time as the second entry rule is passed in a later bar.
How would I do this?
Thank you for your help.
Averagecrosslong= ExponentialAverage[8](close) crosses over ExponentialAverage[13](close)
Abovebollinger= ExponentialAverage[8](close) and ExponentialAverage[13](close) > Average[21](close)
Averagecrossshort= ExponentialAverage[8](close) crosses under ExponentialAverage[13](close)
Belowbollinger= ExponentialAverage[8](close) and ExponentialAverage[13](close) < Average[21](close)
If Averagecrosslong and Abovebollinger THEN
BUY 1 PERPOINT AT MARKET
ENDIF
If Averagecrossshort and Belowbollinger THEN
SELLSHORT 1 PERPOINT AT MARKET
ENDIF
SET STOP pTRAILING VLSTOP
Do you need to cross would the one average being above the other in the current period not give the same entry condition?
Forget crosses over and crosses under and us > and < and also adjust the way you think AND works in your bollinger conditions and you have what you want if I understand your question right.
Averagecrosslong= ExponentialAverage[8](close) > ExponentialAverage[13](close)
Abovebollinger = (ExponentialAverage[8](close) > Average[21](close)) and (ExponentialAverage[13](close) > Average[21](close))
Averagecrossshort= ExponentialAverage[8](close) < ExponentialAverage[13](close)
Belowbollinger= (ExponentialAverage[8](close < Average[21](close)) and (ExponentialAverage[13](close) < Average[21](close))
If Averagecrosslong and Abovebollinger THEN
BUY 1 PERPOINT AT MARKET
ENDIF
If Averagecrossshort and Belowbollinger THEN
SELLSHORT 1 PERPOINT AT MARKET
ENDIF
SET STOP pTRAILING VLSTOP
Do you need to cross would the one average being above the other in the current period not give the same entry condition?
The one average simply being above or below the other would give an entry on pretty much every bar, which isn’t the objective. I simply want to react to the instance of a cross and enter one trade, and only re enter once the cross occurs again.
Just add to the top of the code:
defparam cumulateorders = false
Yes, I’m using that piece of code in the bot that generated the above results. It must be something else that is needed 🙁
My guess is that your trailing stop is too close as trades are opening and closing in the same bar then the conditions are met again at the next bar.
Try this (not tested):
defparam cumulateorders = false
Averagecrosslong= ExponentialAverage[8](close) > ExponentialAverage[13](close)
Abovebollinger = (ExponentialAverage[8](close) > Average[21](close)) and (ExponentialAverage[13](close) > Average[21](close))
Averagecrossshort= ExponentialAverage[8](close) < ExponentialAverage[13](close)
Belowbollinger= (ExponentialAverage[8](close < Average[21](close)) and (ExponentialAverage[13](close) < Average[21](close))
if Averagecrosslong and not averagecrosslong[1] then
crossflag = 1
endif
if crossflag = 1 and Abovebollinger THEN
BUY 1 PERPOINT AT MARKET
crossflag = 0
ENDIF
if Averagecrossshort and not averagecrossshort[1] then
crossflag = -1
endif
If crossflag = -1 and Belowbollinger THEN
SELLSHORT 1 PERPOINT AT MARKET
crossflag = 0
ENDIF
SET STOP pTRAILING VLSTOP
There shouldn’t be any delay. All the code does is check for the average cross condition and only checks the bollinger situation if this has occurred. This can happen in the same bar so there should be no delay. It is difficult for me to see in your image what the exact state of the averages is – are you sure they are actually how you think they are?
Add the following to the bottom of the code so that you can see when each condition is true:
graph crossflag
graph averagecrosslong
graph averagecrossshort
graph abovebollinger
graph belowbollinger
I’ve done that with the code. Now the screengrab shows the black X for the Bollinger occurring on Aug 5 (this is the right-hand X of the two) but if you look at the lines on the chart, the purple moving average is above the black Bollinger line on Aug 3 and Aug 4. How can it be that PRT says it was only above the Bollinger on Aug 5?
It’s easier for others to understand if when you take a screenshot it includes dates and possibly hovering over the candle so that we have a data window to see what each line is.
Sorry, I missed the date from the first grab. Here are three screenshots with a hover over Aug 3, Aug 4 and Aug 5.
All criteria are met on Aug 3 and yet the graph cross says “abovebollinger” is 0. It’s even more obvious that all criteria are met on Aug 4, but it’s only on Aug 5 that the graph acknowledges that “abovebollinger” has been met.
I see no issue. At the close of the candle on the 5th all conditions to go long are true. At this point the crossflag is set to zero and an order placed to open a trade at the open of the next candle. This is what happens in your images.
But all the conditions were met on the 3rd and again on the 4th. The averages are above the black bollinger on the 3rd and 4th – it’s clear to see by looking at the lines. Why did it not enter on the 4th or the 5th?