CBOT Counter Trade

Created at 27 May 2014, 00:00
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!
DA

davidp13

Joined 06.05.2014

CBOT Counter Trade
27 May 2014, 00:00


Any help will be appreciated!

Im looking for a CBOT that will automatically open a counter trade as soon as an original trade is opened. How would one go about that?

Thanks


@davidp13
Replies

modarkat
27 May 2014, 09:33

RE:

davidp13 said:

Any help will be appreciated!

Im looking for a CBOT that will automatically open a counter trade as soon as an original trade is opened. How would one go about that?

Thanks

/algos/cbots/show/475


@modarkat

davidp13
25 Mar 2015, 14:07

RE: RE:

Hi,

This robot was great thanks, but now i need to integrate it with another robot I have. The trades all open onBar, but i cant seem to get the countertrade code to work inside the robot. Code is below if anyone can help pls. Thanks

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SP500 : Robot
    {
        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

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

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

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

        [Parameter(DefaultValue = 3, MinValue = 0)]
        public int Volume { get; set; }

        [Parameter("Take Profit", DefaultValue = 300)]
        public int TakeProfit { get; set; }

        [Parameter("Stop Loss", DefaultValue = 40)]
        public int StopLoss { get; set; }

        [Parameter("Counter TP", DefaultValue = 100, MinValue = 10)]
        public int TakeProfitCounter { get; set; }

        [Parameter("Counter SL", DefaultValue = 100, MinValue = 0)]
        public int StopLossCounter { get; set; }

        private MovingAverage slowMa;
        private MovingAverage fastMa;

        private const string label = "A2-1";
        private const string label2 = "counter";



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


        }

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

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

            var lastIndex = SourceSeries.Count - 1;
            var valueClose = SourceSeries[lastIndex];

            Positions.Opened += OnPositionsOpened;


            if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa && shortPosition == null)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label, StopLoss, TakeProfit);
            }
            if (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa && longPosition == null)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label, StopLoss, TakeProfit);
            }

//CLOSE POSITIONS******

            if (shortPosition != null && currentSlowMa <= currentFastMa && previousSlowMa > previousFastMa)
            {

                ClosePosition(shortPosition);
                //Stop();
            }
            else if (longPosition != null && currentSlowMa >= currentFastMa && previousSlowMa < previousFastMa)
            {

                ClosePosition(longPosition);
                //Stop();
            }

        }


        void OnPositionsOpened(PositionOpenedEventArgs args)
        {
            var originalPosition = args.Position;
            if (originalPosition.Label != label)
            {
                var tradeType = originalPosition.TradeType == TradeType.Buy ? TradeType.Sell : TradeType.Buy;
                ExecuteMarketOrder(tradeType, Symbol, originalPosition.Volume, label2, StopLossCounter, TakeProfitCounter);
            }
        }

    }
}

 

modarkat said:

davidp13 said:

Any help will be appreciated!

Im looking for a CBOT that will automatically open a counter trade as soon as an original trade is opened. How would one go about that?

Thanks

/algos/cbots/show/475

 


@davidp13

tradermatrix
25 Mar 2015, 21:09

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SP500 : Robot
    {
        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

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

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

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

        [Parameter(DefaultValue = 3, MinValue = 0)]
        public int Volume { get; set; }

        [Parameter("Take Profit", DefaultValue = 300)]
        public int TakeProfit { get; set; }

        [Parameter("Stop Loss", DefaultValue = 40)]
        public int StopLoss { get; set; }

        [Parameter("Counter TP", DefaultValue = 300, MinValue = 10)]
        public int TakeProfitCounter { get; set; }

        [Parameter("Counter SL", DefaultValue = 40, MinValue = 0)]
        public int StopLossCounter { get; set; }

        private MovingAverage slowMa;
        private MovingAverage fastMa;

        private const string label = "A2-1";
        private const string label2 = "counter";



        protected override void OnStart()
        {
            fastMa = Indicators.ExponentialMovingAverage(SourceSeries, FastPeriods);
            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
            Positions.Opened += OnPositionsOpened;


        }

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

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

            var lastIndex = SourceSeries.Count - 1;
            var valueClose = SourceSeries[lastIndex];



            if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa && shortPosition == null)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label, StopLoss, TakeProfit);
            }
            if (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa && longPosition == null)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label, StopLoss, TakeProfit);
            }

            //CLOSE POSITIONS******

            if (shortPosition != null && currentSlowMa <= currentFastMa && previousSlowMa > previousFastMa)
            {

                ClosePosition(shortPosition);
                //Stop();
            }
            else if (longPosition != null && currentSlowMa >= currentFastMa && previousSlowMa < previousFastMa)
            {

                ClosePosition(longPosition);
                //Stop();
            }

        }


        void OnPositionsOpened(PositionOpenedEventArgs args)
        {
            var originalPosition = args.Position;
            if (originalPosition.Label == label)
            {
                var tradeType = originalPosition.TradeType == TradeType.Buy ? TradeType.Sell : TradeType.Buy;
                ExecuteMarketOrder(tradeType, Symbol, originalPosition.Volume, label2, StopLossCounter, TakeProfitCounter);
            }
        }

    }
}



 


@tradermatrix

davidp13
30 Mar 2015, 20:48

many thanks!


@davidp13