MA Crossover takes another trade after closing by stoploss..

Created at 21 Apr 2018, 19:21
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!
JE

jelle2500

Joined 25.11.2017

MA Crossover takes another trade after closing by stoploss..
21 Apr 2018, 19:21


hello, de code i sent is only part of my bot buy i have a problem..

when  i set a stoploss or trailing stoploss or take profit and the trade is closed but my signals are still active, my bot takes another trade on the same signals.

what i'm trying to do is to make a rule that only one trade can be taken each MA Crossover.

 

can someone help me with this please?

 

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 EMACross_RSI : Robot
    {
        [Parameter("Source")]
        public DataSeries SourceSeries { get; set; }


        [Parameter("Slow Periods", DefaultValue = 30)]
        public int SlowPeriods { get; set; }


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

        [Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Stop Loss (pips)", DefaultValue = 20, MinValue = 0)]
        public int StopLossInPips { get; set; }

        [Parameter("Take Profit (pips)", DefaultValue = 60, MinValue = 0)]
        public int TakeProfitInPips { get; set; }



        private ExponentialMovingAverage slowMa;
        private ExponentialMovingAverage fastMa;

        private const string label = "EMA";
        private Position longPosition;
        private Position shortPosition;

        protected override void OnStart()
        {
            fastMa = Indicators.ExponentialMovingAverage(SourceSeries, FastPeriods);
            slowMa = Indicators.ExponentialMovingAverage(SourceSeries, SlowPeriods);



        }

        protected override void OnBar()
        {

            longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            shortPosition = Positions.Find(label, Symbol, TradeType.Sell);


            if (slowMa.Result.Last(1) < fastMa.Result.Last(1) && longPosition == null)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, label, StopLossInPips, TakeProfitInPips);

            }
            else if (slowMa.Result.Last(1) > fastMa.Result.Last(1) && shortPosition == null)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, label, StopLossInPips, TakeProfitInPips);

            }


        }

        private long VolumeInUnits
        {
            get { return Symbol.QuantityToVolume(Quantity); }
        }
    }
}


@jelle2500
Replies

tradermatrix
22 Apr 2018, 01:10

you have to program one command at a time
can be like that;
 

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 EMACross_RSI : Robot
    {
        [Parameter("Source")]
        public DataSeries SourceSeries { get; set; }

        [Parameter("Slow Periods", DefaultValue = 30)]
        public int SlowPeriods { get; set; }

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

        [Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Stop Loss (pips)", DefaultValue = 20, MinValue = 0)]
        public int StopLossInPips { get; set; }

        [Parameter("Take Profit (pips)", DefaultValue = 60, MinValue = 0)]
        public int TakeProfitInPips { get; set; }

        private ExponentialMovingAverage slowMa;
        private ExponentialMovingAverage fastMa;

        private const string label = "EMA";

        private Position longPosition;
        private Position shortPosition;

        protected override void OnStart()
        {
            fastMa = Indicators.ExponentialMovingAverage(SourceSeries, FastPeriods);
            slowMa = Indicators.ExponentialMovingAverage(SourceSeries, SlowPeriods);
        }

        protected override void OnBar()
        {
            var cBotPositions = Positions.FindAll(label);

            longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            shortPosition = Positions.Find(label, Symbol, TradeType.Sell);

            if (cBotPositions.Length >= 1)
                return;

            if (slowMa.Result.Last(1) < fastMa.Result.Last(1) && longPosition == null)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, label, StopLossInPips, TakeProfitInPips);

            }
            else if (slowMa.Result.Last(1) > fastMa.Result.Last(1) && shortPosition == null)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, label, StopLossInPips, TakeProfitInPips);

            }


        }

        private long VolumeInUnits
        {
            get { return Symbol.QuantityToVolume(Quantity); }
        }
    }
}

 


@tradermatrix

jelle2500
23 Apr 2018, 16:03

RE:

tradermatrix said:

you have to program one command at a time
can be like that;
 

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 EMACross_RSI : Robot
    {
        [Parameter("Source")]
        public DataSeries SourceSeries { get; set; }

        [Parameter("Slow Periods", DefaultValue = 30)]
        public int SlowPeriods { get; set; }

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

        [Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Stop Loss (pips)", DefaultValue = 20, MinValue = 0)]
        public int StopLossInPips { get; set; }

        [Parameter("Take Profit (pips)", DefaultValue = 60, MinValue = 0)]
        public int TakeProfitInPips { get; set; }

        private ExponentialMovingAverage slowMa;
        private ExponentialMovingAverage fastMa;

        private const string label = "EMA";

        private Position longPosition;
        private Position shortPosition;

        protected override void OnStart()
        {
            fastMa = Indicators.ExponentialMovingAverage(SourceSeries, FastPeriods);
            slowMa = Indicators.ExponentialMovingAverage(SourceSeries, SlowPeriods);
        }

        protected override void OnBar()
        {
            var cBotPositions = Positions.FindAll(label);

            longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            shortPosition = Positions.Find(label, Symbol, TradeType.Sell);

            if (cBotPositions.Length >= 1)
                return;

            if (slowMa.Result.Last(1) < fastMa.Result.Last(1) && longPosition == null)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, label, StopLossInPips, TakeProfitInPips);

            }
            else if (slowMa.Result.Last(1) > fastMa.Result.Last(1) && shortPosition == null)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, label, StopLossInPips, TakeProfitInPips);

            }


        }

        private long VolumeInUnits
        {
            get { return Symbol.QuantityToVolume(Quantity); }
        }
    }
}

thanks a lot for your reply, but can i put a rule that make 1 trade posible each MA Crossover till the next crossover comes?

because when i set a stoploss of trailing stoploss and the trade is closed my bot takes another trade when my signals are still active..

my bot needs to wait for the next MA Crossover.

 

 


@jelle2500