Getting exact money value of stop loss
06 Jan 2024, 18:43
I want to sum money value of all stop losses in open positions (total currency risk of open positions). How do I get the exact money value that is displayed next to “pips” on the stop loss label?
I am not sure I understand what you mean but I will try to answer with a concrete example and maybe start from this example to better understand what you need:
using cAlgo.API;
using cAlgo.API.Internals;
using System;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class TradesRisky : Robot
{
class Risky
{
public double Pips { get; set; }
public double Commissions { get; set; }
}
protected override void OnStart()
{
Positions.Modified += OnPositionModified;
UpdateData();
}
protected override void OnTick()
{
UpdateData();
}
public void OnPositionModified(PositionModifiedEventArgs args)
{
UpdateData();
}
private void UpdateData() {
Risky MyRisky = GetTotalPipsRisky();
double MoneyRisky = GetPipValueInMoney(MyRisky.Pips);
Chart.DrawStaticText("Risky",
string.Format("You are risking {0} pips for a total value of {1} {2} and {3} {2} commissions.", MyRisky.Pips, MoneyRisky, Account.Asset, MyRisky.Commissions),
VerticalAlignment.Top,
HorizontalAlignment.Left,
Color.Red
);
}
// This function can be improved
private double GetPipValueInMoney(double pips)
{
return Math.Round(pips * Symbol.PipValue / ( Symbol.PipSize * 100), 2);
}
private Risky GetTotalPipsRisky()
{
double pips = 0;
double commissions = 0;
Position[] MyPositions = Positions.FindAll(null, SymbolName);
foreach (Position position in Positions)
{
if (position.StopLoss != null && position.StopLoss > 0) pips += Math.Abs((double)position.StopLoss - position.EntryPrice);
commissions += position.Commissions;
}
return new Risky
{
Pips = DigitsToPips(pips),
Commissions = commissions
};
}
private double DigitsToPips(double pips)
{
return Math.Round(pips / Symbol.PipSize, 2);
}
}
}
Nevertheless your example still gives me different values. See screenshot (backtesting) below based on copy/paste of your code to new cBot with just ExecuteMarketOrder added;
I am not sure I understand what you mean but I will try to answer with a concrete example and maybe start from this example to better understand what you need:
using cAlgo.API;using cAlgo.API.Internals;using System;namespace cAlgo.Robots{ [Robot(AccessRights = AccessRights.None)] public class TradesRisky : Robot { class Risky { public double Pips { get; set; } public double Commissions { get; set; } } protected override void OnStart() { Positions.Modified += OnPositionModified; UpdateData(); } protected override void OnTick() { UpdateData(); } public void OnPositionModified(PositionModifiedEventArgs args) { UpdateData(); } private void UpdateData() { Risky MyRisky = GetTotalPipsRisky(); double MoneyRisky = GetPipValueInMoney(MyRisky.Pips); Chart.DrawStaticText("Risky", string.Format("You are risking {0} pips for a total value of {1} {2} and {3} {2} commissions.", MyRisky.Pips, MoneyRisky, Account.Asset, MyRisky.Commissions), VerticalAlignment.Top, HorizontalAlignment.Left, Color.Red ); } // This function can be improved private double GetPipValueInMoney(double pips) { return Math.Round(pips * Symbol.PipValue / ( Symbol.PipSize * 100), 2); } private Risky GetTotalPipsRisky() { double pips = 0; double commissions = 0; Position[] MyPositions = Positions.FindAll(null, SymbolName); foreach (Position position in Positions) { if (position.StopLoss != null && position.StopLoss > 0) pips += Math.Abs((double)position.StopLoss - position.EntryPrice); commissions += position.Commissions; } return new Risky { Pips = DigitsToPips(pips), Commissions = commissions }; } private double DigitsToPips(double pips) { return Math.Round(pips / Symbol.PipSize, 2); } }}
You can also remove the class that is no longer needed now:
(...)
Thank you! Sorry for bugging but I still need some adjustment for indices. I get wrong results (they are 10x too big) for GER40 or US100. Methods name have changed but the code inside is the same as yours.
I added another variant for 2 digits:
switch (cBot.Symbol.Digits)
{
case 2:
multy = 1000;
break;
case 3:
multy = 0.1;
break;
case 4:
case 5:
multy = 10;
break;
}
But now XAUUSD is broken (gives 10x to small result). Is there a way to do this properly or I should make adjustments for each symbol (sound quite insane)?
ctrader.guru
07 Jan 2024, 09:21
I am not sure I understand what you mean but I will try to answer with a concrete example and maybe start from this example to better understand what you need:
@ctrader.guru