StopLoss in EUR from manuel opened positions
19 Oct 2018, 18:35
Hi there .... I struggle getting the SL sum in EUR of all open positions.
I want to summarize all SL losses in EUR, to watch my maximum possible loss over all.
By using the function: Positions[i].StopLoss ... I get the price level of the SL each position, but I dont have a clue
how much it means in EUR. As far as I can see, there is even no function to figure out the SL pips of a position.
In the graph, if I hover the SL line, I can see the loss in EUR, I would like to have exaclty this amount summarized over all open positions in my bot / indicator.
Any ideas?
Replies
michael.kolb
20 Oct 2018, 21:05
( Updated at: 21 Dec 2023, 09:20 )
Hi freemangreat, thanks a lot for your example. I copied it and checked, but unfortunately it doesn't give the desired result.
Thing is, with manual opened positions, and arranged SL limites according to chart technik, I need to figure out the SL in pips.
In the code example, the pips are neccessary to be typed in:
[Parameter(DefaultValue = 10)]
public int StoplossLevel { get; set; }
If I would know the SL in pips, it would be easy to calculate the loss by (PositionOpenPrice - StoplossLevel * Symbol.PipSize)
But in this case, I need to figure out what the SL in pips are..... and I can't find a function. Hmm.
@michael.kolb
freemangreat
20 Oct 2018, 22:23
What?)) See line of code #41. And specify any price StopLoss price.
If you have difficulty in programming such elementary things, perhaps you better order or buy ready-made code.
@freemangreat
michael.kolb
21 Oct 2018, 20:37
RE:
Hi. I didn't want to type in the SL in pips into the indicator input field. Means it would be neccessary to adjust the input value everytime I move the SL line by hand.
The difficulty was to figure it out automatically, and summarize it over all open positions ... but I found the solution ... easier than I thougt ...
double maxLoss = 0.0;
for (int i = 0; i < Positions.Count; i++)
{
var lossPIPS = (double)(Positions[i].StopLoss - Positions[i].EntryPrice) * (1 / MarketData.GetSymbol(Positions[i].SymbolCode).PipSize);
var lossEUR = (double)(lossPIPS * MarketData.GetSymbol(Positions[i].SymbolCode).PipValue * Positions[i].VolumeInUnits);
maxLoss = maxLoss + lossEUR;
}
ChartObjects.DrawText("loss", "Current max. Loss over all: " + Math.Round(maxVerlust, 2), StaticPosition.TopCenter, Colors.Red);
Thanks for the help and replying to my request.
@michael.kolb
freemangreat
21 Oct 2018, 21:46
I have given a universal code for calculate profit / loss of position for any currency pair to ---> any currency (not only to the account currency).
Calculate in the account currency is certainly easier.
In your code, you do not use the side of trade (Buy/Sell).
@freemangreat

freemangreat
20 Oct 2018, 17:49
using System; 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 LossProfitToCurrency : Indicator { [Parameter(DefaultValue = 0)] // Price for open position public double PositionOpenPrice { get; set; } [Parameter(DefaultValue = 1.0)] // Open trade in Lots (Buy for example) public double PositionLots { get; set; } [Parameter(DefaultValue = 10)] // Stoploss in standard points (Pts) (for 4 or 2 digits after point) public int StoplossLevel { get; set; } //............................................................................... private string sBaseCurrency; private string sCounterCurrency; // Any valid code major out currency USD EUR GBP JPY CHF AUD CAD ... etc private string sOutCurrency ="EUR"; private double fPtsSize =0.0001; // Standard point/pip size (default 4 digits after point) private double fStopLossPrice; Symbol TransPair; // trans pair for cross currencies int iTransPairType =0; //............................................................................... protected override void Initialize() { // Initialize and create nested indicators if(PositionOpenPrice <= 0.0) PositionOpenPrice =(Symbol.Bid); if(Symbol.Digits == 2 || Symbol.Digits == 3) // correct for 2 or 3 digits pairs fPtsSize =0.01; // set stoploss price fStopLossPrice =(PositionOpenPrice - StoplossLevel * fPtsSize); // for buy side // disassembling a pair of current chart into constituent currencies sBaseCurrency =Symbol.Code.Substring(0, 3); sCounterCurrency =Symbol.Code.Substring(3, 3); // try find transpair if need.. if((sOutCurrency != sBaseCurrency) && (sOutCurrency != sCounterCurrency)) { TransPair =MarketData.GetSymbol(sCounterCurrency + sOutCurrency); if(TransPair != null) iTransPairType =1; // fwd pair else { TransPair =MarketData.GetSymbol(sOutCurrency + sCounterCurrency); if(TransPair != null) iTransPairType =(-1); // bwd pair } } //...... draw lines for Position and Stoploss prices ChartObjects.DrawHorizontalLine("BuyPosition", PositionOpenPrice, Colors.RoyalBlue); ChartObjects.DrawHorizontalLine("StopLoss", fStopLossPrice, Colors.Red); } //--------------------------------------------------------------------------------------------- public override void Calculate(int index) { if(!IsLastBar) return; string sOutText; double fLossProfit =(fStopLossPrice - PositionOpenPrice); // buy side double fLossProfitOutCurrency =0; double fCurrentPairPrice =Symbol.Bid; // actual price double fPositionVolume =(PositionLots * Symbol.LotSize); //........................................................................... // Calculate Loss/Profit in currency // where OUT is out currency // fwd pair xxxOUT if(sCounterCurrency == sOutCurrency) fLossProfitOutCurrency =(fPositionVolume * fLossProfit); else // bwd pair OUTxxx if(sBaseCurrency == sOutCurrency) fLossProfitOutCurrency =(fPositionVolume * fLossProfit) / fCurrentPairPrice; else // cross pair xxxxxx if(iTransPairType != 0) { double fTransPairPrice =TransPair.Bid; // transpair is fwd pair xxxOUT if(iTransPairType > 0) fLossProfitOutCurrency =(fPositionVolume * fLossProfit) * fTransPairPrice; // transpair is bwd pair OUTxxx else fLossProfitOutCurrency =(fPositionVolume * fLossProfit) / fTransPairPrice; } else { ChartObjects.DrawText("Params", "Not valid out currency", StaticPosition.TopLeft); return; } //........................................................................... fLossProfitOutCurrency =Math.Round(fLossProfitOutCurrency, 5); sOutText ="Base currency: " + sBaseCurrency + "\nCounter currency: " + sCounterCurrency + "\nLoss/Profit: " + fLossProfitOutCurrency + " " + sOutCurrency; ChartObjects.DrawText("Params", sOutText, StaticPosition.TopLeft); } // Calculate } // class } // namespace@freemangreat