English Русский 中文 Español Deutsch 日本語
Especulação confortável

Especulação confortável

MetaTrader 4Negociação | 4 fevereiro 2016, 12:51
745 0
Eryomin Sergey
Eryomin Sergey

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.

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.

Traduzido do russo pela MetaQuotes Ltd.
Artigo original: https://www.mql5.com/ru/articles/1509

Arquivos anexados |
trener.mq4 (11.66 KB)
MetaEditor : Modelos como um local para se apoiar MetaEditor : Modelos como um local para se apoiar
Pode ser novidade para muitos de nossos leitores que todas as preparações para escrever um EA podem ser feitas uma vez e então usadas continuamente.
Usando o MetaTrader 4 para análise de padrões baseados em tempo Usando o MetaTrader 4 para análise de padrões baseados em tempo
A análise de padrões baseados em tempo pode ser usada no mercado financeiro para determinar o melhor momento para entrar em uma negociação ou momento no qual uma negociação deve ser totalmente evitada. Aqui usamos o MetaTrader 4 para analisar os dados históricos de mercado e produzir resultados otimizados que podem ser úteis para aplicação em sistemas de negociações mecânicas.
Caça de tendências Caça de tendências
O artigo descreve um algoritmo de aumento de volume de lucros em uma negociação. Sua implementação usando os recursos do MQL4 é apresentada no artigo.
Indicador de linhas de tendências considerando a abordagem de T. Demark Indicador de linhas de tendências considerando a abordagem de T. Demark
O indicador mostra linhas de tendência exibindo os eventos recentes no mercado. O indicador é desenvolvido considerando as recomendações e a abordagem de Thomas Demark, com relação à análise técnica. O indicador exibe ambas a última direção da tendência e a penúltima direção oposta da tendência.