Navigation:Home > Content >

Avalanche_v1[1].2a.mq4

Time: 2010-07-24 | Download file:Avalanche_v1[1].2a.mq4

//+---------------------------------------------------------------------------+
//|                                                         Avalanche_v1.0.mq4|
//|                                                    Version 1.0: 03-04-2006|
//|                                                    Version 1.1: 03-15-2006| Added more error trapping
//|                                                    Version 1.2: 03-16-2006| Corrected some bugs and ERP Buffer
//|                                                Copyright © 2006, HoggZilla|
//|http://www.strategybuilderfx.com/forums/showthread.php?t=16102&page=1&pp=40|
//+---------------------------------------------------------------------------+

#property copyright "Copyright © 2006, HoggZilla"
#property link      "http://www.strategybuilderfx.com/forums/showthread.php?t=16102&page=1&pp=40"

int nOrderMagicNumber                     =     12345;

extern double dBaseLotSize                =     1.0;     // This is the "x" Value (base Lot Size)
extern double dX_Away                     =     1.0;     // X Value for Away Trades
extern double dX_Toward                   =     2.0;     // X Value for Toward Trades
extern double dX_Toward_Int               =     3.0;     // X Value for Toward Trades paying Interest

double dLots_Toward;                                     // Trade Lots after multiplier
double dLots_Away;   

double dPrice_Toward;
double dPrice_Away; 

extern double dStopLoss                   =     0;       // No StopLoss on any orders                                   

extern int nSlippage                      =     20;

string strComment                         =     "Avalanche";

int strArrowColor_Toward;
int strArrowColor_Away;

extern int nERP_ChartPeriod               =     240;     // The chart used in the ERP, 240 = 4hour
extern int nERP_NumberOfPeriods           =     100;     // The number of periods used in the SMA on the chart

extern int nERP_ChangeBuffer              =     50;      // This is the buffer before changing ERP Position

extern int nInterval_Toward               =     10;       // Interval between Toward Trade Orders
extern int nInterval_Away                 =     10;       // Interval between Away Trade Orders

extern double dTakeProfit_Toward          =     10;       // Take Profit setting on Toward Trades
extern double dTakeProfit_Away            =     8;       // Take Profit setting on Away Trades

double dTakeProfitPrice_Toward            =     0;
double dTakeProfitPrice_Away              =     0;

extern int nStackBuffer_Toward            =     5;       // Stacking Buffer on Toward Trades
extern int nStackBuffer_Away              =     5;       // Stacking Buffer on Away Trades

extern bool bCancelOpenOrders             =     true;    // Cancel all open orders, the program will set new orders 
extern bool bCloseOpenTrades              =     false;   // Are you sure you want to set this to true?

extern bool bOpenStartingOrders           =     true;    // Should the program automatically open new orders when initiated

int ticket                                =     0;

int nTradeOperation_Toward;                              // Buy or Sell - the number for OrderSend 
int nTradeOperation_Away;

string strOp_Toward;                                     // Buy or Sell - the word for Printing
string strOp_Away;

double dCurrentOrderPrice_Toward;
double dCurrentOrderPrice_Away;

string strInterestDirection;

double dCurrentPrice_Toward;
double dCurrentPrice_Away;

double dDistCurrentOrder_Away;
double dDistCurrentOrder_Toward;

double dDistCurrentStackOrder_Away;
double dDistCurrentStackOrder_Toward;
int MultiplierPoint;
double DigitPoint;
double StopLevel;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//------------------------------------------------------
//Broker 4 or 5 digits
DigitPoint=MarketInfo(Symbol(),MODE_POINT);
MultiplierPoint=1;
if(MarketInfo(Symbol(),MODE_DIGITS)==3||MarketInfo(Symbol(),MODE_DIGITS)==5)
{
MultiplierPoint=10;
DigitPoint*=MultiplierPoint;
}
//------------------------------------------------------
//Market info
StopLevel=MathMax(MarketInfo(Symbol(),MODE_FREEZELEVEL),MarketInfo(Symbol(),MODE_STOPLEVEL))/MultiplierPoint;
//------------------------------------------------------
//Minimum pips = stop level
if((dStopLoss>0)&&(dStopLoss0)&&(dTakeProfit_Toward0)&&(dTakeProfit_Away0)&&(nInterval_Toward0)&&(nInterval_Away= (nInterval_Toward + nStackBuffer_Toward)) //time to set a new Stack Order Toward
      {
         if(nTradeOperation_Toward==4) 
            {
               // Toward - Buy
               dPrice_Toward                 = (dLastStackOrder_Toward - (nInterval_Toward*DigitPoint));
               dTakeProfitPrice_Toward       = (dPrice_Toward + (dTakeProfit_Toward*DigitPoint));
               Print("Open Toward Stack BUY: ",strOp_Toward,": ",dPrice_Toward," TP: ",dTakeProfitPrice_Toward);
               ticket = OrderSend(Symbol(), nTradeOperation_Toward, dLots_Toward, dPrice_Toward, nSlippage, dStopLoss, dTakeProfitPrice_Toward, strComment, nOrderMagicNumber, 0, strArrowColor_Toward);
               if(ticket<0)
                  {
                     Print("Opening Stack BUY Toward Order ERROR: ",GetLastError());  
                  }
               else
                  {
                     dLastStackOrder_Toward        = dPrice_Toward;  //this is now the 'new' price for Last Stack Order Toward
                     Print("Create NEW Stack BUY Toward at: ",dLastStackOrder_Toward," TP:",dTakeProfitPrice_Toward);
                  }
            }
         else
            {
               // Toward - Sell
               dPrice_Toward                 = (dLastStackOrder_Toward + (nInterval_Toward*DigitPoint));
               dTakeProfitPrice_Toward       = (dPrice_Toward - (dTakeProfit_Toward*DigitPoint));
               Print("Open Toward Stack SELL: ",strOp_Toward,": ",dPrice_Toward," TP: ",dTakeProfitPrice_Toward);
               ticket = OrderSend(Symbol(), nTradeOperation_Toward, dLots_Toward, dPrice_Toward, nSlippage, dStopLoss, dTakeProfitPrice_Toward, strComment, nOrderMagicNumber, 0, strArrowColor_Toward);
               if(ticket<0)
                  {
                     Print("Opening Stack SELL Toward Order ERROR: ",GetLastError());  
                  }
               else
                  {
                     dLastStackOrder_Toward        = dPrice_Toward;  //this is now the 'new' price for Last Stack Order Toward
                     Print("Create NEW Stack SELL Toward at: ",dLastStackOrder_Toward," TP:",dTakeProfitPrice_Toward);
                  }
            }
         
      }
   
   if(dDistCurrentStackOrder_Away >= (nInterval_Away + nStackBuffer_Away)) //time to set a new Stack Order Away
      {
         if(nTradeOperation_Away==4) 
            {
               // Away - Buy
               dPrice_Away                   = (dLastStackOrder_Away - (nInterval_Away*DigitPoint));
               dTakeProfitPrice_Away         = (dPrice_Away + (dTakeProfit_Away*DigitPoint));
               Print("Open Away Stack BUY: ",strOp_Away,": ",dPrice_Away," TP: ",dTakeProfitPrice_Away);
               ticket = OrderSend(Symbol(), nTradeOperation_Away, dLots_Away, dPrice_Away, nSlippage, dStopLoss, dTakeProfitPrice_Away, strComment, nOrderMagicNumber, 0, strArrowColor_Away);
               if(ticket<0)
                  {
                     Print("Opening Stack BUY Away Order ERROR: ",GetLastError());  
                  }
               else
                  {
                     dLastStackOrder_Away          = dPrice_Away;  //this is now the 'new' price for Last Stack Order Away
                     Print("Created NEW Stack BUY Away at: ",dLastStackOrder_Away," TP:",dTakeProfitPrice_Away);
                  }
            }
         else
            {
               // Away - Sell
               dPrice_Away                   = (dLastStackOrder_Away + (nInterval_Away*DigitPoint));
               dTakeProfitPrice_Away         = (dPrice_Away - (dTakeProfit_Away*DigitPoint));
               Print("Open Away Stack SELL: ",strOp_Away,": ",dPrice_Away," TP: ",dTakeProfitPrice_Away);
               ticket = OrderSend(Symbol(), nTradeOperation_Away, dLots_Away, dPrice_Away, nSlippage, dStopLoss, dTakeProfitPrice_Away, strComment, nOrderMagicNumber, 0, strArrowColor_Away);
               if(ticket<0)
                  {
                     Print("Opening Stack SELL Away Order ERROR: ",GetLastError());  
                  }
               else
                  {
                     dLastStackOrder_Away          = dPrice_Away;  //this is now the 'new' price for Last Stack Order Away
                     Print("Created NEW Stack SELL Away at: ",dLastStackOrder_Away," TP:",dTakeProfitPrice_Away);
                  }
            }
         
      }
   
   strLastERPPosition=strERPPosition;
   Print("*****END*******");
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

// Which direction pays interest on this pair
string func_InterestDirection(string symbol) 
   {
	  if(symbol=="AUDUSD")      {	return("BUY");
	} else if(symbol=="AUDNZD") {	return("SELL");

	} else if(symbol=="CHFJPY") {	return("BUY");

	} else if(symbol=="EURAUD") {	return("SELL");
	} else if(symbol=="EURCAD") {	return("SELL");
	} else if(symbol=="EURCHF") {	return("BUY");
	} else if(symbol=="EURGBP") {	return("SELL");
	} else if(symbol=="EURJPY") {	return("BUY");
	} else if(symbol=="EURUSD") {	return("SELL");

	} else if(symbol=="GBPCHF") {	return("BUY");
	} else if(symbol=="GBPJPY") {	return("BUY");
	} else if(symbol=="GBPUSD") { return("BUY");


	} else if(symbol=="USDCAD") {	return("BUY");
	} else if(symbol=="USDCHF") {	return("BUY");
	} else if(symbol=="USDJPY") {	return("BUY");

	} else {	Comment("Unexpected Symbol - Default"); return("BUY");
	}
   }
   
// Position of Current Price related to ERP
// If the price is above the ERP then our position is Above
// If the price is below the ERP then out position is Below
string func_ERPPosition(double dCurrentERP , double dCurrentPrice, string strLastPosition, int nERPBuffer) 
   {
	  if(strLastPosition=="NONE")
	     {
	     if(dCurrentPrice >= dCurrentERP)      
	        {	
	           return("Above");
	        }
	     else
	        {
	           return("Below");
	        }
	     }
	     
	  if(strLastPosition=="Above")
	     {
	     if(dCurrentPrice >= (dCurrentERP-nERPBuffer*DigitPoint))    
	        {	
	           return("Above");
	        }
	     else
	        {
	           return("Below");
	        }
	     }
	     
	  if(strLastPosition=="Below")
	     {
	     if(dCurrentPrice >= (dCurrentERP+nERPBuffer*DigitPoint))     
	        {	
	           return("Above");
	        }
	     else
	        {
	           return("Below");
	        }
	     }

	}


//+------------------------------------------------------------------------+
//| counts the number of open positions                                    |
//+------------------------------------------------------------------------+

int func_CountOpenPositions()
  {  int op =0;
     int totalorders = OrdersTotal();
     for(int i=totalorders-1;i>=0;i--)                                // scan all orders and positions...
      {
        OrderSelect(i, SELECT_BY_POS);
        if ( OrderSymbol()==Symbol() && ( (OrderMagicNumber() == 12345) || (OrderComment() == "Test")) )  // only look if mygrid and symbol...
         {  
          int type = OrderType();
          if ( type == OP_BUY ) {op=op+1;} 
          if ( type == OP_SELL ) {op=op+1;} 
         }
      } 
   return(op);
  }

//+------------------------------------------------------------------------+
//| cancels all pending orders                                             |
//+------------------------------------------------------------------------+

void func_ClosePendingOrders()
{
  int totalorders = OrdersTotal();
  for(int i=totalorders-1;i>=0;i--)
 {
    OrderSelect(i, SELECT_BY_POS);
    bool result = false;
    if ( OrderSymbol()==Symbol() && ( (OrderMagicNumber() == nOrderMagicNumber) || (OrderComment() == strComment)) )  // only look if mygrid and symbol...
     {
           //Close pending orders
           if ( OrderType() > 1 ) result = OrderDelete( OrderTicket() );
      }
  }
  return;
}

//+------------------------------------------------------------------------+
//| closes all open trades                                                 |
//+------------------------------------------------------------------------+

void func_CloseOpenTrades()
{
  int totalorders = OrdersTotal();
  for(int i=totalorders-1;i>=0;i--)
 {
    OrderSelect(i, SELECT_BY_POS);
    bool result = false;
    if ( OrderSymbol()==Symbol() && ( (OrderMagicNumber() == nOrderMagicNumber) || (OrderComment() == strComment)) )  // only look if mygrid and symbol...
     {
           //Close opened long positions
           if ( OrderType() == OP_BUY )  result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
           //Close opened short positions
           if ( OrderType() == OP_SELL )  result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
      }
  }
  return;
}

Recommend