Category Other  Published on 27/05/2019

Max Loss, Max Profit Live Stats

Description

This stats panel show the Gross and Net Max Profit and Max Loss of all open positions.

Display also missing take profit and stop loss.

Show the remaing total pips to get the all Take Profits and to Stop Loss.

Show also the actual Gross and Net profit and total Pips.

Enjoy

Amerigo

 

Updated ---- One Bug Fixing, and one improvement, now you can see also average pips...


// -------------------------------------------------------------------------------------------------
//
//    This code is a cAlgo API example.
//    
//    All changes to this file will be lost on next application start.
//    If you are going to modify this file please make a copy using the "Duplicate" command.
//
// -------------------------------------------------------------------------------------------------

using System;
using System.Text;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AutoRescale = true, AccessRights = AccessRights.None)]
    public class PosStats : Indicator
    {
        [Parameter("Porfolio Currency", DefaultValue = "EUR")]
        public string PorfolioCurrency { get; set; }

        protected override void Initialize()
        {
            Positions.Opened += delegate(PositionOpenedEventArgs args) { UpdateInfo(); };
            Positions.Closed += delegate(PositionClosedEventArgs args) { UpdateInfo(); };
            Positions.Modified += delegate(PositionModifiedEventArgs args) { UpdateInfo(); };
        }
        public override void Calculate(int index)
        {
            if (!IsLastBar)
            {
                return;
            }
            else
            {
                UpdateInfo();
            }
        }

        private void UpdateInfo()
        {
            double maxLossNet = 0;
            double maxProfitNet = 0;
            double actualNet = 0;

            double maxLossGross = 0;
            double maxProfitGross = 0;
            double actualGross = 0;

            double toMaxProfitPips = 0;
            double toMaxProfitPipsAvg = 0;
            double ProfitVolume = 0;
            double actualPips = 0;
            double actualPipsAvg = 0;
            double actualVolume = 0;
            double toMaxLossPips = 0;
            double toMaxLossPipsAvg = 0;
            double LossVolume = 0;
            int missingStopLoss = 0;
            int missingTakeProfit = 0;

            foreach (var p in Positions)
            {
                var s = MarketData.GetSymbol(p.SymbolCode);
                Symbol mainPair = null;
                if (p.SymbolCode.Substring(0, 3) != PorfolioCurrency)
                    mainPair = MarketData.GetSymbol(PorfolioCurrency + p.SymbolCode.Substring(0, 3));

                actualGross += p.GrossProfit;
                actualNet += p.NetProfit;
                actualPips += p.Pips;
                actualPipsAvg += p.Pips * p.VolumeInUnits;
                actualVolume += p.VolumeInUnits;

                double stopLossPips = 0, takeProfitPips = 0;
                double sign = p.TradeType == TradeType.Buy ? 1 : -1;

                if (p.StopLoss != null)
                {
                    stopLossPips = sign * ((double)(p.EntryPrice - p.StopLoss)) / s.PipSize;
                    toMaxLossPips += stopLossPips + p.Pips;
                    toMaxLossPipsAvg += (stopLossPips + p.Pips) * p.VolumeInUnits;
                    LossVolume += p.VolumeInUnits;
                    var loss = (double)(sign * (p.VolumeInUnits - p.VolumeInUnits * p.EntryPrice / p.StopLoss));
                    if (mainPair != null)
                        loss /= mainPair.Ask;
                    maxLossGross += loss;
                    maxLossNet += loss + p.Commissions + p.Swap;
                }
                else
                    missingStopLoss++;

                if (p.TakeProfit != null)
                {
                    takeProfitPips = sign * ((double)(p.TakeProfit - p.EntryPrice)) / s.PipSize;
                    toMaxProfitPips += takeProfitPips - p.Pips;
                    toMaxProfitPipsAvg += (takeProfitPips - p.Pips) * p.VolumeInUnits;
                    ProfitVolume += p.VolumeInUnits;
                    var profit = (double)(sign * (p.VolumeInUnits - p.VolumeInUnits * p.EntryPrice / p.TakeProfit));
                    if (mainPair != null)
                        profit /= mainPair.Ask;
                    maxProfitGross += profit;
                    maxProfitNet += profit + p.Commissions + p.Swap;

                }
                else
                    missingTakeProfit++;
            }
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("Net: {0:N2}  Gross: {1:N2}  Pips {2:N1} ({3:N1}) Missing {4} - Take Profit", maxProfitNet, maxProfitGross, ProfitVolume > 0 ? toMaxProfitPipsAvg / ProfitVolume : 0, toMaxProfitPips, missingTakeProfit);
            sb.AppendLine();
            sb.AppendFormat("Net: {0:N2}  Gross: {1:N2}  Pips {2:N1} ({3:N1}) Missing {4} - Stop Loss", maxLossNet, maxLossGross, LossVolume > 0 ? toMaxLossPipsAvg / LossVolume : 0, toMaxLossPips, missingStopLoss);
            sb.AppendLine();
            sb.AppendFormat("Net: {0:N2}  Gross: {1:N2}  Pips {2:N1} ({3:N1}) - Actual", actualNet, actualGross, actualVolume > 0 ? actualPipsAvg / actualVolume : 0, actualPips);
            Chart.DrawStaticText("StatInfo", sb.ToString(), VerticalAlignment.Top, HorizontalAlignment.Left, Color.White);
        }
    }
}


AS
astevani

Joined on 11.03.2019

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Stats.algo
  • Rating: 5
  • Installs: 2088
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
No comments found.