Download MetaTrader 5
To add comments, please log in or register
Having troubles understanding the code? Articles can help you!
master_dude
73
master_dude 2015.07.23 08:44 

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);

}
whroeder1
13656
whroeder1 2015.07.23 12:09  
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.
JD4
1101
JD4 2015.07.23 19:09  
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.
master_dude
73
master_dude 2015.07.24 10:58  
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
/
To add comments, please log in or register