using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class BreakEvenLine : Indicator
{
[Output("Main")]
public IndicatorDataSeries Result { get; set; }
protected override void Initialize()
{
//DisplayStatusOnChart();
}
public override void Calculate(int index)
{
// Calculate value at specified index
// Result[index] = ...
DisplayStatusOnChart();
}
private void DisplayStatusOnChart()
{
if (CalculateAveragePositionPrice() > 0)
{
Chart.DrawHorizontalLine("be", CalculateAveragePositionPrice(), Color.Gold, 2, LineStyle.LinesDots);
}
else
{
Chart.RemoveObject("be");
}
}
private double CalculateAveragePositionPrice()
{
double result = 0;
double lots = 0;
double equity = 0;
double point = 0;
double cop = 0;
double val = 0;
foreach (var position in Positions)
{
if (position.SymbolName == SymbolName)
{
equity += position.NetProfit;
if (position.TradeType == TradeType.Buy)
{
lots += position.VolumeInUnits;
}
else if (position.TradeType == TradeType.Sell)
{
lots -= position.VolumeInUnits;
}
}
}
if (lots != 0)
{
point = Symbol.TickSize;
cop = lots * Symbol.TickValue;
val = Symbol.Bid - (point * equity / cop);
result = Math.Round(val, Symbol.Digits);
}
return result;
}
}
}