Author: Copyright � 2011, AM2
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
0 Views
0 Downloads
0 Favorites
TTS
//+------------------------------------------------------------------+
//|                                                          TTS.mq4 |
//|                                            Copyright © 2011, AM2 |
//|                                      http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, AM2"
#property link      "http://www.forexsystems.biz"

#define MAGIC  20110316

extern double MAOpenPer1  = 16;
extern double MAOpenPer2  = 60;
extern double MAOpenPer3  = 100;
extern double MAClosePer1 = 10;
extern double MAClosePer2 = 40;
extern double MovingShift = 6;
extern int    ADXPer      = 14;
extern int    ADXLevel    = 30;
extern int    ZeroLevel   = 500;
extern double Lots        = 1;

//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//---- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   int    res;
//---- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//---- get Moving Average 
   double ma1=iMA(NULL,0,MAOpenPer1,MovingShift,MODE_SMA,PRICE_CLOSE,0);
   double ma2=iMA(NULL,0,MAOpenPer2,MovingShift,MODE_SMA,PRICE_CLOSE,0);
   double ma3=iMA(NULL,0,MAOpenPer3,MovingShift,MODE_SMA,PRICE_CLOSE,0);
   double adx1=iADX(NULL,0,ADXPer,PRICE_CLOSE,MODE_MAIN,0);
   double adx2=iADX(NULL,0,ADXPer,PRICE_CLOSE,MODE_MAIN,1);
   double adx3=iADX(NULL,0,ADXPer,PRICE_CLOSE,MODE_MAIN,2);
//---- buy conditions
   if(ma1>ma2 && Open[0]>ma3  && adx1>adx2 && adx2>adx3 && adx3<ADXLevel)  
     {
      res=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"",MAGIC,0,Blue);
      return;
     }   
//---- sell conditions
   if(ma1<ma2 && Open[0]<ma3  && adx1>adx2 && adx2>adx3 && adx3<ADXLevel)  
     {
      res=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"",MAGIC,0,Red);
      return;
     }
//----
  }
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
  {
//---- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//---- get Moving Average 
   double ma1=iMA(NULL,0,MAClosePer1,MovingShift,MODE_SMA,PRICE_CLOSE,0);
   double ma2=iMA(NULL,0,MAClosePer2,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderMagicNumber()!=MAGIC || OrderSymbol()!=Symbol()) continue;
      //---- check order type 
      if(OrderType()==OP_BUY)
        {
         if(ma1<ma2) OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
         break;
        }
      if(OrderType()==OP_SELL)
        {
         if(ma1>ma2) OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
         break;
        }
     }
//----
  }
//+------------------------------------------------------------------+
//|                       Zero                                       |
//+------------------------------------------------------------------+
int Zero()
   {
    if (OrderType() == OP_BUY) 
     { 
      if (OrderOpenPrice() <= (Bid - ZeroLevel * Point) && OrderOpenPrice() > OrderStopLoss()) 
       { 
          OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Green);
       } 
     } 
    if (OrderType() == OP_SELL) 
     {
      if (OrderOpenPrice() >=(Ask + ZeroLevel * Point) && OrderOpenPrice() < OrderStopLoss()) 
       {
          OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Red);
       } 
     } 
   }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else                                    CheckForClose(); 
//----
    for (int i=0; i<OrdersTotal(); i++) 
     {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        if (MAGIC != 0 && OrderMagicNumber() != MAGIC) 
          continue;
        if (OrderSymbol()==Symbol()) 
         {
          RefreshRates();
          Zero();
         }
      }
//----
   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 ---