Author: Copyright � 2010, ZerkMax
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself It automatically opens orders when conditions are reachedIt can change open orders parameters, due to possible stepping strategy
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
MA
//+------------------------------------------------------------------+
//|                                                           MA.mq4 |
//|                                        Copyright © 2010, ZerkMax |
//|                                                      zma@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, ZerkMax"
#property link      "zma@mail.ru"


extern double Lots           =   0; // ëîò, åñëè 0, òî äèíàìè÷åñêèé
extern double RiskPercentage =  70; // % îò äåïî íà ëîò, åñëè äèíàìè÷åñêèé
extern int    FastPer        =   4;
extern int    SlowPer        =  18;
extern int    magicnumber    = 777;
extern int    TrailingStop   =  20; 
extern bool   PolLots        = true;



int prevtime;
int ticket=0;
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----




   if (!IsDemo())
    {
//     Print ("Ýòà âåðñèÿ òîëüêî äëÿ äåìî-ñ÷åòîâ");
//     return(0);
    }   


   if(Time[0] == prevtime)
   { 

       return(0);
   }
   prevtime = Time[0];
   if(!IsTradeAllowed()) 
     {
       prevtime = Time[1];
       return(0);
     }




 int Ord=0;
 double ClLot=0;
int LotsCount=0;
   int i=0;  
   int total = OrdersTotal();   
   for(i = 0; i <= total; i++) 
     {
      if(TrailingStop>0)  
       {                 
       OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
       if(OrderMagicNumber() == magicnumber) 
         {
         ticket=OrderTicket(); 
         ClLot=OrderLots();
         if (OrderType()==OP_BUY)
          {
           Ord=1;
          }
         else
          {
           Ord=2;
          }         
          LotsCount ++;
          TrailingStairs(OrderTicket(),TrailingStop);
         }
       }
      }
/*
 
     for(i = 0; i <= OrdersTotal(); i++) 
      {
       OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
       if(OrderMagicNumber() == magicnumber) 
         {
         ticket=OrderTicket(); 
         ClLot=OrderLots();
         if (OrderType()==OP_BUY)
          {
           Ord=1;
          }
         else
          {
           Ord=2;
          }
         }
      } 
*/    
bool SellOp=false;
bool BuyOp=false;

double MAFast1=NormalizeDouble(iMA(NULL,0,FastPer,0,MODE_SMA,PRICE_CLOSE,2),Digits);
double MAFast2=NormalizeDouble(iMA(NULL,0,FastPer,0,MODE_SMA,PRICE_CLOSE,1),Digits);
double MAFast3=NormalizeDouble(iMA(NULL,0,FastPer,0,MODE_SMA,PRICE_CLOSE,0),Digits);
double MASlow1=NormalizeDouble(iMA(NULL,0,SlowPer,0,MODE_SMA,PRICE_CLOSE,2),Digits);
double MASlow2=NormalizeDouble(iMA(NULL,0,SlowPer,0,MODE_SMA,PRICE_CLOSE,1),Digits);
double MASlow3=NormalizeDouble(iMA(NULL,0,SlowPer,0,MODE_SMA,PRICE_CLOSE,0),Digits);


      
if ((MAFast1<MASlow1)&&
    (MAFast2==MASlow2)&&
    (MAFast3>MASlow3))
{
 BuyOp=true;
}

if ((MAFast1>MASlow1)&&
    (MAFast2==MASlow2)&&
    (MAFast3<MASlow3))
{
 SellOp=true;
}



if (BuyOp)
 {
  if (Ord==2)
   {
    OrderClose(ticket,ClLot,Ask,3,Red);
   }
  if (Ord!=1)
   {
    OrderSend(Symbol(),OP_BUY,Lot(),Ask,3,0,0,"MA_Buy",magicnumber,0,Green);
   }
 }

if (SellOp)
 {
  if (Ord==1)
   {
    OrderClose(ticket,ClLot,Bid,3,Green);
   }
  if (Ord!=2)
   {
    OrderSend(Symbol(),OP_SELL,Lot(),Bid,3,0,0,"MA_Sell",magicnumber,0,Red);
   }
 }



   
//----
   return(0);
  }
//+------------------------------------------------------------------+
void TrailingStairs(int ticket,int trldistance)
   { 
    int Spred=Ask - Bid;
    if (OrderType()==OP_BUY)
      {
       if((Bid-OrderOpenPrice())>(Point*trldistance))
         {
          if(OrderStopLoss()<Bid-Point*trldistance || (OrderStopLoss()==0))
            {
             OrderModify(ticket,OrderOpenPrice(),Bid-Point*trldistance,OrderTakeProfit(),0,Green);
             if (PolLots)
             if (NormalizeDouble(OrderLots()/2,1)>MarketInfo(Symbol(), MODE_MINLOT))
               {
               OrderClose(ticket,NormalizeDouble(OrderLots()/2,1),Bid,3,Green);
               }
             else
               {
               OrderClose(ticket,OrderLots(),Bid,3,Green);
               }
            }
         }
       }
     else
       {
        if((OrderOpenPrice()-Ask)>(Point*trldistance))
          {
           if((OrderStopLoss()>(Ask+Point*trldistance)) || (OrderStopLoss()==0))
             {
              OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*trldistance,OrderTakeProfit(),0,Red);
             if (PolLots)
             if (NormalizeDouble(OrderLots()/2,1)>MarketInfo(Symbol(), MODE_MINLOT))
               {
               OrderClose(ticket,NormalizeDouble(OrderLots()/2,1),Ask,3,Green);
               }
             else
               {
               OrderClose(ticket,OrderLots(),Ask,3,Green);
               }
             }
          }
        }
    }
    
double Lot()
{
 double LotQ = Lots;

 if (Lots==0)
  {
   double margin = MarketInfo(Symbol(), MODE_MARGINREQUIRED);
   double minLot = MarketInfo(Symbol(), MODE_MINLOT);
   double maxLot = MarketInfo(Symbol(), MODE_MAXLOT);
   double step =   MarketInfo(Symbol(), MODE_LOTSTEP);
   double account = AccountFreeMargin();
   
   double percentage = account*RiskPercentage/100;
   
   LotQ = MathRound(percentage/margin/step)*step;
   
   if(LotQ < minLot)
   {
      LotQ = minLot;
   }
   
   if(LotQ > maxLot)
   {
      LotQ = maxLot;
   }
  } 
  return (LotQ);
  }

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 ---