Category Other  Published on 13/02/2021

Break-even line

Description

Break-even indicator, shows the break-even price on the chart.

It can also show the break-even price of only Buy or Sell positions.

The break-even price includes:

  1. Commissions
  2. Spread
  3. Swap

Also take a look at my copy trading service on cTrader, click here.


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


namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class BreakEvenLine : Indicator
    {

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }


        protected override void Initialize()
        {
            //DisplayStatusOnChart();
        }

        public override void Calculate(int index)
        {
            // Calculate value at specified index
            // Result[index] = ...
            DisplayStatusOnChart();
        }


        private void DisplayStatusOnChart()
        {
            if (CalculateAveragePositionPrice() > 0)
            {
                Chart.DrawHorizontalLine("be", CalculateAveragePositionPrice(), Color.Gold, 2, LineStyle.LinesDots);
            }
            else
            {
                Chart.RemoveObject("be");
            }

        }


        private double CalculateAveragePositionPrice()
        {
            double result = 0;
            double lots = 0;
            double equity = 0;
            double point = 0;
            double cop = 0;
            double val = 0;
            double comm = 0;

            foreach (var position in Positions)
            {
                if (position.SymbolName == SymbolName)
                {
                    equity += position.NetProfit;
                    if (position.TradeType == TradeType.Buy)
                    {
                        lots += position.VolumeInUnits;
                    }
                    else if (position.TradeType == TradeType.Sell)
                    {
                        lots -= position.VolumeInUnits;
                    }
                }
            }

            if (lots != 0)
            {

                point = Symbol.TickSize;
                cop = lots * Symbol.TickValue;
                val = Symbol.Bid - (point * equity / cop);
                result = Math.Round(val, Symbol.Digits);

            }

            return result;
        }
    }
}


DeepMethod's avatar
DeepMethod

Joined on 31.10.2020

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: BreakEven Line.algo
  • Rating: 3.75
  • Installs: 2627
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
DA
davidiesis · 2 years ago

Hi, I tried to install your indicator but I get an error from cTrader:

Error CS0219: The variable 'comm' is assigned but its value is never used (line:54, column:20)