MQL4 - automated forex trading   /  

Forum

Creating my own custom library

Back to topics list To post a new topic, please log in or register

avatar
76
inkexit 2008.07.02 23:56 

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?



article

Forex Trading ABC

Working on financial markets represents, first of all, trade operations. We all, starting from the very childhood, have an intuitive idea of what is to buy and to sell. But Forex trading is still something special. This article deals with the ideas necessary to explain some terms. We will also consider the MQL 4 functions that correspond with those terms.


avatar
2462
phy 2008.07.03 08:24 

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.

---

Back to topics list  

To add comments, please log in or register