first hit of a moving average
Forums › ProRealTime English forum › ProOrder support › first hit of a moving average
- This topic has 31 replies, 3 voices, and was last updated 6 years ago by
GraHal.
-
-
07/03/2018 at 1:24 AM #75104
Hi,
Any idea’s on how one would beginning coding the following buy entry:
These are the conditions –
- Let’s say you have the 20MA which first crosses over the 200MA but needs to be higher by X pips [time frames get reset every day for example] => so there’s gaping between the 20MA and the 200MA
- When the above occurs for the first time in the daily window, if prices come back down and touch the 20MA for the first time after the above event this is the entry
I’m new to this auto- trading and just trying to piece together a number of factors which I use currently.
Appreciate any help, thanks
07/03/2018 at 7:17 AM #7510512345678910111213141516171819202122232425262728starttime = 000000gapneeded = 20ma20 = average[20](close)ma200 = average[200](close)magap = ma20 - ma200if opentime = starttime thencrossflag = 0gapflag = 0endifif ma20 crosses over ma200 thencrossflag = 1endifif crossflag and magap > gapneeded thengapflag = 1endifif ma20 crosses under ma200 thencrossflag = 0gapflag = 0endifif crossflag and gapflag thenbuy at ma20 limitendifThis might do what you want if I understand your description correctly. Not tested and written after only half a cup of coffee! You will need to add sell coding etc
1 user thanked author for this post.
07/03/2018 at 7:43 AM #75113Added to here Snippet Link Library
07/03/2018 at 8:46 AM #75118thanks v much!
Actually I need the time frame to be more variable. As sometimes the 20MA crosses over the 200MA on a different day so I want to include that.
Ideally I’d like to start the loop say first thing Monday on say a 10 minute timeframe.
If crossflag and gapflag is triggered for the first time in the week they become 1 as you wrote.
Than if the buy entry is satisfied all good as you wrote it takes the trade. This would be the first hit of the 20MA after condition 1…. if prices than go up and come back down to the 20MA it does nothing as that would be the second hit (I think your code doesn’t buy the second hit if I’m correct?)
Than if the 20MA crosses under the 200MA it resets the variables to zero (as you wrote).
But I’d like to restart the process again once those variables turn to zero? and it just loops through out the whole week.
Another thing – is there anyway to determine the angle of a moving average? so another condition would be if the slope of 200MA is greater than say 15% (measured between say current and X periods back) this would be another condition that needs to be meet? though it may be too complicated to code?
07/03/2018 at 10:00 AM #7513812345678910111213141516171819202122232425262728293031323334353637defparam cumulateorders = falsestarttime = 000000startday = 1gapneeded = 20ma20 = average[20](close)ma200 = average[200](close)magap = ma20 - ma200if opentime = starttime and opendayofweek = startday thencrossflag = 0gapflag = 0endifif ma20 crosses over ma200 thencrossflag = 1endifif crossflag and magap > gapneeded thengapflag = 1endifif ma20 crosses under ma200 thencrossflag = 0gapflag = 0endifif crossflag and gapflag thenbuy at ma20 limitendifif onmarket and (your exit conditions) thensell at marketcrossflag = 0gapflag = 0endifI think this is what you are asking for. It resets at the start of Monday, allows only one trade at a time and looks for another cross over and gap and pullback whenever it is not on the market.
07/03/2018 at 10:15 AM #75144For the gradient you might want to see here:
https://www.prorealcode.com/topic/ema-45-degres-uptrend/
It is not possible to calculate the angle due to the fact that the x value is of a different type to the y value. All you can do is subtract the value n bars ago from the current value and compare this to previous values to determine whether steepness is increasing or decreasing.
07/04/2018 at 8:10 AM #75187I put this into the backtest and didn’t get any results?
I think my exit conditions may be incorrect, you can see what I’m trying to do.
It’s pretty much the exact code you wrote though….
1234567891011121314151617181920212223242526272829303132333435363738394041424344// Definition of code parametersDEFPARAM CumulateOrders = False // Cumulating positions deactivatedstarttime = 000000startday = 1gapneeded = 20ma20 = average[20](close)ma200 = average[200](close)magap = ma20 - ma200if opentime = starttime and opendayofweek = startday thencrossflag = 0gapflag = 0endifif ma20 crosses over ma200 thencrossflag = 1endifif crossflag and magap > gapneeded thengapflag = 1endifif ma20 crosses under ma200 thencrossflag = 0gapflag = 0endifif crossflag and gapflag thenbuy at ma20 limitendif// Stops and targetsSET STOP pLOSS 25SET TARGET pPROFIT 25crossflag = 0gapflag = 0//if onmarket and (exit condition) then//sell at market//crossflag = 0//gapflag = 0//endif07/04/2018 at 8:17 AM #75188Please use the ‘Insert PRT Code’ button when putting code in your future posts to make it esier for everyone to read – I have tidied up your last post for you. 🙂
I tested it yesterday and it worked. You do not say what time frame or market you are testing on.
07/04/2018 at 8:30 AM #75190The problem is that you are setting the flags to zero on the last lines of code so they will always be zero – so no trades!
Try something like this:
12345678910111213141516171819202122232425262728293031323334defparam cumulateorders = falsestarttime = 000000startday = 1gapneeded = 20ma20 = average[20](close)ma200 = average[200](close)magap = ma20 - ma200if (opentime = starttime and opendayofweek = startday) or onmarket thencrossflag = 0gapflag = 0endifif ma20 crosses over ma200 thencrossflag = 1endifif crossflag and magap > gapneeded thengapflag = 1endifif ma20 crosses under ma200 thencrossflag = 0gapflag = 0endifif not onmarket and crossflag and gapflag thenbuy at ma20 limitendifSET STOP pLOSS 25SET TARGET pPROFIT 25There probably is a neater solution but this works.
07/04/2018 at 8:47 AM #7519107/04/2018 at 11:37 AM #75198Hi,
I copied in the code exactly how you had it above.
When I run it on UKX (10 minute chart) it seems to buy when the 20MA is above the 200MA by the gapneeded.
(See attached backtest picture). The light blue line is the 20MA and the yellow line is the 200MA.
However, what it should do is first satisfy the condition above, than when prices retrace back to the 20MA (blue line – first hit). It takes the trade than either at market on the next candle open or limit order on the 20MA as you had it.
It think its missing the second condition?
By the way instead of gap 20, how would I make it 20 pips if I wanted to run it on currency pairs?
Thanks again for your help, really appreciate it.
07/04/2018 at 12:17 PM #75203Are you certain that you have the correct moving average on your chart. This is what mine looks like – seems to work just fine.
The graph is the values of gapflag and crossflag.
If you are with IG then the code should work as it is but you can change the 20 to 20 * pipsize if you want to make sure it is 20 pips.
07/04/2018 at 10:57 PM #7524207/06/2018 at 6:41 AM #75412I modified the above code to the below – difference is the trade should get taken on first hit of the 50MA (rather than the 20MA). But there is another condition and that is when prices do hit the 50MA, the next bar’s high must be greater than the 50MA to take the trade. I can’t quite get it to work though based on the results I have attached. The entry gets taken even when the next bar’s high isn’t greater than the 50MA (in dark blue). Yellow = 200 and light blue = 20. Any idea’s what I am doing wrong? I have brushed up on my coding since last time and can now actually read what’s going on sort of 🙂 … Sorry I tried to entry the code into the correct window, but this is what I got below.
123456789101112131415161718192021222324252627282930313233starttime = 000000startday = 1gapneeded = 20 * pipsizema20 = average[20](close)ma50 = average[50](close)ma200 = average[200](close)magap = ma20 - ma200if (opentime = starttime and opendayofweek = startday) or onmarket thencrossflag = 0gapflag = 0endifif ma20 crosses over ma200 thencrossflag = 1endifif crossflag and magap > gapneeded and Dhigh(0) >= ma50 thengapflag = 1endifif ma20 crosses under ma200 thencrossflag = 0gapflag = 0endifif not onmarket and crossflag and gapflag thenbuy at ma50 limitendifSET STOP pLOSS 25SET TARGET pPROFIT 2507/06/2018 at 8:25 AM #75420Sorry I tried to entry the code into the correct window, but this is what I got below.
I tidied your post up for you…… again! 🙂
-
AuthorPosts
Find exclusive trading pro-tools on