first hit of a moving average

Forums ProRealTime English forum ProOrder support first hit of a moving average

Viewing 15 posts - 1 through 15 (of 32 total)
  • #75104

    Hi,

    Any idea’s on how one would beginning coding the following buy entry:

    These are the conditions –

    1. 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
    2. 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

     

     

     

    #75105

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

    Added to here Snippet Link Library

    #75118

    thanks 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?

     

    #75138

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

    #75144

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

    #75187

    I 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….

     

     

    #75188

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

    #75190

    The 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:

    There probably is a neater solution but this works.

     

     

    #75191

    Sorry – I just remembered that you are buying on a limit order so my last code is wrong. I will edit the code in the last post to correct it.

    #75198

    Hi,

    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.

     

     

    #75203

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

    Screenshot_1-2

    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.

    #75242

    Yes I got it to work! 🙂 Thanks mate

    #75412

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

     

     

    #75420

    Sorry I tried to entry the code into the correct window, but this is what I got below.

    Screenshot_1-3

    I tidied your post up for you…… again! 🙂

Viewing 15 posts - 1 through 15 (of 32 total)

Create your free account now and post your request to benefit from the help of the community
Register or Login