//+------------------------------------------------------------------------------------+
//|                Bliksem.mq4                                                         |
//|                Copyright © 2006                                                    |
//|                http://fatcats.dats.co.za                                           |
//|                Bliksem EA                                                          |
//|                                                                                    |
//+------------------------------------------------------------------------------------+
#property copyright "Bliksem Expert Strategy of Wayne Ovenstone, 4x@ovenstone.co.za, Programmed by Barry Stander" 
#property link      "http://fatcats.dats.co.za"

string version = "Bliksem System Expert Version 2.2 29/01/2007 Strategy of Wayne Ovenstone, 4x@ovenstone.co.za, Programmed by Barry Stander";  
#include <stdlib.mqh>
//#include <BPS include.mqh>

extern int Envelope   = 55;   // 34
extern int Signal_H4  = 13;   //  8
extern int Signal_M30 = 13;   //  8
extern int Signal_M5  = 13;   //  8

extern int Spyker_JimmyEMA=1;  //3
extern int Spyker_SignalSMA=375; //296;

extern bool Use_Fix_Trailing_stop = 1;
extern int  fix_trailing_stop = 50;   // 50

extern   int      Increments    = 8;  // 8
extern   bool     Money_Managment = false;
extern   double   Risk = 0.01;
extern   double   Lots     = 0.01; //  0.01;    //   start lot size 
extern   bool     Fixed_Lots = 0;
  
int      Magic_No_start = 2345;  //  2345;
int      Magic_No;
int      last_m_no = 0;
int      Slippage = 1;

extern bool Remove_pending_orders = 0 ; 

//+------------------------------------------------------------------+
//| close pending orders function                                   |
//+------------------------------------------------------------------+
int close_pending_orders(int m_no)
  {
   int error_check3;
   int orders[10];
   int loop2 =0;
   
 for (int loop = 0; loop <= OrdersTotal(); loop++)
   {
   OrderSelect(loop,SELECT_BY_POS,MODE_TRADES  );
    if( OrderSymbol() == Symbol()     && OrderMagicNumber() == m_no
      && ( OrderType() == OP_BUYLIMIT  || OrderType() == OP_BUYSTOP || OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP ) ) 
       { 
        orders[loop2] = OrderTicket();
        loop2++;
       }
   }

if( m_no == 0 ) 
 for ( loop = 0; loop <= OrdersTotal(); loop++)
   {
   OrderSelect(loop,SELECT_BY_POS,MODE_TRADES  );
    if( OrderSymbol() == Symbol()     && OrderMagicNumber() >= Magic_No_start && OrderMagicNumber() <= Magic_No_start + 100 
      && ( OrderType() == OP_BUYLIMIT  || OrderType() == OP_BUYSTOP || OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP ) ) 
       { 
        orders[loop2] = OrderTicket();
        loop2++;
       }
   }
  
  if( OrdersTotal() > 0 )
   for ( loop = 0; loop <= 10; loop++)
    {
    if( orders[loop] > 0 )
    {
     error_check3 = OrderDelete( orders[loop] );
     
      while( error_check3 != 0 )
       {
       error_check3 = OrderDelete( orders[loop] );
      // Sleep( 1000 );
       }
     } 
   }
    
      
   return(0);
 } 




//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   Comment( version );
   watermark();
   Print("----------  Bliksem  Expert Started  ----------------"); 
   
  if( Remove_pending_orders == 1 )
     close_pending_orders(0);   // close old pending orders if   EA  started  
      
   
      
   return(0);
  }
  
void watermark()
  {
   ObjectCreate("bliksem", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("bliksem", "Programmed by Barry Stander", 8, "Lucida Handwriting", RoyalBlue);
   ObjectSet("bliksem", OBJPROP_CORNER, 2);
   ObjectSet("bliksem", OBJPROP_XDISTANCE, 5);
   ObjectSet("bliksem", OBJPROP_YDISTANCE, 10);
   return(0);
  }
    
//+------------------------------------------------------------------+
//|   expert start function                                                                 |
//+------------------------------------------------------------------+
int start()
  {
//  close_pending_orders(1) ;  
 string comment; 
 string comment_H1  = "  --  ";
 string comment_H4  = "  --  ";
 string comment_M30 = "  --  ";
 string comment_M5  = "  --  ";
 string trade_comment;
 
 int direction_H1  = 0 ;   //  1 = Long    2 =  Short  
 int direction_H4  = 0 ;   //  1 = Long    2 =  Short 
 int direction_M30 = 0 ;   //  1 = Long    2 =  Short 
 int direction_M5  = 0 ;   //  1 = Long    2 =  Short   
 
 int Enable_Long_trade  = 0;
 int Enable_Short_trade = 0;  
 int loop;
 int loop2;
 int error_check;
 int error_check2;
 int Spread;
 int TrailingStop;
 int TargetPoint;
 
 double EntryPrice;
 double EntryPrice2;
 double Open_Lots = 0;  
  
 
 
   if( last_m_no == 0 )
     Magic_No = Magic_No_start;
     watermark();
     
  // 1H signals
  double spyker_trend_H1  = iCustom(NULL, PERIOD_H1 , "Spyker",Spyker_JimmyEMA,Spyker_SignalSMA,0,0);
  double spyker_signal_H1 = iCustom(NULL, PERIOD_H1 , "Spyker",Spyker_JimmyEMA,Spyker_SignalSMA,1,0);
   if( spyker_trend_H1 == 0 && spyker_signal_H1 == 0 )
     Alert(" You need Spyker Indicator  or 1 Hour Data  ! ! ! ", Symbol() );
    
  //  4H signals
  double  ema_1_H4  = iMA( NULL , PERIOD_H4 , Envelope , 0 , MODE_EMA , PRICE_HIGH   , 0 );
  double  ema_2_H4  = iMA( NULL , PERIOD_H4 , Envelope , 0 , MODE_EMA , PRICE_LOW    , 0 );
  double  ema_3_H4  = iMA( NULL , PERIOD_H4 , Signal_H4 , 0 , MODE_EMA , PRICE_MEDIAN , 0 );            //  Outer_Level
       if( ema_1_H4 == 0 && ema_2_H4 == 0 && ema_2_H4 == 0)
         Alert(" You need  4 Hour Data  ! ! ! " , Symbol() ); 
  
  //  30M signals
  double  ema_1_M30  = iMA( NULL , PERIOD_M30 , Envelope , 0 , MODE_EMA , PRICE_HIGH   , 0 );
  double  ema_2_M30  = iMA( NULL , PERIOD_M30 , Envelope , 0 , MODE_EMA , PRICE_LOW    , 0 );
  double  ema_3_M30  = iMA( NULL , PERIOD_M30 , Signal_M30 , 0 , MODE_EMA , PRICE_MEDIAN , 0 );
       if( ema_1_M30 == 0 && ema_2_M30 == 0 && ema_2_M30 == 0)
         Alert(" You need  30 Minute Data  ! ! ! ", Symbol() ); 
  
  //  5M signals
  double  ema_1_M5  = iMA( NULL , PERIOD_M5 , Envelope , 0 , MODE_EMA , PRICE_HIGH   , 0 );
  double  ema_2_M5  = iMA( NULL , PERIOD_M5 , Envelope , 0 , MODE_EMA , PRICE_LOW    , 0 );
  double  ema_3_M5  = iMA( NULL , PERIOD_M5 ,  Signal_M5 , 0 , MODE_EMA , PRICE_MEDIAN , 0 );
       if( ema_1_M5 == 0 && ema_2_M5 == 0 && ema_2_M5 == 0)
         Alert(" You need  5 Minute Data  ! ! ! ", Symbol() ); 

 if( Spread == 0 )
   Spread = (Ask - Bid)/Point;
   
// ---------------- Money Managment to calc lots -------------   
 
if (Money_Managment == true)
   {
   Open_Lots = Lots * (AccountEquity() * Risk);
   Open_Lots = StrToDouble(DoubleToStr(Open_Lots,2));
   if (Open_Lots < Lots) Open_Lots = Lots;
   }

if (Money_Managment == false)
   {
   Open_Lots = Lots;
   if (Open_Lots < Lots) Open_Lots = Lots;
   } 

// ------------------------------------------------------

    
if(  spyker_trend_H1 > spyker_signal_H1 )
    { 
      direction_H1 = 1;
      comment_H1 = "H1     Long";
    } 
      
     
if(  spyker_trend_H1 < spyker_signal_H1 )
    { 
      direction_H1 = 2;
      comment_H1 = "H1     Short";
    } 
    
         
// ---------------------

if( ema_3_H4 > ema_1_H4  && ema_3_H4 > ema_2_H4  )
    { 
      direction_H4 = 1;
      comment_H4 = "H4     Long";  
    }                      
            
if( ema_3_H4 < ema_1_H4  && ema_3_H4 < ema_2_H4  )

    { 
      direction_H4 = 2;
      comment_H4 = "H4     Short";  
    }
           
if( ema_3_H4 < ema_1_H4  && ema_3_H4 > ema_2_H4  )
    { 
      direction_H4 = 0;
      comment_H4 = "H4     Middle"; 
      close_pending_orders(0); 
    }
              
// -----------------------
           
if( ema_3_M30 > ema_1_M30  && ema_3_M30 > ema_2_M30  )
    { 
      direction_M30 = 1;
      comment_M30 = "M30   Long";   
    }                     
            
if( ema_3_M30 < ema_1_M30  && ema_3_M30 < ema_2_M30  )
    { 
      direction_M30 = 2;
      comment_M30 = "M30   Short";   
    }     
          
if( ema_3_M30 < ema_1_M30  && ema_3_M30 > ema_2_M30  )
    { 
      direction_M30 = 0;
      comment_M30 = "M30   Middle";   
    } 
               
// -----------------------

if( ema_3_M5 > ema_1_M5  && ema_3_M5 > ema_2_M5  )
    { 
      direction_M5 = 1;
      comment_M5 = "M5     Long";    
    }            
            
if( ema_3_M5 < ema_1_M5  && ema_3_M5 < ema_2_M5  )
    { 
      direction_M5 = 2;
      comment_M5 = "M5     Short";    
    }     
    
if( ema_3_M5 < ema_1_M5  && ema_3_M5 > ema_2_M5  )
    { 
      direction_M5 = 0;
      comment_M5 = "M5     Middle";    
    }                    
     
//  -----------------------  

int Open_Buy_Orders   = 0;
int Open_Sell_Orders  = 0;


for (loop = 0; loop < OrdersTotal(); loop++)
   {
   OrderSelect(loop,SELECT_BY_POS,MODE_TRADES);
   if (OrderSymbol() == Symbol() && OrderMagicNumber() >= Magic_No_start && OrderMagicNumber() <= Magic_No_start + 100  ) 
      {
      if (OrderType() == OP_BUYLIMIT)
         Open_Buy_Orders++;

      if (OrderType() == OP_SELLLIMIT)
         Open_Sell_Orders++;
         
      if (OrderType() == OP_BUY || OrderType() == OP_SELL || OrderType() == OP_BUYLIMIT || OrderType() == OP_SELLLIMIT )
         if( last_m_no < OrderMagicNumber() )
           last_m_no = OrderMagicNumber();         

      }
   }
   
   if( last_m_no > 1 )
    Magic_No = last_m_no + 1;
     else
      Magic_No = Magic_No_start;   
      
      
   if( Magic_No <= 1 )
     Alert( Magic_No );      

// --------- Trade direction ---------------------------------


if(  direction_H1 == 2 &&  direction_H4 == 1 && direction_M30 == 1  && direction_M5 == 1  )
   Enable_Long_trade = 1;

  
if(  direction_H1 == 1 &&  direction_H4 == 2 && direction_M30 == 2  && direction_M5 == 2  )
  Enable_Short_trade = 1; 

 
// ------  Open Long orders  -----------------
// If the trade opens at 0.01, then set the SL at a value of 120, reducing this value by 12 pips for every new trade opened.
// Table to explain;
//    0.01 = 120
//    0.02 = 108
//    0.04 = 96
//    0.08 = 84
//    0.16 = 72
//    0.32 = 60
//    0.64 = 48
//    1.28 = 36
//    2.56 = 24
//

double Open_Lots_B = Open_Lots;
double Open_Lots_S = Open_Lots;
int    trade_per_basket = 9;
int      Stop_Loss = 0;

          if( Use_Fix_Trailing_stop == 1 && fix_trailing_stop > 0 )
           Stop_Loss = fix_trailing_stop; 
            else
             Stop_Loss = 120;
             

int      Target_Point    = 2000;


      
      
 
if ( Enable_Long_trade == 1 && Enable_Short_trade == 0 &&  Open_Buy_Orders == 0  )
{
  
   EntryPrice = Ask;//Bid+(Spread*Point);\

      
 
    trade_comment = "Bliksem2 "+Symbol()+"  "+Stop_Loss +"  "+ Magic_No;
    
 //   Print( "B:   ", Magic_No );
    
  if( Magic_No >= Magic_No_start )
   error_check = OrderSend(Symbol(),OP_BUY,Open_Lots_B,EntryPrice,Slippage,EntryPrice-(Stop_Loss * Point),EntryPrice+(Target_Point*Point),trade_comment+".0",Magic_No,0,Blue);
    if(error_check < 0)
     Print("Errors opening BUY order = ",GetLastError() ,"    ", ErrorDescription(error_check));
      else
      {
       for (loop2 = 1; loop2 < trade_per_basket ; loop2++)
        {
          if( Fixed_Lots == 0 )
           Open_Lots_B *=  2;
          
          if( Use_Fix_Trailing_stop == 1 && fix_trailing_stop > 0 )
           Stop_Loss = fix_trailing_stop; 
            else
             Stop_Loss -= 12;
          
          trade_comment = "Bliksem2 "+Symbol()+"  "+Stop_Loss +"  "+ Magic_No+"."+loop2;
         EntryPrice2 = EntryPrice-((Increments*loop2))*Point;
        if( Magic_No >= Magic_No_start ) 
         error_check2 = OrderSend(Symbol(),OP_BUYLIMIT,Open_Lots_B,EntryPrice2 ,Slippage,EntryPrice2 -( Stop_Loss * Point) ,EntryPrice2+(Target_Point*Point),trade_comment,Magic_No,0,Blue);
             if(error_check2 < 0)
               Print("Errors opening BUY order 2 = ",GetLastError() ,"    ", ErrorDescription(error_check2) );
        }
      }
      
   return(0);
}


// ------  Open Short orders  -----------------  

if ( Enable_Long_trade == 0 && Enable_Short_trade == 1 &&  Open_Sell_Orders == 0 )
{

  EntryPrice = Bid;
  Magic_No = last_m_no + 1;
   trade_comment = "Bliksem2 "+Symbol()+"  "+Stop_Loss +"  "+ Magic_No;
   
//   Print( "S:   ", Magic_No );
   
 if( Magic_No >= Magic_No_start )   
  error_check = OrderSend(Symbol(),OP_SELL,Open_Lots_S,EntryPrice,Slippage,EntryPrice+( Stop_Loss * Point),EntryPrice-(Target_Point*Point),trade_comment,Magic_No+".",0,Red);
   if(error_check < 0)
     Print("Errors opening SELL order = ",GetLastError() ,"    ", ErrorDescription(error_check) );
      else
       {
        for (loop2 = 1; loop2 < trade_per_basket ; loop2++)
        {
         if( Fixed_Lots == 0 )
          Open_Lots_S *=  2;
             
          if( Use_Fix_Trailing_stop == 1 && fix_trailing_stop > 0 )
           Stop_Loss = fix_trailing_stop; 
            else
             Stop_Loss -= 12;
             
         trade_comment = "Bliksem2 "+Symbol()+"  "+Stop_Loss +"  "+ Magic_No+"."+loop2;
         
         EntryPrice2 = EntryPrice+((Increments*loop2)*Point);
      if( Magic_No >= Magic_No_start )     
         error_check2 = OrderSend(Symbol(),OP_SELLLIMIT,Open_Lots_S,EntryPrice2 ,Slippage,EntryPrice2 + ( Stop_Loss * Point) ,EntryPrice2-(Target_Point*Point),trade_comment,Magic_No,0,Blue);
             if(error_check2 < 0)
               Print("Errors opening SELL order 2 = ",GetLastError() ,"    ", ErrorDescription(error_check2) );
        }
       }
   
   return(0);
}

// -------  EXIT + trailing stop  -------------------------------
// 
//
// Trailing stop table:
//    price move     Stoploss       Gain if close by SL
//       +20            20                 0
//       +30            25                 5
//       +40            30                10
//       +50            35                15
//       +60            40                20
//       +70            45                25
//       +80            50                30
//       +90            50                40
//       +100           50                50
//       +200           50               150
//
//


int  lots_check  = 0;
int  order_check = 0;

 

if( OrdersTotal()  >=  1  )               
{
 int total=OrdersTotal();    


for(int cnt=0;cnt<=total;cnt++)  //   cnt loop 
 {
  OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
    
   if( OrderSymbol() == Symbol() && OrderMagicNumber() >= Magic_No_start && OrderMagicNumber() <= Magic_No_start + 100  && order_check != OrderTicket()   )  // check for symbol
     {
 
if( Use_Fix_Trailing_stop == 1 && fix_trailing_stop > 0 )
   TrailingStop = fix_trailing_stop;
    else
      {
      if( OrderProfit() <= 20 )  TrailingStop  = 20;
      if( OrderProfit() >= 30 && OrderProfit() < 40)   TrailingStop = 25;
      if( OrderProfit() >= 40 && OrderProfit() < 50)   TrailingStop = 30;
      if( OrderProfit() >= 50 && OrderProfit() < 60)   TrailingStop = 35;
      if( OrderProfit() >= 60 && OrderProfit() < 70)   TrailingStop = 40;
      if( OrderProfit() >= 70 && OrderProfit() < 80)   TrailingStop = 45;
      if( OrderProfit() >= 80 )  TrailingStop  = 50;
      }      
     
      TargetPoint = 1000;  // move Target Point way ahead as we exit by Trailing stop 
      
 if(OrderType()==OP_BUYSTOP || OrderType()==OP_BUY  )        // long position is opened        OP_BUY||OP_BUYSTOP
    {
       if(Bid-OrderOpenPrice()>Point*TrailingStop)
           {
            if(OrderStopLoss()<Bid-Point*TrailingStop)
              {
               OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(Point*TrailingStop),Bid+(Point*TargetPoint),0,Green);
                Print(" OrderModify: ",OrderTicket()," ",OrderOpenPrice(),"  ",Bid-Point*TrailingStop,"  ",OrderTakeProfit() );
               close_pending_orders(OrderMagicNumber());
               return(0);
              }
          }
     }
           
      
 if(OrderType()==OP_SELLSTOP || OrderType()==OP_SELL)   // short position is opened         OP_SELL||OP_SELLLIMIT
   {                
      if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
          {
           if(OrderStopLoss()>(Ask+(Point*TrailingStop)) || (OrderStopLoss()==0))
             {
              OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(Point*TrailingStop),Ask-(Point*TargetPoint),0,Red);
               Print(" OrderModify: ",OrderTicket()," ",OrderOpenPrice()," ",Ask+(Point*TrailingStop)," ",OrderTakeProfit() );
               close_pending_orders(OrderMagicNumber());
              return(0);
             }
         }
    }
    
    
  }
}//   cnt loop end


}   

// -------------------------------------------
  
Comment(          version  + "Bliksem  Signals :"  , "\nSpyker        ", comment_H1  , "\n", comment_H4  ,"  (  ", ema_1_H4  ,"  -  ", ema_2_H4  ,"  )  " , ema_3_H4  
                 , "\n", comment_M30 ,"  (  ", ema_1_M30 ,"  -  ", ema_2_M30 ,"  )  " , ema_3_M30 , "\n", comment_M5  ,"  (  ", ema_1_M5  ,"  -  ", ema_2_M5  ,"  )  " , ema_3_M5 
                 , "\nSettings:  Envelope: ",Envelope,"   H4: ",Signal_H4,"   M30: ",Signal_M30,"   M5: ", Signal_M5 
                 , "\n             Spyker:   ", Spyker_JimmyEMA   ,"   ", Spyker_SignalSMA ,"   Stop: ", fix_trailing_stop 
                 , "\n             Magic_No: ", Magic_No  ,"    Orders:    ", OrdersTotal()                             );
 
  return(0);




























































}




















































 

   

