cBots Algorithms cTrader

cBots Algorithms cTrader – Trend Moving Average

                               

This robot validates the slope of the moving averages to indicate the trend and opens orders accordingly.
It uses some configurable moving averages that must be optimized for the role you want to operate.
Indicated papers: EURUSD, GBPUSD, AUDUSD, EURUSD

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 TrendMovingAverageBot : Robot
    {
 
        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }
 
        private DataSeries Source { get; set; }
        private int Periods { get; set; }
        private RelativeStrengthIndex rsi;
        [Parameter("Take Profit", Group = "RSI", DefaultValue = 200)]
        public int takeProfit { get; set; }
 
        [Parameter("Stop Loss", Group = "RSI", DefaultValue = 95)]
        public int stopLoss { get; set; }
 
 
 
        private MovingAverage i_MA_ultraSlow;
        private MovingAverage i_MA_slow;
        private MovingAverage i_MA_fast;
 
 
        private TrendIndicator ti;
        private double volumeInUnits;
 
 
        protected override void OnStart()
        {
            volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
            ti = Indicators.GetIndicator<TrendIndicator>();
            rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 19);
            i_MA_slow = Indicators.MovingAverage(Bars.ClosePrices, 16, MovingAverageType.Exponential);
            i_MA_ultraSlow = Indicators.MovingAverage(Bars.ClosePrices, 196, MovingAverageType.Exponential);
            i_MA_fast = Indicators.MovingAverage(Bars.ClosePrices, 12, MovingAverageType.Exponential);
        }
 
        protected override void OnBar()
        {
            string retorno = ti.Retorno(i_MA_fast.Result.LastValue, i_MA_slow.Result.LastValue, i_MA_ultraSlow.Result.LastValue, rsi.Result.LastValue);
            Print(retorno);
            if (retorno.Equals("Buy"))
            {
                ExecuteMarketOrder(TradeType.Buy, SymbolName, volumeInUnits, "RSI", stopLoss, takeProfit);
            }
            else if (retorno.Equals("Sell"))
            {
                ExecuteMarketOrder(TradeType.Sell, SymbolName, volumeInUnits, "RSI", stopLoss, takeProfit);
            }
        }
    }
}