MAMy Expert

Author: Copyright � 2006, Victor Chebotariov
Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedIt Closes Orders by itself
0 Views
0 Downloads
0 Favorites
MAMy Expert
//+------------------------------------------------------------------+
//|                                                  MAMy Expert.mq4 |
//|                             Copyright © 2006, Victor Chebotariov |
//|                                      http://www.chebotariov.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Victor Chebotariov"
#property link      "http://www.chebotariov.com/"

extern int ma_method = 3;
extern int period = 3;
extern double Lots = 0.1;

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   if(Bars<100){Print("bars less than 100");return(0);}
//----
   int cnt, ticket, total;
   double iMAMy_0=iCustom(NULL,PERIOD_MN1,"MAMy v.3",ma_method,period,0,1);
   double iMAMy_1=iCustom(NULL,PERIOD_MN1,"MAMy v.3",ma_method,period,0,2);
   double iMAMy_2=iCustom(NULL,PERIOD_MN1,"MAMy v.3",ma_method,period,1,1);
   double iMAMy_3=iCustom(NULL,PERIOD_MN1,"MAMy v.3",ma_method,period,1,2);
//----
   total=OrdersTotal();
   if(total<1) 
     {
      if(AccountFreeMargin()<(1000*Lots)){Print("We have no money. Free Margin = ", AccountFreeMargin());return(0);}
      if(iMAMy_0>0 && iMAMy_1<=0)
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,0,16384,0,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
           }
         else Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
        }
      if(iMAMy_0<0 && iMAMy_1>=0)
        {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,0,16384,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
           }
         else Print("Error opening SELL order : ",GetLastError()); 
         return(0); 
        }
      return(0);
     }
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // check for opened position 
         OrderSymbol()==Symbol())  // check for symbol
        {
         if(OrderType()==OP_BUY)   // long position is opened
           {
            if(iMAMy_2<0 && iMAMy_3>=0)
                {
                 OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
                 return(0); // exit
                }
           }
         else // go to short position
           {
            if(iMAMy_2>0 && iMAMy_3<=0)
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
               return(0); // exit
              }
           }
        }
     }
//----
   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 ---