SVM_EA_v25c_06

Author: Copyright � 2007, MetaQuotes Software Corp.
Price Data Components
Series array that contains close prices for each barSeries array that contains the lowest prices of each barSeries array that contains the highest prices of each barSeries array that contains open time of each bar
Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedChecks for the total of closed orders
Indicators Used
Moving average indicator
Miscellaneous
Uses files from the file systemIt writes information to file
0 Views
0 Downloads
0 Favorites
SVM_EA_v25c_06
//+------------------------------------------------------------------+
//|                                                   SVM_EA_v06.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
#include <stdlib.mqh>
//#include <WinUser32.mqh>
#include <SVM_EA_inc_3_2.mqh>
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
#define NR_OF_SAMPLE_LINES   2000
#define NR_OF_TEST_LINES     800
#define NR_OF_TEST_BARS      10
#define NR_OF_ATTRIB         27
#define FUTURE_WINDOW        200
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
extern int     TrainTimeFrame=PERIOD_M5;
extern int     TrainPeriod=PERIOD_M5;
extern int     PredictPeriod=PERIOD_M5;
extern int     StopLoss=70;
extern int     TakeProfit=70;
extern int     PipStep=10;
extern int     TimeStep=10; //minutes
extern double  Lots=0.1;
extern datetime BeginDate= D'2008.09.01 00:00';
extern double C=32.0;
extern double gamma=2.0;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int     MaxOrdersTotal1=1;
//int     MaxOrdersTotal2=1;
//int     MaxOrdersTotal3=1;

int      myBars1;
int      myBars2;

int      MagicNumber1 = 1001;
//int      MagicNumber2 = 1002;
//int      MagicNumber3 = 1003;

double t[NR_OF_SAMPLE_LINES];
double c[NR_OF_SAMPLE_LINES];
double l[NR_OF_SAMPLE_LINES];
double h[NR_OF_SAMPLE_LINES];       
double ma5_M5[NR_OF_SAMPLE_LINES];
double ma10_M5[NR_OF_SAMPLE_LINES];
double ma15_M5[NR_OF_SAMPLE_LINES];
double ma5_H1[NR_OF_SAMPLE_LINES];
double ma10_H1[NR_OF_SAMPLE_LINES];
double ma15_H1[NR_OF_SAMPLE_LINES];
double ma5_H4[NR_OF_SAMPLE_LINES];
double ma10_H4[NR_OF_SAMPLE_LINES];
double ma15_H4[NR_OF_SAMPLE_LINES];

double _ma5_M5[NR_OF_TEST_LINES];
double _ma10_M5[NR_OF_TEST_LINES];
double _ma15_M5[NR_OF_TEST_LINES];
double _ma5_H1[NR_OF_TEST_LINES];
double _ma10_H1[NR_OF_TEST_LINES];
double _ma15_H1[NR_OF_TEST_LINES];
double _ma5_H4[NR_OF_TEST_LINES];
double _ma10_H4[NR_OF_TEST_LINES];
double _ma15_H4[NR_OF_TEST_LINES];

double ptr_predict_label_1[NR_OF_TEST_LINES];
//double ptr_predict_label_2[NR_OF_TEST_LINES];
//double ptr_predict_label_3[NR_OF_TEST_LINES];
//double train_result[NR_OF_SAMPLE_LINES];
//double train_time[NR_OF_SAMPLE_LINES];

string file_name1="c:\\trade_model_dyn_3tf_1_.txt";
//string file_name2="c:\\trade_model_dyn_3tf_2_.txt";
//string file_name3="c:\\trade_model_dyn_3tf_3_.txt";

int NrTrainBars=NR_OF_SAMPLE_LINES;
int NrTestBars=NR_OF_TEST_LINES;   
int NrAttrib=NR_OF_ATTRIB;
int FutureWindow=FUTURE_WINDOW;
double Point1;
int spread=0;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   file_name1="c:\\"+Symbol()+"_SVM_model_dyn_3tf_1_.txt";
   Point1=Point;
   spread=MarketInfo(Symbol(),MODE_SPREAD);
   _train_1();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
 if (Time[0]<BeginDate) 
   {
    return(0);
   }
 if(NrOrders(MagicNumber1 )<MaxOrdersTotal1)             
 {   
   if (isNewBar1(TrainPeriod))
   {
      _train_1();   
   }   

   if (isNewBar2(PredictPeriod))
   {
 
      _predict_1();   
       int res1=0;
/*       
       res1=ptr_predict_label_1[0];
       Print("-->",res1);   
*/       

       int buy=0;
       int sell=0; 
       string s1="";
       for(int q=NR_OF_TEST_BARS;q>=0;q--)
       {       
         if (ptr_predict_label_1[q]==1)
         {
           buy++;   
           s1=s1+"0";
         }  
         if (ptr_predict_label_1[q]==-1)
         {
           sell++;              
           s1=s1+"1";
         }  
       }  
       if(buy>sell)
         res1=1;
       else if(buy<sell)
         res1=-1;
       Print("class=",res1,"->",s1);              
      double LastSellPrice=FindLastSellPrice(MagicNumber1);
      double LastBuyPrice=FindLastBuyPrice(MagicNumber1);
      Print("LastBuyPrice=",LastBuyPrice," ->LastSellPrice=",LastSellPrice); 
      double LastSellProfit=FindLastSellProfit(MagicNumber1);
      double LastBuyProfit=FindLastBuyProfit(MagicNumber1);
      Print("LastBuyProfit=",LastBuyProfit," ->LastSellProfit=",LastSellProfit); 
      datetime LastSellCloseTime=FindLastSellCloseTime(MagicNumber1);
      datetime LastBuyCloseTime=FindLastBuyCloseTime(MagicNumber1);
      Print("LastBuyCloseTime=",TimeToStr(LastBuyCloseTime,TIME_DATE|TIME_MINUTES),
         " ->LastSellCloseTime=",TimeToStr(LastSellCloseTime,TIME_DATE|TIME_MINUTES),
         " ->TimeCurrent=",TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES)); 
       
       // Trade
       if (res1==-1) 
       {
            
           
           //if((Bid - LastSellPrice)>=(PipStep*Point))
           //if(LastSellProfit>=0)
           ///if((TimeCurrent()-LastSellCloseTime)>=TimeStep*60)
           Sell(MagicNumber1,Lots,TakeProfit,StopLoss );
       }  
       else if (res1==1) 
       {
           
           
           //if((LastBuyPrice - Ask)>=(PipStep*Point))      
           //if(LastBuyProfit>=0)
           //if((TimeCurrent()-LastBuyCloseTime)>=TimeStep*60)
           Buy(MagicNumber1,Lots,TakeProfit,StopLoss );
       }  

   }       
 }
//----
   return(0);
  }
  
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

bool isNewBar1(int tf1)
  {
//----
   bool res=false;
   if (myBars1!=iBars(Symbol(),tf1))
   {
      res=true;
      myBars1=iBars(Symbol(),tf1);
   }   
//----
   return(res);
  }
  
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

bool isNewBar2(int tf1)
  {
//----
   bool res=false;
   if (myBars2!=iBars(Symbol(),tf1))
   {
      res=true;
      myBars2=iBars(Symbol(),tf1);
   }   
//----
   return(res);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int NrOrders(int Magic ) 
{
   int total = OrdersTotal();
   int nrLongs = 0;
   int nrShorts = 0;
   for(int cnt=0; cnt<total; cnt++) {
      OrderSelect(cnt, SELECT_BY_POS);
      if( OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic )
         continue;
      if (OrderType()==OP_BUY) nrLongs++;
      if (OrderType()==OP_SELL) nrShorts++;
   }
   return(nrLongs+nrShorts);
}
    
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

void Buy(int Magic,double Lots,int TakeProfit, int StopLoss )
{
 
    int ticket=OrdSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"SVM_EA",Magic,0,Green);
    if(ticket>0)
    {
      if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
    }
    else 
      Print("Error opening BUY order : ",GetLastError()); 
     
    
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

void Sell(int Magic,double Lots,int TakeProfit, int StopLoss )
{
    int ticket=OrdSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"SVM_EA",Magic,0,Red);
    if(ticket>0)
    {
      if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
    }
    else 
      Print("Error opening SELL order : ",GetLastError()); 
   

}         

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

void init_train_bars()
{
   for(int i=0;i<NR_OF_SAMPLE_LINES;i++)
   {

      t[i]=iTime(NULL,TrainTimeFrame,i);
      c[i]=iClose(NULL,TrainTimeFrame,i);
      l[i]=iLow(NULL,TrainTimeFrame,i);
      h[i]=iHigh(NULL,TrainTimeFrame,i);
      
      ma5_M5[i]=iMA(NULL,PERIOD_M5,5,0,MODE_SMA,PRICE_CLOSE,i);
      ma10_M5[i]=iMA(NULL,PERIOD_M5,10,0,MODE_SMA,PRICE_CLOSE,i);
      ma15_M5[i]=iMA(NULL,PERIOD_M5,15,0,MODE_SMA,PRICE_CLOSE,i);  

      ma5_H1[i]=iMA(NULL,PERIOD_H1,5,0,MODE_SMA,PRICE_CLOSE,i);
      ma10_H1[i]=iMA(NULL,PERIOD_H1,10,0,MODE_SMA,PRICE_CLOSE,i);
      ma15_H1[i]=iMA(NULL,PERIOD_H1,15,0,MODE_SMA,PRICE_CLOSE,i);  

      ma5_H4[i]=iMA(NULL,PERIOD_H4,5,0,MODE_SMA,PRICE_CLOSE,i);
      ma10_H4[i]=iMA(NULL,PERIOD_H4,10,0,MODE_SMA,PRICE_CLOSE,i);
      ma15_H4[i]=iMA(NULL,PERIOD_H4,15,0,MODE_SMA,PRICE_CLOSE,i);  

   }
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

void _train_1()
{
   init_train_bars();
   int xx=SvmCreateDyn_3tf_1_par( 
                        t,
                        c,
                        l,
                        h,
                        ma5_M5,
                        ma10_M5,
                        ma15_M5,
                        ma5_H1,
                        ma10_H1,
                        ma15_H1,
                        ma5_H4,
                        ma10_H4,
                        ma15_H4,                        
                        NrTrainBars,
                        NrAttrib,
                        FutureWindow,
                        StopLoss,
                        TakeProfit,
                        Point1,
                        spread,
                        file_name1,
                        C,
                        gamma);   
   //Print("SvmCreateDyn_3tf_1: ",xx);

}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

void init_predict_bars()
{
   for(int i=0;i<NR_OF_TEST_LINES;i++)
   {
      
      _ma5_M5[i]=iMA(NULL,PERIOD_M5,5,0,MODE_SMA,PRICE_CLOSE,i);
      _ma10_M5[i]=iMA(NULL,PERIOD_M5,10,0,MODE_SMA,PRICE_CLOSE,i);
      _ma15_M5[i]=iMA(NULL,PERIOD_M5,15,0,MODE_SMA,PRICE_CLOSE,i);  

      _ma5_H1[i]=iMA(NULL,PERIOD_H1,5,0,MODE_SMA,PRICE_CLOSE,i);
      _ma10_H1[i]=iMA(NULL,PERIOD_H1,10,0,MODE_SMA,PRICE_CLOSE,i);
      _ma15_H1[i]=iMA(NULL,PERIOD_H1,15,0,MODE_SMA,PRICE_CLOSE,i);  

      _ma5_H4[i]=iMA(NULL,PERIOD_H4,5,0,MODE_SMA,PRICE_CLOSE,i);
      _ma10_H4[i]=iMA(NULL,PERIOD_H4,10,0,MODE_SMA,PRICE_CLOSE,i);
      _ma15_H4[i]=iMA(NULL,PERIOD_H4,15,0,MODE_SMA,PRICE_CLOSE,i);  
      
      ptr_predict_label_1[i]=0;
      //ptr_predict_label_2[i]=0;
      //ptr_predict_label_3[i]=0;            
   }

}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

void _predict_1()
{
  init_predict_bars();
  int xx=SvmPredictDyn_3tf_1(
                     _ma5_M5,
                     _ma10_M5,
                     _ma15_M5,
                     _ma5_H1,
                     _ma10_H1,
                     _ma15_H1,
                     _ma5_H4,
                     _ma10_H4,
                     _ma15_H4,
							NrTestBars,
							NrAttrib,
							Point1,
							file_name1,
							ptr_predict_label_1
							);							
							
   //Print("SvmPredictDyn_3tf_1: ",xx,
   //   " ptr_predict_label_1[0]:",ptr_predict_label_1[0]
   //);
}


int Write(string str)
{
//Write log file
   string filename="SVM_EA.log";
   int handle; 
   handle = FileOpen(filename,FILE_READ|FILE_WRITE|FILE_CSV,"/t");
   FileSeek(handle, 0, SEEK_END);      
   FileWrite(handle," Time " + TimeToStr(CurTime(),TIME_DATE|TIME_SECONDS) + ": " + str);
   FileClose(handle);
	Print(str);
}
int OrdSend(string _symbol, int _cmd, double _volume, double _price, int _slippage, double _stoploss, double _takeprofit, string _comment="", int _magic=0, datetime _expiration=0, color _arrow_color=CLR_NONE) 
{
int NumberOfTries=10;
int RetryTime=2;
//Send order with retry capabilities and log
int _stoplevel=MarketInfo(_symbol,MODE_STOPLEVEL);
double _priceop=0;
int ticket,err,tries;
tries = 0;
switch (_cmd) {
    case OP_BUY:
        if (!IsTradeContextBusy() && IsTradeAllowed()) {
        while (tries < NumberOfTries) {
            RefreshRates();
            ticket = OrderSend(_symbol,OP_BUY,_volume,Ask,_slippage,NormalizeDouble(_stoploss,Digits),NormalizeDouble(_takeprofit,Digits),_comment,_magic,_expiration,_arrow_color);
            if(ticket<=0) {
                Write("Error Occured : "+ErrorDescription(GetLastError()));
                Write(Symbol()+" Buy @ "+Ask+" SL @ "+_stoploss+" TP @"+_takeprofit+" ticket ="+ticket);
                tries++;
            } else {
                tries = NumberOfTries;
                Write("Order opened : "+Symbol()+" Buy @ "+Ask+" SL @ "+_stoploss+" TP @"+_takeprofit+" ticket ="+ticket);
            }
            Sleep(RetryTime*1000);
        }
        }
        err=ticket;
        break;
 
    case OP_SELL:
        if (!IsTradeContextBusy() && IsTradeAllowed()) {
        while (tries < NumberOfTries) {
            RefreshRates();
            ticket = OrderSend(_symbol,OP_SELL,_volume,Bid,_slippage,NormalizeDouble(_stoploss,Digits),NormalizeDouble(_takeprofit,Digits),_comment,_magic,_expiration,_arrow_color);
            if(ticket<=0) {
                Write("Error Occured : "+ErrorDescription(GetLastError()));
                Write(Symbol()+" Sell @ "+Bid+" SL @ "+_stoploss+" TP @"+_takeprofit+" ticket ="+ticket);
                tries++;
            } else {
                tries = NumberOfTries;
                Write("Order opened : "+Symbol()+" Sell @ "+Bid+" SL @ "+_stoploss+" TP @"+_takeprofit+" ticket ="+ticket);
            }
            Sleep(RetryTime*1000);    
        }
        }
        err=ticket;
        break;
 
    case OP_BUYSTOP:
        while (tries < NumberOfTries) {
            RefreshRates();
            if ((_price-Ask)<_stoplevel*Point) _priceop=Ask+_stoplevel*Point; else _priceop=_price;            
            ticket = OrderSend(_symbol,OP_BUYSTOP,_volume,NormalizeDouble(_priceop,Digits),_slippage,NormalizeDouble(_stoploss,Digits),NormalizeDouble(_takeprofit,Digits),_comment,_magic,_expiration,_arrow_color);
            if(ticket<=0) {
                Write("Error Occured : "+ErrorDescription(GetLastError()));
                Write(Symbol()+" Buy Stop @ "+_priceop+" SL @ "+_stoploss+" TP @"+_takeprofit+" ticket ="+ticket);
                tries++;
            } else {
                tries = NumberOfTries;
                Write("Order opened : "+Symbol()+" Buy Stop@ "+_priceop+" SL @ "+_stoploss+" TP @"+_takeprofit+" ticket ="+ticket);
            }
            Sleep(RetryTime*1000);
        }
        err=ticket;
        break;
 
 
    case OP_SELLSTOP:
        if (!IsTradeContextBusy() && IsTradeAllowed()) {
        while (tries < NumberOfTries) {
            RefreshRates();
            if ((Bid-_price)<_stoplevel*Point) _priceop=Bid-_stoplevel*Point; else _priceop=_price;            
            ticket = OrderSend(_symbol,OP_SELLSTOP,_volume,NormalizeDouble(_priceop,Digits),_slippage,NormalizeDouble(_stoploss,Digits),NormalizeDouble(_takeprofit,Digits),_comment,_magic,_expiration,_arrow_color);
            if(ticket<=0) {
                Write("Error Occured : "+ErrorDescription(GetLastError()));
                Write(Symbol()+" Sell Stop @ "+_priceop+" SL @ "+_stoploss+" TP @"+_takeprofit+" ticket ="+ticket);
                tries++;
            } else {
                tries = NumberOfTries;
                Write("Order opened : "+Symbol()+" Sell Stop @ "+_priceop+" SL @ "+_stoploss+" TP @"+_takeprofit+" ticket ="+ticket);
            }
            Sleep(RetryTime*1000);
        }
        }
        err=ticket;
        break;
 
 
    case OP_BUYLIMIT:
        if (!IsTradeContextBusy() && IsTradeAllowed()) {
        while (tries < NumberOfTries) {
            RefreshRates();
            if ((Ask-_price)<_stoplevel*Point) _priceop=Ask-_stoplevel*Point; else _priceop=_price;            
            ticket = OrderSend(_symbol,OP_BUYLIMIT,_volume,NormalizeDouble(_priceop,Digits),_slippage,NormalizeDouble(_stoploss,Digits),NormalizeDouble(_takeprofit,Digits),_comment,_magic,_expiration,_arrow_color);
            if(ticket<=0) {
                Write("Error Occured : "+ErrorDescription(GetLastError()));
                Write(Symbol()+" Buy Limit @ "+_priceop+" SL @ "+_stoploss+" TP @"+_takeprofit+" ticket ="+ticket);
                tries++;
            } else {
                tries = NumberOfTries;
                Write("Order opened : "+Symbol()+" Buy Limit @ "+_priceop+" SL @ "+_stoploss+" TP @"+_takeprofit+" ticket ="+ticket);
            }
            Sleep(RetryTime*1000);
        }
        }
        err=ticket;
        break;
 
    case OP_SELLLIMIT:
        if (!IsTradeContextBusy() && IsTradeAllowed()) {
        while (tries < NumberOfTries) {
            RefreshRates();
            if ((_price-Bid)<_stoplevel*Point) _priceop=Bid+_stoplevel*Point; else _priceop=_price;            
            ticket = OrderSend(_symbol,OP_BUYLIMIT,_volume,NormalizeDouble(_priceop,Digits),_slippage,NormalizeDouble(_stoploss,Digits),NormalizeDouble(_takeprofit,Digits),_comment,_magic,_expiration,_arrow_color);
            if(ticket<=0) {
                Write("Error Occured : "+ErrorDescription(GetLastError()));
                Write(Symbol()+" Sell Limit @ "+_priceop+" SL @ "+_stoploss+" TP @"+_takeprofit+" ticket ="+ticket);
                tries++;
            } else {
                tries = NumberOfTries;
                Write("Order opened : "+Symbol()+" Sell Limit @ "+_priceop+" SL @ "+_stoploss+" TP @"+_takeprofit+" ticket ="+ticket);
            }
            Sleep(RetryTime*1000);
        }
        err=ticket;
        }
        break;
 
    default:
        Write("No valid type of order found");
        err=-1;
        break;
}
return(err);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double FindLastBuyPrice(int MagicNumber)
{
            double oldorderopenprice=0, orderprice;
            int cnt, oldticketnumber=0, ticketnumber;
            for(cnt=OrdersHistoryTotal()-1;cnt>=0;cnt--)
              {
               OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY);
               if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
                  continue;
               if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderType()==OP_BUY)
                 {
                  ticketnumber=OrderTicket();
                  if(ticketnumber>oldticketnumber)
                    {
                     orderprice=OrderOpenPrice();
                     oldorderopenprice=orderprice;
                     oldticketnumber=ticketnumber;
                    }
                 }
              }
  return(orderprice);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double FindLastSellPrice(int MagicNumber)
{
            double oldorderopenprice=0, orderprice;
            int cnt, oldticketnumber=0, ticketnumber;
            for(cnt=OrdersHistoryTotal()-1;cnt>=0;cnt--)
              {
               OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY);
               if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
                  continue;
               if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderType()==OP_SELL)
                 {
                  ticketnumber=OrderTicket();
                  if(ticketnumber>oldticketnumber)
                    {
                     orderprice=OrderOpenPrice();
                     oldorderopenprice=orderprice;
                     oldticketnumber=ticketnumber;
                    }
                 }
              }
  return(orderprice);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double FindLastBuyProfit(int MagicNumber)
{
            double oldorderopenprice=0, orderprice;
            int cnt, oldticketnumber=0, ticketnumber;
            for(cnt=OrdersHistoryTotal()-1;cnt>=0;cnt--)
              {
               OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY);
               if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
                  continue;
               if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderType()==OP_BUY)
                 {
                  ticketnumber=OrderTicket();
                  if(ticketnumber>oldticketnumber)
                    {
                     orderprice=OrderProfit();
                     oldorderopenprice=orderprice;
                     oldticketnumber=ticketnumber;
                    }
                 }
              }
  return(orderprice);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double FindLastSellProfit(int MagicNumber)
{
            double oldorderopenprice=0, orderprice;
            int cnt, oldticketnumber=0, ticketnumber;
            for(cnt=OrdersHistoryTotal()-1;cnt>=0;cnt--)
              {
               OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY);
               if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
                  continue;
               if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderType()==OP_SELL)
                 {
                  ticketnumber=OrderTicket();
                  if(ticketnumber>oldticketnumber)
                    {
                     orderprice=OrderProfit();
                     oldorderopenprice=orderprice;
                     oldticketnumber=ticketnumber;
                    }
                 }
              }
  return(orderprice);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
datetime FindLastBuyCloseTime(int MagicNumber)
{
            double oldorderopenprice=0;
            datetime orderprice;
            int cnt, oldticketnumber=0, ticketnumber;
            for(cnt=OrdersHistoryTotal()-1;cnt>=0;cnt--)
              {
               OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY);
               if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
                  continue;
               if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderType()==OP_BUY)
                 {
                  ticketnumber=OrderTicket();
                  if(ticketnumber>oldticketnumber)
                    {
                     orderprice=OrderCloseTime();
                     oldorderopenprice=orderprice;
                     oldticketnumber=ticketnumber;
                    }
                 }
              }
  return(orderprice);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
datetime FindLastSellCloseTime(int MagicNumber)
{
            double oldorderopenprice=0;
            datetime orderprice;
            int cnt, oldticketnumber=0, ticketnumber;
            for(cnt=OrdersHistoryTotal()-1;cnt>=0;cnt--)
              {
               OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY);
               if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
                  continue;
               if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderType()==OP_SELL)
                 {
                  ticketnumber=OrderTicket();
                  if(ticketnumber>oldticketnumber)
                    {
                     orderprice=OrderCloseTime();
                     oldorderopenprice=orderprice;
                     oldticketnumber=ticketnumber;
                    }
                 }
              }
  return(orderprice);
}


Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---