Download MetaTrader 5

need help with renko ea

To add comments, please log in or register
The entire MQL5.community history on a single page!
Dartmouth Robinson
219
Dartmouth Robinson 2016.03.22 07:31 

I got the code from ICoff at forexfactory i wanted to take out the stop loss settings in it and have it just close op on opposite candle

It will open buy orders but not sell orders on sell orders i get error 130 Invalid stops


//+------------------------------------------------------------------+
//|                                              Renko-Apfelsaft.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern int MagicNumber =   1234;    // Magic Number
extern int OpenBars =      2;       // Number of bars before opening
extern int ExtendedTP =    8;       // Number of bars before extending exit
extern double LotSize =    0.01;    // Lot size
extern int Slippage =      10;      // Allowed slippage in Points


datetime BarTime;
bool MinMovement;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit(){
   BarTime = Time[1];
   MinMovement = false;
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason){
      
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick(){
  
   string Bar1,Bar2;
   bool OpenTrade = false;
   int Ticket;

   // We only need to perform actions on creation of a new bar
   if(BarTime != Time[0]){
      
      // evaluate previous 2 bars
      if (Open[1] < Close[1]) Bar1 = "up";
      else Bar1 = "down";
      if (Open[2] < Close[2]) Bar2 = "up";
      else Bar2 = "down";
      
      // is there an open trade?
      for(int i=0; i<OrdersTotal(); i++){
         if((OrderSelect(i,SELECT_BY_POS) == true)&&(OrderSymbol() == Symbol())&&(OrderMagicNumber() == MagicNumber)){
            if (OrderType() == OP_BUY){
               OpenTrade = true;
               if (ConsecutiveBars("buy") >= 4) MinMovement = true;
               // take profit hit?
               if (ConsecutiveBars("buy") >= (ExtendedTP + OpenBars)){
                  if ((Bar1 == "down") && (Bar2 == "down")){
                     if ((OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrRed)) == false)Print("buy order close failed with error #",GetLastError());
                     else OpenTrade = false;
                  }
               } else {
                  if ((Bar1 == "down")&&(MinMovement == true)){
                     if ((OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrRed)) == false)Print("buy order close failed with error #",GetLastError());
                     else {
                        OpenTrade = false;
                        MinMovement = false;
                     }
                  }
               }
            }
            if (OrderType() == OP_SELL){
               OpenTrade = true;
               if (ConsecutiveBars("sell") >= 4) MinMovement = true;
               // take profit hit?
               if (ConsecutiveBars("sell") >= (ExtendedTP + OpenBars)){
                  if ((Bar1 == "up") && (Bar2 == "up")){
                     if ((OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,clrRed)) == false)Print("sell order close failed with error #",GetLastError());
                     else OpenTrade = false;
                  }
               } else {
                  if ((Bar1 == "up")&&(MinMovement == true)){
                     if ((OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,clrRed)) == false)Print("sell order close failed with error #",GetLastError());
                     else OpenTrade = false;
                  }
               }
            }
         }
      }
      
      // look for new trades
      if (OpenTrade == false){
         MinMovement = false;
         if ((Bar1 == "up") && (Bar2 == "up")){
            // open buy trade
           
            Ticket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,Slippage,"Renko-Apfelsaft",MagicNumber,0,clrGreen);
               if (Ticket < 0) Print("buy order failed with error #", GetLastError());
         }
         if ((Bar1 == "down") && (Bar2 == "down")){
            // open sell trade
            
            Ticket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,Slippage,"Renko-Apfelsaft",MagicNumber,0,clrGreen);
               if (Ticket < 0) Print("sell order failed with error #", GetLastError());
         }
      }
      
      BarTime = Time[0];
   }
}
//+------------------------------------------------------------------+
//| Functions                                                        |
//+------------------------------------------------------------------+

// count consecutive bars of same direction
int ConsecutiveBars(string TradeDirection){
   int a = 1;
   
   if (TradeDirection == "buy") while (Open[a] < Close[a]) a++;
   if (TradeDirection == "sell")while (Open[a] > Close[a]) a++;

   return a-1;
}

Alain Verleyen
Moderator
30707
Alain Verleyen 2016.03.22 08:08  
Check the documentation of OrderSend(), the parameters are not good.
Dartmouth Robinson
219
Dartmouth Robinson 2016.03.22 09:17  

so it should be like this  i have to have ,0,0, for stoploss and take profit if i dont want to use them

Ticket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,Slippage,0,0,"Renko-Apfelsaft",MagicNumber,0,clrGreen);
To add comments, please log in or register