Category Other  Published on 27/07/2024

Scanner Telegram Alerte RSI STOCH PIVOT

Description

Cbot telegram alerte of RSI and Stochastique. 
Open Position Only in Backtest…

use this pivot point in referency : https://ctrader.com/algos/indicators/show/4402/


Enjoy for Free =) 


Previous account here : https://ctrader.com/users/profile/70920
Contact telegram :  https://t.me/nimi012 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using System.Threading;


namespace cAlgo.Robots
{
    //Set time zone to Eastern Standard Time EP9-Best time to trade
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ScannerAlerteRSISTOCHPIVOT : Robot
    {
        //Create Parameters EP10-Functions and Parameters

        [Parameter("Symbol Selection Method", DefaultValue = SymbolSelectionMethodType.WatchList, Group = "Symbol Management")]
        public SymbolSelectionMethodType SymbolSelectionMethod { get; set; }
        public enum SymbolSelectionMethodType
        {
            CurrentChart,
            SymbolList,
            WatchList
        }

        [Parameter("Symbol List", DefaultValue = "USDHUF USDCZK NOKSEK NOKJPY EURZAR EURCZK USDPLN EURNOK USDSEK NZDCAD CHFJPY GBPJPY EURJPY NZDUSD GBPCHF GBPAUD EURCHF AUDCAD GBPUSD", Group = "Symbol Management")]
        public string TradedSymbols { get; set; }
        [Parameter("Watchlist Name", DefaultValue = "EUR", Group = "Symbol Management")]
        public string WatchlistName { get; set; }

        [Parameter("Token", DefaultValue = "Your Token", Group = "Telegram")]
        public string TelegramToken { get; set; }
        [Parameter("Id", DefaultValue = "Your id", Group = "Telegram")]
        public string TelegramId { get; set; }

        ///////////////////////////////////////////////

        [Parameter("Rsi Alerte", DefaultValue = true, Group = "RSI")]
        public bool RsiAlerte { get; set; }
        [Parameter("PeriodRSI", DefaultValue = 14, Group = "RSI")]
        public int PeriodRSI { get; set; }

        [Parameter("ALerte Cross Down", DefaultValue = 70, Group = "RSI")]
        public int ALerteCrossDownRSI { get; set; }
        [Parameter("ALerte Cross Up", DefaultValue = 30, Group = "RSI")]
        public int ALerteCrossUpRSI { get; set; }


        [Parameter("StochAlerte", DefaultValue = true, Group = "STOCH")]
        public bool StochAlerte { get; set; }
        [Parameter("PeriodK", DefaultValue = 255, Group = "STOCH")]
        public int PeriodK { get; set; }
        [Parameter("Kslow", DefaultValue = 3, Group = "STOCH")]
        public int Kslow { get; set; }
        [Parameter("PeriodD", DefaultValue = 3, Group = "STOCH")]
        public int PeriodD { get; set; }
        [Parameter("MaType", DefaultValue = 1, Group = "STOCH")]
        public MovingAverageType MaType { get; set; }

        [Parameter("ALerte Cross Down", DefaultValue = 95, Group = "STOCH")]
        public int ALerteCrossDownSTOCH { get; set; }
        [Parameter("ALerte Cross Up", DefaultValue = 05, Group = "STOCH")]
        public int ALerteCrossUpSTOCH { get; set; }


        [Parameter("PivotMode", DefaultValue = cAlgo.PivotPointAIO.ModeType.Camarilla, Group = "PIVOT")]
        public cAlgo.PivotPointAIO.ModeType PivotMode { get; set; }
        [Parameter("PivotMode", DefaultValue = "Daily", Group = "PIVOT")]
        public TimeFrame PivotTF { get; set; }

        //Create indicator variables EP5-ATR
        private RelativeStrengthIndex[] rsi;
        private StochasticOscillator[] stoch;
        private PivotPointAIO[] pivot;

        private string botName;
        private Symbol[] TradeList;
        private int barsBack;
        private DateTime startTime;
        private int[] lastLongCloseIndex;
        private int[] lastShortCloseIndex;
        private bool[] longSlLevelReached;
        private bool[] shortSlLevelReached;
        private int[] martingaleStep;
        private DateTime[] LookBackOverBuy, LookBackOverSell;

        protected override void OnStart()
        {
            if (SymbolSelectionMethod == SymbolSelectionMethodType.WatchList)
            {
                // Get the trade list from the watchlist provided by the user
                foreach (Watchlist w in Watchlists)
                {
                    if (w.Name == WatchlistName.ToUpper())
                    {
                        TradeList = Symbols.GetSymbols(w.SymbolNames.ToArray());
                    }
                }
            }
            else if (SymbolSelectionMethod == SymbolSelectionMethodType.SymbolList)
            {
                // Get the trade list from the sysmbol list provided by the user
                string[] SymbolList = TradedSymbols.Split(' ');

                TradeList = Symbols.GetSymbols(SymbolList);
            }
            else
            {
                TradeList = new Symbol[1];
                TradeList[0] = Symbol;
            }

            // Get all available symbols
            //TradeList = new Symbol[Symbols.Count];

            rsi = new RelativeStrengthIndex[TradeList.Length];
            stoch = new StochasticOscillator[TradeList.Length];
            pivot = new PivotPointAIO[TradeList.Length];

            lastLongCloseIndex = new int[TradeList.Length];
            lastShortCloseIndex = new int[TradeList.Length];
            longSlLevelReached = new bool[TradeList.Length];
            shortSlLevelReached = new bool[TradeList.Length];
            martingaleStep = new int[TradeList.Length];

            LookBackOverBuy = new DateTime[TradeList.Length];
            LookBackOverSell = new DateTime[TradeList.Length];

            // We subscribe to the bar event for each symbol for the current timeframe

            Print("{0} traded symbols: ", TradeList.Length);
            int i = 0;
            foreach (var symbol in TradeList)
            {
                Print(symbol.Name);

                var bars = MarketData.GetBars(TimeFrame, symbol.Name);

                bars.BarOpened += OnBarOpened;
                barsBack = 1;

                //Load indicators on start up EP5-ATR
                rsi[i] = Indicators.RelativeStrengthIndex(bars.ClosePrices, PeriodRSI);
                stoch[i] = Indicators.StochasticOscillator(bars, PeriodK, Kslow, PeriodD, MaType);
                pivot[i] = Indicators.GetIndicator<PivotPointAIO>(bars, PivotMode, PivotTF);

                lastLongCloseIndex[i] = 0;
                lastShortCloseIndex[i] = 0;
                longSlLevelReached[i] = false;
                shortSlLevelReached[i] = false;
                martingaleStep[i] = 0;
                LookBackOverBuy[i] = bars.OpenTimes.Last(150 + 2);
                LookBackOverSell[i] = bars.OpenTimes.Last(150 + 2);

                i++;
            }
            //subscribe to position close event - to move stoploss to Breakeven for the 2nd Trade
            Positions.Closed += OnPositionClosed;
            //Get Name of Bot and Currency pair of current instance EP15-Deploy
            botName = GetType().ToString();
            startTime = DateTime.Now;
        }
        private void OnBarOpened(BarOpenedEventArgs obj)
        {
            DoEntry(obj.Bars);
            DoExit(obj.Bars);
        }
        private void DoEntry(Bars bars)
        {
            if (Monitor.TryEnter(this))
            {
                try
                {
                    int symbolIndex = Array.FindIndex(TradeList, x => x.Name == bars.SymbolName);
                    Symbol symbol = Array.Find(TradeList, x => x.Name == bars.SymbolName);
                    if (symbol != null)
                    {
                        // Put your core logic here EP7-MACD and EP8-Custom Indicators
                        var ClosePrice = bars.ClosePrices.Last(barsBack);
                        var PrevClosePrice = bars.ClosePrices.Last(barsBack + 1);
                        var Prev2ClosePrice = bars.ClosePrices.Last(barsBack + 2);
                        var LowPrice = bars.LowPrices.Last(barsBack);
                        var PrevLowPrice = bars.LowPrices.Last(barsBack + 1);
                        var HighPrice = bars.HighPrices.Last(barsBack);
                        var PrevHighPrice = bars.HighPrices.Last(barsBack + 1);

                        //Check for Entry Signal
                        {
                            var BuyTriggerRsi = rsi[symbolIndex].Result.Last(barsBack) < ALerteCrossDownRSI && rsi[symbolIndex].Result.Last(barsBack + 1) > ALerteCrossDownRSI;
                            var SellTriggerRsi = rsi[symbolIndex].Result.Last(barsBack) > ALerteCrossUpRSI && rsi[symbolIndex].Result.Last(barsBack + 1) < ALerteCrossUpRSI;

                            var BuyTriggerStoch = stoch[symbolIndex].PercentK.Last(barsBack) < ALerteCrossDownSTOCH && stoch[symbolIndex].PercentK.Last(barsBack + 1) > ALerteCrossDownSTOCH;
                            var SellTriggerStoch = stoch[symbolIndex].PercentK.Last(barsBack) > ALerteCrossUpSTOCH && stoch[symbolIndex].PercentK.Last(barsBack + 1) < ALerteCrossUpSTOCH;


                            var PivotSup = pivot[symbolIndex].GetNextHigherLevel(Bars.ClosePrices.Last(0));
                            var PivotInf = pivot[symbolIndex].GetNextLowerLevel(Bars.ClosePrices.Last(0));

                            if (BuyTriggerRsi && RsiAlerte)
                            {
                                if (IsBacktesting)
                                    Open(TradeType.Buy, bars, 3, 3, botName);
                                Notify(TradeType.Buy, "RSI Buy : Cross Up " + ALerteCrossUpRSI, symbol, PivotSup, PivotInf);
                            }
                            if (SellTriggerRsi && RsiAlerte)
                            {
                                if (IsBacktesting)
                                    Open(TradeType.Sell, bars, 3, 3, botName);
                                Notify(TradeType.Sell, "RSI Sell : Cross Down " + ALerteCrossDownRSI, symbol, PivotSup, PivotInf);
                            }
                            if (BuyTriggerStoch && StochAlerte)
                            {
                                if (IsBacktesting)
                                    Open(TradeType.Buy, bars, 3, 3, botName);
                                Notify(TradeType.Buy, "STOCH Buy : Cross Up " + ALerteCrossUpSTOCH, symbol, PivotSup, PivotInf);
                            }
                            if (SellTriggerStoch && StochAlerte)
                            {
                                if (IsBacktesting)
                                    Open(TradeType.Sell, bars, 3, 3, botName);
                                Notify(TradeType.Sell, "STOCH Sell : Cross Down " + ALerteCrossDownSTOCH, symbol, PivotSup, PivotInf);
                            }
                        }
                    }
                }
                finally
                {
                    Monitor.Exit(this);
                }
            }
        }
        private void DoExit(Bars bars)
        {
            if (Monitor.TryEnter(this))
            {
                try
                {
                    int symbolIndex = Array.FindIndex(TradeList, x => x.Name == bars.SymbolName);
                    Symbol symbol = TradeList[symbolIndex];
                    if (symbol != null)
                    {
                        var ClosePrice = bars.ClosePrices.Last(barsBack);
                        var LowPrice = bars.LowPrices.Last(barsBack);
                        var HighPrice = bars.HighPrices.Last(barsBack);
                    }
                }
                finally
                {
                    Monitor.Exit(this);
                }
            }
        }
        private void Open(TradeType TradeType, Bars bars, double? SLpips, double? TPpips, string Label)
        {
            int symbolIndex = Array.FindIndex(TradeList, x => x.Name == bars.SymbolName);
            Symbol symbol = TradeList[symbolIndex];
            if (Positions.FindAll(Label, symbol.Name, TradeType).Length == 0 )
            {
                int index = bars.Count - 1;
                TradeResult result;
                result = ExecuteMarketOrder(TradeType, symbol.Name, symbol.VolumeInUnitsMin, Label, SLpips, TPpips);
            }
        }
        //Function for closing trades - EP10-Functions and Parameters
        private void Close(TradeType TradeType, string symbolName, string Label)
        {
            if (symbolName != "")
            {
                foreach (var position in Positions.FindAll(Label, symbolName, TradeType))
                    ClosePosition(position);

                foreach (var order in PendingOrders.Where(x => x.Label == Label && x.SymbolName == symbolName && x.TradeType == TradeType))
                    CancelPendingOrder(order);
            }
            else
            {
                foreach (var symbol in TradeList)
                {
                    foreach (var position in Positions.FindAll(Label, symbol.Name, TradeType))
                        ClosePosition(position);

                    foreach (var order in PendingOrders.Where(x => x.Label == Label && x.SymbolName == symbol.Name && x.TradeType == TradeType))
                        CancelPendingOrder(order);
                }
            }
        }
        protected override void OnStop()
        {
            var stopTime = DateTime.Now;
            Print("Execution time: {0}", stopTime - startTime);
            // Close all positions except in real-time (for better results in backtesting or optimization)
            if (RunningMode != RunningMode.RealTime)
            {
                Close(TradeType.Buy, "", botName);
                Close(TradeType.Sell, "", botName);
            }
            // Print more details of individual results per symbol
            if ((RunningMode == RunningMode.SilentBacktesting || RunningMode == RunningMode.VisualBacktesting) && History.Count > 0)
            {
                double totalProfit = 0.0;
                foreach (var sym in TradeList)
                {
                    int lostBull = 0;
                    int lostBear = 0;
                    int wonBull = 0;
                    int wonBear = 0;
                    double wonPips = 0.0;
                    double lostPips = 0.0;
                    double profit = 0.0;
                    foreach (var hist in History.Where(x => x.Label == botName && x.SymbolName == sym.Name))
                    {
                        profit += hist.NetProfit;
                        if (hist.Pips >= 0)
                        {
                            wonPips += hist.Pips;
                            if (hist.TradeType == TradeType.Buy)
                            {
                                wonBull++;
                            }
                            else
                            {
                                wonBear++;
                            }
                        }
                        else
                        {
                            lostPips += hist.Pips;
                            if (hist.TradeType == TradeType.Buy)
                            {
                                lostBull++;
                            }
                            else
                            {
                                lostBear++;
                            }
                        }
                    }
                }
            }
        }

        /////////////////////////////////////////////////////// On position Close + Management Martingale /////////////////////////////////////////////////////
        private void OnPositionClosed(PositionClosedEventArgs args)
        {
            int symbolIndex = Array.FindIndex(TradeList, x => x.Name == args.Position.SymbolName);
        }

        private void Notify(TradeType tradeType, string action, Symbol symbolname, cAlgo.PivotPointAIO.LevelType pivotSup, cAlgo.PivotPointAIO.LevelType pivotInf)
        {
            var direction = tradeType == TradeType.Buy ? "BUY " : "SELL ";
            var notification = action + direction + SymbolName;
            var message = "Entry " + direction + symbolname + " on " + TimeFrame.ToString() + "\n\n ==>Bot : SCANNER \n" + action + " " + "\n\nPivot Sup : " + pivotSup + "\nPivot Inf : " + pivotInf;
            // send email
            // send Telegram
            var Token = TelegramToken;
            var Id = TelegramId;

            var result = Http.Get($"https://api.telegram.org/bot{Token}/sendMessage?chat_id={Id}&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

        }
    }
}


YE
YesOrNot2

Joined on 17.05.2024

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Scanner telegram Alerte RSI and Stoch.algo
  • Rating: 0
  • Installs: 296
  • Modified: 27/07/2024 12:25
Comments
Log in to add a comment.
No comments found.