Download MetaTrader 5
To add comments, please log in or register
Join MQL5 Cloud Network. Improve your rating!
Nurudeen Amedu
39379
Nurudeen Amedu 2016.07.13 23:25 

i am trying to write all open trades to file but its not working, the file is created and 0 is written into it, please help

//+------------------------------------------------------------------+
//|                                                Write to file.mq4 |
//|                            Copyright 2016, Hudson Analytics Team |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, Hudson Analytics Team"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

int delay=1000;
int start,TickCount;
int Size=0,PrevSize=0;
int cnt,TotalCounter;
string cmt;
string nl="\n";

int OrdId[],PrevOrdId[];
string OrdSym[],PrevOrdSym[];
int OrdTyp[],PrevOrdTyp[];
double OrdLot[],PrevOrdLot[];
double OrdPrice[],PrevOrdPrice[];
double OrdSL[],PrevOrdSL[];
double OrdTP[],PrevOrdTP[];
int Counter;


int file_handle;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   get_positions();

   write_to_file();
   



}


   
  
//+------------------------------------------------------------------+
void get_positions() {
   //Counter=0;
  Size=OrdersTotal();
  if (Size!= PrevSize) {
    ArrayResize(OrdId,Size);
    ArrayResize(OrdSym,Size);
    ArrayResize(OrdTyp,Size);
    ArrayResize(OrdLot,Size);
    ArrayResize(OrdPrice,Size);
    ArrayResize(OrdSL,Size);
    ArrayResize(OrdTP,Size);
  }

  for(cnt=0;cnt<Size;cnt++) {

    OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
    OrdId[cnt]=OrderTicket();
    OrdSym[cnt]=OrderSymbol();
    OrdTyp[cnt]=OrderType();
    OrdPrice[cnt]=OrderOpenPrice();

  }  
  cmt=cmt+nl+"Size: "+Size;  
}   
   
bool compare_positions() {
  if (PrevSize != Size)return(true);
  for(int i=0;i<Size;i++) {
    
    if (PrevOrdPrice[i]!=OrdPrice[i])return(true);
    if (PrevOrdId[i]!=OrdId[i])return(true);
    if (PrevOrdSym[i]!=OrdSym[i])return(true);
    if (PrevOrdPrice[i]!=OrdPrice[i])return(true);
    if (PrevOrdTyp[i]!=OrdTyp[i])return(true);
    
  }    
  return(false);
}

void save_positions() {

  if (PrevSize != Size) {
    ArrayResize(PrevOrdId,Size);
    ArrayResize(PrevOrdSym,Size);
    ArrayResize(PrevOrdTyp,Size);
    ArrayResize(PrevOrdPrice,Size);
    PrevSize=Size;
  }
  
  
  for(int i=0;i<Size;i++) {
    PrevOrdId[i]=OrdId[i];
    PrevOrdSym[i]=OrdSym[i];
    PrevOrdTyp[i]=OrdTyp[i];
    PrevOrdPrice[i]=OrdPrice[i];
      
  Counter=Size;
   
    
      
    
  
  }

}


void write_to_file(){
         if(file_handle==0){
         file_handle = FileOpen("OpenTrades.csv",FILE_COMMON|FILE_CSV|FILE_WRITE|FILE_READ|FILE_REWRITE|FILE_SHARE_READ);
         
         for(int i=0;i=OrdersTotal();i++){
         FileWrite(file_handle,OrdId[i],OrdSym[i],OrdTyp[i],OrdPrice[i]);
         FileWrite(file_handle,i);
         //}
         }
      }

}
/
To add comments, please log in or register