![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Returns number of the ticket assigned to the order by the trade server or -1 if it fails. To get additional error information, one has to call the GetLastError() function.
Notes:
At opening of a market order (OP_SELL or OP_BUY), only the latest prices of Bid (for selling) or Ask (for buying) can be used as open price. If operation is performed with a security different from the current one, the MarketInfo() function must be used with MODE_BID or MODE_ASK parameter for the latest quotes for this security to be obtained. Calculated or unnormalized price cannot be applied. If there has not been the requested open price in the price thread or it has not been normalized according to the amount of digits after decimal point, the error 129 (ERR_INVALID_PRICE) will be generated. If the requested open price is fully out of date, the error 138 (ERR_REQUOTE) will be generated independently on the slippage parameter. If the requested price is out of date, but present in the thread, the position will be opened at the current price and only if the current price lies within the range ofprice+-slippage.
StopLoss and TakeProfit levels cannot be too close to the market. The minimal distance of stop levels in points can be obtained using the MarketInfo() function with MODE_STOPLEVEL parameter. In the case of erroneous or unnormalized stop levels, the error 130 (ERR_INVALID_STOPS) will be generated.
At placing of a pending order, the open price cannot be too close to the market. The minimal distance of the pending price from the current market one in points can be obtained using the MarketInfo() function with the MODE_STOPLEVEL parameter. In case of false open price of a pending order, the error 130 (ERR_INVALID_STOPS) will be generated.
Applying of pending order expiration time can be disabled in some trade servers. In this case, when a non-zero value is specified in theexpiration parameter, the error 147 (ERR_TRADE_EXPIRATION_DENIED) will be generated.
On some trade servers, the total amount of open and pending orders can be limited. If this limit has been exceeded, no new position will be opened (or no pending order will be placed) and trade server will return error 148 (ERR_TRADE_TOO_MANY_ORDERS).
//--- input parameters
extern int period=16;
extern int porog=50;
extern double lot=1;
extern inttern k=5000;
extern bool pl=true;
//+------------------------------------------------------------------+
//| expert initialisation function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialisation function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
double bs=NormalizeDouble(iCustom(Symbol(),0, "Aver",period,0,0),Digits);
double ss=NormalizeDouble(iCustom(Symbol(),0, "Aver",period,1,0),Digits);
double tpb=NormalizeDouble(bs+(bs-ss),Digits);
double tps=NormalizeDouble(ss-(bs-ss),Digits);
if(pl==true)double lots=MathMin(AccountFreeMargin()/k,5);
if(pl==false)lots=lot;
if(OrdersTotal()==0)
{
OrderSend(Symbol(),OP_BUYSTOP,lots,bs,3,ss,tpb,"",1394,0,Red);//Open Buy Limit.
OrderSend(Symbol(),OP_SELLSTOP,lots,ss,3,bs,tps,"",1394,0,Blue);//Open Sell Limit.
}
if(OrdersTotal()==2)
{
OrderSelect(0,SELECT_BY_POS,MODE_TRADES);//select the nearest order.
if(OrderType()==OP_BUYSTOP &&bs<OrderOpenPrice())OrderModify(OrderTicket(),bs,ss,tpb,0,Red);
if(OrderType()==OP_SELLSTOP&&&ss>OrderOpenPrice())OrderModify(OrderTicket(),ss,bs,tps,0,Blue);
if(OrderType()==OP_BUY &&ss>OrderStopLoss())OrderModify(OrderTicket(),OrderOpenPrice(),ss,OrderTakeProfit(),0,Red);
if(OrderType()==OP_SELL&&bs<OrderStopLoss())OrderModify(OrderTicket(),OrderOpenPrice(),bs,OrderTakeProfit(),0,Red);
OrderSelect(1,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_BUYSTOP &&bs<OrderOpenPrice())OrderModify(OrderTicket(),bs,ss,tpb,0,Red);
if(OrderType()==OP_SELLSTOP&&&ss>OrderOpenPrice())OrderModify(OrderTicket(),ss,bs,tps,0,Blue);
if(OrderType()==OP_BUY &&ss>OrderStopLoss())OrderModify(OrderTicket(),OrderOpenPrice(),ss,OrderTakeProfit(),0,Red);
if(OrderType()==OP_SELL&&bs<OrderStopLoss())OrderModify(OrderTicket(),OrderOpenPrice(),bs,OrderTakeProfit(),0,Red);
}
if(OrdersTotal()==1)
{
OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_BUY) {OrderModify(OrderTicket(),OrderOpenPrice(),ss,OrderTakeProfit(),0,Red);
OrderSend(Symbol(),OP_SELLSTOP,lots,ss,3,bs,tps,"",1394,0,Blue);}
if(OrderType()==OP_SELL) {OrderModify(OrderTicket(),OrderOpenPrice(),bs,OrderTakeProfit(),0,Red);
OrderSend(Symbol(),OP_BUYSTOP,lots,bs,3,ss,tpb,",1394,0,Red);}
if(OrderType()==OP_BUYSTOP) {OrderModify(OrderTicket(),bs,ss,tpb,0,Red);
OrderSend(Symbol(),OP_SELLSTOP,lots,ss,3,bs,tps,"",1394,0,Blue);}
if(OrderType()==OP_SELLSTOP){OrderModify(OrderTicket(),ss,bs,tps,0,Blue);
OrderSend(Symbol(),OP_BUYSTOP,lots,bs,3,ss,tpb,",1394,0,Red);}
}
return(0);
}
//+------------------------------------------------------------------+