Category Other  Published on 29/06/2023

IR Manager(Percent)

Description

this bot close position in daily percent or step percent

this bot could modify tp and sl for all position based on your balance percent.

Telegram Channle

support us for more free indicator and bot by sign up in LiteFinance broker from this link

LiteFinance Signup


using System;
using System.Linq;
using cAlgo.API;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class IRManagerPercent : Robot
    {


        //Set Tp-Sl Parameter

        [Parameter("Modify TP ", DefaultValue = true)]
        public bool TpOption { get; set; }

        [Parameter("Take Profit Percent", DefaultValue = 2)]
        public double TpPercent { get; set; }

        [Parameter("Modify Sl ", DefaultValue = true)]
        public bool SlOption { get; set; }

        [Parameter("Stop Loss Profit Percent", DefaultValue = 1)]
        public double SlPercent { get; set; }


        //Step Parameter

        [Parameter("Step TP Percent", DefaultValue = true, Group = "Step Percent")]
        public bool StepTpPercentOption { get; set; }

        [Parameter("Step TP Percent", DefaultValue = 1, Group = "Step Percent")]
        public double StepTpPercent { get; set; }

        [Parameter("Step SL Percent", DefaultValue = true, Group = "Step Percent")]
        public bool StepSlPercentOption { get; set; }

        [Parameter("Step Sl Percent", DefaultValue = -1, Group = "Step Percent")]
        public double StepSlPercent { get; set; }

        //Daily Parameter

        [Parameter("Daily TP Percent", DefaultValue = true, Group = "Daily Percent")]
        public bool DailyTpPercentOption { get; set; }

        [Parameter("Daily TP Percent", DefaultValue = 2, Group = "Daily Percent")]
        public double DailyTpPercent { get; set; }

        [Parameter("Daily SL Percent", DefaultValue = true, Group = "Daily Percent")]
        public bool DailySlPercentOption { get; set; }

        [Parameter("Daily Sl Percent", DefaultValue = -1, Group = "Daily Percent")]
        public double DailySlPercent { get; set; }




        protected override void OnStart()
        {

            Settpsl();

            Positions.Modified += PositionsOnModified;
            Positions.Opened += PositionsOnOpened;
            Positions.Closed += PositionsOnClosed;
            PendingOrders.Filled += PendingOrdersOnFilled;

        }

        protected override void OnTick()
        {


            #region Step Percent
            //calc STEP Percent Profit

            double StepPercent = ((Account.Equity / Account.Balance) - 1) * 100;

            foreach (var position in Positions)
            {
                if (StepPercent > StepTpPercent && StepTpPercentOption)
                {
                    ClosePositionAsync(position);
                }

                if (StepPercent < StepSlPercent && StepSlPercentOption)
                {
                    ClosePositionAsync(position);
                }


            }

            foreach (var order in PendingOrders)
            {
                if (StepPercent > StepTpPercent && StepTpPercentOption)
                {
                    CancelPendingOrderAsync(order);
                }

                if (StepPercent < StepSlPercent && StepSlPercentOption)
                {
                    CancelPendingOrderAsync(order);
                }
            }

            #endregion

            #region Daily percent
            double DailyNet1 = Positions.Sum(p => p.NetProfit);
            double DailyNet2 = History.Where(x => x.ClosingTime.Date == Time.Date).Sum(x => x.NetProfit);
            double DailyNet = Math.Round(DailyNet1 + DailyNet2, 2);

            // get Starting Balance
            var oldTrades = History.Where(x => x.ClosingTime.Date != Time.Date).OrderBy(x => x.ClosingTime).ToArray();
            double StartingBalance;

            if (oldTrades.Length == 0)
            {
                StartingBalance = History.Count == 0 ? Account.Balance : History.OrderBy(x => x.ClosingTime).First().Balance;
            }
            else
            {
                StartingBalance = oldTrades.Last().Balance;
            }


            //calc Daily Percent Profit

            double DailyPercent = DailyNet / StartingBalance * 100;

            foreach (var position in Positions)
            {

                if (DailyPercent > DailyTpPercent && DailyTpPercentOption)
                {
                    ClosePositionAsync(position);
                }

                if (DailyPercent < DailySlPercent && DailySlPercentOption)
                {
                    ClosePositionAsync(position);
                }

            }

            foreach (var order in PendingOrders)
            {
                if (DailyPercent > DailyTpPercent && DailyTpPercentOption)
                {
                    CancelPendingOrderAsync(order);
                }

                if (DailyPercent < DailySlPercent && DailySlPercentOption)
                {
                    CancelPendingOrderAsync(order);
                }

            }

            #endregion


        }

        #region Set TPSL
        private void PositionsOnModified(PositionModifiedEventArgs obj)
        {
            Settpsl();

        }

        private void PositionsOnOpened(PositionOpenedEventArgs args)
        {
            Settpsl();

        }
        private void PositionsOnClosed(PositionClosedEventArgs args)
        {
            Settpsl();
        }
        private void PendingOrdersOnFilled(PendingOrderFilledEventArgs args)
        {
            Settpsl();
        }


        private void Settpsl()
        {


            foreach (var position in Positions)
            {
                var smValue = Symbols.GetSymbol(position.SymbolName).PipValue;

                var smSize = Symbols.GetSymbol(position.SymbolName).PipSize;

                double tpcalc = Math.Round((((Account.Balance * (1 + TpPercent / 100)) - Account.Balance) - (2 * position.Commissions) + position.Swap) / (smValue * position.VolumeInUnits), 1);

                double slcalc = Math.Round((Account.Balance - (Account.Balance * (1 - SlPercent / 100)) + (2 * position.Commissions) + position.Swap) / (smValue * position.VolumeInUnits), 1);

                Print(smValue * position.VolumeInUnits);
                if (position.TradeType == TradeType.Buy)
                {
                    var tp = position.EntryPrice + tpcalc * smSize;
                    var sl = position.EntryPrice - slcalc * smSize;


                    if (TpOption && position.TakeProfit != tp)
                    {
                        ModifyPosition(position, position.StopLoss, tp);
                    }
                    if (SlOption && position.StopLoss != sl)
                    {
                        ModifyPosition(position, sl, position.TakeProfit);
                    }
                }



           

            if (position.TradeType == TradeType.Sell)
            {
                var tp = position.EntryPrice - tpcalc * smSize;
                var sl = position.EntryPrice + slcalc * smSize;


                if (TpOption && position.TakeProfit != tp)
                {
                    ModifyPosition(position, position.StopLoss, tp);
                }
                if (SlOption && position.StopLoss != sl)
                {
                    ModifyPosition(position, sl, position.TakeProfit);
                }
            }
        }

    }
    #endregion
}
}


IR
IRCtrader

Joined on 17.06.2021

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: IR Manager(Percent).algo
  • Rating: 5
  • Installs: 159
  • Modified: 14/07/2024 17:01
Comments
Log in to add a comment.
No comments found.