"Position.Margin" provides wrong values
"Position.Margin" provides wrong values
09 Nov 2024, 13:57
Hello,
I made an indicator, which shows me the percentage profit/loss of current position in a panel.
For one broker the “Position.Margin” is correct (IC Markets, both in Demo and Live account), but for another broker “Pepperstone” the Live account shows wrong data. All considered for the backtesting module of cTrader.
Backtesting starting capital is 10.000, based on it an order is excuted for the maximal amount (deducted approx. 5%) and volume rounded.
Pepperstone Demo account shows a correct Position.Margin value of these approx. 95% of starting capital of 10.000 (last row, right side), so the calculated start position profit-loss percentage looks correct: PositionPercentage: -0,25, position.Margin: 9.380,92
Pepperstone-DEMO:
08/11/2024 00:00:00.000 | Info | Indicator instance [TradingP, XAUUSD, m1] loaded.
08.11.2024 03:03:05.667 | Trade | Executing Market Order to Sell 75 XAUUSD
08.11.2024 03:03:05.677 | Trade | → Executing Market Order to Sell 75 XAUUSD SUCCEEDED, Position PID1
08.11.2024 03:03:06.905 | Info | position.NetProfit: -23,08, position.Commissions: -5,63, PositionPercentage: -0,25, position.Margin: 9.380,92
Pepperstone Live account unfortunately shows a wrong value, although order is made about the same volume “Executing Market Order to Sell 75 XAUUSD
”, so calculated start position profit-loss percentage is completely wrong: PositionPercentage: -17,89, position.Margin: 125,08
Pepperstone-LIVE:
08/11/2024 00:00:00.000 | Info | Indicator instance [TradingP, XAUUSD, m1] loaded.
08.11.2024 03:17:14.942 | Trade | Executing Market Order to Sell 75 XAUUSD
08.11.2024 03:17:14.952 | Trade | → Executing Market Order to Sell 75 XAUUSD SUCCEEDED, Position PID1
08.11.2024 03:17:20.433 | Info | position.NetProfit: -22,38, position.Commissions: -5,63, PositionPercentage: -17,89, position.Margin: 125,08
Is it a bug, or do I need to consider something special regarding Position.Margin?
Indicator to test it in backtesting module:
using System;
using System.Drawing;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using static System.Net.Mime.MediaTypeNames;
namespace cAlgo
{
[Indicator(AccessRights = AccessRights.None, IsOverlay = true)]
public class TradingPanel : Indicator
{
private TextBox ProfitText;
protected override void Initialize()
{
var ButtonSell = new Button
{
Text = "SHORT",
FontWeight = FontWeight.DemiBold,
FontSize = 12,
ForegroundColor = cAlgo.API.Color.White,
BackgroundColor = cAlgo.API.Color.OrangeRed,
Height = 30,
Width = 80,
Margin = 0,
Top = 10,
Left = 10
};
ButtonSell.Click += ButtonSell_Click;
var ButtonBuy = new Button
{
Text = "LONG",
FontWeight = FontWeight.DemiBold,
FontSize = 12,
ForegroundColor = cAlgo.API.Color.White,
BackgroundColor = cAlgo.API.Color.Green,
Height = 30,
Width = 80,
Margin = 0,
Top = 10,
Left = 100
};
ButtonBuy.Click += ButtonBuy_Click;
this.ProfitText = new TextBox
{
Text = "XXX",
Width = 170,
Height = 30,
Margin = 0,
Top = 50,
Left = 10,
FontWeight = FontWeight.DemiBold,
FontSize = 16,
ForegroundColor = cAlgo.API.Color.Black,
BackgroundColor = cAlgo.API.Color.Silver,
TextAlignment = TextAlignment.Center,
VerticalContentAlignment = VerticalAlignment.Center,
HorizontalContentAlignment = HorizontalAlignment.Center,
IsReadOnly = true,
IsEnabled = false
};
var canvas = new Canvas
{
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Bottom,
Width = 190,
Height = 100,
BackgroundColor = cAlgo.API.Color.Silver,
Opacity = 1.0
};
canvas.AddChild(ButtonSell);
canvas.AddChild(ButtonBuy);
canvas.AddChild(ProfitText);
Chart.AddControl(canvas);
}
private void ButtonBuy_Click(ButtonClickEventArgs obj)
{
if (Positions.Count == 0)
{
ExecuteMarketOrderAsync(TradeType.Buy, SymbolName, 75, "Label");
}
}
private void ButtonSell_Click(ButtonClickEventArgs obj)
{
if (Positions.Count == 0)
{
ExecuteMarketOrderAsync(TradeType.Sell, SymbolName, 75, "Label");
}
}
public override void Calculate(int index)
{
foreach (var position in Positions)
{
double PositionPercentage = Math.Round(100 * position.NetProfit / position.Margin, 2);
this.ProfitText.Text = PositionPercentage.ToString("n2") + " %";
if (position.NetProfit > 0)
this.ProfitText.ForegroundColor = cAlgo.API.Color.Green;
else
this.ProfitText.ForegroundColor = cAlgo.API.Color.Firebrick;
Print(string.Format("position.NetProfit: {0}, position.Commissions: {1}, PositionPercentage: {2}, position.Margin: {3}", position.NetProfit.ToString(), position.Commissions.ToString(), PositionPercentage.ToString("n2"), position.Margin.ToString("n2")));
}
if (Positions.Count == 0)
{
this.ProfitText.Text = string.Empty;
}
}
}
}
Thank you.
Best regards,
Bernhard
Replies
BernhardCS
11 Nov 2024, 06:57
RE: "Position.Margin" provides wrong values
Hello,
of course, leverage of both seems to be the same, account 30, symbol 20.
What I also found strange is that commission mentioned in the symbol = 0, but in backtesting the position had a value for commission:
position.Commissions: -5,63
A trading test in trading section of Pepperstone Demo account did not charge a commission:
Thank you.
Best regards,
Bernhard
@BernhardCS
PanagiotisCharalampous
11 Nov 2024, 06:21
Hi there,
Can you please also share the symbol leverage for each case? Share screenshots from the active symbol panel.
Best regards,
Panagiotis
@PanagiotisCharalampous