Creating my own custom library

 

I have created the the following code and placed it in the library folder, as well as the experst folder.  I also renamed it MyLib.mqh,  and I also renamed the ex4 resulting from compiling MyLib.mqh. :

//+------------------------------------------------------------------+
//|MyLib.mq4                                                       |
//+------------------------------------------------------------------+
#property library
 
 
//+------------------------------------------------------------------+
//|                                                   TrailStop1.mq4 |
//|                                 Copyright © 2008, . |
//|                                                                  |
//+------------------------------------------------------------------+
 
int TrailStop1(int TrailStart,int TrailPercentageStart, int TrailPercentageStop,
                  int TrailFirstPercentage,int TrailFinalPercentage)
{
 
   double TrailStopProfit;    
   double TrailStopStop;
 
   //if there are any orders open
   if(OrdersTotal()>0)
   {
      //look through each order one by one
      for(int i=OrdersTotal()-1;i>=0;i--)
      {
         OrderSelect(i,SELECT_BY_POS); 
      
         //POSITIVE STOP LOSS
         //(if trade is positive)
      
         //if order is a sell 
         if(OrderType() == OP_SELL)
         {
            TrailStopProfit = OrderOpenPrice()-Bid;
             if (Bid<OrderOpenPrice()-TrailStart && 
                  Bid>OrderOpenPrice()-TrailPercentageStart)
            {
                 TrailStopStop=OrderOpenPrice()-1*Point;
             }
             if (Bid<=OrderOpenPrice()-TrailPercentageStart && 
                  Bid>OrderOpenPrice()-TrailPercentageStop)
            {
                 TrailStopStop = OrderOpenPrice() - (TrailStopProfit*TrailFirstPercentage*0.01);
            }
             if (Bid <= OrderOpenPrice() -  TrailPercentageStop)
            {
                 TrailStopStop = OrderOpenPrice() - (TrailStopProfit*TrailFinalPercentage*0.01);
            }
            //if new calculated stop is less than previous stop (more profit)
            if (TrailStopStop < OrderStopLoss())
            {
               //if there has been at least 10 seconds since last order modify
               if(TimeCurrent()>OrderOpenTime()+30)
               {
            
                  OrderModify(OrderTicket(),OrderOpenPrice(),TrailStopStop,
                              OrderTakeProfit(),0,NULL);
               }
            }
          }   
      
         //if order is a Buy 
         if(OrderType() == OP_BUY)
         {
            TrailStopProfit = Ask-OrderOpenPrice();
             if (Ask>OrderOpenPrice()+TrailStart && 
                  Ask<OrderOpenPrice()+TrailPercentageStart)
            {
                 TrailStopStop=OrderOpenPrice()+1*Point;
             }
             if (Ask>=OrderOpenPrice()+TrailPercentageStart && 
                  Ask<OrderOpenPrice()+TrailPercentageStop)
            {
                 TrailStopStop = OrderOpenPrice() + (TrailStopProfit*TrailFirstPercentage*0.01);
            }
             if (Ask >= OrderOpenPrice() +  TrailPercentageStop)
            {
                 TrailStopStop = OrderOpenPrice() + (TrailStopProfit*TrailFinalPercentage*0.01);
            }
            //if new calculated stop is less than previous stop (more profit)
            if (TrailStopStop > OrderStopLoss())
            {
               //if there has been at least 10 seconds since last order modify
               if(TimeCurrent()>OrderOpenTime()+30)
               {
            
                  OrderModify(OrderTicket(),OrderOpenPrice(),TrailStopStop,
                              OrderTakeProfit(),0,NULL);
               }
            }
          }   
      }
   }
}

Then I created a simple EA with a call to MyLib(.mqh, .mq4, .ex4 (each in turn)):







#include <MyLib.mq4>
 
int start()
{
TrailStop1(3,10,20,50,60);
 
return(0);
}

and I get this error:

'RyanLib.mq4' - cannot open the program file C:\Program Files\MetaTrader - Alpari UK\experts\use library.mq4 (2, 1)

The documentation is very scarce concerning how to create your own libraries.  It basically just says that it is possible, and deosn't describe any of the details.

What am I doing wrong?


 

MQH -- I suppose that means MetaQuotes Header

Open metaeditor.

Create new file, check Include.mqh

Give it a name, Functions.mqh

Paste your code above into the editor.

Delete the line -- #property library -- as it does not belong in an MQH file

Compile it, just to see if there are errors. If so, correct the errors.

---

In your main program:

#include <Functions.mqh>

Compile and run the main program.

---

Reason: