Forum on trading, automated trading systems and testing trading strategies
How to Start with Metatrader 5
Simon Gniadkowski, 2013.07.24 10:18
How to post code on this forum . . .
i am currently building a simple hedging algo for MT4 (in MQL4) the problem is i keep on getting error 4051?! why does this happen all the time and how can i fix this. can someone please help
First try to make these changes.
double ZoneRecovery = 0; double GoalLongs = 0; double GoalShorts = 0;
and put on OnInit() function these parameters
ZoneRecovery = Price - ZRval; GoalLongs = Price + GLval; GoalShorts = Price - GSval;
first, browse about error 4051 then you'll find that it's invalid function parameter value.
it means you accidentally put wrong parameter value for a function. (eg. a function need a positif integer for one of its parameter, but you use a negatif integer fo it)
then check all of your function parameter to find which one is invalid. (use Print/Alert/Comment to debug your parameter value)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
i am currently building a simple hedging algo for MT4 (in MQL4) the problem is i keep on getting error 4051?! why does this happen all the time and how can i fix this. can someone please help
//+------------------------------------------------------------------+
//| 4Lines.mq5 |
//| Copyright 2016, MetaQuotes Software Corp. |
//| https://www.pornhub.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
input double GLval = 0.0015;
double ZRval = GLval * 0.4;
double GSval = GLval + ZRval;
input double AmntPROFIT = 0.007;
double Price = Ask;
double ZoneRecovery = Price - ZRval;
double GoalLongs = Price + GLval;
double GoalShorts = Price - GSval;
int PosSizeShort = 0;
int PosSizeLong = 0;
int OpenLongs = 0;
int OpenShorts = 0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
PosSizeLong = -((OpenLongs * GLval - OpenShorts * GSval - AmntPROFIT)/GLval)+1;
OpenLongs = PosSizeLong;
GlobalVariableSet("OpenLongs", OpenLongs);
int TICKET = OrderSend(Symbol(), OP_SELL, (double)PosSizeLong, Ask, 5, 0, 0);
if(TICKET < 0)
{Alert("order failes error #", GetLastError());}
else{Alert("Order placed succesfully");}
Alert("+-------------------------------------------+");
Alert("Zone Recovery: " + ZoneRecovery);
Alert("Long Goal: " + GoalLongs);
Alert("Short Goal: " + GoalShorts);
return (INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
int Total = OrdersTotal();
OrderSelect(Total - 1, SELECT_BY_POS);
int Type = OrderType();
switch(Type)
{
case(OP_BUY):
{
PosSizeLong = -((OpenLongs * GLval - OpenShorts * GSval - AmntPROFIT)/GLval)+1;
int TIcket = OrderSend(Symbol(), OP_SELLSTOP, (double)PosSizeLong, Ask - 0.0004, 5, 0, 0);
if(TIcket < 0)
{Alert("order failes error #", GetLastError());}
else{Alert("Order placed succesfully");}
}
case(OP_SELL):
{
PosSizeShort = -((OpenShorts * GLval - OpenLongs * GSval - AmntPROFIT)/GLval)+1;
int Ticket = OrderSend(Symbol(), OP_BUYSTOP, (double)PosSizeLong, Ask + 0.0004, 5, 0, 0);
if(Ticket < 0)
{Alert("order failes error #", GetLastError());}
else{Alert("Order placed succesfully");}
}
default:
{
}
}
if(OrdersTotal() == 0)
{
Price = Ask;
ZoneRecovery = Ask - ZRval;
GoalLongs = Ask + GLval;
GoalShorts = Ask - GSval;
OpenLongs = 0;
OpenShorts = 0;
PosSizeLong = -((OpenLongs * GLval - OpenShorts * GSval - AmntPROFIT)/GLval)+1;
OpenLongs = PosSizeLong;
int ticket = OrderSend(Symbol(), OP_BUY, (double)PosSizeLong, Ask, 5, 0, 0);
if(ticket < 0)
{Alert("order failes error #", GetLastError());}
else{Alert("Order placed succesfully");}
Alert("+-------------------------------------------+");
Alert("Zone Recovery: " + ZoneRecovery);
Alert("Long Goal: " + GoalLongs);
Alert("Short Goal: " + GoalShorts);
}
else if(Bid >= GoalLongs + 0.0005 || Bid <= GoalShorts - 0.0005)
{
int total = OrdersTotal();
OrderSelect(total - 1, SELECT_BY_POS);
int type = OrderType();
bool result = false;
switch(type)
{
//Close opened long positions
case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
break;
//Close opened short positions
case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
break;
//Close pending orders
case OP_BUYLIMIT : result = OrderDelete( OrderTicket() );
case OP_BUYSTOP : result = OrderDelete( OrderTicket() );
case OP_SELLLIMIT : result = OrderDelete( OrderTicket() );
case OP_SELLSTOP : result = OrderDelete( OrderTicket() );
}
if(result == false)
{
Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
}
}
}
//+------------------------------------------------------------------+