KA
    
        
            EMA Trading robot
            
                 04 Feb 2023, 19:49
            
                    
This is a cAlgo trading robot written in C#. It implements an exponential moving average (EMA) strategy. The robot uses two EMAs with periods of 5 and 50 to generate trading signals. If the 5-period EMA is greater than the 50-period EMA, it buys. If the 5-period EMA is less than the 50-period EMA, it sells. If a position is open, the robot checks if the current 5-period EMA value crosses the 50-period EMA, in which case it closes the position. The volume of each trade is specified by the "Volume" parameter.
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.API.MarketData;
namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class EMAStrategy : Robot
    {
        private ExponentialMovingAverage _ema5;
        private ExponentialMovingAverage _ema50;
        private Position _position;
        [Parameter("Volume", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
        public double Volume { get; set; }
        protected override void OnStart()
        {
            var marketSeries = MarketData.GetPriceSeries(PriceComponent.Close, TimeFrame.Minute15);
            _ema5 = Indicators.ExponentialMovingAverage(marketSeries, 5);
            _ema50 = Indicators.ExponentialMovingAverage(marketSeries, 50);
        }
        protected override void OnTick()
        {
            if (marketSeries.Count < 5)
                return;
            if (_position != null)
            {
                if (_position.TradeType == TradeType.Buy && _ema5.Result.LastValue < _ema50.Result.LastValue)
                {
                    ClosePosition(_position);
                }
                else if (_position.TradeType == TradeType.Sell && _ema5.Result.LastValue > _ema50.Result.LastValue)
                {
                    ClosePosition(_position);
                }
            }
            else
            {
                if (_ema5.Result.LastValue > _ema50.Result.LastValue)
                {
                    _position = OpenPosition(TradeType.Buy, Volume);
                }
                else if (_ema5.Result.LastValue < _ema50.Result.LastValue)
                {
                    _position = OpenPosition(TradeType.Sell, Volume);
                }
            }
        }
    }
}
This code was written by OpenAI. Is it possible for Ctrader to handle it too?

PanagiotisChar
06 Feb 2023, 10:33
Hi there,
Some of the code generated by ChatGPT is nonsense. It's better to learn coding yourself or assign this to a professional rather than trying to fix the nonsense.
Aieden Technologies
Need help? Join us on Telegram
Need premium support? Trade with us
@PanagiotisChar