Author: ��������
Price Data Components
Series array that contains the highest prices of each barSeries array that contains the lowest prices of each bar
Orders Execution
It automatically opens orders when conditions are reachedChecks for the total of open orders
Indicators Used
FractalsMoving average indicator
0 Views
0 Downloads
0 Favorites
Razor
//+------------------------------------------------------------------+
//|                                                        Razor.mq4 |
//|                                                         Ñòåöåíêî |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Ñòåöåíêî"
#property link      ""

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int lastTime;
int x, limitX;
int controlTime;
int direction = 0;
double TakeProfit, StopLoss, y, Lots;
int MagicNumber = 150;
extern double Lot = 0.1;
int FractalUp[3];
int FractalDown[3];
int w1, w2, w3;
int Ord;
bool ReadySet = false;
extern double point = 0.0001;
int distance = 15;
extern bool Rul_EMA = false;
extern bool Rul_Close = true;
extern bool Rul_Count = false;
extern bool Rul_DelOnNewChannel = true;
int SystemState=0;

extern double ProfitFactor = 0.6;//0.8
void DrawFiboChannel(int pos1, int pos2, int pos3, int dir)
{
   if(Rul_DelOnNewChannel==true)DelOrder();
   int x1, x2, x3, x;
   x1 = iTime(NULL, 0, pos1);
   x2 = iTime(NULL, 0, pos2);
   x3 = iTime(NULL, 0, pos3);
   double k = 0.85;
   x = x2 + k*x3 - k*x1;
   k = 1.15;
   limitX = x2 + k*x3 - k*x1;
   direction = dir;
   if(controlTime!=x2)ReadySet = true;
   controlTime = x2;
   ObjectDelete("Fibo_Channel");
   ObjectDelete("Fibo_HLine");
   ObjectDelete("Fibo_VLine");
   w1 = pos1; w2 = pos2; w3 = pos3;
   if(dir==1)   
   {
      //Alert("Channel Up");
      ObjectCreate("Fibo_Channel", OBJ_FIBOCHANNEL, 0, iTime(NULL, 0, pos1), iLow(NULL, 0, pos1), iTime(NULL, 0, pos2), iHigh(NULL, 0, pos2), iTime(NULL, 0, pos3), iLow(NULL, 0, pos3)); 
      ObjectCreate("Fibo_HLine", OBJ_HLINE, 0, 0, iHigh(NULL, 0, pos2));
      ObjectCreate("Fibo_VLine", OBJ_VLINE, 0, x, 0);
      y = iHigh(NULL, 0, pos2);
      TakeProfit = NormalizeDouble((iHigh(NULL, 0, pos1) - iLow(NULL, 0, pos2))*ProfitFactor, Digits);
      StopLoss = iLow(NULL, 0, pos3);
   }
   if(dir==-1)   
   {
      //Alert("Channel Down");
      ObjectCreate("Fibo_Channel", OBJ_FIBOCHANNEL, 0, iTime(NULL, 0, pos1), iHigh(NULL, 0, pos1), iTime(NULL, 0, pos2), iLow(NULL, 0, pos2), iTime(NULL, 0, pos3), iHigh(NULL, 0, pos3)); 
      ObjectCreate("Fibo_HLine", OBJ_HLINE, 0, 0, iLow(NULL, 0, pos2));
      ObjectCreate("Fibo_VLine", OBJ_VLINE, 0, x, 0);
      y = iLow(NULL, 0, pos2);
      TakeProfit = NormalizeDouble((iHigh(NULL, 0, pos2) - iLow(NULL, 0, pos1))*ProfitFactor, Digits);
      StopLoss = iHigh(NULL, 0, pos3);
   }
   ObjectSet("Fibo_Channel", OBJPROP_FIBOLEVELS, 4);
   ObjectSet("Fibo_Channel", OBJPROP_FIRSTLEVEL+0, 0.15);
   ObjectSet("Fibo_Channel", OBJPROP_FIRSTLEVEL+1, -0.15);
   ObjectSet("Fibo_Channel", OBJPROP_FIRSTLEVEL+2, -0.85);
   ObjectSet("Fibo_Channel", OBJPROP_FIRSTLEVEL+3, -1.15);
   ObjectSet("Fibo_Channel", OBJPROP_LEVELCOLOR, FireBrick);
}
void SetFiboChannel()
{
   GetFractals();
   if(FractalUp[0]==FractalDown[0])return;
   if(FractalUp[1]==FractalDown[1])return;
  
   if(FractalUp[0]>FractalDown[0])   //Trend Up
   {
      if(FractalUp[0]>FractalDown[1])return;
      if(FractalDown[1]>FractalUp[1])return;
      
      DrawFiboChannel(FractalDown[1], FractalUp[0], FractalDown[0], 1);
   }
   if(FractalUp[0]<FractalDown[0])   //Trend Down
   {
      if(FractalDown[0]>FractalUp[1])return;
      if(FractalUp[1]>FractalDown[1])return;
      
      DrawFiboChannel(FractalUp[1], FractalDown[0], FractalUp[0], -1);
   }
}

void GetFractals()
{
   int posUp=0;
   int posDown=0;
   bool cont=true;
   int pos=3;
   while(cont==true)
   {
      double valUp = iFractals(NULL, 0, MODE_UPPER, pos);
      double valDown = iFractals(NULL, 0, MODE_LOWER, pos);
      if((valUp>0)&&(posUp<3))
      {
         FractalUp[posUp]=pos;
         posUp++;
      }
      if((valDown>0)&&(posDown<3))
      {
         FractalDown[posDown]=pos;
         posDown++;
      }
      pos++;
      if((posDown>2)&&(posUp>2))cont=false;
   }
}

int init()
  {
//----
   Lots = Lot;
   DelOrder();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }

bool CheckEMA(int dir)
{
   int i;
if(Rul_EMA==true)
   for(i=w2; i<w1; i++)
   {
      if(dir==1)
      {
          double h1 = iMA(NULL, 0, 2, 0, MODE_EMA, PRICE_HIGH, i);
          double h2 = iMA(NULL, 0, 2, 0, MODE_EMA, PRICE_HIGH, i+1);
          if(h1<h2)
          {
            //Alert("not allow");
            return(false);
          }
      }
      if(dir==-1)
      {
          double l1 = iMA(NULL, 0, 2, 0, MODE_EMA, PRICE_LOW, i);
          double l2 = iMA(NULL, 0, 2, 0, MODE_EMA, PRICE_LOW, i+1);
          if(l1>l2)
          {
            //Alert("not allow");
            return(false);
          }
      }
   }
if(Rul_Close==true)
   for(i=w3; i<w2; i++)
   {
      if(dir==1)
      {
          if(Close[i]>Close[w2])
          {
            //Alert("not allow");
            return(false);
          }
      }
      if(dir==-1)
      {
          if(Close[i]<Close[w2])
          {
            //Alert("not allow");
            return(false);
          }
      }
   }
if(Rul_Count==true)
   {
      if((w2-w1)<(w3-w2))
      {
         //Alert("not allow");
         return(false);
      }
   }
   return(true);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+

void SetOrder()
{
   bool ema;
   if(direction==1)
   {
      ema = CheckEMA(1);
      //if((y-Close[0])<20*point)ema=false;
      //if((y-StopLoss)<15*point)ema=false;
      //if(TakeProfit<15*point)ema=false;
      if(ema==true)
      {
            Ord = OrderSend(Symbol(), OP_BUYSTOP, Lots, y+3*point, 3, StopLoss, y+3*point+TakeProfit, 0, MagicNumber, 0, Red);
      }
   }
   if(direction==-1)
   {
      ema = CheckEMA(-1);
      //if((Close[0]-y)<20*point)ema=false;
      //if((StopLoss-y)<15*point)ema=false;
      //if(TakeProfit<15*point)ema=false;
      if(ema==true)
      {
            Ord = OrderSend(Symbol(), OP_SELLSTOP, Lots, y-3*point, 3, StopLoss, y-3*point-TakeProfit, 0, MagicNumber, 0, Red);
      }
   }
   ReadySet = false;
}

void DelOrder()
{
   int i;
   bool select, success;
   for( i=OrdersTotal();i>=0;i--)
   {
      select = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if((select == true) && (OrderMagicNumber() == MagicNumber)&& (OrderSymbol()==Symbol()) && ((OrderType() == OP_SELLSTOP)||(OrderType() == OP_BUYSTOP)))
      {
         OrderDelete(Ord);
      }       
   }
}

int start()
  {
//----
  if(Time[0]>lastTime)
   {
      lastTime=Time[0];
      SetFiboChannel();
      int t0 = iTime(NULL, 0, 0);
      int t1 = iTime(NULL, 0, 1);
      if((x<t0)&&(ReadySet==true))
      {
         SetOrder();
      }
      if(limitX<t0)
      {
         DelOrder();
      }
      
   }
//----
   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 ---