Hi I wrote an EA to test part of a strategy but while backtesting it I get a Ordersend error 131. I have changed my lotsize to 0.1 and then it works fine but when I change to a lotsize of 0.01 it gives me the error. My brokrer allows 0.01 lotsize trading. If I trade manualy there is no problem with trading 0.01 lot sizes.
I wil post the code here. If anyone could please help me fine the problem I would be very greatfull.
//+------------------------------------------------------------------+
//| 15MBreakOut.mq4 |
//| PN |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "PN"
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| input parameters |
//+------------------------------------------------------------------+
extern double LotSize = 0.01;
extern int UurBegin = 1;
extern int Uur = 10;
extern int UurSluit = 23;
int MinuutBegin = 0;
int SekondBegin = 0;
int Minuut = 0;
int Sekond = 0;
int UurTans;
int MinuutTans;
int SekondTans;
double Tradespan = 0;
double HighBreak = 0;
double LowBreak = 0;
double TP = 0;
double SL = 0;
double Call = 0;
double Put = 0;
string TradeToday = "No";
string SellToday = "No";
string BuyToday = "No";
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick(){
UurTans = Hour();
MinuutTans = Minute();
SekondTans = Seconds();
if(UurTans == Uur && MinuutTans == 0 && SekondTans == 0){
TradeToday = "No";
SellToday = "No";
BuyToday = "No";
LowBreak = 0;
Tradespan = 0;
SL = 0;
TP = 0;
FindSpan();
}
if(OrdersTotal() == 0){
TradeToday = "No";
}
for( int i = 0 ; i < OrdersTotal() ; i++ ) {
OrderSelect( i, SELECT_BY_POS, MODE_TRADES );
if( OrderSymbol() == Symbol() ){
TradeToday = "Yes";
}
}
if(UurTans >= Uur && UurTans < UurSluit){
if(TradeToday == "No"){
Tradetoday();
}
}
if(UurTans == UurSluit && MinuutTans == 0 && SekondTans == 0){
CloseAll();
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Expert Find Span function |
//+------------------------------------------------------------------+
void FindSpan(){
int BarCount = (((Uur*60) - (UurBegin*60))/15);
HighBreak = High[iHighest(NULL,0,MODE_HIGH,BarCount,0)];
LowBreak = Low[iLowest(NULL,0,MODE_LOW,BarCount,0)];
Tradespan = HighBreak - LowBreak;
SL = Tradespan;
TP = (Tradespan/2);
return;
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Trading placement |
//+------------------------------------------------------------------+
void Tradetoday(){
Call = Ask;
Put = Bid;
//Print("Put >= HighBreak = ",Put >= HighBreak);
//Print("Put <= HighBreak+0.00003 = ",Put <= HighBreak+0.00003);
//Print("Uur >= UurBegin && Minuut >= 0 && Sekond >= 0 = ",Uur >= UurBegin && Minuut >= 0 && Sekond >= 0);
if(Put >= HighBreak && Put <= HighBreak+0.00003 && Uur >= UurBegin && Minuut >= 0 && Sekond >= 0 && BuyToday == "No"){
OrderSend(Symbol(),OP_BUY,NormalizeDouble(LotSize,2),Call,3,HighBreak-SL,HighBreak+TP,IntegerToString(Day())+";"+Tradespan,0,0,clrBlack);
OrderSend(Symbol(),OP_BUY,NormalizeDouble(LotSize,2),Call,3,HighBreak-SL,HighBreak+(TP*2),IntegerToString(Day())+";"+Tradespan,0,0,clrBlack);
OrderSend(Symbol(),OP_BUY,NormalizeDouble(LotSize,2),Call,3,HighBreak-SL,HighBreak+(TP*3),IntegerToString(Day())+";"+Tradespan,0,0,clrBlack);
OrderSend(Symbol(),OP_BUY,NormalizeDouble(LotSize,2),Call,3,HighBreak-SL,HighBreak+(TP*4),IntegerToString(Day())+";"+Tradespan,0,0,clrBlack);
TradeToday = "Yes";
BuyToday = "Yes";
}
if(Put <= LowBreak && Put >= LowBreak-0.00003 && Uur >= UurBegin && Minuut >= 0 && Sekond >= 0 && SellToday == "No"){
OrderSend(Symbol(),OP_SELL,NormalizeDouble(LotSize,2),Put,3,LowBreak+SL,LowBreak-TP,IntegerToString(Day())+";"+Tradespan,0,0,clrBlack);
OrderSend(Symbol(),OP_SELL,NormalizeDouble(LotSize,2),Put,3,LowBreak+SL,LowBreak-(TP*2),IntegerToString(Day())+";"+Tradespan,0,0,clrBlack);
OrderSend(Symbol(),OP_SELL,NormalizeDouble(LotSize,2),Put,3,LowBreak+SL,LowBreak-(TP*3),IntegerToString(Day())+";"+Tradespan,0,0,clrBlack);
OrderSend(Symbol(),OP_SELL,NormalizeDouble(LotSize,2),Put,3,LowBreak+SL,LowBreak-(TP*4),IntegerToString(Day())+";"+Tradespan,0,0,clrBlack);
Print("NormalizeDouble(LotSize,2) = ",NormalizeDouble(LotSize,2));
TradeToday = "Yes";
SellToday = "Yes";
}
return;
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Close All Trades |
//+------------------------------------------------------------------+
void CloseAll(){
int numOfOrders = OrdersTotal();
int FirstOrderType = 0;
TradeToday = "No";
for (int index = 0; index < OrdersTotal(); index++)
{
OrderSelect(index, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol())
{
FirstOrderType = OrderType();
break;
}
}
for(int index = numOfOrders - 1; index >= 0; index--)
{
OrderSelect(index, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol())
switch (OrderType())
{
case OP_BUY:
if (!OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 3, Red))
break;
case OP_SELL:
if (!OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 3, Red))
break;
case OP_BUYLIMIT:
case OP_SELLLIMIT:
case OP_BUYSTOP:
case OP_SELLSTOP:
if (!OrderDelete(OrderTicket()))
break;
}
}
return;
}
I wil post the code here. If anyone could please help me fine the problem I would be very greatfull.
//+------------------------------------------------------------------+
//| 15MBreakOut.mq4 |
//| PN |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "PN"
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| input parameters |
//+------------------------------------------------------------------+
extern double LotSize = 0.01;
extern int UurBegin = 1;
extern int Uur = 10;
extern int UurSluit = 23;
int MinuutBegin = 0;
int SekondBegin = 0;
int Minuut = 0;
int Sekond = 0;
int UurTans;
int MinuutTans;
int SekondTans;
double Tradespan = 0;
double HighBreak = 0;
double LowBreak = 0;
double TP = 0;
double SL = 0;
double Call = 0;
double Put = 0;
string TradeToday = "No";
string SellToday = "No";
string BuyToday = "No";
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick(){
UurTans = Hour();
MinuutTans = Minute();
SekondTans = Seconds();
if(UurTans == Uur && MinuutTans == 0 && SekondTans == 0){
TradeToday = "No";
SellToday = "No";
BuyToday = "No";
LowBreak = 0;
Tradespan = 0;
SL = 0;
TP = 0;
FindSpan();
}
if(OrdersTotal() == 0){
TradeToday = "No";
}
for( int i = 0 ; i < OrdersTotal() ; i++ ) {
OrderSelect( i, SELECT_BY_POS, MODE_TRADES );
if( OrderSymbol() == Symbol() ){
TradeToday = "Yes";
}
}
if(UurTans >= Uur && UurTans < UurSluit){
if(TradeToday == "No"){
Tradetoday();
}
}
if(UurTans == UurSluit && MinuutTans == 0 && SekondTans == 0){
CloseAll();
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Expert Find Span function |
//+------------------------------------------------------------------+
void FindSpan(){
int BarCount = (((Uur*60) - (UurBegin*60))/15);
HighBreak = High[iHighest(NULL,0,MODE_HIGH,BarCount,0)];
LowBreak = Low[iLowest(NULL,0,MODE_LOW,BarCount,0)];
Tradespan = HighBreak - LowBreak;
SL = Tradespan;
TP = (Tradespan/2);
return;
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Trading placement |
//+------------------------------------------------------------------+
void Tradetoday(){
Call = Ask;
Put = Bid;
//Print("Put >= HighBreak = ",Put >= HighBreak);
//Print("Put <= HighBreak+0.00003 = ",Put <= HighBreak+0.00003);
//Print("Uur >= UurBegin && Minuut >= 0 && Sekond >= 0 = ",Uur >= UurBegin && Minuut >= 0 && Sekond >= 0);
if(Put >= HighBreak && Put <= HighBreak+0.00003 && Uur >= UurBegin && Minuut >= 0 && Sekond >= 0 && BuyToday == "No"){
OrderSend(Symbol(),OP_BUY,NormalizeDouble(LotSize,2),Call,3,HighBreak-SL,HighBreak+TP,IntegerToString(Day())+";"+Tradespan,0,0,clrBlack);
OrderSend(Symbol(),OP_BUY,NormalizeDouble(LotSize,2),Call,3,HighBreak-SL,HighBreak+(TP*2),IntegerToString(Day())+";"+Tradespan,0,0,clrBlack);
OrderSend(Symbol(),OP_BUY,NormalizeDouble(LotSize,2),Call,3,HighBreak-SL,HighBreak+(TP*3),IntegerToString(Day())+";"+Tradespan,0,0,clrBlack);
OrderSend(Symbol(),OP_BUY,NormalizeDouble(LotSize,2),Call,3,HighBreak-SL,HighBreak+(TP*4),IntegerToString(Day())+";"+Tradespan,0,0,clrBlack);
TradeToday = "Yes";
BuyToday = "Yes";
}
if(Put <= LowBreak && Put >= LowBreak-0.00003 && Uur >= UurBegin && Minuut >= 0 && Sekond >= 0 && SellToday == "No"){
OrderSend(Symbol(),OP_SELL,NormalizeDouble(LotSize,2),Put,3,LowBreak+SL,LowBreak-TP,IntegerToString(Day())+";"+Tradespan,0,0,clrBlack);
OrderSend(Symbol(),OP_SELL,NormalizeDouble(LotSize,2),Put,3,LowBreak+SL,LowBreak-(TP*2),IntegerToString(Day())+";"+Tradespan,0,0,clrBlack);
OrderSend(Symbol(),OP_SELL,NormalizeDouble(LotSize,2),Put,3,LowBreak+SL,LowBreak-(TP*3),IntegerToString(Day())+";"+Tradespan,0,0,clrBlack);
OrderSend(Symbol(),OP_SELL,NormalizeDouble(LotSize,2),Put,3,LowBreak+SL,LowBreak-(TP*4),IntegerToString(Day())+";"+Tradespan,0,0,clrBlack);
Print("NormalizeDouble(LotSize,2) = ",NormalizeDouble(LotSize,2));
TradeToday = "Yes";
SellToday = "Yes";
}
return;
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Close All Trades |
//+------------------------------------------------------------------+
void CloseAll(){
int numOfOrders = OrdersTotal();
int FirstOrderType = 0;
TradeToday = "No";
for (int index = 0; index < OrdersTotal(); index++)
{
OrderSelect(index, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol())
{
FirstOrderType = OrderType();
break;
}
}
for(int index = numOfOrders - 1; index >= 0; index--)
{
OrderSelect(index, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol())
switch (OrderType())
{
case OP_BUY:
if (!OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 3, Red))
break;
case OP_SELL:
if (!OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 3, Red))
break;
case OP_BUYLIMIT:
case OP_SELLLIMIT:
case OP_BUYSTOP:
case OP_SELLSTOP:
if (!OrderDelete(OrderTicket()))
break;
}
}
return;
}