Navigation:Home > Content >

Suicidio.mq4

Time: 2015-11-01 | Download file:Suicidio.mq4

//+------------------------------------------------------------------+
//|                                                          GUO.mq4 |
//|                      Copyright © 2012, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

extern double        LOTS = 0.01;
extern int           START_OP = 0;
extern double        MULT_FACTOR = 2;
extern int           MAGIC = 1111;
extern int           MAX_BUY = 100;
extern int           MAX_SELL = 100;
extern int           HEIGHT_CHANNEL = 4; //pips
extern int           REDUCE_CHANNEL = 0; //pips
extern double        START_PROFIT = 0.1;
extern double        SL = 3;
extern double        TP = 5;
extern bool          TRAIL = true;
extern bool          TRAIL_COMPENSATION = true;
extern bool          USE_ERROR = false;
extern bool          USE_RSI = false;
extern int           RSI_PERIOD = 14;
extern int           RSI_PRICE = 0; //0-6
extern int           RSI_SHIFT = 0; 
extern double        RSI_LIMIT = 50; 
extern bool          USE_BAND = false;
extern double        BAND_LIMIT = 0.002;
extern bool          USE_SPIKE = false;
extern double        SPIKE_VAL = 0.002;
extern bool          USE_ATR = false;
extern double        ATR_VAL = 0.004;

int last_trade = 3, total_buy, total_sell, total_buystop, total_sellstop;
double Poin, up_limit=0, down_limit=0, lot_size,lot_mult=1, PROFIT;
bool started = false;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   if (Point == 0.00001) Poin = 0.0001; //5 digits
   else if (Point == 0.001) Poin = 0.01; //3 digits
   else Poin = Point; //Normal
   lot_size = LOTS;
   PROFIT = START_PROFIT;
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
   Run();
   return(0);
}
//+------------------------------------------------------------------+


int error = 0;
bool start_trail = false;
void Run()
{ 
   double rsi = iRSI(NULL,0,RSI_PERIOD,RSI_PRICE,RSI_SHIFT);
   double upper_band = iBands(NULL, NULL,20,2,0,PRICE_MEDIAN,MODE_UPPER,0);
   double main_band = iBands(NULL, NULL,20,2,0,PRICE_MEDIAN,MODE_MAIN,0);
   double lower_band = iBands(NULL, NULL,20,2,0,PRICE_MEDIAN,MODE_LOWER,0);
   double atr = iATR(NULL,0,12,0);
   //PROFIT = 0.01*lot_mult;
   if(TotalOrders1(0)==0 && TotalOrders1(1)==0 && !started )
   {
      if(USE_RSI)
      {
         if(rsi < RSI_LIMIT)
         {
            return (0);
         }
      }
      if(USE_BAND)
      {
         if(upper_band-lower_band < BAND_LIMIT)
         {
            return (0);
         }
      }
      if(USE_SPIKE)
      {
         if((High[0]-Low[0])= up_limit && (last_trade == 1 || last_trade == 3))
      {
         if(AccountFreeMarginCheck(Symbol(),OP_SELL,NormalizeDouble(LOTS*lot_mult, 2))<=0)
         {
            error = GetLastError();
            return;
         }
         lot_size = NormalizeDouble(LOTS*lot_mult, 2);
         OrderSend(Symbol(),OP_BUY,lot_size,Ask,3,0,0,"",MAGIC,0,Blue);
         error = GetLastError();
         last_trade = 0;
         lot_mult= lot_mult*MULT_FACTOR;
         error = GetLastError();
         if(up_limit - down_limit > NormalizeDouble(((HEIGHT_CHANNEL - REDUCE_CHANNEL) * Poin), Digits)) up_limit = up_limit - Point;
         if(PROFIT > 0.1) PROFIT = PROFIT - 0.1;
      }
      if(Bid <= down_limit && (last_trade == 0 || last_trade == 3))
      {
         if(AccountFreeMarginCheck(Symbol(),OP_BUY,NormalizeDouble(LOTS*lot_mult, 2))<=0)
         {
            error = GetLastError();
            return;
         }
         lot_size = NormalizeDouble(LOTS*lot_mult, 2);
         OrderSend(Symbol(),OP_SELL,lot_size,Bid,3,0,0,"",MAGIC,0,Blue);
         error = GetLastError();
         
         last_trade = 1;
         lot_mult= lot_mult*MULT_FACTOR;
         if(up_limit - down_limit > NormalizeDouble(((HEIGHT_CHANNEL - REDUCE_CHANNEL) * Poin), Digits)) down_limit = down_limit + Point;
         
         if(PROFIT > 0.1) PROFIT = PROFIT - 0.1;
      }
   }
   
   Comment("PROFITTO DA RAGGIUNGERE: ", PROFIT, 
           "\nPROFITTO ATTUALE: ", TotalProfit(), 
           "\nPROFITTO BUY: ", BuyProfit(), 
           "\nPROFITTO SELL: ", SellProfit(), 
           "\nLOTS TO PROFIT: ", LotsToProfit(), 
           "\nLOT SIZE: ", lot_size, 
           "\nLOT MULT: ", lot_mult,
           "\nUP LIMIT: ", up_limit,
           "\nDOWN LIMIT: ", down_limit);
   
   if(TRAIL)
   {
      if(TRAIL_COMPENSATION)
      {
         if(USE_ERROR)
         {
            
            if(error== 134 && BuyProfit() > PROFIT /*SellProfit()*/)
            {
               //CloseOpen();
               CloseSell();
               started = false;
               start_trail = true;
               error= 0;
            }
            if(error== 134 && SellProfit() > PROFIT /*BuyProfit()*/)
            {
               //CloseOpen();
               CloseBuy();
               started = false;
               start_trail = true;
               error= 0;
            }
         }
         if(TotalProfit() >= PROFIT && BuyProfit() > 0)
         {
            if(TotalProfit() >= LotsToProfit())
            {
               CloseSell();
               started = false;
               start_trail = true;
            }
         }
         else if(TotalProfit() >= PROFIT && SellProfit() > 0)
         {
            if(TotalProfit() >= LotsToProfit())
            {
               CloseBuy();
               started = false;
               start_trail = true;
            }
         }
         
      }
      else
      {
         if(TotalProfit() >= (PROFIT + 1) && TRAIL)
         {
            if(BuyProfit() > 0)
            {
               CloseSell();
         
            }
            else
            {
               CloseBuy();
         
            }
            
            started = false;
            start_trail = true;
         }
      }
      
   }
   else
   {
      if(TotalProfit() >= PROFIT)
      {
         CloseOpen();
         started = false;
      }
   }
   if(start_trail)
   {
      Trailing();
   }
   
   
}



double LotsToProfit()
{
   double total_lots=0;
   double profit=0;
   for (int i=0;i 0)
         {
            total_lots = total_lots + OrderLots();
         } 
      }
   }
   profit = PROFIT + ((total_lots * 100000)*(SL*Poin));
   return (profit);
}

void TotalOrders()
{
   int i = 0, total=0;
   total_buy=0;
   total_sell=0;
   total_buystop=0;
   total_sellstop=0;
   
      for (i=0;i0.0 && Price[1]>0.0);
}

void CloseOpen()
{
   int iOrders=OrdersTotal()-1;
   for(int i=iOrders; i>=0; i--) {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderType()<=OP_SELL) && GetMarketInfo() && !OrderClose(OrderTicket(),OrderLots(),Price[1-OrderType()],giSlippage)){}
    }
}

void CloseBuy()
{
   int iOrders=OrdersTotal()-1;
   for(int i=iOrders; i>=0; i--) {
      OrderSelect(i,SELECT_BY_POS);
      if(OrderType()==OP_BUY)
      {
         GetMarketInfo();
         OrderClose(OrderTicket(),OrderLots(),Price[1-OrderType()],giSlippage);
      }
    }
}

void CloseSell()
{
   int iOrders=OrdersTotal()-1;
   for(int i=iOrders; i>=0; i--) {
      OrderSelect(i,SELECT_BY_POS);
      if(OrderType()==OP_SELL)
      {
         GetMarketInfo();
         OrderClose(OrderTicket(),OrderLots(),Price[1-OrderType()],giSlippage);
      }
    }
}

void DeleteAllPending()
{
   int iOrders=OrdersTotal()-1, i;
   for(i=iOrders; i>=0; i--) 
   {
      OrderSelect(i,SELECT_BY_POS);
      if((OrderType()>OP_SELL))
      {
         OrderDelete(OrderTicket());
       }
   }
}

void Trailing()
{
   for (int i=0;iOrderStopLoss()+(SL * Poin)  && OrderStopLoss() != OrderStopLoss()+(SL * Poin))
                      {
                         OrderModify(OrderTicket(),0,NormalizeDouble(Bid-(SL * Poin),5),NormalizeDouble(Bid+(TP * Poin),5),0,CLR_NONE);
                                              
                      }
                      break;
                }
              }
          }
}

Recommend