I wrote an analytic script, want to take it a step further.. code included..

 

Hi all,

I wrote this script to extract all trades from order history from within specified dates based on order open time.

I also am gathering data such as Ticket# Symbol, Open/Close time, Open/Closing price, Profit/Loss, highest profit during trade, and lowest profit (drawdown) during trade..(need to add Magic Number to code)

I am including code, first any advice to make code more efficient would be great, and also would like to go a step further..

I would like to run this script on several accounts, then I would like to write another script that calculates winning percentage and number of wins/losses per Magic Number per account (each account is running several different Magic numbers).

Can anyone help on the second script as well? or point me in the right direction??

Here is code for first script..

Thanks in advance!

#property copyright "sammy515"
#property link      "http://www.forexmalibu.com"
#property show_inputs

extern datetime StartOpenTime = D'04.12.2012';
extern datetime EndOpenTime = D'06.12.2012';
string tradeOpenTime, tradeCloseTime;
int OpenBarShift, CloseBarShift, distShift;
double hipoint, lowpoint;
double hipips,lopips,hiprofit,loprofit,hipro,lopro, pipProfit;
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
  
//----------------
//Open CSV file for editing
//-----------------
string filedate = TimeToStr(StartOpenTime, TIME_DATE);
string filename = StringConcatenate(filedate,"_hilo.csv");
int handle = FileOpen( filename, FILE_CSV|FILE_READ|FILE_WRITE, ',');
   if(handle<1) 
      {
      Print(" File not found, last error: ", GetLastError());
      return(false);
      }
  FileWrite (handle, "Ticket #","Symbol","Open Time","Close Time","Open Price","Close Price","Profit/Loss","Highest Profit","Lowest Profit", "Trade Setup");
  
//----search history for closed orders
 int h, hstTotal=OrdersHistoryTotal();
   for(h=0;h<hstTotal;h++)
      {
      if (OrderSelect (h,SELECT_BY_POS, MODE_HISTORY)==true)
         {
         if (OrderOpenTime() > StartOpenTime && OrderOpenTime() < EndOpenTime)//if trade was opened within specified time
            {
            tradeOpenTime = TimeToStr(OrderOpenTime(), TIME_MINUTES);
            tradeCloseTime = TimeToStr(OrderCloseTime(), TIME_MINUTES);
            OpenBarShift = iBarShift(OrderSymbol(),1,OrderOpenTime(),true);
            CloseBarShift = iBarShift(OrderSymbol(),1,OrderCloseTime(),true);
            distShift = OpenBarShift - CloseBarShift;
            
            if(OrderType()==OP_BUY)
               {  
               hipoint = iHigh(OrderSymbol(), 1, iHighest(OrderSymbol(), 1, MODE_HIGH, distShift, CloseBarShift));
               lowpoint = iLow(OrderSymbol(), 1, iLowest(OrderSymbol(), 1, MODE_LOW, distShift, CloseBarShift));
               hipips =  hipoint - OrderOpenPrice();
               lopips =  lowpoint - OrderOpenPrice();
               }
               
            if(OrderType()==OP_SELL)
               {  
               hipoint = iHigh(OrderSymbol(), 1, iHighest(OrderSymbol(), 1, MODE_HIGH, distShift, CloseBarShift))+ (MarketInfo(OrderSymbol(),MODE_SPREAD)*MarketInfo(OrderSymbol(),MODE_POINT));
               lowpoint = iLow(OrderSymbol(), 1, iLowest(OrderSymbol(), 1, MODE_LOW, distShift, CloseBarShift))+ (MarketInfo(OrderSymbol(),MODE_SPREAD)*MarketInfo(OrderSymbol(),MODE_POINT));
               lopips =  OrderOpenPrice() - hipoint;
               hipips =  OrderOpenPrice()-lowpoint;
               Print (OrderSymbol(),MarketInfo(OrderSymbol(),MODE_SPREAD)*MarketInfo(OrderSymbol(),MODE_POINT));
               } 
            hiprofit = NormalizeDouble(hipips,MarketInfo(OrderSymbol(),MODE_DIGITS));
            loprofit = NormalizeDouble(lopips,MarketInfo(OrderSymbol(),MODE_DIGITS));
            if (MarketInfo(OrderSymbol(),MODE_DIGITS) == 3)
               {
               hipro = hiprofit*100;
               lopro = loprofit*100;
               }
            if (MarketInfo(OrderSymbol(),MODE_DIGITS) == 5)
               {
               hipro = hiprofit*10000;
               lopro = loprofit*10000;
               }   
            pipProfit= OrderProfit()/OrderLots()/MarketInfo(OrderSymbol(),MODE_TICKVALUE)*0.1;     
            FileSeek (handle, 0, SEEK_END);
            FileWrite (handle,OrderTicket(),OrderSymbol(), tradeOpenTime, tradeCloseTime, OrderOpenPrice(), OrderClosePrice(), NormalizeDouble(pipProfit,1), hipro, lopro, OrderComment());
            }//end OrderOpenTime Loop
         }//end OrderSelect loop
      }//end of hstTotal h loop
//----
   return(0);
  }
//+------------------------------------------------------------------+
 

ok I added Magic Number and account number to first collection of individual account data

code below..

#property copyright "sammy515"
#property link      "http://www.forexmalibu.com"
#property show_inputs

extern datetime StartOpenTime = D'04.12.2012';
extern datetime EndOpenTime = D'06.12.2012';
string tradeOpenTime, tradeCloseTime;
int OpenBarShift, CloseBarShift, distShift;
double hipoint, lowpoint;
double hipips,lopips,hiprofit,loprofit,hipro,lopro, pipProfit;
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
  
//----------------
//Open CSV file for editing
//-----------------
string filedate = TimeToStr(StartOpenTime, TIME_DATE);
string filename = StringConcatenate(filedate,"_",AccountNumber(),"_hilo.csv");
int handle = FileOpen( filename, FILE_CSV|FILE_READ|FILE_WRITE, ',');
   if(handle<1) 
      {
      Print(" File not found, last error: ", GetLastError());
      return(false);
      }
  FileWrite (handle,"Account Number", "Ticket #","Symbol","Magic","Open Time","Close Time","Open Price","Close Price","Profit/Loss","Highest Profit","Lowest Profit", "Trade Setup");
  
//----search history for closed orders
 int h, hstTotal=OrdersHistoryTotal();
   for(h=0;h<hstTotal;h++)
      {
      if (OrderSelect (h,SELECT_BY_POS, MODE_HISTORY)==true)
         {
         if (OrderOpenTime() > StartOpenTime && OrderOpenTime() < EndOpenTime)//if trade was opened within specified time
            {
            tradeOpenTime = TimeToStr(OrderOpenTime(), TIME_MINUTES);
            tradeCloseTime = TimeToStr(OrderCloseTime(), TIME_MINUTES);
            OpenBarShift = iBarShift(OrderSymbol(),1,OrderOpenTime(),true);
            CloseBarShift = iBarShift(OrderSymbol(),1,OrderCloseTime(),true);
            distShift = OpenBarShift - CloseBarShift;
            
            if(OrderType()==OP_BUY)
               {  
               hipoint = iHigh(OrderSymbol(), 1, iHighest(OrderSymbol(), 1, MODE_HIGH, distShift, CloseBarShift));
               lowpoint = iLow(OrderSymbol(), 1, iLowest(OrderSymbol(), 1, MODE_LOW, distShift, CloseBarShift));
               hipips =  hipoint - OrderOpenPrice();
               lopips =  lowpoint - OrderOpenPrice();
               }
               
            if(OrderType()==OP_SELL)
               {  
               hipoint = iHigh(OrderSymbol(), 1, iHighest(OrderSymbol(), 1, MODE_HIGH, distShift, CloseBarShift))+ (MarketInfo(OrderSymbol(),MODE_SPREAD)*MarketInfo(OrderSymbol(),MODE_POINT));
               lowpoint = iLow(OrderSymbol(), 1, iLowest(OrderSymbol(), 1, MODE_LOW, distShift, CloseBarShift))+ (MarketInfo(OrderSymbol(),MODE_SPREAD)*MarketInfo(OrderSymbol(),MODE_POINT));
               lopips =  OrderOpenPrice() - hipoint;
               hipips =  OrderOpenPrice()-lowpoint;
               Print (OrderSymbol(),MarketInfo(OrderSymbol(),MODE_SPREAD)*MarketInfo(OrderSymbol(),MODE_POINT));
               } 
            hiprofit = NormalizeDouble(hipips,MarketInfo(OrderSymbol(),MODE_DIGITS));
            loprofit = NormalizeDouble(lopips,MarketInfo(OrderSymbol(),MODE_DIGITS));
            if (MarketInfo(OrderSymbol(),MODE_DIGITS) == 3)
               {
               hipro = hiprofit*100;
               lopro = loprofit*100;
               }
            if (MarketInfo(OrderSymbol(),MODE_DIGITS) == 5)
               {
               hipro = hiprofit*10000;
               lopro = loprofit*10000;
               }   
            pipProfit= OrderProfit()/OrderLots()/MarketInfo(OrderSymbol(),MODE_TICKVALUE)*0.1;     
            FileSeek (handle, 0, SEEK_END);
            FileWrite (handle,AccountNumber(),OrderTicket(),OrderSymbol(),OrderMagicNumber(), tradeOpenTime, tradeCloseTime, OrderOpenPrice(), OrderClosePrice(), NormalizeDouble(pipProfit,1), hipro, lopro, OrderComment());
            }//end OrderOpenTime Loop
         }//end OrderSelect loop
      }//end of hstTotal h loop
//----
   return(0);
  }
//+------------------------------------------------------------------+

Now need to write the script to access the csv created and calculate number of wins and losses and percentage wins per Magic Number, and write results to new csv.

trying to create one master csv that will show win pct for magic numbers over several accounts..

I will prob need to manually copy all written csv's from each acct to one mt4 acct, then run script that will read each and extract info to master csv.

does anyone think this is possible? I am working on it, but if anyone thinks i am wasting time.. would appreciate a shout out!

 
sammy515: trying to create one master csv that will show win pct for magic numbers over several accounts. I will prob need to manually copy all written csv's from each acct to one mt4 acct, then run script that will read each and extract info to master csv. does anyone think this is possible? I am working on it, but if anyone thinks i am wasting time.. would appreciate a shout out!

Yeah its possible.

If you want to merge files from different mt4_terminals then you'll need programming outside of mql4.

I'm aware of one program which works with mt4_reports and generates all_information you mentioned.

Is it worth it... Well thats a question only you can answer. Are you doing this for clients? for learning? etc.

 
ubzen:

Yeah its possible.

If you want to merge files from different mt4_terminals then you'll need programming outside of mql4.

I'm aware of one program which works with mt4_reports and generates all_information you mentioned.

Is it worth it... Well thats a question only you can answer. Are you doing this for clients? for learning? etc.


Thanks ubzen,

I am doing it for analysis, i have programmed an ea with several different strategies all with many diff external variables, so I am running several instances side by side with different groups of settings to which i have seperated bby magic number.. since my ea is a multitimeframe ea, i cannot backtest using mt4, so must do the slow roll :(

so trying to find a quick way to compile all results.. so can see whats working and whats not..

 

Also, I have noticed an issue with my code, if i want to go back several months, the history is not there, I know i can increase the history manually but is there a way to do it programmatically?

thanks again ubzen for your quick response!

 
sammy515: I am doing it for analysis, i have programmed an ea with several different strategies all with many diff external variables, so I am running several instances side by side with different groups of settings to which i have seperated bby magic number.. since my ea is a multitimeframe ea, i cannot backtest using mt4, so must do the slow roll :( so trying to find a quick way to compile all results.. so can see whats working and whats not..
extern datetime StartOpenTime = D'04.12.2012';
extern datetime EndOpenTime = D'06.12.2012';

Have you been running them since the above dates?

How do you check your data integrity for missing_bars?

 
sammy515: Also, I have noticed an issue with my code, if i want to go back several months, the history is not there, I know i can increase the history manually but is there a way to do it programmatically? thanks again ubzen for your quick response!

Lol.... I was just asking that.

Im working on something similar... link below.

https://www.mql5.com/en/forum/15385.

Brokers usually* only allow you to download about 3_months of Period_M1 data.

I intend to force my users to download historical data. This cannot be auto-mated.

 
ubzen:

Have you been running them since the above dates?

How do you check your data integrity for missing_bars?


no i havent been using those dates, i just put those in there when i began coding it.. they are external so i change them per run

as far as integrity for missing bars..i added a line of code.. is this what you mean?

if(OpenBarShift<0) Print(OrderTicket()," Open Bar Missing, last error: ", GetLastError());
if(CloseBarShift<0) Print(OrderTicket()," Close Bar Missing, last error: ", GetLastError());

----

so here is the new code..

#property copyright "sammy515"
#property link      "http://www.forexmalibu.com"
#property show_inputs

extern datetime StartOpenTime = D'04.12.2012';
extern datetime EndOpenTime = D'06.12.2012';
string tradeOpenTime, tradeCloseTime;
int OpenBarShift, CloseBarShift, distShift;
double hipoint, lowpoint;
double hipips,lopips,hiprofit,loprofit,hipro,lopro, pipProfit;

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
  
//----------------
//Open CSV file for editing
//-----------------
string filedate = TimeToStr(StartOpenTime, TIME_DATE);
string filename = StringConcatenate(filedate,"_",AccountNumber(),"_hilo.csv");
int handle = FileOpen( filename, FILE_CSV|FILE_READ|FILE_WRITE, ',');
   if(handle<1) 
      {
      Print(" File not found, last error: ", GetLastError());
      return(false);
      }
  FileWrite (handle,"Account Number", "Ticket #","Symbol","Magic","Open Time","Close Time","Open Price","Close Price","Profit/Loss","Highest Profit","Lowest Profit", "Trade Setup");
  
//----search history for closed orders
 int h, hstTotal=OrdersHistoryTotal();
   for(h=0;h<hstTotal;h++)
      {
      if (OrderSelect (h,SELECT_BY_POS, MODE_HISTORY)==true)
         {
         if (OrderOpenTime() > StartOpenTime && OrderOpenTime() < EndOpenTime)//if trade was opened within specified time
            {
            tradeOpenTime = TimeToStr(OrderOpenTime(), TIME_DATE|TIME_MINUTES);
            tradeCloseTime = TimeToStr(OrderCloseTime(), TIME_DATE|TIME_MINUTES);
            OpenBarShift = iBarShift(OrderSymbol(),1,OrderOpenTime(),true);
            CloseBarShift = iBarShift(OrderSymbol(),1,OrderCloseTime(),true);
            distShift = OpenBarShift - CloseBarShift;
            
            if(OpenBarShift<0) Print(OrderTicket()," Open Bar Missing, last error: ", GetLastError());
            if(CloseBarShift<0) Print(OrderTicket()," Close Bar Missing, last error: ", GetLastError());
            
            if(OrderType()==OP_BUY)
               {  
               hipoint = iHigh(OrderSymbol(), 1, iHighest(OrderSymbol(), 1, MODE_HIGH, distShift, CloseBarShift));
               lowpoint = iLow(OrderSymbol(), 1, iLowest(OrderSymbol(), 1, MODE_LOW, distShift, CloseBarShift));
               hipips =  hipoint - OrderOpenPrice();
               lopips =  lowpoint - OrderOpenPrice();
               }
               
            if(OrderType()==OP_SELL)
               {  
               hipoint = iHigh(OrderSymbol(), 1, iHighest(OrderSymbol(), 1, MODE_HIGH, distShift, CloseBarShift))+ (MarketInfo(OrderSymbol(),MODE_SPREAD)*MarketInfo(OrderSymbol(),MODE_POINT));
               lowpoint = iLow(OrderSymbol(), 1, iLowest(OrderSymbol(), 1, MODE_LOW, distShift, CloseBarShift))+ (MarketInfo(OrderSymbol(),MODE_SPREAD)*MarketInfo(OrderSymbol(),MODE_POINT));
               lopips =  OrderOpenPrice()-hipoint;
               hipips =  OrderOpenPrice()-lowpoint;
               Print (OrderSymbol(),MarketInfo(OrderSymbol(),MODE_SPREAD)*MarketInfo(OrderSymbol(),MODE_POINT));
               } 
               
            hiprofit = NormalizeDouble(hipips,MarketInfo(OrderSymbol(),MODE_DIGITS));
            loprofit = NormalizeDouble(lopips,MarketInfo(OrderSymbol(),MODE_DIGITS));
            if (MarketInfo(OrderSymbol(),MODE_DIGITS) == 3)
               {
               hipro = hiprofit*100;
               lopro = loprofit*100;
               }
            if (MarketInfo(OrderSymbol(),MODE_DIGITS) == 5)
               {
               hipro = hiprofit*10000;
               lopro = loprofit*10000;
               }  
                
            pipProfit= OrderProfit()/OrderLots()/MarketInfo(OrderSymbol(),MODE_TICKVALUE)*0.1;   
            FileSeek (handle, 0, SEEK_END);
            FileWrite (handle,AccountNumber(),OrderTicket(),OrderSymbol(),OrderMagicNumber(), tradeOpenTime, tradeCloseTime, OrderOpenPrice(), OrderClosePrice(), NormalizeDouble(pipProfit,1), hipro, lopro, OrderComment());
            }//end OrderOpenTime Loop
         }//end OrderSelect loop
      }//end of hstTotal h loop
//----
   return(0);
  }
//+------------------------------------------------------------------+

everything works good as long as the history is there to calculate high point and low point during trade..

 
ubzen:

Lol.... I was just asking that.

Im working on something similar... link below.

https://www.mql5.com/en/forum/15385.

Brokers usually* only allow you to download about 3_months of Period_M1 data.

I intend to force my users to download historical data. This cannot be auto-mated.



thanks for that, still cant figure out solution.. I thought I saw someone on this board making an attempt, not sure who..

used a function like EnableAllHistory().. something like that..

just cant find it on the forum or by searching..

aaahhh!! just found it, but not sure how to use it..

can this work?

https://www.mql5.com/en/forum/138127

 
sammy515:


no i havent been using those dates, i just put those in there when i began coding it.. they are external so i change them per run

as far as integrity for missing bars..i added a line of code.. is this what you mean?

----

so here is the new code..

everything works good as long as the history is there to calculate high point and low point during trade..

Doesn't it cause you to use wrong values when the BarShift returns -1? I mean this is for personal use and probably not a big_deal as you get a comment and just dis_reguard the report.

But yeah, I was also thinking about missing bars in_between the OrderOpenTime() and OrderCloseTime(). I don't think there's any legit_methods of handling these except just Printing a Warning. Reason I feel this way is because there's not of telling if the missing bar is due to {No_Ticks that Minute} || {Bad_Data}.

So in the end, we can only perform these with the assumption || acceptance that the data we have is enough.

Do you plan on using dlls or were you hoping to use mql only?

Was your only question about getting the data?

 
sammy515:


thanks for that, still cant figure out solution.. I thought I saw someone on this board making an attempt, not sure who..

used a function like EnableAllHistory().. something like that..

just cant find it on the forum or by searching..

aaahhh!! just found it, but not sure how to use it..

can this work?

https://www.mql5.com/en/forum/138127

That one is used to automatically force the history to allow all History. Example: should the date range be set for Last_Month instead of All_History, then trades 3_Months earlier wouldn't be within Mode_History. It doesn't help give you more history from your broker.

Its also using a .dll instead of pure mql.

Reason: