

#property copyright ""
#property link ""
#property version ""
#include <stdlib.mqh>

extern int MagicNumber = 779;
extern double PmProfitTarget = 0;
extern double PmMaxLoss = 0;
bool ShowTick=True;
string chartcomment4409;
string eaname4687="PofitTaker";
int PmState = 0;
bool PmNoOpen = false;
bool PmCloseAll = false;
datetime PmCloseTime;

void fnComment3725()
{
	Comment( chartcomment4409 );
}

int fnCloseAllEx(int ordertype, int ticket, int magic, string symbol)
{
	double price = 0;
	int count = 0;
	for ( int i=0; i < OrdersTotal(); i++ )
	{
		if ( OrderSelect(i, SELECT_BY_POS) == false )
			continue;
		if( (ticket > 0) && (OrderTicket() != ticket) )
			continue;
		if( (StringLen(symbol) > 0) && (OrderSymbol() != symbol) )
			continue;
		if( (magic > 0) && (OrderMagicNumber() != magic) )
			continue;
		if( (ordertype > 0) && (ordertype != OrderType()) )
			continue;
		count++;
		if( OrderType() == OP_BUY )
		{
			price = MarketInfo(OrderSymbol(), MODE_BID);
		}
		if( OrderType() == OP_SELL )
		{
			price = MarketInfo(OrderSymbol(), MODE_ASK);
		}
		bool status = OrderClose( OrderTicket(), OrderLots(), price, 3, Red );
		if( status == false )
		{
			int error = GetLastError();
			string msg = StringConcatenate("fnCloseAllEx OrderClose Failed: ", error);
			Print(msg);
		}
	}
	return(count);
}

void fnManageProfit( int resetType, int resetValue )
{
	if( PmCloseAll == true )
	{
		chartcomment4409 = StringConcatenate(chartcomment4409,"ProfitManager Closing All Trades");
		if( fnCloseAllEx(-1, 0, 779, "") == 0 )
		{
			PmCloseAll = false;
		}
	}

	if( resetType != 0 )
	{
		int waitSeconds = resetValue*60;
		if( resetType == 1 )
		{
			waitSeconds = resetValue * Period() * 60;
		}
		if( TimeCurrent() > (PmCloseTime + waitSeconds) )
		{
			PmNoOpen = false;
			PmCloseTime = 0;
		}
		if( PmNoOpen == true )
			chartcomment4409 = StringConcatenate(chartcomment4409,"Trade Open Disabled: Profit Manager next open time:",  TimeToStr(PmCloseTime + waitSeconds), " \n  ");
	}
}

int fnCheckForProfit(int lookbackminutes, double target, double maxloss, int ordertype, int ticket, int magic, string symbol)
{
	int i, total;
	double profit = 0;
	if( maxloss > 0 )
		maxloss = maxloss *(-1);
	// get history profit
	if( lookbackminutes > 0 )
	{
		total = OrdersHistoryTotal();
		for( i=0; i<total; i++ )
		{
			if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) == false)
				continue;
			if( (ticket > 0) && (OrderTicket() != ticket) )
				continue;
			if( (StringLen(symbol) > 0) && (OrderSymbol() != symbol) )
				continue;
			if( (magic > 0) && (OrderMagicNumber() != magic) )
				continue;
			if( (ordertype > 0) && (ordertype != OrderType()) )
				continue;
			if( (OrderCloseTime() + (lookbackminutes*60)) >= TimeCurrent() )
				profit += OrderProfit();
		}
	} 

	total = OrdersTotal();
	for( i=0; i<total; i++ )
	{
		if( OrderSelect(i,SELECT_BY_POS) == false )
			continue;
		if( (ticket > 0) && (OrderTicket() != ticket) )
			continue;
		if( (StringLen(symbol) > 0) && (OrderSymbol() != symbol) )
			continue;
		if( (magic > 0) && (OrderMagicNumber() != magic) )
			continue;
		if( (ordertype > 0) && (ordertype != OrderType()) )
			continue;
		profit += OrderProfit();
	} // end open
	chartcomment4409 = StringConcatenate(chartcomment4409,"Profit Manager profit:",  DoubleToStr(profit, 2), "\n");
	chartcomment4409 = StringConcatenate(chartcomment4409,"Profit Manager ProfitTarget:",  DoubleToStr(target, 2), "\n");
	chartcomment4409 = StringConcatenate(chartcomment4409,"Profit Manager MaxLoss:",  DoubleToStr(maxloss, 2), "\n");
	chartcomment4409 = StringConcatenate(chartcomment4409,"Profit Manager lookback:",  lookbackminutes, "\n");
	if( profit >= target )
		return(1);
	if( profit <= maxloss )
		return(-1);
	return(0);
}

int start()
{
	static int tick9033;
	tick9033++;
	chartcomment4409="";

	if (ShowTick == true ) 
	   chartcomment4409="Build # 1(VTS EA Builder 4.0.0.85) MAGIC: "+MagicNumber+"\nTICK:"+tick9033+ "\n" ;

	PmState = fnCheckForProfit(0, PmProfitTarget, PmMaxLoss, -1, 0, MagicNumber, "");
	if (PmState != 0 )
	{
		// max loss
		PmNoOpen = true;
		PmCloseTime = TimeCurrent();
		PmCloseAll = true;
	}
	
	fnManageProfit(0, 12 );
	fnComment3725();

	return( 0 );
} 
