6. Making Your First Program - Enter the Market Randomly

Bookmark and Share

1. Imitating

There is a well-known claim (or perhaps an urban legend) that mankind can only repeat "buying" and "selling" repeatedly like an ape, even if all the wisdo ms of the world was brought together.

True or not, let's now create a program that randomly enters a market! 

2. Let's Make the Main Program!

This one is very simple. Here we will develop a program that enters the market "once every hour" in either short or long direciton. 

  1. int init(){
  2. }
  1. int deinit(){
  2. }

We will not be using the two segments above, so nothing needs to be written in. 

  1. int start(){
  2.   if(Time[0]%360 != 0){
  3.     return(0);
  4.   }
  5.   MathSrand(TimeLocal());
  6.   if(MathRand()%2 == 0){
  7.     OrderSend(Symbol(),OP_BUY,0.1,Ask,3,Ask-StopLoss*Point,
  8.     Ask+TakeProfit*Point,NULL,0,0,Blue);
  9.   }else{
  10.     OrderSend(Symbol(),OP_SELL,0.1,Bid,3,Bid+StopLoss*Point,
  11.     Bid-TakeProfit*Point,NULL,0,0,Red);
  12.   }
  13. }

Lines 2~4 orders to disregard the rest if the current Unix time (number of seconds since January 1, 1970) cannot be divided evenly by 360. Time[0] points to the current Unix time (strictly speaking, about a second prior), and "return(0);" is a command to finish off the processes within start().  

Next up is the fifth line, but for the moment, just pretend it's a magic

Look at lines 6~12.
MathRand() generates a random number between 0 and 32767.
(Actually the magic in fifth line was to set up this random generation). 

Then it proceeds to check if the remainder of the generated number divided by 2 is 0. Indeed, this is simply looking for an even number. 

Processes on the seventh and eighth lines is directing to "buy the currency in 0.1 long position, within the range of 3 pips". 
(The code does not have to be split into two lines).
Additionally, StopLoss and TakeProfit are set as Ask-StopLoss*Point and Ask+TakeProfit*Point, respectively.

Additional explanations may be needed here.
Point is something that cancels out the differences in units between the traded currencies.
For example, in cases where USD/JPY moves "123.45"→"123.47", or when EUR/USD moves "1.2345"→"1.2347", both price movements are 2 pips, but they hold different digits. "Point" adjusts such digit gaps. 

In the example above, Ask-StopLoss*Point essentially means to "stop when it decreased as much as the StopLoss points from the buying price". 

3. Definition of Variables

As many of you may have notice by this point, StopLoss and TakeProfit are variables that are yet to be defined. 
The program is unusable if you do not define these variables first. 

  1. int init()

Add the following lines before the segment shown above. 

  1. extern int StopLoss = 15;
  2. extern int TakeProfit = 15;

Finally, the program should now look something like this. 

  1. extern int StopLoss = 15;
  2. extern int TakeProfit = 15;
  4. int init(){
  5. }
  7. int deinit(){
  8. }
  10. int start(){
  11.   if(Time[0]%360 != 0){
  12.     return(0);
  13.   }
  14.   MathSrand(TimeLocal());
  15.   if(MathRand()%2 == 0){
  16.     OrderSend(Symbol(),OP_BUY,0.1,Ask,3,Ask-StopLoss*Point,
  17.     Ask+TakeProfit*Point,NULL,0,0,Blue);
  18.   }else{
  19.     OrderSend(Symbol(),OP_SELL,0.1,Bid,3,Bid+StopLoss*Point,
  20.     Bid-TakeProfit*Point,NULL,0,0,Red);
  21.   }
  22. }