BE Calculator

by cysecsbin.01 in category Trend at 08/07/2019
Description

Follow my cTrader Telegram group at https://t.me/cTraderCommunity; it's a new community but it will grow fast, plus everyone can talk about cTrader indicators and algorithm without restrictions, though it is not allowed to spam commercial indicators to sell them. There's also a Discord Server now @ https://discord.gg/5GAPMtp

This indi calculates BE levels for both long and short position and orders open on the chart.

For any bug report or suggestion, follow my telegram group or comment below

Download
69 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
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System.Collections.Generic;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class BECalculator : Indicator
    {
        [Parameter("Show Long BreakEven", DefaultValue = true)]
        public bool showBuyBE { get; set; }
        [Parameter("Show Short BreakEven", DefaultValue = true)]
        public bool showSellBE { get; set; }

        [Parameter("Show Orders BreakEven", DefaultValue = true)]
        public bool showOrdBE { get; set; }

        private List<Position> Longs = new List<Position>();
        private List<Position> Shorts = new List<Position>();

        private List<PendingOrder> LongOrds = new List<PendingOrder>();
        private List<PendingOrder> ShortOrds = new List<PendingOrder>();

        protected override void Initialize()
        {
        }

        public override void Calculate(int index)
        {
            populateLists();
            CalculateBE();
        }

        private void CalculateBE()
        {
            double LongBE = 0, ShortBE = 0, LongVolSum = 0, ShortVolSum = 0;
            Longs.ForEach(delegate(Position pos)
            {
                LongBE += pos.EntryPrice * pos.VolumeInUnits;
                LongVolSum += pos.VolumeInUnits;
            });
            Shorts.ForEach(delegate(Position pos)
            {
                ShortBE += pos.EntryPrice * pos.VolumeInUnits;
                ShortVolSum += pos.VolumeInUnits;
            });

            if (showBuyBE)
                Chart.DrawHorizontalLine("LongBE", LongBE / LongVolSum, Color.Green);
            if (showSellBE)
                Chart.DrawHorizontalLine("ShortBE", ShortBE / ShortVolSum, Color.DarkRed);

            if (showOrdBE)
            {
                double LongOrdBE = 0, ShortOrdBE = 0, LongOrdVolSum = 0, ShortOrdVolSum = 0;
                LongOrds.ForEach(delegate(PendingOrder ord)
                {
                    LongOrdBE += ord.TargetPrice * ord.VolumeInUnits;
                    LongOrdVolSum += ord.VolumeInUnits;
                });
                ShortOrds.ForEach(delegate(PendingOrder ord)
                {
                    ShortOrdBE += ord.TargetPrice * ord.VolumeInUnits;
                    ShortOrdVolSum += ord.VolumeInUnits;
                });

                if (showBuyBE)
                    Chart.DrawHorizontalLine("LongOrdBE", LongOrdBE / LongOrdVolSum, Color.Green, 1, LineStyle.DotsRare);
                if (showSellBE)
                    Chart.DrawHorizontalLine("ShortOrdBE", ShortOrdBE / ShortOrdVolSum, Color.DarkRed, 1, LineStyle.DotsRare);
            }
        }

        private void populateLists()
        {
            Longs.RemoveAll(x => true);
            Shorts.RemoveAll(x => true);

            LongOrds.RemoveAll(x => true);
            ShortOrds.RemoveAll(x => true);

            foreach (var pos in Positions)
            {
                if (pos.SymbolName == Symbol.Name)
                    if (pos.TradeType == TradeType.Buy)
                        Longs.Add(pos);
                    else
                        Shorts.Add(pos);
            }

            foreach (var ord in PendingOrders)
            {
                if (ord.SymbolCode == Symbol.Name)
                    if (ord.TradeType == TradeType.Buy)
                        LongOrds.Add(ord);
                    else
                        ShortOrds.Add(ord);
            }
        }
    }
}
Comments
0