Author: Copyright 2012, Guriev Invest
Orders Execution
Checks for the total of closed ordersChecks for the total of open ordersIt can change open orders parameters, due to possible stepping strategyIt automatically opens orders when conditions are reached
Indicators Used
Force indexMACD Histogram
0 Views
0 Downloads
0 Favorites
elder_v3
//+------------------------------------------------------------------+
//|                                                        elder.mq4 |
//|                                    Copyright 2012, Guriev Invest |
//|                                   http://www.gurievinvest.org.ua |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, Guriev Invest"
#property link      "http://gurieveugen.blogspot.com/2012/06/blog-post.html"

extern int maFast          = 12;       // Áûñòðàÿ ÌÀØÊÀ
extern int maSlow          = 26;       // Ìåäëåííàÿ ÌÀØÊÀ
extern int maSignal        = 9;        // Ñèãíàëüíàÿ ÌÀØÊÀ
extern int Delta           = 3;        // Âî ñêîëüêî ðàç òåéïðîôèò áóäåò áîëüøå ñòîïëîññà
extern int LevelWLoss      = 10;       // Óðîâåíü áåçóáûòêà
extern int LevelProfit     = 0;        // Óðîâåíü ïðîôèòà; Åñëè 0 òî áóäåò áðàòü ñ ðàçíèöû ñòîïëîññà è öåíû îòåðûòèÿ
extern int MaxRisk         = 2;        // Ïðîöåíò ðèñêà
extern int MAXLOSS         = 2;        // Ìàêñèìàëüíî êîëè÷åñòâî ïðîèãðûøíûõ ñäåëîê çà ìåñÿö

int prevtime   = 0;
int ParentTimeFrame  = PERIOD_W1;

bool TrendUp   = false;
bool TrendDown = false;
//+------------------------------------------------------------------+
//| Ïðîâåðêà íà êîëè÷åñòâî óáûòî÷íûõ ñäåëîê                          |
//+------------------------------------------------------------------+
int CalculateLossOrders()
{
   datetime t;
   int o = 0;
   
   for(int i=0; i<OrdersHistoryTotal(); i++)
   if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
   if(OrderSymbol() == Symbol())
   if(OrderType() == OP_BUY || OrderType() == OP_SELL)
   {
      t = OrderCloseTime();
      if(Year() == TimeYear(t) && Month() == TimeMonth(t))
      if(OrderProfit()<0) o++;
   }
   return (o);
}
//+------------------------------------------------------------------+
//| Ïðîöåäóðà ïåðåâîäà ñäåëîê â áåçóáûòîê                            |
//+------------------------------------------------------------------+
void bu()
{
   bool fm;   
   for(int i=0; i<OrdersTotal(); i++)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if(OrderType() == OP_BUY)
         {
            if(LevelProfit>0)
            {
               if(Bid-OrderOpenPrice() > LevelProfit)
               fm = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+LevelWLoss*Point,OrderTakeProfit(),CLR_NONE);
            }
            else
            {
               if(Bid-OrderOpenPrice() > (OrderOpenPrice()-OrderStopLoss()))
               fm = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+LevelWLoss*Point,OrderTakeProfit(),CLR_NONE);
            }
         }
         if(OrderType() == OP_SELL)
         {
            if(LevelProfit>0)
            {
               if(OrderOpenPrice()-Ask > LevelProfit)
               fm = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-LevelWLoss*Point,OrderTakeProfit(),CLR_NONE);
            }
            else
            {
               if(OrderOpenPrice()-Ask > (OrderStopLoss()-OrderOpenPrice()))
               fm = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-LevelWLoss*Point,OrderTakeProfit(),CLR_NONE);   
            }
         }
         if(fm) break;
      }
   }
}
bool isOrder(int order)
{
   if(OrdersTotal()<1) return (false);
   for(int i = 0; i<OrdersTotal();i++)
   if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
   if(OrderType() == order && OrderSymbol() == Symbol())
   return (true);
}
bool isOrders(int order, bool isBuy)
{
   if(OrdersTotal()<1) return (false);
   for(int i = 0; i<OrdersTotal();i++)
   if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
   {
      if(OrderType() == order && OrderSymbol() == Symbol())
      if(isBuy)
      {
         if(OrderOpenPrice()>High[1])
         {
            OrderDelete(OrderTicket());
            return (false);
         }
         else return (true);
      }
      else
      {
         if(OrderOpenPrice()<Low[1])
         {
            OrderDelete(OrderTicket());
            return (false);
         }
         else return (true);
      }
   }
}
bool Force(bool isBuy)
{
   double force = iForce(Symbol(), Period(), 2, 0, 0, 1);
   if(isBuy)
   {
      if(force>0) return (false);
      else return (true);
   }   
   else
   {
      if(force<0) return (false);
      else return (true);
   }   
}
//+------------------------------------------------------------------+
//| Ðàñ÷åò ëîòà â çàâèñèìîñòè îò  ïðîöåíòà ðèñêà è ñòîï-ïðèêàçà      |
//+------------------------------------------------------------------+
double GetLots(double sl)
{
   double Free    = AccountFreeMargin();                    // Ñâîáîäíûå äåíüãè
   double LotVal  = MarketInfo(Symbol(),MODE_TICKVALUE);    //ñòîèìîñòü 1 ïóíêòà äëÿ 1 ëîòà
   double MinLot  = MarketInfo(Symbol(),MODE_MINLOT);       // Ìèíèìàëüíûé ëîò
   double MaxLot  = MarketInfo(Symbol(),MODE_MAXLOT);       // Ìàêñèìàëüíûé ëîò
   double Step    = MarketInfo(Symbol(),MODE_LOTSTEP);      // Øàã èçìåíåíèÿ ëîòà
   double Lot     = MathFloor((Free*MaxRisk/100)/(sl*LotVal)/Step)*Step;
   
   if(Lot<MinLot) Lot = MinLot;
   if(Lot>MaxLot) Lot = MaxLot;
   
   return (Lot);
}
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   if(Time[0] == prevtime) return (0);
   prevtime = Time[0];
   
   bool up        = false;
   bool down      = false;
   
   
   double TakeProfit = 0;
   double Lots    = NormalizeDouble((AccountBalance()/10000),2);   
   double macd1   = iMACD(Symbol(), ParentTimeFrame, maFast, maSlow, maSignal, PRICE_CLOSE, MODE_MAIN,1);    
   double macd2   = iMACD(Symbol(), ParentTimeFrame, maFast, maSlow, maSignal, PRICE_CLOSE, MODE_MAIN,2);
   double macd3   = iMACD(Symbol(), ParentTimeFrame, maFast, maSlow, maSignal, PRICE_CLOSE, MODE_MAIN,3);
   
   
   //=========Ôîðìèðóåì Ñèãíàë=========
   if(macd1>macd2 && macd2<macd3)
   {
      TrendUp = true;
      TrendDown = false;
   }
   else if(macd1<macd2 && macd2>macd3)
   {
      TrendUp = false;
      TrendDown = true;
   }
   
   if(TrendUp) up = Force(true);
   if(TrendDown) down = Force(false);
   
   if(CalculateLossOrders()>MAXLOSS-1){up = false; down = false;}
   
   if(up)
   {
      if(!isOrders(OP_BUYSTOP, true) && !isOrder(OP_BUY))
      {
         TakeProfit = High[1] + ((High[1]-Low[1])*Delta);
         OrderSend(Symbol(), OP_BUYSTOP, GetLots((High[1]-Low[1])*100000), High[1], 1, Low[1], TakeProfit, "Ïîêóïêà",0,0, Green);
      }
   }
   if(down)
   {
      if(!isOrders(OP_SELLSTOP, false) && !isOrder(OP_SELL))
      {
         TakeProfit = Low[1] - ((High[1]-Low[1])*Delta);
         OrderSend(Symbol(), OP_SELLSTOP, GetLots((High[1]-Low[1])*100000), Low[1], 1, High[1], TakeProfit, "Ïðîäàæà",0,0, Red);
      }
   }   
   bu();
   //==================================   
   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 ---