//+------------------------------------------------------------------+ //| DOM.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" //--- ulong MagicNumber=12; //--- int TakeProfitDist=20, StopLossDist=20; //--- static bool was_position=false; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- MarketBookAdd(_Symbol); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- MarketBookRelease(_Symbol); //--- } //+------------------------------------------------------------------+ //| BookEvent function | //+------------------------------------------------------------------+ void OnBookEvent(const string &symbol) { //--- OrdersMonitioring(); //--- MqlBookInfo DomArr[]; //--- MarketBookGet(_Symbol,DomArr); //--- for(int i=0; i50) { //--- if(!IsPosition() && OrderCount()==0) { //--- switch(DomArr[i].type) { //--- case BOOK_TYPE_BUY: { //--- double filledbuyPrice; //--- bool buyordersend_mkt=BuyOrderMarket(filledbuyPrice); //--- if(!buyordersend_mkt) { //--- Print("Error has occured!!"); } else { //--- double filled_buy_price=filledbuyPrice; //--- double tp_price_buy=filled_buy_price+TakeProfitDist*_Point; //--- bool tp_send_buy=SendTakeProfitLong(tp_price_buy); //--- if(!tp_send_buy) { //--- Print("Send Take Profit function failed Long!!!"); } else { //--- double sl_price_buy=filled_buy_price-StopLossDist*_Point; //--- if(!SendStopLossLong(sl_price_buy)) { //--- Print("Send Stop Loss 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...."); //--- was_position=true; } } } } //--- break; //--- case BOOK_TYPE_SELL: { //--- double filledsellPrice; //--- bool sellordersend_mkt=SellOrderMarket(filledsellPrice); //--- if(!sellordersend_mkt) { //--- Print("Error has occured!!"); } else { //--- double filled_sell_price=filledsellPrice; //--- double tp_price_sell=filled_sell_price-TakeProfitDist*_Point; //--- bool tp_send_sell=SendTakeProfitShort(tp_price_sell); //--- if(!tp_send_sell) { //--- Print("Send TakeProfit function failed Short!!!"); } else { //--- double sl_price_sell=filled_sell_price+StopLossDist*_Point; //--- if(!SendStopLossShort(sl_price_sell)) { //--- 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...."); //--- was_position=true; } } } } //--- break; } } } } //--- } //+------------------------------------------------------------------+ //| BuyOrderMarket | //+------------------------------------------------------------------+ bool BuyOrderMarket(double &filled_buy_price) { //--- MqlTradeRequest trade_request; //--- MqlTradeResult trade_result; //--- ZeroMemory(trade_request); //--- trade_request.action=TRADE_ACTION_DEAL; //--- trade_request.magic=MagicNumber; //--- 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_buy_price=trade_result.price; //--- return(true); } //--- } //+------------------------------------------------------------------+ //| SendTakeProfitLong | //+------------------------------------------------------------------+ bool SendTakeProfitLong(double tp_price_long) { //--- MqlTradeRequest trade_request_tp; //--- MqlTradeResult trade_result_tp; //--- ZeroMemory(trade_request_tp); //--- trade_request_tp.action=TRADE_ACTION_PENDING; //--- trade_request_tp.magic=MagicNumber; //--- trade_request_tp.symbol=_Symbol; //--- trade_request_tp.volume=1; //--- trade_request_tp.price=tp_price_long; //--- 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(false); } else { //--- Print("Take Profit Order was sucessfully filled... @",trade_result_tp.price); //--- return(true); } //--- } //+------------------------------------------------------------------+ //| SendStopLossLong | //+------------------------------------------------------------------+ bool SendStopLossLong(double sl_price_long) { //--- MqlTradeRequest trade_request_sl; //--- MqlTradeResult trade_result_sl; //--- ZeroMemory(trade_request_sl); //--- trade_request_sl.action=TRADE_ACTION_PENDING; //--- trade_request_sl.magic=MagicNumber; //--- trade_request_sl.symbol=_Symbol; //--- trade_request_sl.volume=1; //--- trade_request_sl.price=sl_price_long; //--- 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(false); } else { //--- Print("Stop Loss Order was sucessfully filled... @",trade_result_sl.price); //--- return(true); } //--- } //+------------------------------------------------------------------+ //| SellOrderMarket | //+------------------------------------------------------------------+ bool SellOrderMarket(double &filled_sell_price) { //--- MqlTradeRequest trade_request; //--- MqlTradeResult trade_result; //--- ZeroMemory(trade_request); //--- trade_request.action=TRADE_ACTION_DEAL; //--- trade_request.magic=MagicNumber; //--- trade_request.symbol=_Symbol; //--- trade_request.volume=1; //--- trade_request.price=SymbolInfoDouble(_Symbol,SYMBOL_BID); //--- trade_request.type=ORDER_TYPE_SELL; //--- 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_sell_price=trade_result.price; //--- return(true); } //--- } //+------------------------------------------------------------------+ //| SendTakeProfitShort | //+------------------------------------------------------------------+ bool SendTakeProfitShort(double tp_price_short) { //--- MqlTradeRequest trade_request_tp; //--- MqlTradeResult trade_result_tp; //--- ZeroMemory(trade_request_tp); //--- trade_request_tp.action=TRADE_ACTION_PENDING; //--- trade_request_tp.magic=MagicNumber; //--- trade_request_tp.symbol=_Symbol; //--- trade_request_tp.volume=1; //--- trade_request_tp.price=tp_price_short; //--- trade_request_tp.type=ORDER_TYPE_BUY_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(false); } else { //--- Print("Take Profit Order was sucessfully filled... @",trade_result_tp.price); //--- return(true); } //--- } //+------------------------------------------------------------------+ //| SendStopLossShort | //+------------------------------------------------------------------+ bool SendStopLossShort(double sl_price_short) { //--- MqlTradeRequest trade_request_sl; //--- MqlTradeResult trade_result_sl; //--- ZeroMemory(trade_request_sl); //--- trade_request_sl.action=TRADE_ACTION_PENDING; //--- trade_request_sl.magic=MagicNumber; //--- trade_request_sl.symbol=_Symbol; //--- trade_request_sl.volume=1; //--- trade_request_sl.price=sl_price_short; //--- trade_request_sl.type=ORDER_TYPE_BUY_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(false); } else { //--- Print("Stop LossOrder was sucessfully filled... @",trade_result_sl.price); //--- return(true); } //--- } //+------------------------------------------------------------------+ //| IsPosition | //+------------------------------------------------------------------+ bool IsPosition() { //--- bool result=false; //--- for(int i=0; i0) { //--- OrdersDeleteAll(); //--- was_position=false; } } } //--- } //+------------------------------------------------------------------+ //| OrdersDeleteAll | //+------------------------------------------------------------------+ void OrdersDeleteAll() { //--- for(int i=0 ; i