//+----------------------------------------------------------------------------------------------------------------------+
//| The function opens or sets an order |
//| symbol - simbol dimana order dilakukan |
//| cmd - order |
//| volume - jumlah lots |
//| price - buka harga |
//| slippage - |
//| stoploss - |
//| takeprofit - |
//| comment - |
//| magic - |
//| expiration - |
//| arrow_color - |
//+----------------------------------------------------------------------------------------------------------------------+
int OrderOpenF(string OO_symbol,
int OO_cmd,
double OO_volume,
double OO_price,
int OO_slippage,
double OO_stoploss,
double OO_takeprofit,
string OO_comment,
int OO_magic,
datetime OO_expiration,
color OO_arrow_color)
{
int result = -1; //result of opening an order
int Error = 0; //error when opening an order
int attempt = 0; //amount of performed attempts
int attemptMax = 3; //maximum amount of attempts
bool exit_loop = false; //exit the loop
string lang=TerminalInfoString(TERMINAL_LANGUAGE); //trading terminal language, for defining the language of the messages
double stopllvl=NormalizeDouble(MarketInfo(OO_symbol,MODE_STOPLEVEL)*MarketInfo(OO_symbol,MODE_POINT),Digits); //minimum stop loss/ take profit level, in points
//the module provides safe order opening.
//--- check stop orders for buying
if(OO_cmd==OP_BUY || OO_cmd==OP_BUYLIMIT || OO_cmd==OP_BUYSTOP)
{
double tp = (OO_takeprofit - OO_price)/MarketInfo(OO_symbol, MODE_POINT);
double sl = (OO_price - OO_stoploss)/MarketInfo(OO_symbol, MODE_POINT);
if(tp>0 && tp<=stopllvl)
{
OO_takeprofit=OO_price+stopllvl+2*MarketInfo(OO_symbol,MODE_POINT);
}
if(sl>0 && sl<=stopllvl)
{
OO_stoploss=OO_price -(stopllvl+2*MarketInfo(OO_symbol,MODE_POINT));
}
}
//--- check stop orders for selling
if(OO_cmd==OP_SELL || OO_cmd==OP_SELLLIMIT || OO_cmd==OP_SELLSTOP)
{
double tp = (OO_price - OO_takeprofit)/MarketInfo(OO_symbol, MODE_POINT);
double sl = (OO_stoploss - OO_price)/MarketInfo(OO_symbol, MODE_POINT);
if(tp>0 && tp<=stopllvl)
{
OO_takeprofit=OO_price -(stopllvl+2*MarketInfo(OO_symbol,MODE_POINT));
}
if(sl>0 && sl<=stopllvl)
{
OO_stoploss=OO_price+stopllvl+2*MarketInfo(OO_symbol,MODE_POINT);
}
}
//--- while loop
while(!exit_loop)
{
result=OrderSend(OO_symbol,OO_cmd,OO_volume,OO_price,OO_slippage,OO_stoploss,OO_takeprofit,OO_comment,OO_magic,OO_expiration,OO_arrow_color); //attempt to open an order using the specified parameters
//--- if there is an error when opening an order
if(result<0)
{
Error = GetLastError(); //assign a code to an error
switch(Error) //error enumeration
{ //order closing error enumeration and an attempt to fix them
case 2:
if(attempt<attemptMax)
{
attempt=attempt+1; //define one more attempt
Sleep(3000); //3 seconds of delay
RefreshRates();
break; //exit switch
}
if(attempt==attemptMax)
{
attempt=0; //reset the amount of attempts to zero
exit_loop = true; //exit while
break; //exit switch
}
case 3:
RefreshRates();
exit_loop = true; //exit while
break; //exit switch
case 4:
if(attempt<attemptMax)
{
attempt=attempt+1; //define one more attempt
Sleep(3000); //3 seconds of delay
RefreshRates();
break; //exit switch
}
if(attempt==attemptMax)
{
attempt = 0; //reset the amount of attempts to zero
exit_loop = true; //exit while
break; //exit switch
}
case 5:
exit_loop = true; //exit while
break; //exit switch
case 6:
if(attempt<attemptMax)
{
attempt=attempt+1; //define one more attempt
Sleep(5000); //3 seconds of delay
break; //exit switch
}
if(attempt==attemptMax)
{
attempt = 0; //reset the amount of attempts to zero
exit_loop = true; //exit while
break; //exit switch
}
case 8:
if(attempt<attemptMax)
{
attempt=attempt+1; //define one more attempt
Sleep(7000); //3 seconds of delay
break; //exit switch
}
if(attempt==attemptMax)
{
attempt = 0; //reset the amount of attempts to zero
exit_loop = true; //exit while
break; //exit switch
}
case 64:
exit_loop = true; //exit while
break; //exit switch
case 65:
exit_loop = true; //exit while
break; //exit switch
case 128:
Sleep(3000);
RefreshRates();
continue; //exit switch
case 129:
if(attempt<attemptMax)
{
attempt=attempt+1; //define one more attempt
Sleep(3000); //3 seconds of delay
RefreshRates();
break; //exit switch
}
if(attempt==attemptMax)
{
attempt = 0; //reset the amount of attempts to zero
exit_loop = true; //exit while
break; //exit switch
}
case 130:
exit_loop=true; //exit while
break;
case 131:
exit_loop = true; //exit while
break; //exit switch
case 132:
Sleep(10000); //sleep for 10 seconds
RefreshRates(); //update data
//exit_loop = true; //exit while
break; //exit switch
case 133:
exit_loop=true; //exit while
break; //exit switch
case 134:
exit_loop=true; //exit while
break; //exit switch
case 135:
if(attempt<attemptMax)
{
attempt=attempt+1; //define one more attempt
RefreshRates();
break; //exit switch
}
if(attempt==attemptMax)
{
attempt = 0; //reset the amount of attempts to zero
exit_loop = true; //exit while
break; //exit switch
}
case 136:
if(attempt<attemptMax)
{
attempt=attempt+1; //define one more attempt
RefreshRates();
break; //exit switch
}
if(attempt==attemptMax)
{
attempt = 0; //reset the amount of attempts to zero
exit_loop = true; //exit while
break; //exit switch
}
case 137:
if(attempt<attemptMax)
{
attempt=attempt+1;
Sleep(2000);
RefreshRates();
break;
}
if(attempt==attemptMax)
{
attempt=0;
exit_loop=true;
break;
}
case 138:
if(attempt<attemptMax)
{
attempt=attempt+1;
Sleep(1000);
RefreshRates();
break;
}
if(attempt==attemptMax)
{
attempt=0;
exit_loop=true;
break;
}
case 139:
exit_loop=true;
break;
case 141:
Sleep(5000);
exit_loop=true;
break;
case 145:
exit_loop=true;
break;
case 146:
if(attempt<attemptMax)
{
attempt=attempt+1;
Sleep(2000);
RefreshRates();
break;
}
if(attempt==attemptMax)
{
attempt=0;
exit_loop=true;
break;
}
case 147:
if(attempt<attemptMax)
{
attempt=attempt+1;
OO_expiration=0;
break;
}
if(attempt==attemptMax)
{
attempt=0;
exit_loop=true;
break;
}
case 148:
exit_loop=true;
break;
default:
Print("Error: ",Error);
exit_loop=true; //exit while
break; //other options
}
}
//--- if no errors detected
else
{
if(lang == "Indonesian") {Print("Order berhasil dilakukan. ", result);}
if(lang == "English") {Print("The order is successfully opened.", result);}
Error = 0; //reset the error code to zero
break; //exit while
//errorCount =0; //reset the amount of attempts to zero
}
}
return(result);