User login

  

Moving Averages

Simple daily moving averages of 3,13 and 39 can keep you in and out of markets fairly efficiently and profitably, (in any time frame actually).

Some basic principles to understand are:

- The market moves in long (secular) trends.

- Intermediate trends can last for months to years. 

- Short term trends can last for days to weeks. 

- Trade intermediate trends in either direction. 

- Trade short term trends only in the direction of the intermediate trend.

Proxies:

3 Day MA - a proxy for price 

13 Day MA - a proxy for the short term trend (a moving trend line) 

39 Day MA - a proxy for the intermediate trend (a moving trend line).

The Basics of MAs

MAs lag market reversals at tops and bottoms, the larger the MA the longer the lag period, the shorter the MA the shorter the lag but the more frequent the whipsaws. MAs work well when markets trend but get frequently whipsawed when they are in a range. 

Therefore, trade trends with the MAs but do not trade ranges using MAs. Just stand aside and be patient until a new trend emerges.

The intermediate trend is in the direction of the 39 MA which acts like a moving trend line. If the 39 MA is pointing up then the intermediate trend is up, if down the trend is down. If the 39 MA is horizontal the market is in a range, from which a trend will, sooner or later, emerge. 

Simple Trading Rules 

1. When the 39 MA is moving up buy when the 3 MA crosses up over the 13 MA. and/or when the 3 MA crosses above the 39 MA.. When the 13 MA crosses above the 39 MA consider adding to your long position. Exit and stand aside when the 3 crosses back below the 13 MA.. 

2. When the 39 MA is moving down sell short when the 3 MA crosses below the 13 MA. and/or when the 3 MA crosses below the 39 MA.. When the 13 MA crosses below the 39 MA consider adding to your short position. Exit and stand aside when the 3 MA crosses back up over the 13 MA. 

3. Only initiate trades in the opposite direction of the intermediate trend when the 3 MA crosses above or below the 39 MA, preferably after the 39 MA has already changed direction. 

4. This 3:13 MA crossover will keep you trading in the trend with only a small lag and on the sidelines during corrections. The lag only becomes more substantial at reversals of the intermediate trend (a 3:39 crossover), a small price to pay at these uncertain times of trend transition.

You can set your technical analysis sofware to show bar charts with these 3X13x39 simple MAs. This trading system will help you select the best traders while avoiding the less profitable trades in choppy markets.

 

Display/hide source code
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using PTLRuntime.NETScript;
 
namespace MovingAverage
{
    /// <summary>
    /// MovingAverage
    /// Moving Averages .NET
    /// </summary>
    public class MovingAverage : NETStrategy
    {
        [InputParameter("Lots Count", 0, 0.1, 50, 1, 0.1)]
        public double Lots = 0.1;
        [InputParameter("Maximum Risk", 1, 0.01, 10, 2, 0.01)]
        public double MaximumRisk = 0.02;
        [InputParameter("Decrease Factor", 2, 1, 100)]
        public double DecreaseFactor = 3;
        [InputParameter("Moving Average Period", 3, 1, 100)]
        public int MovingPeriod = 12;
        [InputParameter("Moving Average Shift", 4, 1, 100)]
        public int MovingShift = 6;
 
        private const int MAGICMA = 20050610;
 
        public MovingAverage()
            : base()
        {
            #region // Initialization
            base.Author = "nicky";
            base.Comments = "Moving Averages .NET";
            base.Company = "";
            base.Copyrights = "";
            base.DateOfCreation = "19.01.2011";
            base.ExpirationDate = 0;
            base.Version = "1.0";
            base.Password = "66b4a6416f59370e942d353f08a9ae36";
            base.ProjectName = "MovingAverage";
            #endregion
 
 
        }
 
 
        /// <summary>
        /// Calculate open positions
        /// </summary>
        private int CalculateCurrentOrders(string symbol)
        {
            int buys = 0, sells = 0;
            //----
            for (int i = 0; i < mql4.OrdersTotal(); i++)
            {
                if (mql4.OrderSelect(i, mql4.SELECT_BY_POS, mql4.MODE_TRADES) == false) break;
                if (mql4.OrderSymbol() == mql4.Symbol() && platform.OrderMagicNumber() == MAGICMA)
                {
                    if (mql4.OrderType() == mql4.OP_BUY) buys++;
                    if (mql4.OrderType() == mql4.OP_SELL) sells++;
                }
            }
            //---- return orders volume
            if (buys > 0) return (buys);
            else return (-sells);
        }
 
 
        /// <summary>
        ///  Calculate optimal lot size
        /// </summary>
        private double LotsOptimized()
        {
            double lot = Lots;
            int orders = mql4.HistoryTotal();     // history orders total
            int losses = 0;                  // number of losses orders without a break
            //---- select lot size
            lot = mql4.NormalizeDouble(platform.AccountFreeMargin() * MaximumRisk / 1000.0, 1);
            //---- calcuulate number of losses orders without a break
            if (DecreaseFactor > 0)
            {
                for (int i = orders - 1; i >= 0; i--)
                {
                    if (mql4.OrderSelect(i, mql4.SELECT_BY_POS, mql4.MODE_HISTORY) == false) { platform.Print("Error in history!"); break; }
                    if (mql4.OrderSymbol() != mql4.Symbol() || mql4.OrderType() > mql4.OP_SELL) continue;
                    //----
                    if (platform.OrderProfit() > 0) break;
                    if (platform.OrderProfit() < 0) losses++;
                }
                if (losses > 1) lot = mql4.NormalizeDouble(lot - lot * losses / DecreaseFactor, 1);
            }
            //---- return lot size
            if (lot < 0.1) lot = 0.1;
            return (lot);
        }
 
 
        /// <summary>
        ///  Check for open order conditions
        /// </summary>
        void CheckForOpen()
        {
            double ma;
            int res;
            //---- go trading only for first tiks of new bar
            if (mql4.Volume[0] > 1) return;
            //---- get Moving Average 
            ma = mql4.iMA("", 0, MovingPeriod, MovingShift, mql4.MODE_SMA, mql4.PRICE_CLOSE, 0);
            //---- sell conditions
            if (mql4.Open[1] > ma && mql4.Close[1] < ma)
            {
                res = mql4.OrderSend(mql4.Symbol(), mql4.OP_SELL, 10, mql4.Bid, 3, 0, 0, "", MAGICMA, 0, mql4.Red);
                return;
            }
            //---- buy conditions
            if (mql4.Open[1] < ma && mql4.Close[1] > ma)
            {
                res = mql4.OrderSend(mql4.Symbol(), mql4.OP_BUY, 10, mql4.Ask, 3, 0, 0, "", MAGICMA, 0, mql4.Blue);
                return;
            }
        }
        /// <summary>
        ///  Check for close order conditions
        /// </summary>
        void CheckForClose()
        {
            double ma;
            //---- go trading only for first tiks of new bar
            if (mql4.Volume[0] > 1) return;
            //---- get Moving Average 
            ma = mql4.iMA("", 0, MovingPeriod, MovingShift, mql4.MODE_SMA, mql4.PRICE_CLOSE, 0);
            //----
            for (int i = 0; i < mql4.OrdersTotal(); i++)
            {
                if (mql4.OrderSelect(i, mql4.SELECT_BY_POS, mql4.MODE_TRADES) == false) break;
                if (platform.OrderMagicNumber() != MAGICMA || mql4.OrderSymbol() != mql4.Symbol()) continue;
                //---- check order type 
                if (mql4.OrderType() == mql4.OP_BUY)
                {
                    if (mql4.Open[1] > ma && mql4.Close[1] < ma) mql4.OrderClose(platform.OrderTicket(), platform.OrderLots(), mql4.Bid, 3, mql4.White);
                    break;
                }
                if (mql4.OrderType() == mql4.OP_SELL)
                {
                    if (mql4.Open[1] < ma && mql4.Close[1] > ma) mql4.OrderClose(platform.OrderTicket(), platform.OrderLots(), mql4.Ask, 3, mql4.White);
                    break;
                }
            }
            //----
        }
 
 
 
 
        /// <summary>
        /// This function will be called after creating
        /// </summary>
        public override void Init()
        {
 
        }
 
        /// <summary>
        /// Entry point. This function is called when new quote comes 
        /// </summary>
        public override void OnQuote()
        {
            //---- check for history and trading
            if (mql4.Bars < 100 || platform.IsTradeAllowed() == false) return;
            //---- calculate open orders by current symbol
            if (CalculateCurrentOrders(mql4.Symbol()) == 0) CheckForOpen();
            else CheckForClose();
            //----
        }
 
        /// <summary>
        /// This function will be called before removing
        /// </summary>
        public override void Complete()
        {
 
        }
    }
}
12345

Comments