need help with logic for trading log

 

good day to you all :)

following code is of a trading log function i have written, which writes a number of variables to a file. its all working good besides it writes each order over and over again by each new run of the loop. now i was kind of suspecting this to happen and wanted to come up with a piece of logic that would check if a particular order in orderhistory had already been written or not. however, i have not been able to come up with a solution... perhaps one of you guys have an idea? you can see i have added a comment for buy and sell orders respectively where i was imagining the code to perform the check.

void TRADING_LOG()
{ 
   int handle=FileOpen("random_log",FILE_READ|FILE_WRITE|FILE_CSV,';');
  
   FileSeek(handle,0,SEEK_END);
  
   for(int i=0;i<OrdersHistoryTotal();i++)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
      {
         if(OrderType()==OP_BUY /*&&  condition that this order isnt already in log*/)
         {
            double OrderDuration = OrderCloseTime()-OrderOpenTime();
            double Initial_SLPrice = OrderOpenPrice()-StopLoss;
            double StopLoss_Real = OrderOpenPrice()-Initial_SLPrice;
            double PL_inPips =(OrderClosePrice()-OrderOpenPrice())*Point;
            double Risk_Reward_Actual = PL_inPips/StopLoss_Real;
            double Risk_Reward_Planned= OrderTakeProfit()/StopLoss_Real;
            if(OrderProfit()<0) int TradePL=-1; else TradePL=1;
           
            FileWrite(handle,OrderTicket(),OrderSymbol(),OrderType(),OrderOpenTime(),OrderCloseTime(),OrderDuration,
            OrderLots(),OrderOpenPrice(),OrderClosePrice(),Initial_SLPrice,OrderTakeProfit(), TradePL,PL_inPips,Risk_Reward_Actual, Risk_Reward_Planned);
         }
        
         else if(OrderType()==OP_SELL /*&&  condition that this order isnt already in log*/)
         {
            OrderDuration = OrderCloseTime()-OrderOpenTime();
            Initial_SLPrice = OrderOpenPrice()+StopLoss;
            StopLoss_Real = OrderOpenPrice()+Initial_SLPrice;
            PL_inPips =(OrderOpenPrice()-OrderClosePrice())*Point;
            Risk_Reward_Actual = PL_inPips/StopLoss_Real;
            Risk_Reward_Planned= OrderTakeProfit()/StopLoss_Real;
            if(OrderProfit()<0) TradePL=-1; else TradePL=1;
           
            FileWrite(handle,OrderTicket(),OrderSymbol(),OrderType(),OrderOpenTime(),OrderCloseTime(),OrderDuration,
            OrderLots(),OrderOpenPrice(),OrderClosePrice(),Initial_SLPrice,OrderTakeProfit(), TradePL,PL_inPips,Risk_Reward_Actual, Risk_Reward_Planned);
         }
       }
    }
  
   FileClose(handle);

}
 
master_dude: check if a particular order in orderhistory had already been written or not. however, i have not been able to come up with a solution...
Read the file in OnInit(), find the highest ticket number. Only append higher ones.
 
Could use a bool flag for this, within the portion that you write to the file, set it to false, and do the test WH suggested.  If it is higher than the last one you wrote, set that flag true and only write to the file if the flag is true.
 
WHRoeder:
Read the file in OnInit(), find the highest ticket number. Only append higher ones.
thanks this worked for me. i create a variable called Log_Ticket, which updates at each run of the loop
Reason: