Category Trend  Published on 20/04/2020

Nielsen Scalper

Description

By Forexcove. The Nielsen Scalper trades the trend, making use of MA, RSI and ADX for trend strength. With a built-in Multiplication feature, it enables you to trade with our without Martingale, but always in the direction of potential trend. This version is for DEMO purposes only, and does not work on LIVE accounts.

For full version, and/or further information, visit us at https://www.forexcove.com/

Sample setting for volatile periods e.g. Jan 2020 until April 2020 can be downloaded here.

 


using System;
using cAlgo.API;
using cAlgo.API.Requests;
using cAlgo.Indicators;
using cAlgo.API.Indicators;
using System.Collections.Generic;
using System.Linq;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NielsMartingale_v5A : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 3000, MinValue = 0)]
        public int ParamVolume { get; set; }

        [Parameter("Max Losing Trades", DefaultValue = 12, MinValue = 0)]
        public int ParamMaxLosingTrades { get; set; }

        [Parameter("Stop Loss Pips", DefaultValue = 90)]
        public double ParamStopLossPips { get; set; }

        [Parameter("Take Profit Pips", DefaultValue = 90)]
        public double ParamTakeProfitPips { get; set; }

        [Parameter("Enable Martingale", DefaultValue = true)]
        public bool ParamEnableMartingale { get; set; }

        [Parameter("Multiplication Factor", DefaultValue = 2)]
        public double ParamMultiplicationFactor { get; set; }


        [Parameter("MA Periods", DefaultValue = 150)]
        public int ParamMAPeriods { get; set; }

        [Parameter("MA Type", DefaultValue = MovingAverageType.Exponential)]
        public MovingAverageType ParamMAType { get; set; }

        [Parameter("RSI Periods", DefaultValue = 14)]
        public int ParamRSIPeriods { get; set; }

        [Parameter("RSI Top Level", DefaultValue = 90)]
        public double ParamRSITopLevel { get; set; }

        [Parameter("RSI Bottom Level", DefaultValue = 10)]
        public double ParamRSIBottomLevel { get; set; }

        [Parameter("Enable ADX", DefaultValue = true)]
        public bool ParamEnableADX { get; set; }



        [Parameter("ADX Periods", DefaultValue = 14)]
        public int ParamADXPeriods { get; set; }

        [Parameter("ADX Level", DefaultValue = 20)]
        public double ParamADXLevel { get; set; }



        [Parameter("Adjust Initial Volume  (true/false)", DefaultValue = true)]
        public bool ParamAdjustInitialVolume { get; set; }

        [Parameter("Initial Volume Adjustment", DefaultValue = 40, Step = 1)]
        public double ParamVolumeAdjustment { get; set; }

        [Parameter("Enable Equity/Balance", DefaultValue = true)]
        public bool ParamEnableEquityBalanceClose { get; set; }

        [Parameter("Close equity X% > Balance", DefaultValue = 5, Step = 0.1)]
        public double ParamEquityBalanceRatio { get; set; }

        [Parameter("Close above Xk trades", DefaultValue = 2, Step = 1)]
        public int ParamCloseAboveXtrades { get; set; }


        //[Parameter("Whirlwind", DefaultValue = true)]
        public bool ParamEnableWhirlWind { get; set; }

        [Parameter("Stop above x-pips", DefaultValue = 90)]
        public double ParamStopAboveXPips { get; set; }

        [Parameter("Total Stop", DefaultValue = false)]
        public bool ParamEnableTotalStop { get; set; }

        [Parameter("Parcial stop", DefaultValue = false)]
        public bool ParamEnablePartialStop { get; set; }

        [Parameter("Timer", DefaultValue = false)]
        public bool ParamEnableTimer { get; set; }

        [Parameter("Timer Countdown Minutes", DefaultValue = 60)]
        public int ParamTimerCountdown { get; set; }

        /*List<double> TpList = new List<double>();
        List<double> SlList = new List<double>();*/
        bool StopFlag = false;

        private DirectionalMovementSystem adx;


        public string Label
        {
            get { return string.Format("Nielsenv5a-{0}-{1}-{2}-{3}-{4}-{5}", TimeFrame, ParamStopLossPips, ParamTakeProfitPips, ParamRSIPeriods, ParamRSIBottomLevel, ParamRSITopLevel); }
        }

        private MovingAverage ma;
        private RelativeStrengthIndex rsi;

        int losingTradesCount = 0;

        protected int GetInitialVolume(double balance, double adjustment)
        {
            if (ParamAdjustInitialVolume == true)
            {
                double volumeDouble = balance * adjustment / 100;
                long volumeLong = Symbol.NormalizeVolume(volumeDouble, RoundingMode.Down);
                volumeLong = Math.Max(volumeLong, Symbol.VolumeMin);
                return (int)volumeLong;
            }
            else
            {
                return ParamVolume;
            }
        }


        protected override void OnStart()
        {

            // parse TP & SL
                        /*foreach (var strVal in ParamTakeProfit.Split(new char[] 
            {
                ','
            }))
            {
                TpList.Add(double.Parse(strVal));
            }

            foreach (var strVal in ParamStopLoss.Split(new char[] 
            {
                ','
            }))
            {
                SlList.Add(double.Parse(strVal));
            }

            if (TpList.Count < ParamMaxLosingTrades)
            {
                throw new ArgumentException("TakeProfit values should be equal max losing trades");
            }

            if (SlList.Count < ParamMaxLosingTrades)
            {
                throw new ArgumentException("StopLoss values should be equal max losing trades");
            }*/

ParamEnableWhirlWind = true;
            Positions.Closed += OnPositionsClosed;
            Positions.Opened += OnPositionsOpened;
            ma = Indicators.MovingAverage(MarketSeries.Close, ParamMAPeriods, ParamMAType);
            rsi = Indicators.RelativeStrengthIndex(MarketSeries.Close, ParamRSIPeriods);
            adx = Indicators.DirectionalMovementSystem(ParamADXPeriods);


            if (!ParamEnableADX)
            {
                if (PositionsCount() == 0)
                    ExecuteOrder(ParamVolume, GetTradeType(), 1);
            }

        }

        void OnPositionsOpened(PositionOpenedEventArgs obj)
        {
            Position openedPosition = obj.Position;

            if (openedPosition.SymbolCode == Symbol.Code && openedPosition.Label == Label)
            {
                // set sl
                int index = int.Parse(openedPosition.Comment);
                //Print("Index " + index);
                //double slPips = SlList[index - 1];
                double slPips = ParamStopLossPips;
                double SL = openedPosition.TradeType == TradeType.Buy ? openedPosition.EntryPrice - (slPips * Symbol.PipSize) : openedPosition.EntryPrice + (slPips * Symbol.PipSize);



                //double tpPips = TpList[index - 1];
                double tpPips = ParamTakeProfitPips;
                double TP = openedPosition.TradeType == TradeType.Buy ? openedPosition.EntryPrice + (tpPips * Symbol.PipSize) : openedPosition.EntryPrice - (tpPips * Symbol.PipSize);

                ModifyPosition(openedPosition, SL, TP);
            }


        }

        protected override void OnTimer()
        {
            Timer.Stop();
            if (StopFlag)
            {
                StopFlag = false;
                if (!ParamEnableADX)
                {
                    if (PositionsCount() == 0)
                        ExecuteOrder(ParamVolume, GetTradeType(), 1);
                }
                else
                {

                }

            }

        }

        protected int PositionsCount()
        {
            return Positions.FindAll(Label, Symbol).Length;
        }

        void OnPositionsClosed(PositionClosedEventArgs obj)
        {
            if (StopFlag)
                return;
            if (ParamEnableADX)
                return;
            Position closedPosition = obj.Position;
            if (closedPosition.Label == Label && closedPosition.SymbolCode == Symbol.Code)
            {
                if (closedPosition.NetProfit > 0)
                {
                    losingTradesCount = 0;
                    int volume = GetInitialVolume(Account.Balance, ParamVolumeAdjustment);
                    ExecuteOrder(volume, GetTradeType(), 1);
                }
                else
                {
                    double currentBarHeightPips = (MarketSeries.High.LastValue - MarketSeries.Low.LastValue) / Symbol.PipSize;
                    if ((currentBarHeightPips > ParamStopAboveXPips) && ParamEnablePartialStop)
                    {



                        Print(currentBarHeightPips + " " + ParamStopAboveXPips);
                        StopFlag = true;
                        if (ParamEnableTimer)
                        {
                            Timer.Start(TimeSpan.FromMinutes(ParamTimerCountdown));
                        }


                    }
                    else
                    {
                        int volume = (int)closedPosition.Volume;

                        if (ParamEnableMartingale == true)
                        {
                            volume = (int)Symbol.NormalizeVolume(volume * ParamMultiplicationFactor, RoundingMode.ToNearest);
                        }
                        losingTradesCount = int.Parse(closedPosition.Comment);
                        int newIndex = losingTradesCount + 1;
                        if (ParamMaxLosingTrades > 0)
                        {
                            if (losingTradesCount >= ParamMaxLosingTrades)
                            {
                                volume = ParamVolume;
                                newIndex = 1;
                            }
                        }

                        TradeType newTradeType = closedPosition.TradeType;
                        if (ParamEnableWhirlWind)
                        {
                            newTradeType = GetTradeType();
                        }

                        ExecuteOrder(volume, newTradeType, newIndex);
                    }

                }

            }

        }

        private void ExecuteOrder(int volume, TradeType tradeType, int index)
        {
            TradeResult tradeResult = ExecuteMarketOrder(tradeType, Symbol, volume, Label, null, null, null, string.Format("{0}", index));


        }




        protected override void OnError(Error error)
        {
            if (error.Code == ErrorCode.BadVolume)
                Stop();
        }

        private TradeType GetTradeType()
        {
            if (rsi.Result.LastValue > ParamRSITopLevel)
            {
                return TradeType.Sell;
            }
            else if (rsi.Result.LastValue < ParamRSIBottomLevel)
            {
                return TradeType.Buy;
            }
            else if (Symbol.Bid > ma.Result.LastValue)
            {
                return TradeType.Buy;
            }
            else
            {
                return TradeType.Sell;
            }
        }

        public void TryAdxEntry()
        {
            int volume = GetInitialVolume(Account.Balance, ParamVolumeAdjustment);
            int index = 1;
            if (ParamEnableADX)
            {
                if (PositionsCount() == 0)
                {
                    // buy condition
                    if (Symbol.Bid > ma.Result.LastValue)
                    {

                        if (rsi.Result.LastValue < ParamRSIBottomLevel)
                        {
                            HistoricalTrade lastClosed = History.FindLast(Label, Symbol);
                            if (lastClosed != null)
                            {
                                if (lastClosed.NetProfit < 0 && lastClosed.TradeType == TradeType.Buy)
                                    return;
                                if (lastClosed.NetProfit < 0)
                                {
                                    if (ParamEnableMartingale == true)
                                    {
                                        volume = (int)Symbol.NormalizeVolume(lastClosed.Volume * ParamMultiplicationFactor, RoundingMode.ToNearest);
                                    }
                                    index = int.Parse(lastClosed.Comment) + 1;
                                }


                            }
                            ExecuteOrder(volume, TradeType.Buy, index);
                        }
                        else if (rsi.Result.LastValue < ParamRSITopLevel)
                        {
                            if (adx.ADX.LastValue > ParamADXLevel)
                            {

                                if (adx.ADX.Last(1) > adx.ADX.Last(2))
                                {

                                    if (MarketSeries.Close.Last(1) > MarketSeries.Open.Last(1))
                                    {

                                        HistoricalTrade lastClosed = History.FindLast(Label, Symbol);
                                        if (lastClosed != null)
                                        {
                                            if (lastClosed.NetProfit < 0 && lastClosed.TradeType == TradeType.Buy)
                                                return;
                                            if (lastClosed.NetProfit < 0)
                                            {
                                                if (ParamEnableMartingale == true)
                                                {
                                                    volume = (int)Symbol.NormalizeVolume(lastClosed.Volume * ParamMultiplicationFactor, RoundingMode.ToNearest);
                                                }
                                                index = int.Parse(lastClosed.Comment) + 1;
                                            }

                                        }
                                        ExecuteOrder(volume, TradeType.Buy, index);
                                    }
                                }
                            }
                        }



                    }
                    else if (Symbol.Bid < ma.Result.LastValue)
                    {
                        if (rsi.Result.LastValue > ParamRSITopLevel)
                        {
                            HistoricalTrade lastClosed = History.FindLast(Label, Symbol);
                            if (lastClosed != null)
                            {
                                if (lastClosed.NetProfit < 0 && lastClosed.TradeType == TradeType.Sell)
                                    return;
                                if (lastClosed.NetProfit < 0)
                                {

                                    if (ParamEnableMartingale == true)
                                    {
                                        volume = (int)Symbol.NormalizeVolume(lastClosed.Volume * ParamMultiplicationFactor, RoundingMode.ToNearest);
                                    }
                                    index = int.Parse(lastClosed.Comment) + 1;
                                }


                            }
                            ExecuteOrder(volume, TradeType.Sell, index);

                        }
                        else if (rsi.Result.LastValue > ParamRSIBottomLevel)
                        {

                            if (adx.ADX.LastValue > ParamADXLevel)
                            {

                                if (adx.ADX.Last(1) > adx.ADX.Last(2))
                                {

                                    if (MarketSeries.Close.Last(1) < MarketSeries.Open.Last(1))
                                    {
                                        HistoricalTrade lastClosed = History.FindLast(Label, Symbol);
                                        if (lastClosed != null)
                                        {
                                            if (lastClosed.NetProfit < 0 && lastClosed.TradeType == TradeType.Sell)
                                                return;
                                            if (lastClosed.NetProfit < 0)
                                            {
                                                if (ParamEnableMartingale == true)
                                                {
                                                    volume = (int)Symbol.NormalizeVolume(lastClosed.Volume * ParamMultiplicationFactor, RoundingMode.ToNearest);
                                                }
                                                index = int.Parse(lastClosed.Comment) + 1;
                                            }


                                        }
                                        ExecuteOrder(volume, TradeType.Sell, index);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }


        protected override void OnBar()
        {

            TryAdxEntry();
        }

        protected override void OnTick()
        {
            if (StopFlag)
            {
                return;
            }

            if (ParamEnableTotalStop)
            {
                double currentBarHeightPips = (MarketSeries.High.LastValue - MarketSeries.Low.LastValue) / Symbol.PipSize;
                if (currentBarHeightPips > ParamStopAboveXPips)
                {
                    Print(currentBarHeightPips + " " + ParamStopAboveXPips);
                    StopFlag = true;
                    foreach (var position in Positions)
                    {
                        if (position.Label == Label && position.SymbolCode == Symbol.Code)
                        {
                            ClosePosition(position);
                        }
                    }
                    if (ParamEnableTimer)
                    {
                        Timer.Start(TimeSpan.FromMinutes(ParamTimerCountdown));
                    }
                }

            }
            // observe equity balance
            if (ParamEnableEquityBalanceClose)
            {

                double equityToBalanceRatio = ((Account.Equity - Account.Balance) / Account.Balance) * 100;
                // Print(string.Format("{0} {1}",equityToBalanceRatio,ParamEquityBalanceRatio));
                if (equityToBalanceRatio >= ParamEquityBalanceRatio)
                {
                    foreach (var position in Positions)
                    {
                        if (position.Label == Label && position.SymbolCode == Symbol.Code)
                        {

                            if (position.Volume > (ParamCloseAboveXtrades * Symbol.VolumeMin))
                            {
                                //Print(position.Volume + " " + (ParamCloseAboveXtrades * Symbol.VolumeMin));
                                ClosePosition(position);

                            }
                        }
                    }
                }
            }

        }
    }
}


CT
ctid1731325

Joined on 10.01.2020 Blocked

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: NielsMartingale_v5A.algo
  • Rating: 5
  • Installs: 3758
Comments
Log in to add a comment.
TG
tgjobscv · 3 years ago

How cTrader demo to  live ctrader, mt4/mt5 Trade Copier ?