Problem with moving average bias

Created at 07 Jul 2021, 20:46
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
WM

wmclennan77

Joined 29.06.2021

Problem with moving average bias
07 Jul 2021, 20:46


Hi all, I am having a problem with an exponential moving average filter, where two 50-period EMA's, one applied to high prices and one to low prices, set the buy and sell direction.

For the most part, the filter works as intended. However, if the price is lower than the high EMA, buys cannot take place and vice versa (if the price is higher than the low EMA, no selling).

On backtesting, positions seem to still stay open even when going past the set limits.

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace MA_Bias
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class cAlgo : Robot
    {
        private double _volumeInUnits;

        [Parameter("Label", DefaultValue = "MA Bias")]
        public string Label { get; set; }

        [Parameter("Starting Volume (Lots)", DefaultValue = 1.0, MinValue = 0.01)]
        public double StartingVolumeInLots { get; set; }

        [Parameter(DefaultValue = 10, MinValue = 1)]
        public int StopLoss { get; set; }

        [Parameter(DefaultValue = 15, MinValue = 1)]
        public int TakeProfit { get; set; }

        //public static TimeFrame Minute15 { get; set; }

        private ExponentialMovingAverage _MA_50_Buy;
        private ExponentialMovingAverage _MA_50_Sell;
        private bool MA_BiasBuy;
        private bool MA_BiasSell;

        protected override void OnStart()
        {
            // Declare necessary vars
            _volumeInUnits = Symbol.QuantityToVolumeInUnits(StartingVolumeInLots);
            MA_BiasBuy = false;
            MA_BiasSell = false;
            _MA_50_Buy = Indicators.ExponentialMovingAverage(Bars.HighPrices, 50);
            _MA_50_Sell = Indicators.ExponentialMovingAverage(Bars.LowPrices, 50);
        }

        protected override void OnBar()
        {
            var ActiveBuy = Positions.Find(Label, SymbolName, TradeType.Buy);
            var ActiveSell = Positions.Find(Label, SymbolName, TradeType.Sell);

            // MA Bias
            if (Symbol.Ask > _MA_50_Buy.Result.LastValue)
            {
                MA_BiasBuy = true;
                MA_BiasSell = false;
            }

            else if (Symbol.Bid < _MA_50_Sell.Result.LastValue)
            {
                MA_BiasSell = true;
                MA_BiasBuy = false;
            }

            if (MA_BiasBuy == true)
            {
                if (ActiveSell != null)
                {
                    ClosePosition(ActiveSell);
                }
            }

            if (MA_BiasSell == true)
            {
                if (ActiveBuy != null)
                {
                    ClosePosition(ActiveBuy);
                }
            }

            //Open Buy
            if (MA_BiasBuy == true && ActiveBuy == null)
            {
                if (ActiveSell != null)
                {
                    ClosePosition(ActiveSell);
                }
                ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, StopLoss, TakeProfit);
            }

            //Open Sell
            if (MA_BiasSell == true && ActiveSell == null)
            {
                if (ActiveBuy != null)
                {
                    ClosePosition(ActiveBuy);
                }
                ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, Label, StopLoss, TakeProfit);
            }
        }

    }
}

 


@wmclennan77
Replies

PanagiotisCharalampous
08 Jul 2021, 09:31

Hi wmclennan77,

For the most part, the filter works as intended.

Can you clearly explain the intention?

Best Regards,

Panagiotis 

Join us on Telegram and Facebook 


@PanagiotisCharalampous

wmclennan77
08 Jul 2021, 10:00

RE:

PanagiotisCharalampous said:

Hi wmclennan77,

For the most part, the filter works as intended.

Can you clearly explain the intention?

Best Regards,

Panagiotis 

Join us on Telegram and Facebook 

Hi Panagiotis,

So the intention of this indicator is to dictate in which direction positions are allowed to open.

If the price is higher than the high EMA, then buys are allowed, sells are not.

The opposite is true with selling, where if the price is lower than the low EMA, then sells are allowed and buys are not.

 

As mentioned, the buying and selling bias works fine.

 

An example: if the price was above the high EMA, a buy would be opened. If the price dips down to below the high EMA, it needs to close that buy and either wait for the price to go above high EMA again for a buy or go below low EMA for a sell. There are no trades allowed between high and low EMAs.

 

The problem is positions stay open when crossing their respective EMA. Like the example buy will stay open and only close on the cross of the low EMA.


@wmclennan77