get today first balance
get today first balance
27 Jul 2021, 14:20
i have indicator that show multi data on chart.
i calcualte daily net profit percent. i use below code but it got error for first day of account. ex: i open account today ctrader hang because account have no history.
i know this part of code have problem.. i get last balance of yesterday. i think if i could get should first balance of today the problem should be solved.
double StartingBalance = History.Where(x => x.ClosingTime.Date != Time.Date).OrderBy(x => x.ClosingTime).Last().Balance;
i dont know how to fix it.
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class AWMultiData : Indicator
{
TextBlock tb = new TextBlock();
[Parameter("FontSize", DefaultValue = 12)]
public int FontSize { get; set; }
[Parameter("Space to Corner", DefaultValue = 10)]
public int Margin { get; set; }
[Parameter("Horizental Alignment", DefaultValue = HorizontalAlignment.Right)]
public HorizontalAlignment HAlignment { get; set; }
[Parameter("Vertical Alignment", DefaultValue = VerticalAlignment.Bottom)]
public VerticalAlignment VAlignment { get; set; }
[Parameter("Color", DefaultValue = "Red")]
public string Color1 { get; set; }
[Parameter("Opacity", DefaultValue = 0.7, MinValue = 0.1, MaxValue = 1)]
public double Opacity { get; set; }
[Parameter("Text Alignment", DefaultValue = TextAlignment.Center)]
public TextAlignment Alignment { get; set; }
protected override void Initialize()
{
Chart.AddControl(tb);
}
public override void Calculate(int index)
{
if (IsLastBar)
DisplaySpreadOnChart();
}
public void DisplaySpreadOnChart()
{
// Calc Spread
var spread = Math.Round(Symbol.Spread / Symbol.PipSize, 2);
string sp = string.Format("{0}", spread);
//Calc daily Net Profit
double DailyNet1 = Positions.Sum(p => p.NetProfit);
double DailyNet2 = History.Where(x => x.ClosingTime.Date == Time.Date).Sum(x => x.NetProfit);
double DailyNet = Math.Round(DailyNet1 + DailyNet2, 2);
// get Starting Balance
double StartingBalance = History.Where(x => x.ClosingTime.Date != Time.Date).OrderBy(x => x.ClosingTime).Last().Balance;
//calc Daily Percent Profit
string DailyPercent = Math.Round(DailyNet / StartingBalance * 100, 2).ToString();
//text property
tb.Text = Symbol.Name + ", " + TimeFrame.ShortName + "\n" + DailyPercent + " %" + "\n" + DailyNet + " $" + "\n" + sp;
tb.FontSize = FontSize;
tb.ForegroundColor = Color1.TrimEnd();
tb.HorizontalAlignment = HAlignment;
tb.VerticalAlignment = VAlignment;
tb.TextAlignment = Alignment;
tb.Margin = Margin;
tb.Opacity = Opacity;
}
}
}
Replies
IRCtrader
28 Jul 2021, 14:13
( Updated at: 28 Jul 2021, 14:16 )
RE:
amusleh said:
Hi,
Try this:
using System; using System.Linq; using cAlgo.API; using cAlgo.API.Internals; using cAlgo.API.Indicators; using cAlgo.Indicators; namespace cAlgo { [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class AWMultiData : Indicator { private TextBlock tb = new TextBlock(); [Parameter("FontSize", DefaultValue = 12)] public int FontSize { get; set; } [Parameter("Space to Corner", DefaultValue = 10)] public int Margin { get; set; } [Parameter("Horizental Alignment", DefaultValue = HorizontalAlignment.Right)] public HorizontalAlignment HAlignment { get; set; } [Parameter("Vertical Alignment", DefaultValue = VerticalAlignment.Bottom)] public VerticalAlignment VAlignment { get; set; } [Parameter("Color", DefaultValue = "Red")] public string Color1 { get; set; } [Parameter("Opacity", DefaultValue = 0.7, MinValue = 0.1, MaxValue = 1)] public double Opacity { get; set; } [Parameter("Text Alignment", DefaultValue = TextAlignment.Center)] public TextAlignment Alignment { get; set; } protected override void Initialize() { Chart.AddControl(tb); } public override void Calculate(int index) { if (IsLastBar) DisplaySpreadOnChart(); } public void DisplaySpreadOnChart() { // Calc Spread var spread = Math.Round(Symbol.Spread / Symbol.PipSize, 2); string sp = string.Format("{0}", spread); //Calc daily Net Profit double DailyNet1 = Positions.Sum(p => p.NetProfit); double DailyNet2 = History.Where(x => x.ClosingTime.Date == Time.Date).Sum(x => x.NetProfit); double DailyNet = Math.Round(DailyNet1 + DailyNet2, 2); var oldTrades = History.Where(x => x.ClosingTime.Date != Time.Date).OrderBy(x => x.ClosingTime).ToArray(); // get Starting Balance double StartingBalance = oldTrades.Length > 0 ? oldTrades.Last().Balance : Account.Balance; //calc Daily Percent Profit string DailyPercent = Math.Round(DailyNet / StartingBalance * 100, 2).ToString(); //text property tb.Text = Symbol.Name + ", " + TimeFrame.ShortName + "\n" + StartingBalance + "\n" + DailyPercent + " %" + "\n" + DailyNet + " $" + "\n" + sp; tb.FontSize = FontSize; tb.ForegroundColor = Color1.TrimEnd(); tb.HorizontalAlignment = HAlignment; tb.VerticalAlignment = VAlignment; tb.TextAlignment = Alignment; tb.Margin = Margin; tb.Opacity = Opacity; } } }
it works but it has a bug yet.
in first day after closing every position account.balance changed so daily net percent calculate wrong. i think we need to get first balance of opening account to fix that.
ex: balance is 1000 i got a positon and get 5%. now balance is 1050 and for other position daily net profit calculate based on 1050 balance not 1050.
@IRCtrader
amusleh
28 Jul 2021, 14:47
Hi,
Try this:
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class AWMultiData : Indicator
{
private TextBlock tb = new TextBlock();
[Parameter("FontSize", DefaultValue = 12)]
public int FontSize { get; set; }
[Parameter("Space to Corner", DefaultValue = 10)]
public int Margin { get; set; }
[Parameter("Horizental Alignment", DefaultValue = HorizontalAlignment.Right)]
public HorizontalAlignment HAlignment { get; set; }
[Parameter("Vertical Alignment", DefaultValue = VerticalAlignment.Bottom)]
public VerticalAlignment VAlignment { get; set; }
[Parameter("Color", DefaultValue = "Red")]
public string Color1 { get; set; }
[Parameter("Opacity", DefaultValue = 0.7, MinValue = 0.1, MaxValue = 1)]
public double Opacity { get; set; }
[Parameter("Text Alignment", DefaultValue = TextAlignment.Center)]
public TextAlignment Alignment { get; set; }
protected override void Initialize()
{
Chart.AddControl(tb);
}
public override void Calculate(int index)
{
if (IsLastBar)
DisplaySpreadOnChart();
}
public void DisplaySpreadOnChart()
{
// Calc Spread
var spread = Math.Round(Symbol.Spread / Symbol.PipSize, 2);
string sp = string.Format("{0}", spread);
//Calc daily Net Profit
double DailyNet1 = Positions.Sum(p => p.NetProfit);
double DailyNet2 = History.Where(x => x.ClosingTime.Date == Time.Date).Sum(x => x.NetProfit);
double DailyNet = Math.Round(DailyNet1 + DailyNet2, 2);
var oldTrades = History.Where(x => x.ClosingTime.Date != Time.Date).OrderBy(x => x.ClosingTime).ToArray();
// get Starting Balance
double StartingBalance;
if (oldTrades.Length == 0)
{
StartingBalance = History.Count == 0 ? Account.Balance : History.OrderBy(x => x.ClosingTime).First().Balance;
}
else
{
StartingBalance = oldTrades.Last().Balance;
}
//calc Daily Percent Profit
string DailyPercent = Math.Round(DailyNet / StartingBalance * 100, 2).ToString();
//text property
tb.Text = Symbol.Name + ", " + TimeFrame.ShortName + "\n" + StartingBalance + "\n" + DailyPercent + " %" + "\n" + DailyNet + " $" + "\n" + sp;
tb.FontSize = FontSize;
tb.ForegroundColor = Color1.TrimEnd();
tb.HorizontalAlignment = HAlignment;
tb.VerticalAlignment = VAlignment;
tb.TextAlignment = Alignment;
tb.Margin = Margin;
tb.Opacity = Opacity;
}
}
}
@amusleh
IRCtrader
28 Jul 2021, 15:25
RE:
amusleh said:
Hi,
Try this:
using System; using System.Linq; using cAlgo.API; using cAlgo.API.Internals; using cAlgo.API.Indicators; using cAlgo.Indicators; namespace cAlgo { [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class AWMultiData : Indicator { private TextBlock tb = new TextBlock(); [Parameter("FontSize", DefaultValue = 12)] public int FontSize { get; set; } [Parameter("Space to Corner", DefaultValue = 10)] public int Margin { get; set; } [Parameter("Horizental Alignment", DefaultValue = HorizontalAlignment.Right)] public HorizontalAlignment HAlignment { get; set; } [Parameter("Vertical Alignment", DefaultValue = VerticalAlignment.Bottom)] public VerticalAlignment VAlignment { get; set; } [Parameter("Color", DefaultValue = "Red")] public string Color1 { get; set; } [Parameter("Opacity", DefaultValue = 0.7, MinValue = 0.1, MaxValue = 1)] public double Opacity { get; set; } [Parameter("Text Alignment", DefaultValue = TextAlignment.Center)] public TextAlignment Alignment { get; set; } protected override void Initialize() { Chart.AddControl(tb); } public override void Calculate(int index) { if (IsLastBar) DisplaySpreadOnChart(); } public void DisplaySpreadOnChart() { // Calc Spread var spread = Math.Round(Symbol.Spread / Symbol.PipSize, 2); string sp = string.Format("{0}", spread); //Calc daily Net Profit double DailyNet1 = Positions.Sum(p => p.NetProfit); double DailyNet2 = History.Where(x => x.ClosingTime.Date == Time.Date).Sum(x => x.NetProfit); double DailyNet = Math.Round(DailyNet1 + DailyNet2, 2); var oldTrades = History.Where(x => x.ClosingTime.Date != Time.Date).OrderBy(x => x.ClosingTime).ToArray(); // get Starting Balance double StartingBalance; if (oldTrades.Length == 0) { StartingBalance = History.Count == 0 ? Account.Balance : History.OrderBy(x => x.ClosingTime).First().Balance; } else { StartingBalance = oldTrades.Last().Balance; } //calc Daily Percent Profit string DailyPercent = Math.Round(DailyNet / StartingBalance * 100, 2).ToString(); //text property tb.Text = Symbol.Name + ", " + TimeFrame.ShortName + "\n" + StartingBalance + "\n" + DailyPercent + " %" + "\n" + DailyNet + " $" + "\n" + sp; tb.FontSize = FontSize; tb.ForegroundColor = Color1.TrimEnd(); tb.HorizontalAlignment = HAlignment; tb.VerticalAlignment = VAlignment; tb.TextAlignment = Alignment; tb.Margin = Margin; tb.Opacity = Opacity; } } }
it works. thanks for your help.
@IRCtrader
amusleh
28 Jul 2021, 08:50
Hi,
Try this:
@amusleh