Showing breakeven on the chart

Created at 27 Nov 2019, 13:16
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
FR

freedoomltr

Joined 27.11.2019

Showing breakeven on the chart
27 Nov 2019, 13:16


Hi there,

 

Anyone knows if there is an indicator that shows a breakeven line on the charts?

 

Thank you


@freedoomltr
Replies

freedoomltr
03 Dec 2019, 13:00

No one with this?


@freedoomltr

ctid1574514
05 Dec 2019, 04:35

RE:

freedoomltr said:

No one with this?

Try this.  It is the first indicator I have done 

 

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 breakeven : Indicator
    {

        [Parameter("Buy & Sell Lines")]
        public bool BuySell { get; set; }

        [Parameter("Average Line")]
        public bool Avg { get; set; }

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

        private double AvgPrice;
        private double BuyPrice;
        private double SellPrice;


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

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


        private void DisplayStatusOnChart()
        {
            if (Avg && Positions.Count(x => x.SymbolName == SymbolName) > 1)
            {
                Chart.DrawHorizontalLine("apoint", CalculateAveragePositionPrice(), Color.Yellow, 2, LineStyle.Dots);
            }
            else
                Chart.RemoveObject("apoint");
            if (BuySell && Positions.Count(x => x.TradeType == TradeType.Buy && x.SymbolName == SymbolName) > 1)
            {
                Chart.DrawHorizontalLine("bpoint", CalculateAveragePositionPriceSplit(TradeType.Buy), Color.Lime, 2, LineStyle.Dots);
            }
            else
                Chart.RemoveObject("bpoint");
            if (BuySell && Positions.Count(x => x.TradeType == TradeType.Sell && x.SymbolName == SymbolName) > 1)
            {
                Chart.DrawHorizontalLine("spoint", CalculateAveragePositionPriceSplit(TradeType.Sell), Color.LightPink, 2, LineStyle.Dots);
            }
            else
            {
                Chart.RemoveObject("spoint");
            }

            Chart.DrawStaticText("pan", GenerateStatusText(), VerticalAlignment.Top, HorizontalAlignment.Left, Color.Tomato);
        }


        private double CalculateAveragePositionPrice()
        {
            double result = 0;
            double averagePrice = 0;
            double count = 0;
            AvgPrice = 0;

            foreach (var position in Positions)
            {
                if (position.SymbolName == SymbolName)
                {
                    averagePrice += position.EntryPrice * position.VolumeInUnits;
                    count += position.VolumeInUnits;
                    AvgPrice += position.NetProfit;
                }
            }

            if (averagePrice > 0 && count > 0)
            {
                result = Math.Round(averagePrice / count, Symbol.Digits);
            }
            return result;
        }


        private double CalculateAveragePositionPriceSplit(TradeType tradeType)
        {
            double result = 0;
            double averagePrice = 0;
            double count = 0;
            BuyPrice = 0;
            SellPrice = 0;

            foreach (var position in Positions)
            {
                if (position.SymbolName == SymbolName)
                {
                    if (position.TradeType == tradeType)
                    {
                        averagePrice += position.EntryPrice * position.VolumeInUnits;
                        count += position.VolumeInUnits;
                    }
                    if (position.TradeType == TradeType.Buy)
                    {
                        BuyPrice += position.NetProfit;
                    }
                    else if (position.TradeType == TradeType.Sell)
                    {
                        SellPrice += position.NetProfit;
                    }
                }
            }

            if (averagePrice > 0 && count > 0)
            {
                result = Math.Round(averagePrice / count, Symbol.Digits);
            }
            return result;
        }

        private string GenerateStatusText()
        {
            var statusText = "";
            if (Avg)
            {
                statusText += "Average |" + Math.Round(AvgPrice, 2) + "\n";
            }
            if (BuySell)
            {
                statusText += "Buy |" + Math.Round(BuyPrice, 2) + "\n";
                statusText += "Sell |" + Math.Round(SellPrice, 2) + "\n";
            }
            return statusText;
        }
    }
}

 


@ctid1574514

ctid1574514
05 Dec 2019, 04:41

RE:

deleted


@ctid1574514

freedoomltr
05 Dec 2019, 12:18

RE: RE:

ctid1574514 said:

deleted

thanks a lot!

Where can I add this? Does it include the swap?


@freedoomltr

ctid1574514
05 Dec 2019, 12:33

RE: RE: RE:

freedoomltr said:

ctid1574514 said:

deleted

thanks a lot!

Where can I add this? Does it include the swap?

 

Create a new indicator and paste the code in to it and click the build icon. 

The <code>  at the start and </code> at the end should not entered 

No it does not include the swap.  It just works out where the average price it.


@ctid1574514

freedoomltr
05 Dec 2019, 13:46

RE: RE: RE: RE:

ctid1574514 said:

freedoomltr said:

ctid1574514 said:

deleted

thanks a lot!

Where can I add this? Does it include the swap?

 

Create a new indicator and paste the code in to it and click the build icon. 

The <code>  at the start and </code> at the end should not entered 

No it does not include the swap.  It just works out where the average price it.

Thanks!

 

If anyone have a code with the swap and commissions included let me know.


@freedoomltr

ctid1574514
05 Dec 2019, 18:00

RE: RE: RE: RE: RE:

freedoomltr said:

If anyone have a code with the swap included let me know.

 

Not sure how to add it.  I use the line as a rough visual guide in a cbot and close with the exact profit value that has swap, commission spread etc.

I have added text top right with the value if you close the trade if this helps. 

Updated the Code above


@ctid1574514

freedoomltr
05 Dec 2019, 18:58

RE: RE: RE: RE: RE: RE:

ctid1574514 said:

freedoomltr said:

If anyone have a code with the swap included let me know.

 

Not sure how to add it.  I use the line as a rough visual guide in a cbot and close with the exact profit value that has swap, commission spread etc.

I have added text top right with the value if you close the trade if this helps. 

Updated the Code above

Thanks


@freedoomltr