Today & Live Profit v2 free

by Amin.fx in category Other at 27/10/2021
Description

Show live trading profit, profit percentage

Show daily profit net and daily percentage

Show equity 

You can also choose to have the indicator displayed on the main Chart or in the bottom windows

Changes in this version

Fix Bug

Decrease parameters

Better display

Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgocTrader

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.IranStandardTime, AccessRights = AccessRights.None)]
    public class TodayProfitV2 : Indicator
    {
        [Parameter("SubWindows", DefaultValue = 0, MinValue = 0, Group = "Text")]
        public int SubWindows { get; set; }

        [Parameter("Font Size", DefaultValue = 12, Group = "Text")]
        public int FontSize { get; set; }

        [Parameter("Font Weight", DefaultValue = 11, Group = "Text")]
        public FontWeight FontWeight1 { get; set; }

        [Parameter("Shift", DefaultValue = "4 3 0 0", Group = "Text")]
        public string ShiftText { get; set; }

        [Parameter("Text Alignment", DefaultValue = TextAlignment.Left, Group = "Text")]
        public TextAlignment Alignment { get; set; }

        [Parameter("Today", DefaultValue = "White", Group = "Text Color")]
        public string TodayColor { get; set; }

        [Parameter("Positive Live", DefaultValue = "Green", Group = "Text Color")]
        public string PositiveColor { get; set; }

        [Parameter("Negative Live", DefaultValue = "Red", Group = "Text Color")]
        public string NegativeColor { get; set; }


        private TextBlock[] textblock = new TextBlock[3];
        private static bool Hide;
        private StackPanel ProfitPanel;
        private ToggleButton togglebutton;
        private bool One = false;
        private double StartingBalance = 0, DailyNetHistory = 0;


        protected override void Initialize()
        {
            var AllPanel = new DockPanel 
            {
                VerticalAlignment = VerticalAlignment.Bottom,
                HorizontalAlignment = HorizontalAlignment.Right,
                BackgroundColor = Chart.ColorSettings.BackgroundColor
            };

            togglebutton = new ToggleButton 
            {
                Text = "☰",
                Dock = Dock.Right,
                FontSize = 13,
                FontWeight = FontWeight.Bold,
                VerticalAlignment = VerticalAlignment.Bottom,
                Width = 21,
                Height = 21,
                Margin = 4,
                Padding = 1
            };
            togglebutton.Click += ToggleButton_Click;
            AllPanel.AddChild(togglebutton);

            ProfitPanel = new StackPanel 
            {
                Margin = "0 0 2 5",
                Dock = Dock.Right
            };

            for (int i = 0; i < 3; i++)
            {
                textblock[i] = new TextBlock 
                {
                    FontSize = FontSize,
                    FontWeight = FontWeight1,
                    TextAlignment = Alignment,
                    Margin = ShiftText
                };
                ProfitPanel.AddChild(textblock[i]);
            }
            textblock[2].ForegroundColor = TodayColor;
            AllPanel.AddChild(ProfitPanel);

            if (SubWindows > Chart.IndicatorAreas.Count)
                SubWindows = Chart.IndicatorAreas.Count;
            if (SubWindows > 0)
                Chart.IndicatorAreas[SubWindows - 1].AddControl(AllPanel);
            else
                Chart.AddControl(AllPanel);
            //DailyProfit
            if (History.Count > 0)
                DailyNetHistory = History.Where(x => x.ClosingTime.Date == Time.Date).Sum(x => x.NetProfit);
            var oldTrades = History.Where(x => x.ClosingTime.Date != Time.Date).OrderBy(x => x.ClosingTime).ToArray();
            if (oldTrades.Length > 0)
                StartingBalance = History.Where(x => x.ClosingTime.Date != Time.Date).OrderBy(x => x.ClosingTime).Last().Balance;

            if (Positions.Count > 0 && Hide == false)
                Timer.Start(TimeSpan.FromMilliseconds(500));
            else
                DailyProfit();

            if (Hide == true)
            {
                togglebutton.IsChecked = true;
                ProfitPanel.IsVisible = false;
            }

            Positions.Opened += Positions_Opened;
            Positions.Closed += Positions_Closed;
        }


        private void Positions_Opened(PositionOpenedEventArgs obj)
        {
            if (Positions.Count == 1 && Hide == false)
                Timer.Start(TimeSpan.FromMilliseconds(500));
        }


        private void Positions_Closed(PositionClosedEventArgs obj)
        {
            DailyNetHistory = History.Where(x => x.ClosingTime.Date == Time.Date).Sum(x => x.NetProfit);
            if (Positions.Count == 0 && Hide == false)
            {
                Timer.Stop();
                DailyProfit();
            }
        }


        protected override void OnTimer()
        {
            LiveProfit();
            DailyProfit();
        }


        private void ToggleButton_Click(ToggleButtonEventArgs obj)
        {
            if (ProfitPanel.IsVisible == false)
            {
                togglebutton.IsChecked = false;
                Hide = false;
                ProfitPanel.IsVisible = true;
                if (Positions.Count > 0)
                    Timer.Start(TimeSpan.FromMilliseconds(500));
            }

            else
            {
                togglebutton.IsChecked = true;
                Hide = true;
                ProfitPanel.IsVisible = false;
                if (Positions.Count > 0)
                    Timer.Stop();
            }
        }


        public override void Calculate(int index)
        {
        }


        private void DailyProfit()
        {
            //Calc daily Net Profit
            double DailyNetLive = Positions.Sum(p => p.NetProfit);
            var DailyNet = Math.Round(DailyNetLive + DailyNetHistory, 2);
            // get Starting Balance
            if (StartingBalance == 0)
                StartingBalance = Account.Balance - DailyNetHistory;
            //calc Daily Percent Profit
            var DailyPercent = Math.Round(DailyNet / StartingBalance * 100, 2);

            var Space = "";
            if (DailyNet >= 0)
                Space = " ";
            textblock[2].Text = string.Format("Today: $ {0}{1:#,##0.00}/ {0}{2:#,##0.00}%", Space, DailyNet, DailyPercent);


            if (One == false)
            {
                if (DailyNet == 0 && Positions.Count == 0 && ProfitPanel.IsVisible == true)
                {
                    togglebutton.IsChecked = true;
                    ProfitPanel.IsVisible = false;
                }

                else if ((DailyNet > 0 || Positions.Count > 0) && ProfitPanel.IsVisible == false && Hide == false)
                {
                    togglebutton.IsChecked = false;
                    ProfitPanel.IsVisible = true;
                }

                One = true;
            }
        }


        private void LiveProfit()
        {
            var netProfit = Account.UnrealizedNetProfit;
            var PercentProfit = Math.Round(netProfit / Account.Balance * 100, 2);

            var Space = "";
            var netclr = TodayColor;
            //negative
            if (netProfit < 0)
                netclr = NegativeColor;
            //positive && Zero
            else
            {
                netclr = PositiveColor;
                Space = " ";
            }

            textblock[0].Text = string.Format("Live: $ {0}{1:#,##0.00}/ {0}{2:#,##0.00}%", Space, netProfit, PercentProfit);
            textblock[0].ForegroundColor = netclr;

            textblock[1].Text = string.Format("Equity: $ {0:#,##0.00}", Account.Equity);
            textblock[1].ForegroundColor = netclr;
        }
    }
}
Comments
0