first hit of a moving average

Viewing 15 posts - 1 through 15 (of 32 total)
  • Author
    Posts
  • #75104 quote
    Bullets
    Participant
    Junior

    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 quote
    Vonasi
    Moderator
    Master
    starttime = 000000
    gapneeded = 20
    
    ma20 = average[20](close)
    ma200 = average[200](close)
    magap = ma20 - ma200
    
    if opentime = starttime then
    crossflag = 0
    gapflag = 0
    endif
    
    if ma20 crosses over ma200 then
    crossflag = 1
    endif
    
    if crossflag and magap > gapneeded then
    gapflag = 1
    endif
    
    if ma20 crosses under ma200 then 
    crossflag = 0
    gapflag = 0
    endif
    
    if crossflag and gapflag then
    buy at ma20 limit
    endif
    
    

    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

    Pepsmile thanked this post
    #75113 quote
    GraHal
    Participant
    Master

    Added to here Snippet Link Library

    #75118 quote
    Bullets
    Participant
    Junior

    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 quote
    Vonasi
    Moderator
    Master
    defparam cumulateorders = false
    
    starttime = 000000
    startday = 1
    gapneeded = 20
     
    ma20 = average[20](close)
    ma200 = average[200](close)
    magap = ma20 - ma200
     
    if opentime = starttime and opendayofweek = startday then
    crossflag = 0
    gapflag = 0
    endif
     
    if ma20 crosses over ma200 then
    crossflag = 1
    endif
     
    if crossflag and magap > gapneeded then
    gapflag = 1
    endif
     
    if ma20 crosses under ma200 then
    crossflag = 0
    gapflag = 0
    endif
     
    if crossflag and gapflag then
    buy at ma20 limit
    endif
    
    if onmarket and (your exit conditions) then
    sell at market
    crossflag = 0
    gapflag = 0
    endif
    

    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 quote
    Vonasi
    Moderator
    Master

    For the gradient you might want to see here:

    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 quote
    Bullets
    Participant
    Junior

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

     

    // Definition of code parameters
    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    
    starttime = 000000
    startday = 1
    gapneeded = 20
    
    ma20 = average[20](close)
    ma200 = average[200](close)
    magap = ma20 - ma200
    
    if opentime = starttime and opendayofweek = startday then
    crossflag = 0
    gapflag = 0
    endif
    
    if ma20 crosses over ma200 then
    crossflag = 1
    endif
    
    if crossflag and magap > gapneeded then
    gapflag = 1
    endif
    
    if ma20 crosses under ma200 then
    crossflag = 0
    gapflag = 0
    endif
    
    if crossflag and gapflag then
    buy at ma20 limit
    endif
    
    // Stops and targets
    SET STOP pLOSS 25
    SET TARGET pPROFIT 25
    crossflag = 0
    gapflag = 0
    
    //if onmarket and (exit condition) then
    //sell at market
    //crossflag = 0
    //gapflag = 0
    //endif
    #75188 quote
    Vonasi
    Moderator
    Master

    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 quote
    Vonasi
    Moderator
    Master

    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:

    defparam cumulateorders = false
    
    starttime = 000000
    startday = 1
    gapneeded = 20
     
    ma20 = average[20](close)
    ma200 = average[200](close)
    magap = ma20 - ma200
     
    if (opentime = starttime and opendayofweek = startday) or onmarket then
    crossflag = 0
    gapflag = 0
    endif
    
    if ma20 crosses over ma200 then
    crossflag = 1
    endif
     
    if crossflag and magap > gapneeded then
    gapflag = 1
    endif
     
    if ma20 crosses under ma200 then
    crossflag = 0
    gapflag = 0
    endif
     
    if not onmarket and crossflag and gapflag then
    buy at ma20 limit
    endif
    
    SET STOP pLOSS 25
    SET TARGET pPROFIT 25
    

    There probably is a neater solution but this works.

    #75191 quote
    Vonasi
    Moderator
    Master

    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 quote
    Bullets
    Participant
    Junior

    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.

    Example.png Example.png
    #75203 quote
    Vonasi
    Moderator
    Master

    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.

    [attachment file=75204]

    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.

    Screenshot_1-2.png Screenshot_1-2.png
    #75242 quote
    Bullets
    Participant
    Junior

    Yes I got it to work! 🙂 Thanks mate

    #75412 quote
    Bullets
    Participant
    Junior

    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.

    starttime = 000000
    startday = 1
    gapneeded = 20 * pipsize
    
    ma20 = average[20](close)
    ma50 = average[50](close)
    ma200 = average[200](close)
    magap = ma20 - ma200
    
    if (opentime = starttime and opendayofweek = startday) or onmarket then
    crossflag = 0
    gapflag = 0
    endif
    
    if ma20 crosses over ma200 then
    crossflag = 1
    endif
    
    if crossflag and magap > gapneeded and Dhigh(0) >= ma50 then
    gapflag = 1
    endif
    
    if ma20 crosses under ma200 then
    crossflag = 0
    gapflag = 0
    endif
    
    if not onmarket and crossflag and gapflag then
    buy at ma50 limit
    endif
    
    SET STOP pLOSS 25
    SET TARGET pPROFIT 25
    pic.png pic.png
    #75420 quote
    Vonasi
    Moderator
    Master

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

    [attachment file=75421]

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

    Screenshot_1-3.png Screenshot_1-3.png
Viewing 15 posts - 1 through 15 (of 32 total)
  • You must be logged in to reply to this topic.

first hit of a moving average


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Bullets @bullets Participant
Summary

This topic contains 31 replies,
has 3 voices, and was last updated by GraHal
7 years, 8 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 07/03/2018
Status: Active
Attachments: 9 files
Logo Logo
Loading...