Category Other  Published on 28/01/2021

Profit Counter

Description

有効証拠金(Equity)・本日の収支・エントリ中の全ポジションの合計(Net Profit)をチャート上に表示します

有効証拠金と本日の収支の表示はオンオフできます。

 


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

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


        [Parameter("有効証拠金表示", DefaultValue = false)]
        public bool viewEquity { get; set; }

        [Parameter("今日の収支表示", DefaultValue = false)]
        public bool todayTotal { get; set; }

        [Parameter("位置を上に移動", DefaultValue = true)]
        public bool upperPosition { get; set; }

        private int moveDistance;

        protected override void Initialize()
        {
            if (upperPosition == true)
            {
                moveDistance = 4;
            }
            else
            {
                moveDistance = 2;
            }

            TimeSpan timeSpan = new TimeSpan(0, 0, 0, 0, 1);
            Timer.Start(timeSpan);
        }

        protected override void OnTimer()
        {
            double totalNetprofit = 0;
            string text = "";

            //ポジションの数
            foreach (var position in Positions)
            {
                totalNetprofit += position.NetProfit;
            }

            text += String.Format("{0:#,0}", totalNetprofit);


            for (int i = 0; i < moveDistance - 1; i++)
            {
                text += "\n";
            }

            var totalText = Chart.DrawStaticText("Net Profit Total", text, VerticalAlignment.Bottom, HorizontalAlignment.Right, Color.White);

            if (totalNetprofit < 0)
            {
                totalText.Color = Color.Pink;
            }
            else if (totalNetprofit > 0)
            {
                totalText.Color = Color.Aqua;
            }


            if (todayTotal)
            {

                string todyTotalText = "today: ";

                double todyTotal = 0;
                foreach (HistoricalTrade trade in History)
                {
                    if (trade.ClosingTime.ToLocalTime().Date.Day == Server.Time.ToLocalTime().Date.Day && trade.ClosingTime.ToLocalTime().Date.Month == Server.Time.ToLocalTime().Date.Month)
                    {
                        todyTotal += trade.NetProfit;
                    }
                }

                todyTotal += totalNetprofit;

                todyTotalText += String.Format("{0:#,0}", todyTotal);

                for (int i = 0; i < moveDistance; i++)
                {
                    todyTotalText += "\n";
                }

                var todyTotalDrawing = Chart.DrawStaticText("Tody Total", todyTotalText, VerticalAlignment.Bottom, HorizontalAlignment.Right, Color.White);

                if (todyTotal < 0)
                {
                    todyTotalDrawing.Color = Color.Pink;
                }
                else if (todyTotal > 0)
                {
                    todyTotalDrawing.Color = Color.Aqua;
                }
            }


            if (viewEquity)
            {
                string equity = "equity: " + String.Format("{0:#,0}", Account.Equity);

                for (int i = 0; i < moveDistance + 1; i++)
                {
                    equity += "\n";
                }

                var equityText = Chart.DrawStaticText("Equity", equity, VerticalAlignment.Bottom, HorizontalAlignment.Right, Color.White);
            }



        }

        public override void Calculate(int index)
        {


        }
    }
}


summer's avatar
summer

Joined on 10.08.2020

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Profit Counter.algo
  • Rating: 0
  • Installs: 1256
Comments
Log in to add a comment.
No comments found.