Stop trading after reaching daily Profit/Loss

 

Hello everyone,


I am trying to code a function to return the accumulated daily Profit/Loss, but I cant make the code work. Can any guru here be so kind to help me? I Appreciate!


double ProfitAll(datetime& XHoraPrimeiroCandle, double& XProfit)
   {
   int err=0;
   if (IsFirstBar())
      {
      XHoraPrimeiroCandle = iTime(_Symbol,_Period,0);
      }

   HistorySelect(XHoraPrimeiroCandle,TimeCurrent());

   Comment("Hora: ",XHoraPrimeiroCandle,"Profit: ", XProfit);
   for(int i = HistoryDealsTotal()-1; i >= 0; i--)
      {
      ulong ticket = HistoryDealGetTicket(i);
      string symbol = HistoryDealGetString(ticket,DEAL_SYMBOL);
      ulong magic = HistoryDealGetInteger(ticket,DEAL_MAGIC);
      ulong order = HistoryDealGetInteger(ticket,DEAL_ORDER);
      long dealType = HistoryDealGetInteger(ticket,DEAL_ENTRY);
      long orderType = HistoryDealGetInteger(ticket,DEAL_TYPE);
      double profitAtual = HistoryDealGetDouble(ticket,DEAL_PROFIT);
      
      if(symbol == _Symbol && magic == magicNum && dealType == DEAL_ENTRY_OUT)
         {
         XProfit = XProfit + profitAtual;
         }
      else
         {
         Print("Falha em selecionar ordem");
         err = GetLastError();
         Print("Erro encontado ao selecionar ordem, erro número "+(string)err);
         }
      }      
   return XProfit;  
   }
 
Just looking at the code, there is nothing obvious. What is "not working" ?
 
Alain Verleyen:
Just looking at the code, there is nothing obvious. What is "not working" ?
Hey Alain. It is ERR_TRADE_DEAL_NOT_FOUND. I have changed the function a lot of different ways, but it just doesnt find any order in the history.
 
gustgdp:
Hey Alain. It is ERR_TRADE_DEAL_NOT_FOUND. I have changed the function a lot of different ways, but it just doesnt find any order in the history.

You run it on a Daily chart ?

      XHoraPrimeiroCandle = iTime(_Symbol,_Period,0);
 
Alain Verleyen:

You run it on a Daily chart ?

It is on the M5 chart.


For instance, this piece of code below returns:


History: 1, 2, 3, 4...according to number of items in history. The only item working well.

Ticket: 2 (and stays stuck at 2 from the first order)

Symbol: WDO@N (this one is also OK)

Order: 2 (same as ticket)

DealType: 0 (stuck)

OrderType: 1 (stuck)

Profit: 0 (stuck)



double ProfitAll(datetime& XHoraPrimeiroCandle, double& XProfit)
   {
   int err=0;
   if (IsFirstBar())
      {
      XHoraPrimeiroCandle = iTime(_Symbol,_Period,0);
      }

   HistorySelect(XHoraPrimeiroCandle,TimeCurrent());
 
   for(int i = HistoryDealsTotal()-1; i >= 0; i--)
      {
      long ticket = HistoryDealGetTicket(i);
      string symbol = HistoryDealGetString(ticket,DEAL_SYMBOL);
      long magic = HistoryDealGetInteger(ticket,DEAL_MAGIC);
      long order = HistoryDealGetInteger(ticket,DEAL_ORDER);
      double dealType = HistoryDealGetInteger(ticket,DEAL_ENTRY);
      double orderType = HistoryDealGetInteger(ticket,DEAL_TYPE);
      double profitAtual = HistoryDealGetDouble(ticket,DEAL_PROFIT);

  Comment("Hora: ",XHoraPrimeiroCandle,"\nHistory: ",HistoryDealsTotal(),"\nTicket: ",ticket,"\nSymbol: ",symbol,"\nOrder: ",order,"\nDealType: ",dealType,"\nOrderType: ",orderType,"\nProfit: ",profitAtual);


}
 

Hi guys I think this is related to this topic hope someone can help me out :)


I would like to set rules for my trading account.


Daily loss/gain limit setting

Example: Equity = €1000. If Equity hits €950 (-5%) all open positions close, all open orders get cancelled and no more positions can be opened on this trading day.

Once Equity cross €1025 (+2,5%) the daily loss limit loss moves to €1000 (+-0%)
Once Equity cross €1050 (+5%) daily loss limit moves to €1025 (+2,5%) and so forth until the max daily profit is reached with +25% resulting again in closing of all open positions and cancelling all open orders for this trading day.


Setting max. trades per timeframe

Limiting the opening of positions. Example 3 positions max each 30minutes.


p.s. I will give a nice tip for the one who can explain and help setting this up for me :)