Category Trend  Published on 02/06/2021

ForexCove Swiss Army Knife

Description

Try the engine behind one of our best-selling algos. 

The Swiss Army Knife Forex Robot is a beast. We’ve been able to pack an impressive number of indicators, functionalities and modes of operation into this bot, making it an incredibly sophisticated and inspiring algorithm to use.

You can do simple trend trading with multiple moving averages, work with up to four timeframes for confirmation, it gives your volatility and candle filters, an RSI indicator, you can adjust trade volume dynamically, and you can enable Martingale if that’s your thing.

You can even set the individual trailing stop in a Martingale sequence – something never seen before.

We set out to build a complete, innovative and all-inspiring Forex trading robot, and I believe we have managed to do so. Visit us at ForexCove.

DOWNLOAD SAMPLE CONFIG FILE HERE

DOWNLOAD TRADE HISTORY HERE.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Visit us at ForexCove.

 


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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NielsGuppy_V3_3 : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 25000, MinValue = 0)]
        public int ParamVolume { 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("TPM TPT (true/false)", DefaultValue = true)]
        public bool ParamEnableTPMTPT { get; set; }

        [Parameter("TPM Pips", DefaultValue = 3)]
        public double ParamTPModificationPips { get; set; }

        [Parameter("TPT", DefaultValue = 1)]
        public int ParamTPModificationActiveFrom { get; set; }

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


        [Parameter("Trailing SL 01", DefaultValue = true)]
        public bool ParamEnableTrailingSL01 { get; set; }

        [Parameter("Trailing Stop trigger 01", DefaultValue = 10, MinValue = 0)]
        public double ParamFirstTradeTrailingStopPips01 { get; set; }

        [Parameter("Trailing Step pips 01", DefaultValue = 5, MinValue = 0)]
        public double ParamFirstTradeTrailingStepPips01 { get; set; }

        [Parameter("Trailing SL 02", DefaultValue = true)]
        public bool ParamEnableTrailingSL02 { get; set; }

        [Parameter("Trailing Stop trigger 02", DefaultValue = 10, MinValue = 0)]
        public double ParamFirstTradeTrailingStopPips02 { get; set; }

        [Parameter("Trailing Step pips 02", DefaultValue = 5, MinValue = 0)]
        public double ParamFirstTradeTrailingStepPips02 { get; set; }

        [Parameter("Trailing SL 03", DefaultValue = true)]
        public bool ParamEnableTrailingSL03 { get; set; }

        [Parameter("Trailing Stop trigger 03", DefaultValue = 10, MinValue = 0)]
        public double ParamFirstTradeTrailingStopPips03 { get; set; }

        [Parameter("Trailing Step pips 03", DefaultValue = 5, MinValue = 0)]
        public double ParamFirstTradeTrailingStepPips03 { get; set; }

        [Parameter("Trailing SL 04", DefaultValue = true)]
        public bool ParamEnableTrailingSL04 { get; set; }

        [Parameter("Trailing Stop trigger 04", DefaultValue = 10, MinValue = 0)]
        public double ParamFirstTradeTrailingStopPips04 { get; set; }

        [Parameter("Trailing Step pips 04", DefaultValue = 5, MinValue = 0)]
        public double ParamFirstTradeTrailingStepPips04 { get; set; }

        [Parameter("Trailing SL 05", DefaultValue = true)]
        public bool ParamEnableTrailingSL05 { get; set; }

        [Parameter("Trailing Stop trigger 05", DefaultValue = 10, MinValue = 0)]
        public double ParamFirstTradeTrailingStopPips05 { get; set; }

        [Parameter("Trailing Step pips 05", DefaultValue = 5, MinValue = 0)]
        public double ParamFirstTradeTrailingStepPips05 { get; set; }

        [Parameter("Trailing SL 06", DefaultValue = true)]
        public bool ParamEnableTrailingSL06 { get; set; }

        [Parameter("Trailing Stop trigger 06", DefaultValue = 10, MinValue = 0)]
        public double ParamFirstTradeTrailingStopPips06 { get; set; }

        [Parameter("Trailing Step pips 06", DefaultValue = 5, MinValue = 0)]
        public double ParamFirstTradeTrailingStepPips06 { get; set; }

        [Parameter("Multiple Trades (true/false)", DefaultValue = true)]
        public bool ParamEnableMultipleTrades { get; set; }

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

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

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

        [Parameter("MA 01 type", DefaultValue = MovingAverageType.Exponential)]
        public MovingAverageType ParamMA1Type { get; set; }

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

        [Parameter("MA 02 type", DefaultValue = MovingAverageType.Exponential)]
        public MovingAverageType ParamMA2Type { get; set; }



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

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

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

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

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

        [Parameter("RSI Low", DefaultValue = 10)]
        public double ParamRSIBottomLevel { 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("Volatility Filter (true/false)", DefaultValue = true)]
        public bool ParamEnableVolatilityFilter { get; set; }

        [Parameter("Volatility Filter Pips", DefaultValue = 20)]
        public double ParamVolatilityFilterPips { get; set; }

        [Parameter("Volatility Filter Lookback", DefaultValue = 1)]
        public int ParamVolatilityFilterLookback { get; set; }

        [Parameter("S/P Filter (true/false)", DefaultValue = true)]
        public bool ParamEnableSupportResistanceFilter { get; set; }

        [Parameter("S/P Filter X", DefaultValue = 3)]
        public int ParamSupportResistanceFilterX { get; set; }

        [Parameter("Strict Alignment", DefaultValue = true)]
        public bool ParamEnableStrictAlignment { get; set; }

        [Parameter("Enable Close Equity%", DefaultValue = true)]
        public bool ParamEnableCloseEquityPercent { get; set; }

        [Parameter("Close Equity %", DefaultValue = 105, MinValue = 100)]
        public double ParamCloseEquityPercent { get; set; }

        [Parameter("Enable Min Candle Size", DefaultValue = true)]
        public bool ParamEnableMinCndleSize { get; set; }

        [Parameter("Min Candle Size", DefaultValue = 5)]
        public double ParamMinCandleSize { get; set; }

        [Parameter("MA Close Feature", DefaultValue = true)]
        public bool ParamEnableMACloseFeature { get; set; }

        [Parameter("MA Close Feature", DefaultValue = 150)]
        public int ParamMAClosePeriods { get; set; }

        [Parameter("MA Close type", DefaultValue = MovingAverageType.Exponential)]
        public MovingAverageType ParamMACloseType { get; set; }

        [Parameter("Enable Move to Breakeven", DefaultValue = true)]
        public bool ParamEnableBreakeven { get; set; }

        [Parameter("Breakeven Trigger Pips", DefaultValue = 5)]
        public double ParamBreakevenTriggerPips { get; set; }

        [Parameter("== LT01 ==")]
        public string ParamLabel01 { get; set; }

        [Parameter("LT01 Filter (true/false)", DefaultValue = true)]
        public bool ParamEnableLT01Filter { get; set; }

        [Parameter("LT01 Timeframe")]
        public TimeFrame ParamLT01Timeframe { get; set; }

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

        [Parameter("LT01 MA type", DefaultValue = MovingAverageType.Exponential)]
        public MovingAverageType ParamLT01MAType { get; set; }

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

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

        [Parameter("== LT02 ==")]
        public string ParamLabel02 { get; set; }

        [Parameter("LT02 Filter (true/false)", DefaultValue = true)]
        public bool ParamEnableLT02Filter { get; set; }

        [Parameter("LT02 Timeframe")]
        public TimeFrame ParamLT02Timeframe { get; set; }

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

        [Parameter("LT02 MA type", DefaultValue = MovingAverageType.Exponential)]
        public MovingAverageType ParamLT02MAType { get; set; }

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

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

        [Parameter("== LT03 ==")]
        public string ParamLabel03 { get; set; }

        [Parameter("LT03 Filter (true/false)", DefaultValue = true)]
        public bool ParamEnableLT03Filter { get; set; }

        [Parameter("LT03 Timeframe")]
        public TimeFrame ParamLT03Timeframe { get; set; }

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

        [Parameter("LT03 MA type", DefaultValue = MovingAverageType.Exponential)]
        public MovingAverageType ParamLT03MAType { get; set; }

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

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

        [Parameter("== LT04 ==")]
        public string ParamLabel04 { get; set; }

        [Parameter("LT04 Filter (true/false)", DefaultValue = true)]
        public bool ParamEnableLT04Filter { get; set; }

        [Parameter("LT04 Timeframe")]
        public TimeFrame ParamLT04Timeframe { get; set; }

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

        [Parameter("LT04 MA type", DefaultValue = MovingAverageType.Exponential)]
        public MovingAverageType ParamLT04MAType { get; set; }

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

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


        private MovingAverage ma1, ma2, maCloseFeature;
        private MovingAverage lt01Ma, lt02Ma, lt03Ma, lt04Ma;
        private DirectionalMovementSystem adx;
        private DirectionalMovementSystem lt01Adx, lt02Adx, lt03Adx, lt04Adx;

        private RelativeStrengthIndex rsi;

        public string Label
        {
            get { return string.Format("NielsenGuppy-{0}-{1}-{2}-{3}-{4}-{5}", TimeFrame, ParamStopLossPips, ParamVolume, ParamADXPeriods, ParamADXLevel, ParamMA1Periods); }
        }

        protected override void OnStart()
        {
            ma1 = Indicators.MovingAverage(MarketSeries.Close, ParamMA1Periods, ParamMA1Type);
            ma2 = Indicators.MovingAverage(MarketSeries.Close, ParamMA2Periods, ParamMA2Type);

            adx = Indicators.DirectionalMovementSystem(ParamADXPeriods);

            if (ParamEnableLT01Filter)
            {
                lt01Ma = Indicators.MovingAverage(MarketData.GetSeries(ParamLT01Timeframe).Close, ParamLT01MAPeriods, ParamLT01MAType);
                lt01Adx = Indicators.DirectionalMovementSystem(ParamLT01ADXPeriods);
            }

            if (ParamEnableLT02Filter)
            {
                lt02Ma = Indicators.MovingAverage(MarketData.GetSeries(ParamLT02Timeframe).Close, ParamLT02MAPeriods, ParamLT02MAType);
                lt02Adx = Indicators.DirectionalMovementSystem(ParamLT02ADXPeriods);
            }

            if (ParamEnableLT03Filter)
            {
                lt03Ma = Indicators.MovingAverage(MarketData.GetSeries(ParamLT03Timeframe).Close, ParamLT03MAPeriods, ParamLT03MAType);
                lt03Adx = Indicators.DirectionalMovementSystem(ParamLT03ADXPeriods);
            }

            if (ParamEnableLT04Filter)
            {
                lt04Ma = Indicators.MovingAverage(MarketData.GetSeries(ParamLT04Timeframe).Close, ParamLT04MAPeriods, ParamLT04MAType);
                lt04Adx = Indicators.DirectionalMovementSystem(ParamLT04ADXPeriods);
            }

            if (ParamEnableRSI)
            {
                rsi = Indicators.RelativeStrengthIndex(MarketSeries.Close, ParamRSIPeriods);
            }

            if (ParamEnableMACloseFeature)
            {
                maCloseFeature = Indicators.MovingAverage(MarketSeries.Close, ParamMAClosePeriods, ParamMACloseType);
            }

            Positions.Opened += OnPositionsOpened;

            if (ParamEnableTrailingSL01 || ParamEnableTrailingSL02 || ParamEnableTrailingSL03 || ParamEnableTrailingSL04 || ParamEnableTrailingSL05 || ParamEnableTrailingSL06)
            {
                Timer.Start(1);
            }
        }

        protected override void OnTimer()
        {
            foreach (var position in Positions.FindAll(Label, Symbol))
            {
                if (ParamEnableMartingale)
                {
                    int index = parseMartingaleIndex(position.Comment);
                    if (index <= 6)
                    {
                        TrailPositionPips(position);
                    }
                }
                else
                {
                    TrailPositionPips(position);
                }

                if (ParamEnableBreakeven)
                {
                    BreakevenPositions();
                }
            }
        }

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

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

            if (openedPosition.SymbolCode == Symbol.Code && openedPosition.Label == Label)
            {
                // set sl


                double slPips = ParamStopLossPips;
                double SL = openedPosition.TradeType == TradeType.Buy ? openedPosition.EntryPrice - (slPips * Symbol.PipSize) : openedPosition.EntryPrice + (slPips * Symbol.PipSize);


                double tpPips = ParamTakeProfitPips;
                HistoricalTrade lastClosed = History.FindLast(Label, Symbol);
                if (lastClosed != null)
                {
                    int tpIndex = parseTpIndex(lastClosed.Comment);

                    if (lastClosed.NetProfit < 0)
                    {
                        if (ParamEnableTPMTPT)
                        {
                            if (tpIndex >= ParamTPModificationActiveFrom)
                            {
                                tpPips = ParamTakeProfitPips - (tpIndex * ParamTPModificationPips);
                            }
                        }
                    }
                }

                double TP = openedPosition.TradeType == TradeType.Buy ? openedPosition.EntryPrice + (tpPips * Symbol.PipSize) : openedPosition.EntryPrice - (tpPips * Symbol.PipSize);

                ModifyPosition(openedPosition, SL, TP);
            }


        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }

        protected void BreakevenPositions()
        {
            foreach (var pos in Positions.FindAll(Label, Symbol))
            {
                if (pos.Pips > ParamBreakevenTriggerPips)
                {
                    double? newPosSL = pos.EntryPrice;
                    if (pos.StopLoss.HasValue)
                    {
                        if (pos.TradeType == TradeType.Buy)
                        {
                            if (pos.StopLoss.Value > newPosSL)
                            {
                                newPosSL = null;
                            }
                        }
                        else
                        {
                            if (pos.StopLoss.Value < newPosSL)
                            {
                                newPosSL = null;
                            }
                        }
                    }
                    if (newPosSL.HasValue)
                    {
                        ModifyPosition(pos, newPosSL, pos.TakeProfit);
                    }
                }
            }
        }

        protected void TrailPositionPips(Position Position)
        {
            double distance = -1;
            double ParamFirstTradeTrailingStopPips = 0;
            double ParamFirstTradeTrailingStepPips = 0;
            if (ParamEnableMartingale)
            {
                int index = parseMartingaleIndex(Position.Comment);
                if (index == 1)
                {
                    ParamFirstTradeTrailingStopPips = ParamFirstTradeTrailingStopPips01;
                    ParamFirstTradeTrailingStepPips = ParamFirstTradeTrailingStepPips01;
                }
                else if (index == 2)
                {
                    ParamFirstTradeTrailingStopPips = ParamFirstTradeTrailingStopPips02;
                    ParamFirstTradeTrailingStepPips = ParamFirstTradeTrailingStepPips02;

                }
                else if (index == 3)
                {
                    ParamFirstTradeTrailingStopPips = ParamFirstTradeTrailingStopPips03;
                    ParamFirstTradeTrailingStepPips = ParamFirstTradeTrailingStepPips03;

                }
                else if (index == 4)
                {
                    ParamFirstTradeTrailingStopPips = ParamFirstTradeTrailingStopPips04;
                    ParamFirstTradeTrailingStepPips = ParamFirstTradeTrailingStepPips04;

                }
                else if (index == 5)
                {
                    ParamFirstTradeTrailingStopPips = ParamFirstTradeTrailingStopPips05;
                    ParamFirstTradeTrailingStepPips = ParamFirstTradeTrailingStepPips05;
                }
                else if (index == 6)
                {
                    ParamFirstTradeTrailingStopPips = ParamFirstTradeTrailingStopPips06;
                    ParamFirstTradeTrailingStepPips = ParamFirstTradeTrailingStepPips06;
                }


            }
            else
            {
                ParamFirstTradeTrailingStopPips = ParamFirstTradeTrailingStopPips01;
                ParamFirstTradeTrailingStepPips = ParamFirstTradeTrailingStepPips01;
            }

            if (Position.TradeType == TradeType.Buy)
            {

                distance = Math.Floor((Symbol.Bid - Position.EntryPrice) / Symbol.PipSize);


            }
            else
            {

                distance = Math.Floor((Position.EntryPrice - Symbol.Ask) / Symbol.PipSize);

            }

            if (distance > ParamFirstTradeTrailingStopPips)
            {
                double newSL = 0;
                if (Position.TradeType == TradeType.Buy)
                {
                    newSL = Symbol.Bid - (ParamFirstTradeTrailingStepPips * Symbol.PipSize);

                }
                else
                {
                    newSL = Symbol.Ask + (ParamFirstTradeTrailingStepPips * Symbol.PipSize);
                }

                bool askBidCondition = false;
                if (Position.TradeType == TradeType.Buy)
                {

                    askBidCondition = newSL < Symbol.Bid;
                }
                else
                {

                    askBidCondition = newSL > Symbol.Ask;
                }

                if (!Position.StopLoss.HasValue && askBidCondition)
                {
                    ModifyPosition(Position, newSL, Position.TakeProfit);
                }
                // Position.StopLoss.HasValue
                else
                {
                    double newOldDiff = 0;

                    if (Position.TradeType == TradeType.Buy)
                    {
                        newOldDiff = (newSL - Position.StopLoss.Value);

                    }
                    else
                    {
                        newOldDiff = (Position.StopLoss.Value - newSL);

                    }

                    if (newOldDiff > Symbol.TickSize && askBidCondition)
                    {
                        ModifyPosition(Position, newSL, Position.TakeProfit);
                    }


                }
            }
        }

        protected bool VolatilityFilter()
        {
            if (ParamEnableVolatilityFilter)
            {
                for (int i = 1; i <= ParamVolatilityFilterLookback; i++)
                {
                    if ((Math.Abs(MarketSeries.Close.Last(i) - MarketSeries.Open.Last(i)) / Symbol.PipSize) >= ParamVolatilityFilterPips)
                    {
                        return false;
                    }
                }

            }
            return true;
        }

        protected bool SRFilter(TradeType tradeType)
        {
            if (ParamEnableSupportResistanceFilter)
            {
                double lastClose = MarketSeries.Close.Last(1);
                for (var i = 2; i < 2 + ParamSupportResistanceFilterX; i++)
                {
                    if (tradeType == TradeType.Sell)
                    {
                        bool closedNegative = MarketSeries.Close.Last(i) < MarketSeries.Open.Last(i);
                        if (closedNegative)
                        {
                            if (MarketSeries.Close.Last(i) < lastClose)
                            {
                                return false;
                            }
                        }
                    }
                    else
                    {
                        // buy
                        bool closedPositive = MarketSeries.Close.Last(i) > MarketSeries.Open.Last(i);
                        {
                            if (closedPositive)
                            {
                                if (MarketSeries.Close.Last(i) > lastClose)
                                {
                                    return false;
                                }
                            }
                        }
                    }

                }
            }

            return true;
        }

        protected bool LowerTFFilter(TradeType tradeType)
        {
            return LowerTFFilter(ParamEnableLT01Filter, tradeType, lt01Ma, lt01Adx, ParamLT01ADXLevel) && LowerTFFilter(ParamEnableLT02Filter, tradeType, lt02Ma, lt02Adx, ParamLT02ADXLevel) && LowerTFFilter(ParamEnableLT03Filter, tradeType, lt03Ma, lt03Adx, ParamLT03ADXLevel) && LowerTFFilter(ParamEnableLT04Filter, tradeType, lt04Ma, lt04Adx, ParamLT04ADXLevel);
        }

        protected bool LowerTFFilter(bool ParamEnableLowerTimeframeFilter, TradeType tradeType, MovingAverage ltMa, DirectionalMovementSystem ltAdx, double ParamLTADXLevel)
        {
            if (ParamEnableLowerTimeframeFilter)
            {
                if (tradeType == TradeType.Buy)
                {
                    bool alignmentCondition = ParamEnableStrictAlignment ? MarketSeries.Close.Last(1) > ltMa.Result.Last(1) : true;
                    if (alignmentCondition)
                    {
                        if (ltAdx.ADX.Last(1) > ltAdx.ADX.Last(2))
                        {
                            if (ltAdx.ADX.Last(1) > ParamLTADXLevel)
                            {
                                if (ltAdx.ADX.Last(1) >= adx.ADX.Last(1))
                                {
                                    return true;
                                }
                            }
                        }
                    }
                    return false;
                }
                else
                {
                    // sell
                    bool alignmentCondition = ParamEnableStrictAlignment ? MarketSeries.Close.Last(1) < ltMa.Result.Last(1) : true;
                    if (alignmentCondition)
                    {
                        if (ltAdx.ADX.Last(1) > ltAdx.ADX.Last(2))
                        {
                            if (ltAdx.ADX.Last(1) > ParamLTADXLevel)
                            {
                                if (ltAdx.ADX.Last(1) >= adx.ADX.Last(1))
                                {
                                    return true;
                                }
                            }
                        }
                    }
                    return false;
                }

            }
            return true;
        }

        protected bool RSIFilter()
        {
            if (ParamEnableRSI)
            {

                if (rsi.Result.Last(1) < ParamRSITopLevel && rsi.Result.Last(1) > ParamRSIBottomLevel)
                {
                    return true;
                }
                return false;
            }
            return true;
        }

        protected bool MinCandleBarSizeFilter()
        {
            if (!ParamEnableMinCndleSize)
                return true;
            double barSize = Math.Abs(MarketSeries.Close.Last(1) - MarketSeries.Open.Last(1)) / Symbol.PipSize;
            if (barSize < ParamMinCandleSize)
            {
                return false;
            }
            return true;
        }

        protected bool ADXLevelCondition()
        {
            return adx.ADX.Last(1) > ParamADXLevel;
        }

        protected bool ClosesInExtremeZoneCondition(TradeType tradeType)
        {
            if (!RSIFilter() && ParamEnableMultipleTrades)
            {
                if (tradeType == TradeType.Buy)
                {
                    if (MarketSeries.Close.Last(1) > ma1.Result.Last(1) && MarketSeries.Close.Last(1) > ma2.Result.Last(1))
                    {
                        if (MarketSeries.Close.Last(1) > MarketSeries.Open.Last(1))
                        {
                            return true;
                        }
                    }
                }
                else
                {
                    if (MarketSeries.Close.Last(1) < ma1.Result.Last(1) && MarketSeries.Close.Last(1) < ma2.Result.Last(1))
                    {
                        if (MarketSeries.Close.Last(1) < MarketSeries.Open.Last(1))
                        {
                            return true;
                        }
                    }
                }
            }

            return false;
        }

        protected override void OnBar()
        {
            // close test
            if (ParamEnableCloseEquityPercent)
            {
                double equityBalanceRatio = 100 * (Account.Equity / Account.Balance);
                if (equityBalanceRatio > ParamCloseEquityPercent)
                {
                    foreach (var pos in Positions.FindAll(Label, Symbol))
                    {
                        ClosePosition(pos);

                    }
                    return;
                }
            }
            if (ParamEnableMACloseFeature)
            {
                double lastClose = MarketSeries.Close.Last(2);
                double currentClose = MarketSeries.Close.Last(1);
                if (lastClose > maCloseFeature.Result.Last(1) && currentClose < maCloseFeature.Result.Last(1))
                {
                    foreach (var pos in Positions.FindAll(Label, Symbol, TradeType.Buy))
                    {
                        ClosePosition(pos);

                    }
                }
                else if (lastClose < maCloseFeature.Result.Last(1) && currentClose > maCloseFeature.Result.Last(1))
                {
                    foreach (var pos in Positions.FindAll(Label, Symbol, TradeType.Sell))
                    {
                        ClosePosition(pos);

                    }
                }
            }

            if (!ParamEnableMultipleTrades)
            {
                if (PositionsCount() > 0)
                {
                    return;
                }
            }

            // opening
            if (adx.ADX.Last(1) > adx.ADX.Last(2))
            {
                if (ADXLevelCondition())
                {
                    // buy MA
                    if (MarketSeries.Close.Last(1) > ma1.Result.Last(1) && MarketSeries.Close.Last(1) > ma2.Result.Last(1))
                    {
                        if (MarketSeries.Close.Last(1) > MarketSeries.Open.Last(1))
                        {
                            if (VolatilityFilter() == true)
                            {
                                if (SRFilter(TradeType.Buy) == true)
                                {
                                    if (LowerTFFilter(TradeType.Buy) == true)
                                    {
                                        if (RSIFilter() || (ADXLevelCondition() && !ParamEnableRSI) || ClosesInExtremeZoneCondition(TradeType.Buy))
                                        {
                                            if (MinCandleBarSizeFilter())
                                            {



                                                ExecuteOrder(TradeType.Buy);

                                            }
                                        }

                                    }
                                }
                            }
                        }
                    }

                    else if (MarketSeries.Close.Last(1) < ma1.Result.Last(1) && MarketSeries.Close.Last(1) < ma2.Result.Last(1))
                    {
                        if (MarketSeries.Close.Last(1) < MarketSeries.Open.Last(1))
                        {
                            if (VolatilityFilter() == true)
                            {
                                if (SRFilter(TradeType.Sell) == true)
                                {
                                    if (LowerTFFilter(TradeType.Sell) == true)
                                    {
                                        if (RSIFilter() || (ADXLevelCondition() && !ParamEnableRSI) || ClosesInExtremeZoneCondition(TradeType.Sell))
                                        {
                                            if (MinCandleBarSizeFilter())
                                            {


                                                ExecuteOrder(TradeType.Sell);

                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        protected void ExecuteOrder(TradeType tradeType)
        {
            int volume = GetInitialVolume(Account.Balance, ParamVolumeAdjustment);
            int newIndex = 1;
            int rawIndex = 1;
            HistoricalTrade lastClosed = History.FindLast(Label, Symbol);
            if (lastClosed != null)
            {
                bool breakevenTest = (Math.Abs(lastClosed.EntryPrice - lastClosed.ClosingPrice) / Symbol.PipSize) < 1;
                if (lastClosed.NetProfit < 0 && !breakevenTest)
                {
                    int index = parseMartingaleIndex(lastClosed.Comment);
                    int tpIndex = parseTpIndex(lastClosed.Comment);
                    rawIndex = tpIndex + 1;
                    if (ParamEnableMartingale == true)
                    {
                        if (ParamMaxLosingTrades > 0)
                        {
                            if (index >= ParamMaxLosingTrades)
                            {

                            }
                            else
                            {
                                volume = (int)Symbol.NormalizeVolumeInUnits(lastClosed.VolumeInUnits * ParamMultiplicationFactor, RoundingMode.ToNearest);
                                newIndex = index + 1;
                            }
                        }
                        else
                        {
                            volume = (int)Symbol.NormalizeVolumeInUnits(lastClosed.VolumeInUnits * ParamMultiplicationFactor, RoundingMode.ToNearest);
                            newIndex = index + 1;
                        }
                    }

                }
            }

            string comment = createComment(newIndex, rawIndex);
            ExecuteMarketOrder(tradeType, Symbol, volume, Label, null, null, null, comment);

        }



        int parseMartingaleIndex(string comment)
        {
            return int.Parse(comment.Split(new char[] 
            {
                '-'
            })[0]);
        }

        int parseTpIndex(string comment)
        {
            return int.Parse(comment.Split(new char[] 
            {
                '-'
            })[1]);
        }

        string createComment(int martingaleIndex, int tpIndex)
        {
            return string.Format("{0}-{1}", martingaleIndex, tpIndex);
        }

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

    }
}


CT
ctid1731325

Joined on 10.01.2020 Blocked

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: NielsGuppy V3.3.algo
  • Rating: 5
  • Installs: 2701
Comments
Log in to add a comment.
OM
Omega · 1 year ago

Hi,

I wanted to use this to close all trades when equity get to a certain level, not sure it's working. Example say I have US$500 in my account, I want to close all orders at 15% gain (500 x 15% = $75, $575). I have set the equity gain on swiss Armey Knife to 115, is this correct?

Thanks

RI

Nice bot!!!