Category Trend  Published on 25/07/2024

cTrader Aroon Trading Robot

Description

Traders can utilize the cTrader Aroon Oscillator to determine trend direction and make trading decisions accordingly. When the positive oscillator signals an uptrend, traders can open long positions. Conversely, traders can initiate short positions if the negative oscillator indicates a downtrend. This strategy automatically opens and manages your trades 24/7 with a Telegram alert. Full Source Code is included for educational purposes to help traders with various levels of programming knowledge learn the following skills using Microsoft C# and the cTrader API.

This cBot was created by the ClickAlgo Strategy Building Tool in a few minutes with no coding.

The results below were achieved by running the optimisation application of cTrader Algo in less than 1 minute, they show a 1-year backtest.

 

 

cTrader Algo Strategy Builder

You can download the Algo Strategy Builder and start creating your strategies, it can also be used to create a system that will send Telegram signals.

 

Watch Video Demo's

 

Need Modifications?

You can contact our development team if you need to add additional indicators or features to this cBot.

If you also would like us to convert your ideas or manual strategy into an automated system, we can help

 

A black text with a white background

Description automatically generated

ClickAlgo is a leading supplier of cTrader cBots & indicators.

 

DISCLAIMER

This product is for educational use only and should not be considered financial advice. Trading and Investing Activities are Inherently Risky, and Users of Our Material Should Be Prepared to Incur Losses in Connection with Such Activities.


using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

/*
   This code was created using the Algo Strategy Building Tool by ClickAlgo Limited
   https://clickalgo.com/strategy-builder
 
   This cBot does not guarantee any particular outcome or
   profit of any kind. Use it at your own risk.

   Created: 11/07/2024
   Name: Aroon & RSI Strategy

   This uses the Aroon and Relative Strength Indicator to determine a strong change in the direction of the trade. It also includes Telegram alerts and a pop up message when a trade opens.
*/

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class AlgoBuilderBot : Robot
    {
        // user parameter settings

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

        [Parameter("Stop loss", Group = "Protection", DefaultValue = 84)]
        public int StopLoss { get; set; }

        [Parameter("Take profit", Group = "Protection", DefaultValue = 9)]
        public int TakeProfit { get; set; }

        [Parameter("Bot Token", Group = "Telegram Alert", DefaultValue = "")]
        public string BotToken { get; set; }

        [Parameter("Chat ID", Group = "Telegram Alert", DefaultValue = "")]
        public string ChatID { get; set; }

        // Indicator settings for bullish signals

        [Parameter("Timeframe", DefaultValue = "h1", Group = "RSI Buy #1")]
        public TimeFrame RsiTimeFrameBuy1 { get; set; }
        
        [Parameter("RSI Periods", DefaultValue =  14, Group = "RSI Buy #1")]
        public int RsiPeriodsBuy1 { get; set; }
        
        [Parameter("RSI Lower", DefaultValue =  30, Group = "RSI Buy #1")]
        public int RsiLowerBuy1 { get; set; }

        [Parameter("Timeframe", DefaultValue = "h1", Group = "Aroon Buy #2")]
        public TimeFrame AroonTimeFrameBuy2 { get; set; }
        
        [Parameter("Signal Periods", DefaultValue =  25, Group = "Aroon Buy #2")]
        public int AroonPeriodsBuy2 { get; set; }

        // Indicator settings for bearish signals

        [Parameter("Timeframe", DefaultValue = "h1", Group = "RSI Sell #1")]
        public TimeFrame RsiTimeFrameSell1 { get; set; }
        
        [Parameter("RSI Periods", DefaultValue =  14, Group = "RSI Sell #1")]
        public int RsiPeriodsSell1 { get; set; }
        
        [Parameter("RSI Upper", DefaultValue =  70, Group = "RSI Sell #1")]
        public int RsiUpperSell1 { get; set; }

        [Parameter("Timeframe", DefaultValue = "h1", Group = "Aroon Sell #2")]
        public TimeFrame AroonTimeFrameSell2 { get; set; }
        
        [Parameter("Signal Periods", DefaultValue =  25, Group = "Aroon Sell #2")]
        public int AroonPeriodsSell2 { get; set; }

        // declaring the standard bullish indicators

        private RelativeStrengthIndex _rsi_Buy1;
        private Aroon _aroon_Buy2;

        // declaring the standard bearish indicators

        private RelativeStrengthIndex _rsi_Sell1;
        private Aroon _aroon_Sell2;

        // declaring the market data variables for bullish signals.

        private Bars _rsiBars_Buy1;
        private Bars _aroonBars_Buy2;

        // declaring the market data variables for bearish signals.

        private Bars _rsiBars_Sell1;
        private Bars _aroonBars_Sell2;

        // Private fields.

        private string StrategyName { get; set; }

        // makes sure only 1 trade opens per signal
        bool IsBullish { get; set; }
        bool IsBearish { get; set; }    

        // Initialise variables when the cBot starts.

        protected override void OnStart()
        {
            StrategyName = "Aroon & RSI Strategy";

            // constructing the market data for the bullish indicators
            _rsiBars_Buy1 = MarketData.GetBars(RsiTimeFrameBuy1);
            _aroonBars_Buy2 = MarketData.GetBars(AroonTimeFrameBuy2);

            // constructing the market data for the bearish indicators
            _rsiBars_Sell1 = MarketData.GetBars(RsiTimeFrameSell1);
            _aroonBars_Sell2 = MarketData.GetBars(AroonTimeFrameSell2);

            // constructing the BULLISH indicators
            _rsi_Buy1 = Indicators.RelativeStrengthIndex(_rsiBars_Buy1.ClosePrices, RsiPeriodsBuy1);
            _aroon_Buy2 = Indicators.Aroon(AroonPeriodsBuy2);

            // constructing the BEARISH indicators
            _rsi_Sell1 = Indicators.RelativeStrengthIndex(_rsiBars_Sell1.ClosePrices, RsiPeriodsSell1);
            _aroon_Sell2 = Indicators.Aroon(AroonPeriodsSell2);
        }

        // The onTick method is called on each price change.
        protected override void OnTick()
        {
          
        }

         // this method is called when the candle closes and is used to check the trade signals, execute trades and send alerts.
        protected override void OnBarClosed()
        {
            TradeRulesBullish();            
            TradeRulesBearish();
        }

        // parent method to check rules and open bullish trade or send signal.
        private void TradeRulesBullish()
        {
            // flag to open a trade if all rules true.
            bool OpenBuyTrade = false;

            if (IsBullishSignal())
            {
                OpenBuyTrade = true;
            }
 
            if (OpenBuyTrade)
            {
                if (!IsBullish)
                {
                    if (!IsTradeOpen(TradeType.Buy))
                    {
                        OpenTrade(TradeType.Buy);
                    }

                    SendTradeAlerts(TradeType.Buy);
                }

                IsBullish = true;
            }
            else
            {
                IsBullish = false;
            }
        }


        // parent method to check rules and open bearish trade or send signal.
        private void TradeRulesBearish()
        {
            // flag to open a trade if all rules true.
            bool OpenSellTrade = false;

           if (IsBearishSignal())
            {
                OpenSellTrade = true;
            }

            if (OpenSellTrade)
            {
                if (!IsBearish)
                {
                    if (!IsTradeOpen(TradeType.Sell))
                    {
                        OpenTrade(TradeType.Sell);
                    }

                    SendTradeAlerts(TradeType.Sell);
                }

                IsBearish = true;
            }
            else
            {
                IsBearish = false;
            }
        }

        // returns true if all signals are bullish
        private bool IsBullishSignal() 
        {
            if (_rsi_Buy1.Result.LastValue > RsiLowerBuy1)
            {
                return false;
            }

            if (_aroon_Buy2.Up.LastValue !< _aroon_Buy2.Down.LastValue)
            {
                return false;
            }

            return true;
        }

        // returns true if all signals are bearish
        private bool IsBearishSignal() 
        {
            if (_rsi_Sell1.Result.LastValue < RsiUpperSell1)
            {
                return false;
            }

            if (_aroon_Sell2.Up.LastValue !> _aroon_Sell2.Down.LastValue)
            {
                return false;
            }

            return true;
        }

        private void OpenTrade(TradeType type)
        {
            // calculate volume from lots
            var volume = Symbol.QuantityToVolumeInUnits(Quantity);
            volume = Symbol.NormalizeVolumeInUnits(volume, RoundingMode.Down);

            ExecuteMarketOrder(type, SymbolName, volume, StrategyName, StopLoss, TakeProfit);
        }

        // returns true if buy trade is open.
        private bool IsTradeOpen(TradeType type)
        {
            var positions = Positions.FindAll(StrategyName, SymbolName, type);
            if (positions.Count() > 0)
            {
                return true;
            }
            else return false;
        }

        // this method manages sending trade alerts
        private void SendTradeAlerts(TradeType type)
        {
            if (IsBacktesting)
            {
                return;
            }

            string direction = "Bullish";

            if (type == TradeType.Sell)
            {
                direction = "Bearish";
            }
            
            // send Telegram
            var Token = BotToken;
            var message = direction + " alert on " + SymbolName + " with " + TimeFrame.ToString();

            var result = Http.Get($"https://api.telegram.org/bot{Token}/sendMessage?chat_id={ChatID}&text={message}");

            // the following error codes may be incorrect and need updating.
            switch (result.StatusCode)
            {
                case 0:
                    Print("Telegram bot sleeping, wake it up.");
                    break;

                case 200:
                    Print("Telegram sent");
                    break;

                case 400:
                    Print("The chat id is incorrect.");
                    break;

                case 401:
                    Print("The bot token is incorrect.");
                    break;

                case 404:
                    Print("The bot token or chat id is missing or incorrect.");
                    break;

                default:
                    Print("unknown error code: " + result.StatusCode);
                    break;
            }

            // show popup
            MessageBox.Show(direction + " alert on " + SymbolName + " with " + TimeFrame.ToString(), StrategyName);
        }
    }
}

ClickAlgo's avatar
ClickAlgo

Joined on 05.02.2015

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: ClickAlgo Aroon & RSI.algo
  • Rating: 0
  • Installs: 319
  • Modified: 25/07/2024 12:40
Comments
Log in to add a comment.
No comments found.