Price/EMA crossover cBot

Created at 22 Dec 2016, 15:58
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

david.papershop

Joined 22.12.2016

Price/EMA crossover cBot
22 Dec 2016, 15:58


Hi all,

I am very new to programming but would like to try understand the basics. Saying that I wonder if someone to could help me by posting a code that creates a cbot to do the following.

1) Once the price crosses above the predefined EMA it buys, adding a stop and limit order.

2) Once price crosses above the predefined EMA it buys, and when it crosses below it closes the existing long position, and goes short instead.

By me being able to compare the two will help me enormously.

Thanking you inadvance.

 

 


@david.papershop
Replies

... Deleted by UFO ...

... Deleted by UFO ...

david.papershop
25 Dec 2016, 22:55

Hi Lucian thanks so much for posting - much appreciated. 


@david.papershop

tekASH
29 Sep 2018, 21:28

RE:

lucian said:

1)


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

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

        [Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }
        [Parameter("Stop Loss", DefaultValue = 10)]
        public int sl { get; set; }
        [Parameter("Take Profit", DefaultValue = 10)]
        public int tp { get; set; }
        private ExponentialMovingAverage ema;

        protected override void OnStart()
        {
            ema = Indicators.ExponentialMovingAverage(Source, Periods);
        }

        protected override void OnBar()
        {
            int index = MarketSeries.Close.Count;
            if (ema.Result[index - 2] < MarketSeries.Close[index - 2] && ema.Result[index - 1] > MarketSeries.Close[index - 1])
            {

                Open(TradeType.Sell);
            }
            else if (ema.Result[index - 2] > MarketSeries.Close[index - 2] && ema.Result[index - 1] < MarketSeries.Close[index - 1])
            {


                Open(TradeType.Buy);
            }
        }



        private void Open(TradeType tradeType)
        {

            var volumeInUnits = Symbol.QuantityToVolume(Quantity);


            ExecuteMarketOrder(tradeType, Symbol, volumeInUnits, "EMA", sl, tp);
        }
    }
}

2)


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

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

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

        private ExponentialMovingAverage ema;

        protected override void OnStart()
        {
            ema = Indicators.ExponentialMovingAverage(Source, Periods);
        }

        protected override void OnBar()
        {
            int index = MarketSeries.Close.Count;
            if (ema.Result[index - 2] < MarketSeries.Close[index - 2] && ema.Result[index - 1] > MarketSeries.Close[index - 1])
            {
                Close(TradeType.Buy);
                Open(TradeType.Sell);
            }
            else if (ema.Result[index - 2] > MarketSeries.Close[index - 2] && ema.Result[index - 1] < MarketSeries.Close[index - 1])
            {

                Close(TradeType.Sell);
                Open(TradeType.Buy);
            }
        }

        private void Close(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll("EMA", Symbol, tradeType))
                ClosePosition(position);
        }

        private void Open(TradeType tradeType)
        {
            var position = Positions.Find("EMA", Symbol, tradeType);
            var volumeInUnits = Symbol.QuantityToVolume(Quantity);

            if (position == null)
                ExecuteMarketOrder(tradeType, Symbol, volumeInUnits, "EMA");
        }
    }
}

 

Hey Lucian

When building it brings error: Error CS0618: 'cAlgo.API.Internals.Symbol.QuantityToVolume(double)' is obsolete: 'Use QuantityToVolumeInUnits instead'

is it normal? Also, the cBot interface looks incomplete, no choosing EMA, two EMAs should be there I guess?


@tekASH

matt92
25 Oct 2018, 18:53

Does anyone know where I can download a cbot for instance #2 mentioned above.. price close above ema, auto(bot) long, till price close below ema, auto(bot) close long, and then takes the short?


@matt92

nguyendan81985
26 Oct 2018, 05:20

RE:

matt_graham_92@hotmail.com said:

Does anyone know where I can download a cbot for instance #2 mentioned above.. price close above ema, auto(bot) long, till price close below ema, auto(bot) close long, and then takes the short?

just copy and paste. hihi


@nguyendan81985

mrlukesprague
10 Nov 2022, 13:58 ( Updated at: 21 Dec 2023, 09:23 )

RE: RE:

im tying to build a bot that reversed an open trade once a 9 and 18 ema cross can anyone help?

 

nguyendan81985 said:

matt_graham_92@hotmail.com said:

Does anyone know where I can download a cbot for instance #2 mentioned above.. price close above ema, auto(bot) long, till price close below ema, auto(bot) close long, and then takes the short?

just copy and paste. hihi

 


@mrlukesprague