Max Loss, Max Profit Live Stats

by astevani in category Other at 10/05/2019
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

Download
98 downloads
How to install
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
// -------------------------------------------------------------------------------------------------
//
//    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 actualPips = 0;
            double toMaxLossPips = 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;

                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;
                    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;
                    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} Missing {3} - Take Profit", maxProfitNet, maxProfitGross, toMaxProfitPips, missingTakeProfit);
            sb.AppendLine();
            sb.AppendFormat("Net: {0:N2}  Gross: {1:N2}  Pips {2:N1} Missing {3} - Stop Loss", maxLossNet, maxLossGross, toMaxLossPips, missingStopLoss);
            sb.AppendLine();
            sb.AppendFormat("Net: {0:N2}  Gross: {1:N2}  Pips {2:N1} - Actual", actualNet, actualGross, actualPips);
            Chart.DrawStaticText("StatInfo", sb.ToString(), VerticalAlignment.Top, HorizontalAlignment.Left, Color.White);
        }
    }
}
Comments
5