programming moving average & multiple entries

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #78082 quote
    Robert Equizi
    Participant
    New

    Hi, I am a first time user and trying to automate and back test a strategy:

    1. On the simple creation I have set up on the hour chart a buy and sell strategy. However, I only want to buy when the price is above 20 moving average on the weekly chart and sell when the price is below the 20 moving average on the weekly chart. Is this possible over different time frames, if not how is it done on the same time frame.
    2. After my initial entry, I would also like to enter the market every 50 points until it reaches my exit point; and then exit all positions. e.g. – If two moving averages haven’t crossed to give me my exit I want to keep adding to my position until they cross.

    Can I have some help coding this please?

    Thanks, Rob

    #78087 quote
    Vonasi
    Moderator
    Master

    Number 1 is easy but bear in mind that MTF is currently only under test in demo mode so you can’t trade something like this live just yet:

    timeframe(weekly, default)
    c1 = close > average[20](close)
    c2 = close < average[20](close)
    
    timeframe (default)
    if c1 then 
    buy 1 contract at market
    endif
    
    if c2 then 
    sell at market
    endif

    Run it on an hourly chart and this should just buy at the close of every hourly candle if it is above the weekly[20] average and sell if it is below. This weekly average is updated every hour. If you want to use the start of the week average at a fixed level then change the DEFAULT in line 1 to UPDATEONCLOSE.

    You will need to clarify number two a little more. Do you want to buy every time price moves 50 pips in your favour or 50 pips against you?

    #78090 quote
    Vonasi
    Moderator
    Master
    timeframe(weekly, default)
    c1 = close > average[20](close)
    c2 = close < average[20](close)
     
    timeframe (default)
    if not onmarket and c1 then 
    buy 1 contract at market
    buy 1 contract at close + 50 stop
    endif
    
    if onmarket then 
    buy 1 contract at tradeprice + 50 stop
    endif
     
    if c2 or (your other exit conditions) then 
    sell at market
    endif

    This might be the sort of thing you are looking for in number 2?

    #78142 quote
    Robert Equizi
    Participant
    New

    Hi Vonasi,

    Thanks for your help – the MTF coding isn’t doing what I was looking for.

    I already have 4 commands; c1 buy, c2 sell, c3 sellshort, c4 exitshort on the hourly chart. Picture two moving averages e.g. a 10 & 50, each time the 10 crosses over the 50, my strategy buys. Then when the 10 crosses under the 50 it closes the long and opens a short and so on. I want the buy to happen only when the price is trading above a weekly 20 moving average – this limits my exposure in the market and therefore having less entries.

    I want it to only execute c1-c2 when the weekly moving average is above 20, and only execute c3-c4 when the weekly moving average is below 20? Can this be done…?

    With my number 2 question – I want to buy every time it moves 50 pips against me (haven’t tried this code yet as I need to get weekly moving average working first).

    Many thanks,

    Rob

    #78149 quote
    Vonasi
    Moderator
    Master
    timeframe(weekly, default)
    c1 = close > average[20](close)
    c2 = close < average[20](close)
     
    timeframe (default)
    c3 = average[10](close) crosses over average[50](close)
    c4 = average[10](close) crosses under average[50](close)
    
    if c1 and c3 then 
    exitshort at market
    buy 1 contract at market
    buy 1 contract at close - 50 limit
    endif
     
    if longonmarket then 
    buy 1 contract at tradeprice - 50 limit
    endif
    
    if c2 and c4 then 
    sell at market
    sellshort 1 contract at market
    sellshort 1 contract at close + 50 limit
    endif
     
    if shortonmarket then 
    sellshort 1 contract at tradeprice + 50 limit
    endif
     
    

    Not tested and not sure it is what you want as your strategy description is a little difficult to interpret.

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.

programming moving average & multiple entries


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 4 replies,
has 2 voices, and was last updated by Vonasi
7 years, 6 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 08/13/2018
Status: Active
Attachments: No files
Logo Logo
Loading...