17 C
New York
Saturday, November 16, 2024

Introduction to Robotic Forex Trading with Metatrader 4

Think of this post as the first set of lecture notes for the course, Robotic Forex trading 101. Just like with any course their are some prerequisites.

Prerequisite #1

You should have a basic understanding of how trading in the FOREX market works. If you need a little refresher check out this little tutorial.

Prerequisite #2

You should have a copy of Metatrader 4 up and running. I explain how to do that here.

Prerequisite #3

You should choose a broker and sign up for a demo account. I explain how to go about doing that here. Now that you have fulfilled the prerequisites we are ready to get started.

Lesson #1

This first lesson has two main goals. By the end of it you should be able to write a program that can automatically send orders as well as loop through all the open orders to make decisions. Keep in mind, the first couple expert advisors are not designed to make you money! We are just trying to develop the tools to allow you to implement some kind of automated strategy. If your strategy sucks your gonna loose money no matter how awesome a tool you build. For example, suppose your strategy is to total your car and collect the insurance. Even if I show you how to build the most awesome sledgehammer in the world and you wreck the bejesus out of your car, I wouldn’t expect this strategy to make very much money in the long run.

The majority of the work of any expert advisor will take place inside of the start() function. Every time the market ticks up or down the start() function is called. Inside the start() function you have access to lots of global variables. For right now all we will be concerned with are the global variables Bid and Ask. In between the bars is actual code that you can cut and paste into Metatrader 4. Also, the green text are just comments describing what is going on. You can change them to be whatever you want and wont affect what gets compiled by Metatrader 4


/* The following expert advisor is designed to execute a buy order of size 0.1 every 10 pips down and will stop buying once the size of all open positions reaches 1.0. In addition, each order is going to have a stop loss of 100 pips and a take profit of 100 pips. The first thing we are going to do is declare some variables so that we dont have numbers flying around the program with no clue of what they stand for. The only one that needs some explanation here is slippage. Slippage is the parameter which determines how much the market is allowed to move between the time when your computer sends a market order and the server receives it. This value is in tenths of a pip so 5 means the market can move 0.00005 between when you place a market order and the server receives it. Should the market move more than that, the order will be rejected. */

double stopLoss = 0.0100;
double volume=0.1;
double slippage = 5;
double takeProfit = 0.0100;

/* This is the start() function you have been hearing so much about. */

int start()
{ 

/* This while() loop is going to go through all of the open orders and get the opening price for each. At the end of the loop we will have the lowest opening price stored in the variable lowest. In MT4 the orders are stored in a global array. The function OrderSelect() is what you use to get access to those orders. The first parameter to OrderSelect() is an integer which is the index into that global array. If the integer you supply is outside of the bounds of the array OrderSelect() returns false. There are multiple ways to select an order, the second parameter specifies which way we are selecting. In this case we are selecting by position. Once you have an order selected a call to the function OrderOpenPrice() will return the opening price of the selected order */

   double lowest=100;
   int i =0;
   while(OrderSelect(i,SELECT_BY_POS)==true){
      if(OrderOpenPrice()  9){return(0);}

/* This if() block is going to make sure that we do not open a new order unless we are 10 pips below all other open orders. Here we encounter the OrderSend() function. I am going to explain what all the parameters to this function are.

Symbol() : this is the currency pair you are placing an order on, Symbol() returns the 
           pair of the chart that this EA is attached to.
OP_BUY : this parameter specifies that it is a Buy order.
volume : this is the volume of the order.
Bid : this is the price you want to execute the order at.
slippage: this is how much the market is allowed to move between when you place the 
          order and when the server accepts it.
Bid-stopLoss: this is the stopLoss for the order.
Bid+takeProfit: this is the takeProfit for the order.*/
   if(Bid < (lowest-0.0010)){ 
      OrderSend(Symbol(),OP_BUY,volume,Bid,slippage,Bid-stopLoss,Bid+takeProfit); 
   } 

   return(0); 
} 

/* For our purposes you dont have to worry about what is below here */

int init(){ } 
int deinit(){return(0);}

2 COMMENTS

Subscribe
Notify of
2 Comments
Inline Feedbacks
View all comments

Stay Connected

156,489FansLike
396,312FollowersFollow
2,320SubscribersSubscribe

Latest Articles

2
0
Would love your thoughts, please comment.x
()
x