MQL4 - automated forex trading   /  

Articles

Login | Register | Language: ru cn en
MetaTrader Market: we have 650 Expert Advisors for algorithmic trading!

Articles  Trading  Comfortable Scalping To post a new article, please log in or register


Automated trading in
and Trade on the Go!
and Trade on the Go!

This article is about
MetaTrader 4
Download MT 4 - 455 Kb

and Trade on the Go!

and Trade on the Go!

Comfortable Scalping [ ru ]

Introduction

This article describes an algorithm of trade opening that allows to make scalping more comfortable. However, this algorithm can also be applied in other trading approaches. Actually, the article offers a method of helping a trader at such a fast trading.

Generally scalping is considered a very nervous type of trading. Very important here is the necessity to indicate lot, TakeProfit and StopLoss levels each time, thus being distracted from a chart.

This article is the continuation of "Betting Modeling as Means of Developing "Market Intuition"". I recommend reading it before starting to study the present article.

I'd like to remind you of what scalping is. Scalping is a method of quick trading. Usually in such a trading profit is fixed at 1-10 pips (points). Scalping is known for its complexity, nervousness and higher attentiveness required. Someone thinks it not serious, someone considers it a perfect mastery. As for me, I am not going to estimate this type of trading - it is widely discussed and everyone has one's own opinion.

Concept

Probably every trader ever tried to use a scalping strategy. For some traders scalping is the most convenient type of trading, for others - on the contrary. Some consider scalping the most interesting trading, others - a mere waste of time. However everyone can note about the necessity of a higher attention to market and opened trades in this type of trading.

Many traders decline using scalping just because it requires much effort and nerves. However, there is a method to help a scalper.

Suppose a trader is going to use scalping with a fixed lot and take profit at each trade. Obviously, it is reasonable to eliminate the necessity of indicating these parameters every time for each trade. Because it takes extra time and draws a trader's attention from a chart.

It means we need a tool that will open a trade with a fixed lot and TP/SL levels on a trader's command. The tool's operation should be maximally simple; besides it should minimally distract a trader from a chart.

Such a tool can be easily implemented using MQL4 means.

Implementation

As the basis we will take a game described in the article "Betting Modeling as Means of developing "Market Intuition"". We will create a tool, which will help to play this game and trade at the same time.

Short description of the game. Two arrows are drawn on the chart - up and down. A trader deletes an unnecessary arrow, thus making a choice denoting his opinion - whether a security will rise or fall. At the beginning of a new candlestick the EA checks whether a trader's forecast is right or wrong. The correctness of forecasting influences the game score. Moreover, a trader can make his choice within a limited period of time, which can be changed (a trader decides whether to set it or not).

For the implementation we will draw two more arrows one bar back than the current one. The current bar will be still used for the betting. Deletion of an arrow on the previous bar will be a signal for the EA to open a trade in the necessary direction. Besides, limitation of time period for choosing a trade direction will be disabled for trading. There will be the following changeable parameters: TakeProfit and StopLoss levels, lot, acceptable slippage and the magic number. Besides, trading can be disabled using the extern bool variable, thus the EA will be used only for betting.

Besides, at trade opening an arrow named 'buy' or 'sell' will be drawn on a chart, depending on a trade open at the time. This will be done to prevent the EA from opening new trades on this candlestick. This arrow will be drawn 300 points away from a bar opening price, so a user won't probably even notice it.

The EA itself will be divided into two blocks - the game and trade opening. Thus a reader can see what is added to the code.

So, we have the following program code:


//+------------------------------------------------------------------+
//|                                                       trener.mq4 |
//|                                       Copyright © 2008, FXRaider |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, FXRaider"
extern int gap=2;
extern bool Trading=true;
extern int TP=2;
extern int SL=20;
extern double Lots=0.02;
extern int slippage=1;
extern int MagicNumber=777;
extern int time_limit=30;
int start()
  {
//----
//#################################################################################
//####################################### GAME ####################################
//------------------------------
string solution="none"; 
int point, 
    point_neg, 
    point_pos;
//------------------------------    
//+---------------------------------------------------------------+
//|                      "up" choice searching                    | 
 if(
    ObjectGet("up", OBJPROP_PRICE1)==Open[1]+gap*Point
    &&iBarShift(NULL,0,ObjectGet("up",OBJPROP_TIME1))==1
    &&ObjectFind("down") != 0
    &&ObjectFind("up") == 0
    )
    {
     solution="up";
    }
//|                      "up" choice searching                    |  
//+---------------------------------------------------------------+
  
//+---------------------------------------------------------------+
//|                      "down" choice searching                  |     
 if(
    ObjectGet("down", OBJPROP_PRICE1)==Open[1]-gap*Point
    &&iBarShift(NULL,0,ObjectGet("down",OBJPROP_TIME1))==1    
    &&ObjectFind("up") != 0
    &&ObjectFind("down") == 0
    )
    {
     solution="down";
    }
//|                      "down" choice searching                  |       
//+---------------------------------------------------------------+    
 
//+---------------------------------------------------------------+
//|             counting points at a positive answer              |
    if((solution=="up"&&Open[1]<Close[1])
      ||(solution=="down"&&Open[1]>Close[1]))
    {
     point=1;
     point_pos=1;
     point_neg=0;     
    }  
//|             counting points at a positive answer              |   
//+---------------------------------------------------------------+
 
//+---------------------------------------------------------------+
//|             counting points at a negative answer              |    
    if((solution=="up"&&Open[1]>Close[1])
      ||(solution=="down"&&Open[1]<Close[1]))
    {
     point=-1;
     point_pos=0;
     point_neg=1;     
    } 
//|             counting points at a negative answer              |
//+---------------------------------------------------------------+
 
//+----------------------------------------------------------------------------------+
//|                              working with an external file                       |       
      int handle; 
      double points,     //total score
             points_pos, //score of positive answers
             points_neg; //score of negative answers 
       handle=FileOpen("trener_"+Symbol()+"_"+Period()+".csv",
                       FILE_CSV|FILE_WRITE|FILE_READ,";");
       if(handle>0) //if there is a file, read it
       { 
        points=NormalizeDouble(StrToDouble(FileReadString(handle)),Digits); 
        points_pos=NormalizeDouble(StrToDouble(FileReadString(handle)),Digits); 
        points_neg=NormalizeDouble(StrToDouble(FileReadString(handle)),Digits);       
        FileClose(handle);
       } 
       
    if(solution!="none") //if a choice has been made made 
    {        
      handle=FileOpen("trener_"+Symbol()+"_"+Period()+".csv",
                      FILE_CSV|FILE_WRITE|FILE_READ,";");
      FileWrite(handle ,points+point);         //write the total score
      FileWrite(handle ,points_pos+point_pos); //write the score of positive answers
      FileWrite(handle ,points_neg+point_neg); //write the score of negative answers                    
      FileClose(handle); 
    } 
//|                              working with an external file                       | 
//+----------------------------------------------------------------------------------+    
 
//+------------------------------------------------------------------------------------+
//|                                 working with objects                               |   
  if(iBarShift(NULL,0,ObjectGet("down",OBJPROP_TIME1))>0
     ||ObjectGet("down",OBJPROP_PRICE1)!=Open[0]-gap*Point) 
    {
     ObjectDelete("down"); 
    }  
 if(iBarShift(NULL,0,ObjectGet("up",OBJPROP_TIME1))>0
    ||ObjectGet("up",OBJPROP_PRICE1)!=Open[0]+gap*Point)            
    { 
     ObjectDelete("up"); 
    } 
   
  int sec_lim;  
  if(!time_limit)
  {
   sec_lim=0; 
  }
  else
  {
   sec_lim=TimeCurrent()-time_limit;
  }
  if(sec_lim>ObjectGet("up",OBJPROP_TIME1)
     &&sec_lim>ObjectGet("down",OBJPROP_TIME1) 
     &&ObjectFind("down") == 0&&ObjectFind("up") == 0
     &&iBarShift(NULL,0,ObjectGet("down",OBJPROP_TIME1))==0
     &&iBarShift(NULL,0,ObjectGet("up",OBJPROP_TIME1))==0)            
    { 
     ObjectDelete("up"); 
     ObjectDelete("down");      
    } 
  
   if((ObjectFind("down") != 0&&ObjectFind("up") != 0) //if no object
      &&sec_lim<Time[0])
   {
     ObjectCreate("down", OBJ_ARROW, 0, Time[0], Open[0]-gap*Point); //draw a down arrow
     ObjectSet("down", OBJPROP_STYLE, STYLE_DOT);
     ObjectSet("down", OBJPROP_ARROWCODE, SYMBOL_ARROWDOWN);
     ObjectSet("down", OBJPROP_COLOR, Red);
 
     ObjectCreate("up", OBJ_ARROW, 0, Time[0], Open[0]+gap*Point); //draw an up arrow
     ObjectSet("up", OBJPROP_STYLE, STYLE_DOT);
     ObjectSet("up", OBJPROP_ARROWCODE, SYMBOL_ARROWUP);
     ObjectSet("up", OBJPROP_COLOR, Blue);
    }      
//|                                 working with objects                               |   
//+------------------------------------------------------------------------------------+
//####################################### GAME ####################################
//#################################################################################
 
 
//#################################################################################
//#################################### TRADING ####################################
//+------------------------------------------------------------------------------------+  
//|                                working with objects I                              | 
  if(iBarShift(NULL,0,ObjectGet("down_1",OBJPROP_TIME1))>1
  ||ObjectGet("down_1",OBJPROP_PRICE1)!=Open[0]-gap*Point
  ||!Trading)  
  {
   ObjectDelete("down_1"); 
  }  
  
  if(iBarShift(NULL,0,ObjectGet("up_1",OBJPROP_TIME1))>1
  ||ObjectGet("up_1",OBJPROP_PRICE1)!=Open[0]+gap*Point
  ||!Trading)  
  { 
   ObjectDelete("up_1"); 
  } 
  
  if(iBarShift(NULL,0,ObjectGet("sell",OBJPROP_TIME1))>0
  ||ObjectGet("sell",OBJPROP_PRICE1)!=Open[0]-300*Point
  ||!Trading) 
  {
   ObjectDelete("sell"); 
  }
  if(iBarShift(NULL,0,ObjectGet("buy",OBJPROP_TIME1))>0
  ||ObjectGet("buy",OBJPROP_PRICE1)!=Open[0]+300*Point 
  ||!Trading)
  
  {
   ObjectDelete("buy"); 
  } 
  
   if(ObjectFind("down_1") != 0&&ObjectFind("up_1") != 0 && Trading)
   {
     ObjectCreate("down_1", OBJ_ARROW, 0, Time[1], Open[0]-gap*Point);
     ObjectSet("down_1", OBJPROP_STYLE, STYLE_DOT);
     ObjectSet("down_1", OBJPROP_ARROWCODE, SYMBOL_ARROWDOWN);
     ObjectSet("down_1", OBJPROP_COLOR, Red);
 
     ObjectCreate("up_1", OBJ_ARROW, 0, Time[1], Open[0]+gap*Point);
     ObjectSet("up_1", OBJPROP_STYLE, STYLE_DOT);
     ObjectSet("up_1", OBJPROP_ARROWCODE, SYMBOL_ARROWUP);
     ObjectSet("up_1", OBJPROP_COLOR, Blue);
    }  
//|                                working with objects I                              |    
//+------------------------------------------------------------------------------------+     
 if(Trading)
 {
//+----------------------------------------------------------------------------------------------+
//|                              searching open orders for a security                            |
    int pos_sell=0, bar_op_buy, bar_op_sell;
  for (int i_op_sell=OrdersTotal()-1; i_op_sell>=0; i_op_sell--) 
  { 
   if (!OrderSelect(i_op_sell,SELECT_BY_POS,MODE_TRADES)) break; 
   if (Symbol()==OrderSymbol()
   &&(OrderType()==OP_SELLSTOP||OrderType()==OP_SELL)
   &&(OrderMagicNumber()==MagicNumber)
   &&iBarShift(NULL,0,OrderOpenTime())==0)
   {
    pos_sell=1; break;   
   } 
  }
    
    int pos_buy=0;
  for (int i_op_buy=OrdersTotal()-1; i_op_buy>=0; i_op_buy--) 
  { 
   if (!OrderSelect(i_op_buy,SELECT_BY_POS,MODE_TRADES)) break; 
   if (Symbol()==OrderSymbol()
   &&(OrderType()==OP_BUYSTOP||OrderType()==OP_BUY)
   &&(OrderMagicNumber()==MagicNumber)
   &&iBarShift(NULL,0,OrderOpenTime())==0)
   {
    pos_buy=1; break;   
   } 
  }    
//|                              searching open orders for a security                            |
//+----------------------------------------------------------------------------------------------+ 
 
//+------------------------------------------------------------------------------------+  
//|                                working with objects II                             |   
 if(pos_buy==1)
 {   
      ObjectCreate("buy", OBJ_ARROW, 0, Time[0], Open[0]+300*Point);
      ObjectSet("buy", OBJPROP_STYLE, STYLE_DOT);
      ObjectSet("buy", OBJPROP_ARROWCODE, SYMBOL_ARROWUP);
      ObjectSet("buy", OBJPROP_COLOR, Red);
 }
 
 if(pos_sell==1)
 {
      ObjectCreate("sell", OBJ_ARROW, 0, Time[0], Open[0]-300*Point);
      ObjectSet("sell", OBJPROP_STYLE, STYLE_DOT);
      ObjectSet("sell", OBJPROP_ARROWCODE, SYMBOL_ARROWDOWN);
      ObjectSet("sell", OBJPROP_COLOR, Red);    
 }
//|                                working with objects II                             |   
//+------------------------------------------------------------------------------------+ 
  
//+------------------------------------------------------------------------------------+ 
//|                                   opening trades                                   |
double sl_buy, sl_sell;
 if(!SL)
 { 
  sl_buy=0;
  sl_sell=0;
 }
 else
 {
  sl_buy=Ask-SL*Point;
  sl_sell=Bid+SL*Point;
 }
  if(
     ObjectGet("up_1", OBJPROP_PRICE1)==Open[0]+gap*Point
     &&iBarShift(NULL,0,ObjectGet("up_1",OBJPROP_TIME1))==1
     &&ObjectFind("down_1") != 0
     &&ObjectFind("up_1") == 0
     &&!pos_buy
     &&ObjectFind("buy") != 0    
     )
     {
      OrderSend(Symbol(),OP_BUY, Lots,Ask,slippage,sl_buy,Ask+TP*Point,"trener",MagicNumber,0,Blue);            
     }
  if(
     ObjectGet("down_1", OBJPROP_PRICE1)==Open[0]-gap*Point
     &&iBarShift(NULL,0,ObjectGet("down_1",OBJPROP_TIME1))==1
     &&ObjectFind("up_1") != 0
     &&ObjectFind("down_1") == 0
     &&!pos_sell
     &&ObjectFind("sell") != 0
     )
     {
      OrderSend(Symbol(),OP_SELL, Lots,Bid,slippage,sl_sell,Bid-TP*Point,"trener",MagicNumber,0,Red);  
     }
//|                                   opening trades                                   |  
//+------------------------------------------------------------------------------------+   
 }
//#################################### TRADING ####################################
//#################################################################################
 
Comment("Score: ", points," (",points_pos,"/",points_neg,   //displaying score
        ") | Time: ", Hour(),":", Minute(),":", Seconds()); //displaying time (for convenience)
     
   return(0);
  }
//+------------------------------------------------------------------+

The code includes all the necessary comments.

After the Expert Advisor is attached to a chart, we will get the following:

Here the last two arrows are intended for the game, the two arrows before them are used to open orders.

The deletion of an arrow on the previous candlestick will cause the execution of the OrderSend() function and a corresponding order will be opened:


Here is the tab of changing input parameters:



The "gap" variable is responsible for the number of points equal to the distance between an arrow and the open price of a candlestick. The variable "Trading" denotes the trading function, "TP" - TakeProfit in points, "SL" - StopLoss in points. The "Lots" variable is responsible for the volume of opened positions; "slippage" denotes the admissible slippage in points that we are ready to accept. "MagicNumber" indicates the magic number which is assigned by the EA to opened positions (necessary for the EA to be able to track its "own" orders). The "time_limit" limit variable sets the number of seconds within which a user must make his choice. If "0" is indicated, time is not limited, i.e. choice can be made during all the period of candlestick formation.

Conclusion

As a result we have a security for a comfortable trading using orders with standard parameters (TP, SL, Slippage, lot). This tool can be useful in any trading. However, it is the most efficiently used when a large number of trades is opened within a short period of time. For example, in scalping and scalping.

Using this program, a trader does not have to set parameters of an opened order each time. Thus the maximum of his attention is concentrated in a chart. Undoubtedly, this may help to increase the effectiveness of trading.

Translated from Russian by MetaQuotes Software Corp.
Original article: http://articles.mql4.com/ru/553

Attachments:
trener.mq4 (11.7 Kb)
Created: 2008.06.13  Author: Eryomin Sergey
Warning: All rights to these materials are reserved by MetaQuotes Software Corp. Copying or reprinting of these materials in whole or in part is prohibited.
Comparative Analysis of 30 Indicators and Oscillators
Comparative Analysis of 30 Indicators and Oscillators

The article describes an Expert Advisor that allows conducting the comparative analysis of 30 indicators and oscillators aiming at the formation of an effective package of indexes for trading.

Market Diagnostics by Pulse
Market Diagnostics by Pulse

In the article, an attempt is made to visualize the intensity of specific markets and of their time segments, to detect their regularities and behavior patterns.

6 comments  To add comments, please, log in or register
gbolla:

Stop level must be more then 5pips to open price!!! TP=2 pips don't work --> error: invalid stop

Bolla


In some pair, not only 5 pips minimum at stop level to make an order, but also you have to set 10 pips at minimum. I want to remove TP from OrderSend() and move it to OrderClose() instead. I hope it worked.
2010.08.09 20:07 rex_loner

Hi


I have put this trener.mq4 in experts folder, comiled and attached it to my chart (smiley face appeared), but when I remove one of the candles on the 2nd last bar no trade is placed.... I have got Allow Live Trading checked and also have set the Trading value to true in the EA settings.....


any ideas how to get this to work

2009.01.26 15:32 Willowgal

Hi,



Can you add a feature to close the trades using only the mouse at chart window?



Thanks

2008.10.18 03:36 edfiuza

you rock !!!!!!! .. can you contact me at skype : ctzulu


i just want to discuss two of my trading strategies and what you think of it


thx alex

2008.07.30 01:06 cooltrader

Hi!

Great idea! I've downloaded the program and everything seems fine. However, I am in DEMO and am unable to execute a demo order. Per instructions, I delete the unnecessary arrow (in this case, the blue up arrow as I believe market will move lower)...the the red arrow remains...of course this is the arrow preceding the last bar (second to last bar). The moment that the blue arrow is deleted, automatically a sell order (with my presets TP and SL) should be executed as I understand...but nothing happens. No order is sent. Am I doing something incorrectly? I haven't tried this in LIVE as of yet. Thanks for your help.



Guy



Problem is resolved...had to check the box on  "Allow Live Trading" in "Common"...I assumed that this meant only for live trading...but applies to demo, too. Guy

2008.06.16 18:25 GDDakota

Stop level must be more then 5pips to open price!!! TP=2 pips don't work --> error: invalid stop

 

Bolla

2008.06.13 10:53 gbolla
6 comments