Category Trend  Published on 02/05/2023

Moving Average Crossover, 3 Strategies

Description

Hello Traders,

 

This is a Moving Average Crossover robot, that uses 3 Strategies as follows:

Strategy 1: Basic Moving Average Crossover Utilizes two moving averages: a faster and a slower one. Rules: Long position: Enter when the faster MA crosses above the slower MA. Short position: Enter when the faster MA crosses below the slower MA. Exit the position either when the MA recrosses to the other direction, or based on a specific take profit and stop loss.

Strategy 2: Price Crossover with Moving Averages Here, we're not only using the Moving Averages but also the price. Utilizes two moving averages: a faster and a slower one. Long position: Enter when the current price crosses above both MAs, and exit when the price crosses below either MA. Short position: Enter when the current price crosses below both MAs, exit when the price crosses above either MA. Stop loss and trailing stop loss are set at suitable levels to manage risk.

Strategy 3: Moving Averages Trend Confirmation Utilizes two moving averages: a faster and a slower one. The direction of both moving averages is important! Long position: Enter when the faster MA turns up, crosses above the slower MA, and both MAs are moving up. Short position: Enter when the faster MA turns down, crosses below the slower MA, and both MAs are moving down. Stop loss and trailing stop loss are set at suitable levels to manage risk.

I've already backtested the cBot and published all the Trading Results on my Youtube Video, I would appreciate it if you give me a thumbs up and subscribe if you like the cBot. If you have any questions, please don't hesitate!

 

 


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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TradingStrategiesBot : Robot
    {
        [Parameter("Fast Moving Average Period", DefaultValue = 10, MinValue = 1)]
        public int FastMAPeriod { get; set; }
        
        [Parameter("Volume", DefaultValue = 1000)]
        public double vol { get; set; }

        [Parameter("Slow Moving Average Period", DefaultValue = 30, MinValue = 1)]
        public int SlowMAPeriod { get; set; }

        [Parameter("ATR Stoploss", DefaultValue = 100, MinValue = 1)]
        public int StopLoss { get; set; }

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

        [Parameter("Enable Trailing Stop Loss")]
        public bool EnableTrailingStopLoss { get; set; }

        [Parameter("Enable Take Profit")]
        public bool EnableTakeProfit { get; set; }

        [Parameter("Enable Stop Loss")]
        public bool EnableStopLoss { get; set; }

        [Parameter("Strategy Type")]
        public StrategyType Strategy { get; set; }
        


        private MovingAverage _fastMA;
        private MovingAverage _slowMA;
        private AverageTrueRange atr;

        protected override void OnStart()
        {
            _fastMA = Indicators.MovingAverage(Bars.ClosePrices, FastMAPeriod, MovingAverageType.Simple);
            _slowMA = Indicators.MovingAverage(Bars.ClosePrices, SlowMAPeriod, MovingAverageType.Simple);
            Chart.DrawStaticText("Copyright", "Owned by Singularity", VerticalAlignment.Top, HorizontalAlignment.Left, "Gray");
            atr = Indicators.AverageTrueRange(14, MovingAverageType.Exponential);
        }

        protected override void OnBar()
        {
            if (_fastMA.Result.HasCrossedAbove(_slowMA.Result, 1))
            {
                ExecuteStrategy(TradeType.Buy);
            }
            else if (_fastMA.Result.HasCrossedBelow(_slowMA.Result, 1))
            {
                ExecuteStrategy(TradeType.Sell);
            }
        }

        private void ExecuteStrategy(TradeType tradeType)
        {
            CloseAllPositions();

            double volume = vol;
            double atrPips = Math.Ceiling(atr.Result.Last(1) / Symbol.PipSize);


            if (tradeType == TradeType.Buy)
            {
                if (Strategy == StrategyType.Strategy1 || (Strategy == StrategyType.Strategy2 && Symbol.Ask > _fastMA.Result.LastValue && Symbol.Ask > _slowMA.Result.LastValue) || (Strategy == StrategyType.Strategy3 && _fastMA.Result.IsRising() && _slowMA.Result.IsRising()))
                {

                    PlaceOrder(tradeType, Symbol, volume, "Long", atrPips);
                }
            }
            else
            {
                if (Strategy == StrategyType.Strategy1 || (Strategy == StrategyType.Strategy2
                && Symbol.Bid < _fastMA.Result.LastValue && Symbol.Bid < _slowMA.Result.LastValue) || (Strategy == StrategyType.Strategy3
                && _fastMA.Result.IsFalling() && _slowMA.Result.IsFalling()))
                {

                    PlaceOrder(tradeType, Symbol, volume, "Short", atrPips);
                }
            }
        }
        private void PlaceOrder(TradeType tradeType, Symbol symbol, double volume, string label, double atrPips)
        {
            
            Print(atrPips);
            var stopLossPips = EnableStopLoss ? StopLoss*atrPips : 0;
            var takeProfitPips = EnableTakeProfit ? TakeProfit* atrPips : 0;

            var tradeResult = ExecuteMarketOrder(tradeType, symbol.Name, volume, label, stopLossPips, takeProfitPips);

            if (tradeResult.IsSuccessful && EnableTrailingStopLoss)
            {
                tradeResult.Position.ModifyTrailingStop(true);
            }
        }

        private void CloseAllPositions()
        {
            foreach (var position in Positions)
            {
                ClosePosition(position);
            }
        }

        public enum StrategyType
        {
            Strategy1 = 1,
            Strategy2 = 2,
            Strategy3 = 3
        }
    }
}

MH
mh.abualsoud@gmail.com

Joined on 18.07.2018

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Singularity Moving Average Crossover.algo
  • Rating: 5
  • Installs: 1545
  • Modified: 02/05/2023 06:19
Comments
Log in to add a comment.
UT
utsavsharma8910 · 8 months ago

your youtube channel name please?

 

DA
dajiba1021 · 1 year ago

I am often to blogging and i genuinely appreciate your site content. This article has really peaks my interest. I am about to bookmark your internet site and maintain checking for brand spanking new data.Slot777

RA
ralemo2114 · 1 year ago

That is the reason focus on you must specific groundwork well before authoring. Will be possible to more desirable blog post in this manner.selling credit card processing

AR
arhamabdul429 · 1 year ago

An impressive share, I with all this onto a colleague who has been performing a small analysis for this. Anf the husband actually bought me breakfast since I found it for him.. smile. So let me reword that: Thnx for your treat! But yeah Thnkx for spending time to talk about this, I feel strongly about this and love reading more about this topic. If at all possible, as you grow expertise, does one mind updating your blog post with increased details? It really is highly useful for me. Huge thumb up in this text!Packers and Movers Chennai