//+------------------------------------------------------------------+
//|                                                      EqClose.mq4 |
//|                                                            Demon |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Demon"
#property link      ""

#property indicator_chart_window

#include <Demon.mqh>

//--- input parameters
extern double       TakeProfit    = 500.0;
extern bool         UseStop       = false;
extern double       StopLoss      = 0.0;
extern int          Slippage      = 5;
extern int          CloseAttempts = 10;
extern bool         UseChartLabel = true;
extern int          ChartLabelX   = 500;
extern int          ChartLabelY   = 110;
extern color        LabelColor    = DarkOrange;

string Profit_Label = "iEQCLOSE_Profit";

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
	if ( UseChartLabel )
	{
		ObjectCreate(Profit_Label, OBJ_LABEL, 0, 0, 0);
		ObjectSet(Profit_Label, OBJPROP_XDISTANCE, ChartLabelX);
		ObjectSet(Profit_Label, OBJPROP_YDISTANCE, ChartLabelY);
		ObjectSet(Profit_Label, OBJPROP_COLOR, LabelColor);
	}

	start();

	return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
	if ( UseChartLabel )
		ObjectDelete(Profit_Label);

	return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
	double profit;

	if ( OrdersTotal() < 1 )
	{
		if ( UseChartLabel )
			ObjectSetText(Profit_Label, "Profit : NO ORDERS", 20);

		return (0);
	}

	profit = AllOrdersFinalProfit();

	if ( UseChartLabel)
		ObjectSetText(Profit_Label, "Profit : " + DoubleToStr(profit, 2), 20);

	if ( UseStop )
	{
		if ( profit <= StopLoss )
		{
			CallScript("SilentClose", Symbol(), Period());
		
			return (0);
		}
	}

	if ( profit >= TakeProfit )
		CallScript("SilentClose", Symbol(), Period());

	return(0);
}
//+------------------------------------------------------------------+