240 lines
11 KiB
MQL5
240 lines
11 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| Test.mq5 |
|
|
//| Copyright 2020, MetaQuotes Software Corp. |
|
|
//| https://www.mql5.com |
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "Copyright 2020, MetaQuotes Software Corp."
|
|
#property link "https://www.mql5.com"
|
|
#property version "1.00"
|
|
//+------------------------------------------------------------------+
|
|
//| OnStart |
|
|
//+------------------------------------------------------------------+
|
|
void OnStart()
|
|
{
|
|
//---
|
|
double filledPrice;
|
|
|
|
//---
|
|
bool ordersend_mkt=SendOrderMarket(filledPrice);
|
|
|
|
//---
|
|
if(!ordersend_mkt)
|
|
{
|
|
//---
|
|
Print("Error has occured!!");
|
|
}
|
|
else
|
|
{
|
|
//---
|
|
double filled_price=filledPrice;
|
|
|
|
//---
|
|
double tp_price=filled_price+5*_Point;
|
|
|
|
//---
|
|
bool tp_sent=SendTakeProfit(tp_price);
|
|
|
|
//---
|
|
if(!tp_sent)
|
|
{
|
|
//---
|
|
Print("Send TakeProfit function failed!!!");
|
|
}
|
|
else
|
|
{
|
|
//---
|
|
double sl_price=filled_price-5*_Point;
|
|
|
|
//---
|
|
if(!SendStopLoss(sl_price))
|
|
{
|
|
//---
|
|
Print("Send StopLoss function failed!!!");
|
|
}
|
|
else
|
|
{
|
|
//---
|
|
Print("Send StopLoss function sucessfully executed");
|
|
|
|
//---
|
|
Print("All orders have been sucessfully executed.... we are now waiting for sl or tp trigger....");
|
|
}
|
|
}
|
|
}
|
|
//---
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| SendOrderMarket |
|
|
//+------------------------------------------------------------------+
|
|
bool SendOrderMarket(double &filled_price)
|
|
{
|
|
//---
|
|
MqlTradeRequest trade_request;
|
|
|
|
//---
|
|
MqlTradeResult trade_result;
|
|
|
|
//---
|
|
trade_request.action=TRADE_ACTION_DEAL;
|
|
|
|
//---
|
|
trade_request.magic=123;
|
|
|
|
//---
|
|
trade_request.symbol=_Symbol;
|
|
|
|
//---
|
|
trade_request.volume=1;
|
|
|
|
//---
|
|
trade_request.price=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
|
|
|
|
//---
|
|
trade_request.type=ORDER_TYPE_BUY;
|
|
|
|
//---
|
|
trade_request.type_filling=ORDER_FILLING_RETURN;
|
|
|
|
//---
|
|
trade_request.comment="My super EA";
|
|
|
|
//---
|
|
bool ordersend_result=OrderSend(trade_request,trade_result);
|
|
|
|
//---
|
|
if(!ordersend_result)
|
|
{
|
|
//---
|
|
Print("Order send Market failed, error code=",trade_result.retcode);
|
|
|
|
//---
|
|
return(false);
|
|
}
|
|
else
|
|
{
|
|
//---
|
|
Print("Order send Market sucessfully done @",trade_result.price);
|
|
|
|
//---
|
|
filled_price=trade_result.price;
|
|
|
|
//---
|
|
return(true);
|
|
}
|
|
//---
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| SendTakeProfit |
|
|
//+------------------------------------------------------------------+
|
|
bool SendTakeProfit(double tp_price)
|
|
{
|
|
//---
|
|
MqlTradeRequest trade_request_tp;
|
|
|
|
//---
|
|
MqlTradeResult trade_result_tp;
|
|
|
|
//---
|
|
trade_request_tp.action=TRADE_ACTION_PENDING;
|
|
|
|
//---
|
|
trade_request_tp.magic=123;
|
|
|
|
//---
|
|
trade_request_tp.symbol=_Symbol;
|
|
|
|
//---
|
|
trade_request_tp.volume=1;
|
|
|
|
//---
|
|
trade_request_tp.price=tp_price;
|
|
|
|
//---
|
|
trade_request_tp.type=ORDER_TYPE_SELL_LIMIT;
|
|
|
|
//---
|
|
trade_request_tp.type_filling=ORDER_FILLING_RETURN;
|
|
|
|
//---
|
|
trade_request_tp.type_time=ORDER_TIME_DAY;
|
|
|
|
//---
|
|
bool ordersend_result_tp=OrderSend(trade_request_tp,trade_result_tp);
|
|
|
|
//---
|
|
if(!ordersend_result_tp)
|
|
{
|
|
//---
|
|
Print("Error has occured!! Error code=",trade_result_tp.retcode);
|
|
|
|
//---
|
|
return(true);
|
|
}
|
|
else
|
|
{
|
|
//---
|
|
Print("Order was sucessfully filled... @",trade_result_tp.price);
|
|
|
|
//---
|
|
return(false);
|
|
}
|
|
//---
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| SendStopLoss |
|
|
//+------------------------------------------------------------------+
|
|
bool SendStopLoss(double sl_price)
|
|
{
|
|
//---
|
|
MqlTradeRequest trade_request_sl;
|
|
|
|
//---
|
|
MqlTradeResult trade_result_sl;
|
|
|
|
//---
|
|
trade_request_sl.action=TRADE_ACTION_PENDING;
|
|
|
|
//---
|
|
trade_request_sl.magic=123;
|
|
|
|
//---
|
|
trade_request_sl.symbol=_Symbol;
|
|
|
|
//---
|
|
trade_request_sl.volume=1;
|
|
|
|
//---
|
|
trade_request_sl.price=sl_price;
|
|
|
|
//---
|
|
trade_request_sl.type=ORDER_TYPE_SELL_STOP;
|
|
|
|
//---
|
|
trade_request_sl.type_filling=ORDER_FILLING_RETURN;
|
|
|
|
//---
|
|
trade_request_sl.type_time=ORDER_TIME_DAY;
|
|
|
|
//---
|
|
bool ordersend_result_sl=OrderSend(trade_request_sl,trade_result_sl);
|
|
|
|
//---
|
|
if(!ordersend_result_sl)
|
|
{
|
|
//---
|
|
Print("Error has occured!! Error code=",trade_result_sl.retcode);
|
|
|
|
//---
|
|
return(true);
|
|
}
|
|
else
|
|
{
|
|
//---
|
|
Print("Order was sucessfully filled... @",trade_result_sl.price);
|
|
|
|
//---
|
|
return(false);
|
|
}
|
|
//---
|
|
}
|
|
//+------------------------------------------------------------------+
|