Separate Open and Close moving averages for a cbot

Created at 28 Apr 2016, 16:39
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!
AR

armstr.tradie

Joined 30.03.2016

Separate Open and Close moving averages for a cbot
28 Apr 2016, 16:39


Hi there,

I'm looking to see how one could go about changing the moving averages in the 'Sample Trend cbot' to where one MA is using the 'close' price and the other is using the 'open' price.

At present one only has the one option to select the Close or Open or High or Low for both moving averages. I would like to be able to set the fast MA to the close price and the slow MA to the open if possible.

If anyone is able to help it would be greatly appreciated and any ideas are welcomed. 

Here is the code:

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 first_hand_written_code : Robot
    {

        //parameters

        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

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

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

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

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

        [Parameter("TakeProfitPips", DefaultValue = 200)]
        public int tp { get; set; }

        [Parameter("StopLossPips", DefaultValue = 30)]
        public int sl { get; set; }

        [Parameter("trigger ", DefaultValue = 0)]
        public int Trigger { get; set; }

        [Parameter("Trailing", DefaultValue = 0)]
        public int Trailing { get; set; }


        private MovingAverage slowMa;

        private MovingAverage fastMa;

        private const string label = "Trend Trailing";



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

        }

        protected override void OnTick()
        {

            TRAILING();
            int volume = (int)(lots * 100000);

            //declaring variables during ticks
            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);

            // when buy signal is called
            if (previousSlowMa > previousFastMa && currentSlowMa < currentFastMa && longPosition == null)
            {
                //if theres a short position in playy
                if (shortPosition != null)

                    ClosePosition(shortPosition);

                ExecuteMarketOrder(TradeType.Buy, Symbol, volume, label, sl, tp);
            }

            //when short signal is called

            else if (previousSlowMa < previousFastMa && currentSlowMa > currentFastMa && shortPosition == null)
            {
                // if there are long positions in play
                if (longPosition != null)

                    ClosePosition(longPosition);

                ExecuteMarketOrder(TradeType.Sell, Symbol, volume, label, sl, tp);

            }

        }



        private void TRAILING()
        {
            if (Trailing > 0 && Trigger > 0)
            {

                Position[] positions = Positions.FindAll(label, Symbol);

                foreach (Position position in positions)
                {

                    if (position.TradeType == TradeType.Sell)
                    {

                        double distance = position.EntryPrice - Symbol.Ask;

                        if (distance >= Trigger * Symbol.PipSize)
                        {

                            double newStopLossPrice = Symbol.Ask + Trailing * Symbol.PipSize;

                            if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
                            {

                                ModifyPosition(position, newStopLossPrice, position.TakeProfit);

                            }
                        }
                    }

                    else
                    {

                        double distance = Symbol.Bid - position.EntryPrice;

                        if (distance >= Trigger * Symbol.PipSize)
                        {

                            double newStopLossPrice = Symbol.Bid - Trailing * Symbol.PipSize;

                            if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
                            {

                                ModifyPosition(position, newStopLossPrice, position.TakeProfit);

                            }
                        }
                    }
                }
            }
        }



    }
}

Here is the dialogue box in cAlgo showing the current setup.

Thank you.


@armstr.tradie
Replies

Jiri
28 Apr 2016, 17:02

Hi, you need to create another parameter and use it in the initialization of indicators.

Sample code:

[Parameter("Fast Source")]
public DataSeries FastSource { get; set; }

[Parameter("Slow Source")]
public DataSeries SlowSource { get; set; }


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

 


@Jiri

armstr.tradie
30 Apr 2016, 04:52

Thank you tmc. That was of great help. 


@armstr.tradie