Category Other  Published on 19/12/2016

Net profit (PnL) shown in percent

Description

This is a simple indicator. It shows the PnL in %. I think it makes much more sense than a value, but cTrader hasn't implemented something that simple.

Updated: It now also shows the PnL of the whole account.


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

namespace cAlgo
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class PLPercentage : Indicator
    {
        public override void Calculate(int index)
        {
            if (IsLastBar)
                DisplayPLOnChart();
        }

        private void DisplayPLOnChart()
        {
            var symbolPositions = Positions.Where(t => t.SymbolCode == Symbol.Code);
            var symbolPnL = Math.Round(100 * symbolPositions.Sum(t => t.NetProfit) / Account.Balance, 2);
            var accountPnL = Math.Round(100 * Positions.Sum(t => t.NetProfit) / Account.Balance, 2);

            var symbolTextDisplayed = Symbol.Code + " PnL: " + symbolPnL + "% ";
            ChartObjects.DrawText(Symbol.Code + "_SymbolPnL", symbolTextDisplayed, StaticPosition.TopRight, symbolPnL >= 0 ? Colors.Green : Colors.Red);

            var accountTextDisplayed = "\nAccount PnL: " + accountPnL + "% ";
            ChartObjects.DrawText(Symbol.Code + "_AccountPnL", accountTextDisplayed, StaticPosition.TopRight, accountPnL >= 0 ? Colors.Green : Colors.Red);
        }
    }
}


BR
brunoamancio.ti

Joined on 20.08.2016

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: PL Percentage.algo
  • Rating: 5
  • Installs: 3459
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
CL
clctrader · 2 years ago

Great stuff.

I've updated to meet with the evolution of the API and stop throwing "obsolete" warnings.

 

 

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class PLPercentageupdated : Indicator
    {
        public override void Calculate(int index)
        {
            if (IsLastBar)
                DisplayPLOnChart();
        }

        private void DisplayPLOnChart()
        {
            var symbolPositions = Positions.Where(t => t.SymbolName == Symbol.Name);
            var symbolPnL = Math.Round(100 * symbolPositions.Sum(t => t.NetProfit) / Account.Balance, 2);
            var accountPnL = Math.Round(100 * Positions.Sum(t => t.NetProfit) / Account.Balance, 2);

            var symbolTextDisplayed = SymbolName + " PnL: " + symbolPnL + "% ";
            Chart.DrawStaticText("Symbol PnL", symbolTextDisplayed, VerticalAlignment.Top, HorizontalAlignment.Right, symbolPnL >= 0 ? Color.Green : Color.Red);

            var accountTextDisplayed = "\nAccount PnL: " + accountPnL + "% ";
            Chart.DrawStaticText("Account PnL", accountTextDisplayed, VerticalAlignment.Top, HorizontalAlignment.Right, accountPnL >= 0 ? Color.Green : Color.Red);
        }
    }
}