# Profit Calc Function

230

Hello everyone;

I created a function that has been calculating my profit per day.

My mains parameters are "Maximum Profit" e "Maximum Loss". I created this function for manager my limit loss and limit gain per day and when it is achieved the EA stop trade.

However, it doesn't work much better. The function is not calculated very well and sometimes my position is closed at a wrong moment. Someone can help me, please?

```sinput string Maximum; // Maximum Profit/Loss
input bool UseLimit=true;
input double MaximumProfit=400; // Monetary Value
input double MaximumLoss=-400; // Monetary Value

if(UseLimit==true && CalcProfit()==true)
{
if(OpenOrders(_Symbol) > 0)
{
CLOSEALL(0);CLOSEALL(1);
}

Print("Profit limit was reached");
Comment("Profit limit was reached");

//+------------------------------------------------------------------+
//|Function Calcprofit                                               |
//+------------------------------------------------------------------+
bool CalcProfit()
{
// --- determine the time intervals of the required trading history
datetime end=StringToTime(TimeToString(TimeCurrent(), TIME_DATE) + " " + EndHour);
datetime gi_time = StringToTime(TimeToString(TimeCurrent(), TIME_DATE) + " " + StartHour);
datetime start=end-PeriodSeconds(PERIOD_D1);// set the beginning time to 24 hours ago

//--- request in the cache of the program the needed interval of the trading history
HistorySelect(gi_time,end);
//--- obtain the number of deals in the history
int deals=HistoryDealsTotal();

int returns=0;
double profit=0;
double loss=0;
//--- scan through all of the deals in the history
for(int i=0;i<deals;i++)
{
//--- obtain the ticket of the deals by its index in the list
ulong deal_ticket=HistoryDealGetTicket(i);
if(deal_ticket>0) // obtain into the cache the deal, and work with it

{
string symbol             =HistoryDealGetString(deal_ticket,DEAL_SYMBOL);
datetime time             =HistoryDealGetInteger(deal_ticket,DEAL_TIME);
ulong order               =HistoryDealGetInteger(deal_ticket,DEAL_ORDER);
long order_magic          =HistoryDealGetInteger(deal_ticket,DEAL_MAGIC);
long pos_ID               =HistoryDealGetInteger(deal_ticket,DEAL_POSITION_ID);
ENUM_DEAL_ENTRY entry_type=(ENUM_DEAL_ENTRY)HistoryDealGetInteger(deal_ticket,DEAL_ENTRY);
//--- proceed deal with specified DEAL_MAGIC
if(order_magic==MagicNumber)
{
//... processing of deal with some DEAL_MAGIC

if(symbol==_Symbol)
{
if(entry_type==DEAL_ENTRY_OUT)
returns++;
{
double result=HistoryDealGetDouble(deal_ticket,DEAL_PROFIT);
if((result>=MaximumProfit) || (result+Position.Profit()>= MaximumProfit))
{
return(true);
if(result>0)
{
profit=result;

}
}
if((result<=MaximumLoss) ||  (result+Position.Profit()<=MaximumLoss))
{
return(true);
if(result<0)
{
loss=result;

}
}
}
}
}

}
else // unsuccessful attempt to obtain a deal
{
PrintFormat("We couldn't select a deal, with the index %d. Error %d",
i,GetLastError());
}
}
//--- output the results of the calculations
PrintFormat("The total number of %d deals with a financial result. Profit=%.2f , Loss= %.2f",
returns,profit,loss);

return(false);
}

```