Author: fortrader
Price Data Components
Series array that contains close prices for each bar
Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedIt Closes Orders by itself It can change open orders parameters, due to possible stepping strategy
Indicators Used
Moving average indicatorMovement directional index
2 Views
0 Downloads
0 Favorites
ADX_MA
//+------------------------------------------------------------------+
//|                                                     ADX & MA.mq4 |
//|                                                        fortrader |
//|                                                 www.fortrader.ru |
//+------------------------------------------------------------------+
#property copyright "fortrader"
#property link      "www.fortrader.ru"

//---- input parameters
extern int       per_MA = 21;
extern int       per_ADX = 14;
extern int       porog_ADX = 16;
extern int       TakeProfit_Buy = 1300;
extern int       StopLoss_Buy = 30;
extern int       TrailingStop_Buy = 270;
extern int       TakeProfit_Sell = 160;
extern int       StopLoss_Sell = 50;
extern int       TrailingStop_Sell = 20;
extern double     Lots = 0.1;


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  
  if (Volume[0] > 1) return(0);

//----
// Îáúÿâëÿåì ïåðåìåííûå
int total, cnt;
double MA, ADX;
int err;

// Âû÷èñëÿåì íà÷àëüíûå ïàðàìåòðû èíäèêàòîðîâ äëÿ ïîèñêà óñëîâèé âõîäà

  MA = iMA(NULL,0,per_MA,0,MODE_SMMA,PRICE_MEDIAN,1);
  ADX = iADX(NULL,0,per_ADX,PRICE_MEDIAN,MODE_MAIN,1);
  
  total=OrdersTotal();

  // Ïðîâåðêà ñðåäñòâ
  if(AccountFreeMargin()<(1000*Lots))
     {
       Print("We have no money. Free Margin = ", AccountFreeMargin());   
       return(0);  
     }
  
  // Ïðîâåðêà óñëîâèé äëÿ ñîâåðøåíèÿ ñäåëêè
  if(iClose(NULL,0,1)>MA && iClose(NULL,0,2)<MA && ADX>porog_ADX)
     {
       OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-StopLoss_Buy*Point,Ask+TakeProfit_Buy*Point,"Ïîêóïàåì",16384,0,Green);
     }

  if(iClose(NULL,0,1)<MA && iClose(NULL,0,2)>MA && ADX>porog_ADX)
     {
       OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+StopLoss_Sell*Point,Bid-TakeProfit_Sell*Point,"Ïðîäàåì",16385,0,Red);
     }
     
  for(cnt=total-1;cnt>=0;cnt--)
     {
       OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
       if(OrderType()==OP_BUY)
         {
           if(iClose(NULL,0,1)<MA)
             {
               OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
             }
           if(TrailingStop_Buy>0)  
             {                 
               if(Bid-OrderOpenPrice()>Point*TrailingStop_Buy) // Bid - öåíà ïîêóïêè
                 {
                   if(OrderStopLoss()<Bid-Point*TrailingStop_Buy)
                     {
                       OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop_Buy,OrderTakeProfit(),0,Green);
                       return(0);
                     }
                 }
             }
         }
       if(OrderType()==OP_SELL)
         {
           if(iClose(NULL,0,1)>MA)
             {
               OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
             }
           if(TrailingStop_Sell>0)  
             {                 
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop_Sell))  // Ask - öåíà ïðîäàæè
                 {
                   if((OrderStopLoss()>(Ask+Point*TrailingStop_Sell)) || (OrderStopLoss()==0))
                     {
                       OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop_Sell,OrderTakeProfit(),0,Red);
                       return(0);
                     }
                 }
             }
         }
  
     }

   
//----   return(0);
  }
//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---