Allow 1 trade each MA Crossover

Created at 12 May 2018, 09:19
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

Allow 1 trade each MA Crossover
12 May 2018, 09:19


Hello, I have made a bot with a few indicators but i have a problem When my signals are true my bot takes a trade (all perfect). The problem: when my position is closed by trailing stoploss my bot takes another trade when my signals are still active.. I only want to allow 1 buy trade when MA is turning to buy signal And only 1 sell trade when MA is turning to sell signal After this one trade my MA should wait for the next crossover before the next trade is allowed How to fix this? 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

... Deleted by UFO ...

PanagiotisCharalampous
14 May 2018, 09:22

Hi jelle2500,

You can use a condition like below

            if (Positions.Count(x => x.TradeType == TradeType.Buy) == 0)
            {
                // Execute Buy Trade
            }

?Let me know if this helps you

 

Best Regards,

Panagiotis


@PanagiotisCharalampous

jelle2500
14 May 2018, 09:51

RE:

Panagiotis Charalampous said:

Hi jelle2500,

You can use a condition like below

            if (Positions.Count(x => x.TradeType == TradeType.Buy) == 0)
            {
                // Execute Buy Trade
            }

?Let me know if this helps you

 

Best Regards,

Panagiotis

Again, thanks a lot Panagiotis!


@jelle2500

osato.1990
23 Jan 2019, 15:36

RE:

Panagiotis Charalampous said:

Hi jelle2500,

You can use a condition like below

            if (Positions.Count(x => x.TradeType == TradeType.Buy) == 0)
            {
                // Execute Buy Trade
            }

?Let me know if this helps you

 

Best Regards,

Panagiotis

Thank you for your help.
Well I am suffering from the same problem.

if (Positions.Count (x => x.TradeType == TradeType.Buy) == 0)

Where should the above code be inserted?

I wrote the following program but it does not work properly.

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 crossover : Robot
    {
        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

        [Parameter()]
        public DataSeries SourceSeries { get; set; }

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

        [Parameter("Fast Periods", DefaultValue = 5)]
        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 = 50, MinValue = 1)]
        public int StopLossInPips { get; set; }

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

        [Parameter("Position Label", DefaultValue = "My Label")]
        public string MyLabel { get; set; }


        private MovingAverage slowMa;
        private MovingAverage fastMa;
        private const string label = "Sample Trend cBot";


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

        protected override void OnTick()
        {
            var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);

            var currentSlowMa = slowMa.Result.Last(1);
            var currentFastMa = fastMa.Result.Last(1);
            var previousSlowMa = slowMa.Result.Last(2);
            var previousFastMa = fastMa.Result.Last(2);



            if (currentSlowMa < currentFastMa && longPosition == null && Positions.Count(x => x.TradeType == TradeType.Buy) == 0)
            {
                if (shortPosition != null)
                    ClosePosition(shortPosition);

                ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, label, StopLossInPips, TakeProfitInPips);
            }



            else if (currentSlowMa > currentFastMa && shortPosition == null && Positions.Count(x => x.TradeType == TradeType.Sell) == 0)
            {
                if (longPosition != null)
                    ClosePosition(longPosition);
                ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, label, StopLossInPips, TakeProfitInPips);
            }
        }

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

 

I am sorry that I am a beginner so it will be helpful if you let me know・・・
 


@osato.1990

PanagiotisCharalampous
23 Jan 2019, 16:33

Hi oons,

Thanks for posting in our forum. What do you expect the cBot to do and what does it do instead?

Best Regards,

Panagiotis


@PanagiotisCharalampous

osato.1990
24 Jan 2019, 15:16

RE:

Panagiotis Charalampous said:

Hi oons,

Thanks for posting in our forum. What do you expect the cBot to do and what does it do instead?

Best Regards,

Panagiotis

Thank you for the immediate reply.

In the case BUY at a timing when a short-term MA crosses over a long-term MA, even if a stop loss or a take profit is filled, as long as short-term MA is staying above long-term MA, BUY again .

Once the position has been closed, I would like to set the next position to be only in the opposite direction.

For example ...
The trade after the longposition has been closed should only take the shortposition.
The trade after the shortposition has been closed is to ensure that only the longposition can be held.

※ In the case of the MA crossover system,

var currentSlowMa = slowMa.Result.Last (0);
var currentFastMa = fastMa.Result.Last (0);
var previousSlowMa = slowMa.Result.Last (1);
var previousFastMa = fastMa.Result.Last (1);

if (currentSlowMa <currentFastMa && previousSlowMa> previousFastMa && longPosition == null)

By describing as above,I think that it will be possible to prevent the previous position and the new position from being in the same direction.

However, since I want to apply it to others, I would like to know how to set the next position to be only in the opposite direction when the position is closed once.

I'm sorry for the poor explanation, but I'm happy if I get an answer.


@osato.1990

PanagiotisCharalampous
25 Jan 2019, 10:19

Hi oons,

I suggest that you keep the direction of the latest position in a variable. Then, when a position is closed, you will know what was the last direction and you will be able to make the respective decisions i.e. to open a position only in the opposite direction. Let me know if this helps.

Best Regards,

Panagiotis


@PanagiotisCharalampous