vvi-Pessim-grid

Author: Copyright 2014, MetaQuotes Software Corp.
Price Data Components
Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategyIt automatically opens orders when conditions are reached
0 Views
0 Downloads
0 Favorites
vvi-Pessim-grid
//+------------------------------------------------------------------+
//|                                                   vvi-Pessim-grid.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//--- input parameters
input int      Non_Trade_Day=0;//May exclude a day from trading
input double    TakeProfit     =  20;
input double    MinPrePreCandle=  80;//Pre-previous candle minimum to start (with tails)
input double    MaxPreCandle   =  5; //Previous candle maximum to start (without tails)
input double    GridingDiff=150;//Minimal difference for griding
input double    Lot=0.01;
input uint     Magic=0xBACE;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

bool Trading=false;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
//  EventSetTimer(60);

//---
   Trading=false;
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
double Abs(double x)
  {
   if(x<0) return(-x);
   else return(x);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double Sgn(double x)
  {
   if(x!=0)
      return(Abs(x)/x);
   else return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double Candle(int i)
  {
   return(Close[i]-Open[i]);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double CandleFull(int i)
  {
   double ret=(High[i]-Low[i]);
   return(ret*Sgn(Candle(i)));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int CurrentTF()
  {
   if(Period()==PERIOD_M1)
      return(Minute());
   if(Period()==PERIOD_M5)
      return(Minute()/5);
   if(Period()==PERIOD_M15)
      return(Minute()/15);
   if(Period()==PERIOD_M30)
      return(Minute()/30);
   if(Period()==PERIOD_H1)
      return(Hour());
   if(Period()==PERIOD_H4)
      return(Hour()/4);
   if(Period()==PERIOD_D1)
      return(Day());
   if(Period()==PERIOD_W1)
      return(Day()/7);//TODO
   if(Period()==PERIOD_MN1)
      return(Month());
   return(-1);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double Profit()
  {
   double r;
   r=0;
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) continue;
      if(OrderMagicNumber()!=Magic) continue;
      if(OrderType()!=OP_SELL) continue;
      r=r+OrderProfit();
     }
   return(r);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool IsTrade()
  {
   bool r;
   r=false;
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) continue;
      if(OrderMagicNumber()!=Magic) continue;
      if(OrderType()!=OP_SELL) continue;
      r=true;
     }
   return(r);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SetAllTP(double TP)
  {
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) continue;
      if(OrderMagicNumber()!=Magic) continue;
      if(OrderType()!=OP_SELL) continue;
      OrderModify(OrderTicket(),OrderOpenPrice(),0,NormalizeDouble(TP,4),0,clrBlue);
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double BalancedPrice()
  {
   double r;
   int k;
   r=0;k=0;
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) continue;
      if(OrderMagicNumber()!=Magic) continue;
      if(OrderType()!=OP_SELL) continue;
      r=r+OrderOpenPrice();
      k++;
     }
   return(r/k-Abs(Bid-Ask));
  }

int OldFrame=-1;
double OldPrice=-1;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   int TF;
   double P,TP;
   TF=CurrentTF();
   if(OldFrame==TF) return;
   OldFrame= TF;
   Trading = IsTrade();
   if(Trading)
     {
      P=BalancedPrice();
      if((Ask-P)>=GridingDiff*Point())
        {
         OrderSend(Symbol(),OP_SELL,Lot,Bid,100,0,0,"",Magic,0,clrBlue);
         TP=BalancedPrice()-TakeProfit*Point();
         SetAllTP(TP);
         return;
        }
     }
   if((DayOfWeek()==Non_Trade_Day))
     {
      return;
     }
   if((CandleFull(2)>MinPrePreCandle*Point()) && (Abs(Candle(1))<=MaxPreCandle))
     {
      Trading=(OrderSend(Symbol(),OP_SELL,Lot,Bid,100,0,Ask-TakeProfit*Point(),"",Magic,0,clrBlue)>=0);
      //if (Trading) OldPrice=Ask;
     }
   return;
  }

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