Hi,
I tried a search but didn’t find anything, so I ask:
I’m trying to code a buy trigger (f.e.) like this:
c1 = ExponentialAverage[9](MACDline[12,26,9](close))CROSSES OVER 0
c2 = fastMA CROSSES OVER slowMA
IF c1 AND c2 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THEN
BUY 1 CONTRACT AT MARKET
ENDIF
The problem is: the order is not always opened, even if both conditions happen: whenever c1 or c2 happens first, after the remaining condition happens the buy order should always be triggered.
Is there a minimum or max numbers of bars between the 2 events by default?
Or do I need to code a specific instruction for it?
Thanks
IF c1 AND c2 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THEN BUY 1 CONTRACT AT MARKET ENDIF
Hi Alex
All code is read / executed at the end of each bar and so conditions need to occur within that bar.
As you are using AND between each of the 4 conditions then all 4 need to occur on the same bar.
Also your C1 and C2 conditions both use crosses over and to get two ‘crosses over’ in 1 bar could be rare / not often occur??
Exact, that’s what I thought.
But what if I want both crossover within x bar since the first one? Is it possible to code it?
If I just use “>” for one condition it gives too much false signals with a misinterpretation of my strategy.
But what if I want both crossover within x bar since the first one?
sounds like there is 3 crossovers with one crossover repeating itself??
Below might help with C2 crossover occurring x bars ago?
c1 = ExponentialAverage[9](MACDline[12,26,9](close))CROSSES OVER 0
c2 = fastMA[x] CROSSES OVER slowMA[x]
Anybody feel free to add any ideas you may have (my brain needs sleep! :)?
IF (condition1) and Not Flag = 2 THEN
Flag = 1
Bar = BarIndex
ENDIF
IF (condition2) and Not Flag = 1 THEN
Flag = 2
Bar = BarIndex
ENDIF
IF Flag = 1 and BarIndex - Bar < (however many bars you want) and (condition2) THEN
BUY 1 contract at Market
Flag = 0
ENDIF
IF Flag = 2 and BarIndex - Bar < (however many bars you want) and (condition1) THEN
BUY 1 contract at Market
Flag = 0
ENDIF
The above might be what you want? It will look for both conditions and then if one of them is met it will buy if the second condition is met within your desired number of bars I think. Not tested and written after a few glasses of wine so might be rubbish!
Just realised that if both conditions happen in the same candle then a position will not be opened so you would need to change it to this for that to happen:
Still might be rubbish as not tested and another few sips of wine!
IF (condition1) and Not Flag = 2 THEN
Flag = 1
Bar = BarIndex
ENDIF
IF (condition2) and Not Flag = 1 THEN
Flag = 2
Bar = BarIndex
ENDIF
IF Flag = 1 and BarIndex - Bar < (however many bars you want) and (condition2) THEN
BUY 1 contract at Market
Flag = 0
ENDIF
IF Flag = 2 and BarIndex - Bar < (however many bars you want) and (condition1) THEN
BUY 1 contract at Market
Flag = 0
ENDIF
IF (condition1 and condition2) THEN
BUY 1 contract at Market
ENDIF
Thanks guys, I will try your codes asap, but it seems to me that Vonasi’s last one is what I was looking for.
It surprises me nobody else before had a similar problem. Due to the limits of PRT (lack of mtf f.e.) I find useful to have a restricted number of bars within having my conditions met.
No problem AlexF – glad to be of assistance. If it works OK then maybe GraHal as the ‘Keeper of the Snippet Library’ could add it in for others to easily find.
GraHal as the ‘Keeper of the Snippet Library’ could add it in for others to easily find.
Done / added, but I’m not the keeper! 🙂 It’s open to anybody to add Snippets … takes 10 seconds.
What if I get knocked down by a bus!? 🙂
https://docs.google.com/spreadsheets/d/1rgboqj7sVwsP9ZRhOduOefye48QMWC07jWVXCl-KJPU/edit?usp=sharing
but I’m not the keeper! What if I get knocked down by a bus!?
…but I would have to learn a whole new process. You’ll be expecting me to let the butler go next. As for the bus – try to avoid standing in the road when they are approaching. If it does happen then at least you will have left a legacy. ‘Here lies GraHal – creator and keeper of the snippet library at https://docs.google.com/spreadsheets/d/1rgboqj7sVwsP9ZRhOduOefye48QMWC07jWVXCl-KJPU/edit#gid=0‘. Although maybe on second thoughts that would cost a bit too much to have chiselled on your tombstone. 🙂
It doesn’t seems to work.
I excluded the condition
IF (condition1 and condition2) THEN
BUY 1 contract at Market
ENDIF
to test the double conditions within x bars, and I used variable numbers between 3 and 10, but nothing at all.
Any ideas?
I found the working function:
if summation[x](c1) and summation[x](c2) then
where x is the numbers of bar between the two events
I found the working function:
Excellent. Reviewing my code now in the sober light of day I can see that the flag is never reset to zero if both conditions are not met within the number of bars. Your solution is much neater and simpler – I don’t know why but my brain never thinks to use SUMMATION when I’m coding.
Grahal – you might want to change the snippet library to point at AlexF’s code rather than mine as his works!
Grahal – you might want to change the snippet library to point at AlexF’s code rather than mine as his works!
Done! I had them both in for a day or so, thanks for letting me know … 26 Snippets now
Snippet Link Library