Indicator update showing wrong data after Ctrader Update
Indicator update showing wrong data after Ctrader Update
06 Dec 2022, 12:12
Dear All,
I have a position status Indicator which was working fine till last update. After the recent update something went wrong so the Indicator shows wrong value. i.e. when I change the symbol it shows the correct value but immediately the value is changed to wrong value. I have uploaded the short video in the below link, please check and help me to fix this issue. I also updated the indicator to .net6 but still the same problem.
https://drive.google.com/file/d/1TTKyiAtE5hbOuquPxxsWUseNdM5xfPo_/view?usp=share_link
Also after the recent update my Ctrader always crashes with white screen, so I have to restart the Ctrader from Task bar.
Please help me to fix this issues since it is very difficult for me since I am not a developer
Thanks and Regards
R. Vadivelan
Replies
velu130486
08 Dec 2022, 04:43
RE:
Dear Spotware,
If you refer to the Left Top of the chart, some position statistics are displayed based on chart symbol. If you watch it when I change the symbol, Buy profit/sell profit shows the value and immediately it changes to wrong value.
Upto previous update this indicator was working fine, but recently it changes value like this. Attached herewith the indicator code I got from this website.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo
{
[Indicator(AccessRights = AccessRights.None)]
public class PositionStats : Indicator
{
[Parameter("Show balance", DefaultValue = true)]
public bool showBalance { get; set; }
[Parameter("Show equity", DefaultValue = true)]
public bool showEquity { get; set; }
[Parameter("Buy/Sell Amount", DefaultValue = true)]
public bool showBuySellAmount { get; set; }
[Parameter("Buy/Sell Count", DefaultValue = true)]
public bool showBuySellCount { get; set; }
[Parameter("Total amount", DefaultValue = true)]
public bool showTotalAmount { get; set; }
[Parameter("Total count", DefaultValue = true)]
public bool showTotalCount { get; set; }
[Parameter("Pip cost", DefaultValue = true)]
public bool showPipCost { get; set; }
[Parameter("Margin Level", DefaultValue = true)]
public bool showMarginLevel { get; set; }
[Parameter("Chart corner, 1-6", DefaultValue = 1, MinValue = 1, MaxValue = 6)]
public int corner { get; set; }
[Parameter("Show labels", DefaultValue = true)]
public bool showLabels { get; set; }
[Parameter("Show account currency", DefaultValue = true)]
public bool showCurrency { get; set; }
[Parameter("Show base currency", DefaultValue = true)]
public bool showBaseCurrency { get; set; }
[Parameter("Look Back", DefaultValue = 40, MinValue = 0)]
public int lookBack { get; set; }
[Parameter("minVolume:PF ratio ($)", DefaultValue = 0.1, MinValue = 0)]
public double profitfactor { get; set; }
[Parameter("Max Equity Drawdown %", DefaultValue = 10, MinValue = 1)]
public double EquityLoss { get; set; }
protected override void Initialize()
{
Positions.Opened += delegate (PositionOpenedEventArgs args) { Update(); };
Positions.Closed += delegate (PositionClosedEventArgs args) { Update(); };
Positions.Modified += delegate (PositionModifiedEventArgs args) { Update(); };
}
public override void Calculate(int index)
{
if (!IsLastBar)
{
return;
}
else
{
Update();
}
}
public void Update()
{
double buy_volume = 0, sell_volume = 0, total_volume = 0;
double buy_ord_vol = 0, sell_ord_vol = 0, total_ord_vol = 0;
double buy_profit = 0, sell_profit = 0, total_profit = 0;
double buy_count = 0, sell_count = 0, total_count = 0;
double buy_order = 0, sell_order = 0, total_order = 0;
double pfBUY = 0, pfSELL = 0;
var minvolume = Symbol.VolumeInUnitsMin;
foreach (var p in Positions)
{
if (p.SymbolName != Symbol.Name)
{
continue;
}
if (p.TradeType == TradeType.Buy)
{
buy_volume += p.VolumeInUnits;
buy_count++;
buy_profit += p.NetProfit;
}
else
{
sell_volume += p.VolumeInUnits;
sell_count++;
sell_profit += p.NetProfit;
}
total_volume = buy_volume - sell_volume;
total_count = buy_count + sell_count;
total_profit = buy_profit + sell_profit;
total_order = buy_order + sell_order;
pfBUY = (buy_volume / minvolume) * profitfactor;
pfSELL = (sell_volume / minvolume) * profitfactor;
}
foreach (var o in PendingOrders)
{
if (o.SymbolName != Symbol.Name)
{
continue;
}
if (o.TradeType == TradeType.Buy)
{
buy_ord_vol += o.VolumeInUnits;
buy_order++;
}
else
{
sell_ord_vol += o.VolumeInUnits;
sell_order++;
}
total_ord_vol = buy_ord_vol - sell_ord_vol;
total_order = buy_order + sell_order;
}
StringBuilder s = new StringBuilder();
if (showBalance)
{
if (showLabels)
s.Append("Balance: ");
s.AppendFormat("{0:N2}", Account.Balance);
if (showCurrency)
{
s.Append(" ");
s.Append(Account.Asset.Name);
}
s.AppendLine();
}
if (showEquity)
{
if (showLabels)
s.Append("Equity: ");
s.AppendFormat("{0:N2}", Account.Equity);
if (showCurrency)
{
s.Append(" ");
s.Append(Account.Asset.Name);
}
s.AppendLine();
}
if (showBuySellAmount)
{
var spread = Math.Round(Symbol.Spread / Symbol.PipSize, 1);
if (showLabels)
s.Append("Spread: ");
s.AppendFormat("{0:N1}", spread);
s.AppendLine();
if (showLabels)
s.Append("Buy Volume: ");
s.AppendFormat("{0:N0}", buy_volume);
if (showBaseCurrency)
{
s.Append(" ");
s.Append(Symbol.Name.Substring(0, 3));
}
s.AppendLine();
if (showLabels)
s.Append("Sell Volune: ");
s.AppendFormat("{0:N0}", sell_volume);
if (showBaseCurrency)
{
s.Append(" ");
s.Append(Symbol.Name.Substring(0, 3));
}
s.AppendLine();
if (showLabels)
s.Append("Total Volune: ");
s.AppendFormat("{0:+#,###;-#,###;0}", total_volume);
s.AppendLine();
if (showLabels)
s.Append("Order Volume: ");
s.AppendFormat("{0:+#,###;-#,###;0}", total_ord_vol);
s.AppendLine();
}
if (showBuySellCount)
{
if (showLabels)
s.Append("Buy Position: ");
s.AppendFormat("{0:N0}", buy_count);
s.AppendLine();
if (showLabels)
s.Append("Sell Position: ");
s.AppendFormat("{0:N0}", sell_count);
s.AppendLine();
if (showLabels)
s.Append("Total Position: ");
s.AppendFormat("{0:N0}", total_count);
s.AppendLine();
if (showLabels)
s.Append("Buy Order: ");
s.AppendFormat("{0:N0}", buy_order);
s.AppendLine();
if (showLabels)
s.Append("Sell Order: ");
s.AppendFormat("{0:N0}", sell_order);
s.AppendLine();
if (showLabels)
s.Append("Total Order: ");
s.AppendFormat("{0:N0}", total_order);
s.AppendLine();
}
if (showTotalAmount)
{
if (showLabels)
s.Append("Buy Profit: ");
s.AppendFormat("{0:+#,###;-#,###;0}", buy_profit);
if (showCurrency)
{
s.Append(" ");
s.Append(Account.Asset.Name);
}
s.AppendLine();
if (showLabels)
s.Append("Sell Profit: ");
s.AppendFormat("{0:+#,###;-#,###;0}", sell_profit);
if (showCurrency)
{
s.Append(" ");
s.Append(Account.Asset.Name);
}
s.AppendLine();
if (showLabels)
s.Append("Total Profit: ");
s.AppendFormat("{0:+#,###;-#,###;0}", total_profit);
if (showCurrency)
{
s.Append(" ");
s.Append(Account.Asset.Name);
}
s.AppendLine();
}
if (showTotalCount)
{
var averageBuyFromCurrent = Math.Round((CalculateAveragePositionPrice(TradeType.Buy) - Symbol.Bid) / Symbol.PipSize, 1);
var averageSellFromCurrent = Math.Round((Symbol.Ask - CalculateAveragePositionPrice(TradeType.Sell)) / Symbol.PipSize, 1);
if (showLabels)
s.Append("Buy Target Away: ");
s.AppendFormat("{0:N0}", averageBuyFromCurrent);
s.AppendLine();
if (showLabels)
s.Append("Sell Target Away: ");
s.AppendFormat("{0:N0}", averageSellFromCurrent);
s.AppendLine();
}
if (showPipCost)
{
if (showLabels)
s.Append("PIP Cost: ");
s.AppendFormat("{0:N3}", total_volume * Symbol.PipValue);
if (showCurrency)
{
s.Append(" ");
s.Append(Account.Asset.Name);
}
s.AppendLine();
if (showLabels)
s.Append("BuyProfitF: ");
s.AppendFormat("{0:+#,###;-#,###;0}", pfBUY);
if (showCurrency)
{
s.Append(" ");
s.Append(Account.Asset.Name);
}
s.AppendLine();
if (showLabels)
s.Append("SellProfitF: ");
s.AppendFormat("{0:+#,###;-#,###;0}", pfSELL);
if (showCurrency)
{
s.Append(" ");
s.Append(Account.Asset.Name);
}
s.AppendLine();
}
if (showMarginLevel)
{
if (showLabels)
s.Append("Margin Level: ");
if (Account.MarginLevel == null)
{
s.Append("-");
}
else
{
s.AppendFormat("{0:N2}", Account.MarginLevel);
s.Append("%");
}
s.AppendLine();
}
VerticalAlignment posv;
HorizontalAlignment posh;
switch (corner)
{
case 1:
posv = VerticalAlignment.Top;
posh = HorizontalAlignment.Left;
break;
case 2:
posv = VerticalAlignment.Top;
posh = HorizontalAlignment.Center;
break;
case 3:
posv = VerticalAlignment.Top;
posh = HorizontalAlignment.Right;
break;
case 4:
posv = VerticalAlignment.Bottom;
posh = HorizontalAlignment.Right;
break;
case 5:
posv = VerticalAlignment.Bottom;
posh = HorizontalAlignment.Center;
break;
case 6:
posv = VerticalAlignment.Bottom;
posh = HorizontalAlignment.Left;
break;
default:
posv = VerticalAlignment.Top;
posh = HorizontalAlignment.Left;
break;
}
Chart.DrawStaticText("showInfo", s.ToString(), posv, posh, Color.White);
}
private double CalculateAveragePositionPrice(TradeType tradeType)
{
double result = 0;
double averagePrice = 0;
long count = 0;
foreach (var p in Positions)
{
if (p.SymbolName == Symbol.Name)
{
if (p.TradeType == tradeType)
{
averagePrice += p.EntryPrice * p.VolumeInUnits;
count += p.Volume;
}
}
}
if (averagePrice > 0 && count > 0)
result = Math.Round(averagePrice / count, Symbol.Digits);
return result;
}
}
}
Thanks and Regards
R. Vadivelan
Spotware
07 Dec 2022, 08:36
Dear trader,
Can you please explain to us what is the issue we are supposed to see in the video? Also please share your indicator code.
Best regards,
cTrader Team
@Spotware