Firstly, It is good idea to provide code right here
//+------------------------------------------------------------------+ //| Win.mq5 | //| Copyright 2011, xywen | //| https://www.mql5.com/ | //+------------------------------------------------------------------+ #property copyright "Copyright 2011, xywen" #property link "https://www.mql5.com/" #property version "1.00" //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ #define ArrayCount 50 int begin=6; int end=17; int ExtHandle; int ExtHandleQS; int ExtHandleSlow; bool bCloseOrder=false; int nSkip=0; int nStatus=0; int m_magic=970121; double dOpenPrice; int nKeepTick=0; //double ExtBufferClose[CloseCount]; double ExtBufferFast[ArrayCount]; double ExtBufferSlow[ArrayCount]; double ExtBufferQS[ArrayCount]; double ExtBufferQS1[ArrayCount]; double ds1=0.0050; double ds2=0.0090; double dRisk; double g_sl=0.0070; MqlRates rt[ArrayCount]; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int OnInit() { IndicatorsReady(true); return(0); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { if(!IndicatorsReady(false)) return; //--- go trading only for first ticks of new bar int calculated; double dLots; calculated=BarsCalculated(ExtHandle); if(CopyRates(_Symbol,_Period,0,ArrayCount,rt)!=ArrayCount) { Print("CopyRates of ",_Symbol," failed, no history"); return; } if(CopyBuffer(ExtHandle,0,0,ArrayCount,ExtBufferFast)!=ArrayCount) { Print("CopyBuffer from qs0 failed, no data"); return; } if(CopyBuffer(ExtHandleSlow,0,0,ArrayCount,ExtBufferSlow)!=ArrayCount) { Print("CopyBuffer from qs0 failed, no data"); return; } if(CopyBuffer(ExtHandleQS,0,0,ArrayCount,ExtBufferQS)!=ArrayCount) { Print("CopyBuffer from qs2 failed, no data"); return; } if(CopyBuffer(ExtHandleQS,1,0,ArrayCount,ExtBufferQS1)!=ArrayCount) { Print("CopyBuffer from qs2 failed, no data"); return; } double dBanlance=AccountInfoDouble(ACCOUNT_BALANCE); if(dBanlance<8000) dRisk=0.2; else if(dBanlance<10000) dRisk=0.3; else if(dBanlance<20000) dRisk=0.4; else dRisk=0.5; int k=ArrayCount-2; datetime t1=TimeCurrent(); MqlDateTime st1; TimeToStruct(t1,st1); MqlTick MyTick; SymbolInfoTick(_Symbol,MyTick); double dBid=MyTick.bid; double dAsk=MyTick.ask; double dsl=0; double dtp=0; double dOrderCount=GetOrderCount(); if(dOrderCount!=0) { if(st1.day_of_week==5 && st1.hour>20) { CloseAll(); return; } if(dOrderCount>0) { if(rt[k+1].close>dOpenPrice+ds2) { dsl=rt[k+1].close-0.0030; PositionModify(dsl,dtp); } else if(rt[k+1].close>dOpenPrice+ds1) { dsl=rt[k+1].close-0.0020; PositionModify(dsl,dtp); } } else if(dOrderCount<0) { if(rt[k+1].close<dOpenPrice-ds2) { dsl=rt[k+1].close+0.0030; PositionModify(dsl,dtp); } else if(rt[k+1].close<dOpenPrice-ds1) { dsl=rt[k+1].close+0.0020; PositionModify(dsl,dtp); } } if(rt[ArrayCount-1].tick_volume>1) return; if(dOrderCount>0) { if(ExtBufferFast[k]<ExtBufferFast[k-1] && ExtBufferQS[k]<ExtBufferQS[k-1] && rt[k].close<rt[k-1].close) { CloseAll(); dLots=GetLots(dRisk); dsl=dBid+g_sl;//dOpenPrice+dOpenTickSize; dtp=0; if(ExtBufferQS1[k]>dBid && (dBanlance>8000 || ExtBufferSlow[k+1]<ExtBufferSlow[k])) OpenOrder(false,dLots,dBid,dsl,dtp); dOpenPrice=dBid; nSkip=-1; nStatus=-1; } } else if(dOrderCount<0) { if(ExtBufferFast[k]>ExtBufferFast[k-1] && ExtBufferQS[k]>ExtBufferQS[k-1] && rt[k].close>rt[k-1].close) { CloseAll(); dLots=GetLots(dRisk); dsl=dAsk-g_sl;//dOpenPrice+dOpenTickSize; dtp=0; if(ExtBufferQS1[k]<dAsk && (dBanlance>8000 || ExtBufferSlow[k+1]>ExtBufferSlow[k])) OpenOrder(true,dLots,dAsk,dsl,dtp); dOpenPrice=dAsk; nSkip=1; nStatus=1; } } } else //dOrderCount==0 { if(rt[ArrayCount-1].tick_volume>1) return; if(ExtBufferFast[k]<ExtBufferFast[k-1] && ExtBufferQS[k]<ExtBufferQS[k-1] && rt[k].close<rt[k-1].close) { if(nStatus==0) { nStatus=-1; return; } nStatus=-1; if(nSkip==-1) return; if(st1.day_of_week==1 && st1.hour<4) { nSkip=nStatus; return; } if(st1.day_of_week==5 && st1.hour>18) { nSkip=nStatus; return; } dLots=GetLots(dRisk); dsl=dBid+g_sl;//dOpenPrice+dOpenTickSize; dtp=0; if(ExtBufferQS1[k]>dBid && (dBanlance>8000 || ExtBufferSlow[k+1]<ExtBufferSlow[k])) OpenOrder(false,dLots,dBid,dsl,dtp); dOpenPrice=dBid; nSkip=-1; nStatus=-1; } else if(ExtBufferFast[k]>ExtBufferFast[k-1] && ExtBufferQS[k]>ExtBufferQS[k-1] && rt[k].close>rt[k-1].close) { if(nStatus==0) { nStatus=1; return; } nStatus=1; if(nSkip==1) return; if(st1.day_of_week==1 && st1.hour<4) { nSkip=nStatus; return; } if(st1.day_of_week==5 && st1.hour>18) { nSkip=nStatus; return; } dLots=GetLots(dRisk); dsl=dAsk-g_sl;//dOpenPrice+dOpenTickSize; dtp=0; if(ExtBufferQS1[k]<dAsk && (dBanlance>8000 || ExtBufferSlow[k+1]>ExtBufferSlow[k])) OpenOrder(true,dLots,dAsk,dsl,dtp); dOpenPrice=dAsk; nSkip=1; nStatus=1; } } } //+------------------------------------------------------------------+ bool CloseAll() { MqlTradeRequest request; MqlTradeCheckResult result; MqlTradeResult result1; int retry_count =10; uint retcode =TRADE_RETCODE_REJECT; bool bBuy; double d=GetOrderCount(); if(d>0)bBuy=true; else if(d<0) bBuy=false; else return(false); //--- check stopped for(;;) { //--- checking if(PositionSelect(_Symbol)) { if((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) { //--- prepare request for close BUY position if(bBuy==false) return false; request.type =ORDER_TYPE_SELL; request.price=SymbolInfoDouble(_Symbol,SYMBOL_BID); } else { //--- prepare request for close SELL position if(bBuy) return false; request.type =ORDER_TYPE_BUY; request.price=SymbolInfoDouble(_Symbol,SYMBOL_ASK); } } else return(false); double dVolume; dVolume=PositionGetDouble(POSITION_VOLUME); if(dVolume<=0) break; if(dVolume>5) dVolume=5; //--- setting request request.action =TRADE_ACTION_DEAL; request.symbol =_Symbol; request.deviation =3; request.type_filling=ORDER_FILLING_AON; request.volume =dVolume; //--- check volume double max_volume=5; if(request.volume>max_volume) { request.volume=max_volume; } //--- order check if(!OrderCheck(request,result)) { Print(result.retcode); return(false); } //--- order send if(!OrderSend(request,result1)) { if(--retry_count>=0) continue; Print(result1.retcode); return(false); } } return true; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double GetOrderCount() { double dCount=0; if(PositionSelect(_Symbol)) { dCount=PositionGetDouble(POSITION_VOLUME); if((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) { } else dCount=-dCount; } return dCount; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ bool OpenOrder(bool bBuy,double dLots,double dPrice,double dSL,double dTP) { MqlTradeRequest request; MqlTradeCheckResult result; MqlTradeResult result1; request.magic=m_magic; request.deviation=0; request.price=dPrice; request.sl=dSL; request.tp=dTP; request.action=TRADE_ACTION_DEAL; request.symbol=_Symbol; request.type_filling=ORDER_FILLING_AON; request.type_time=ORDER_TIME_GTC; if(bBuy) request.type=ORDER_TYPE_BUY; else request.type=ORDER_TYPE_SELL; int nOrder=0; double lot; // Print("lot",dLots); for(;;) { lot=dLots; if(lot>5) lot=5; //Print(request.price); request.volume=lot; if(!OrderCheck(request,result)) { // Print(result.retcode); break; } //--- order send if(!OrderSend(request,result1)) { Print(result1.retcode); break; } dLots-=lot; if(dLots<0.02) break; } return true; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double GetLots(double Risk) { double price=0.0; double margin=0.0; if(!SymbolInfoDouble(_Symbol,SYMBOL_ASK,price)) return(0.0); if(!OrderCalcMargin(ORDER_TYPE_BUY,_Symbol,1.0,price,margin)) return(0.0); if(margin<=0.0) return(0.0); double lot=NormalizeDouble(AccountInfoDouble(ACCOUNT_FREEMARGIN)*Risk/margin,2); int k=int(lot); // if(k<1) k=1; lot=k; if(lot>15) lot=15; if(lot==0) lot=0.5; return lot; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ bool IndicatorsReady(bool bFirst) { if(bFirst) { ExtHandle=INVALID_HANDLE; ExtHandleQS=INVALID_HANDLE; ExtHandleSlow=INVALID_HANDLE; // ExtHandleQS1=INVALID_HANDLE; } //--- check handles and retry to create the indicators if(ExtHandle==INVALID_HANDLE) { ExtHandle=iCustom(_Symbol,_Period,"wen_log"); } if(ExtHandleQS==INVALID_HANDLE) { ExtHandleQS=iCustom(_Symbol,_Period,"wen_qs"); } if(ExtHandleSlow==INVALID_HANDLE) { ExtHandleSlow=iCustom(_Symbol,PERIOD_H4,"wen_qs"); } // if(ExtHandleQS1==INVALID_HANDLE) { ExtHandleQS1=iCustom(_Symbol,PERIOD_H1,"wen_qs"); } //--- check indicator handles if(ExtHandle==INVALID_HANDLE || ExtHandleQS==INVALID_HANDLE || ExtHandleSlow==INVALID_HANDLE) { return(false); } return(true); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ bool PositionModify(double sl,double tp) { MqlTradeRequest request; MqlTradeCheckResult result; MqlTradeResult result1; if(PositionSelect(_Symbol)) { double dsl=PositionGetDouble(POSITION_SL); if((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) { if(sl<dsl) return false; } else if(sl>dsl) return false; request.action=TRADE_ACTION_SLTP; request.symbol=_Symbol; request.sl =sl; request.tp =tp; //--- order check if(!OrderCheck(request,result)) { return(false); } //--- order send if(!OrderSend(request,result1)) { return(false); } } //--- ok return(true); } //+------------------------------------------------------------------+
- 2010.02.25
- MetaQuotes Software Corp.
- www.mql5.com
Well, nobody answered, so I do. Let's look at the CloseAll() function
bool CloseAll() { MqlTradeRequest request; MqlTradeCheckResult result; MqlTradeResult result1; int retry_count =10; uint retcode =TRADE_RETCODE_REJECT; bool bBuy; double d=GetOrderCount(); if(d>0)bBuy=true; else if(d<0) bBuy=false; else return(false); //--- check stopped for(;;) { //--- checking if(PositionSelect(_Symbol)) { if((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) { //--- prepare request for close BUY position if(bBuy==false) return false; request.type =ORDER_TYPE_SELL; request.price=SymbolInfoDouble(_Symbol,SYMBOL_BID); } else { //--- prepare request for close SELL position if(bBuy) return false; request.type =ORDER_TYPE_BUY; request.price=SymbolInfoDouble(_Symbol,SYMBOL_ASK); } } else return(false); double dVolume; dVolume=PositionGetDouble(POSITION_VOLUME); if(dVolume<=0) break; if(dVolume>5) dVolume=5; //--- setting request request.action =TRADE_ACTION_DEAL; request.symbol =_Symbol; request.deviation =3; request.type_filling=ORDER_FILLING_AON; request.volume =dVolume; //--- check volume double max_volume=5; if(request.volume>max_volume) { request.volume=max_volume; } //--- order check if(!OrderCheck(request,result)) { Print(result.retcode); return(false); } //--- order send if(!OrderSend(request,result1)) { if(--retry_count>=0) continue; Print(result1.retcode); return(false); } } return true; }
1. Why is there for() without any parameter? Actually it means endless loop.
2. What does the OrderSend() function return in fact? See documentation - https://www.mql5.com/en/docs/trading/ordersend
- www.mql5.com
Well, nobody answered, so I do. Let's look at the CloseAll() function
1. Why is there for() without any parameter? Actually it means endless loop.
2. What does the OrderSend() function return in fact? See documentation - https://www.mql5.com/en/docs/trading/ordersend
Thanks for your answer.
1.for() will end when PositionSelect(_Symbol) return false, or OrderCheck(request,result) return false ,or OrderSend(request,result1). If for() loops endless, The EA will run in the for(),and can not do other works, in fact , the EA works normally.
2 . The return value ofOrderSend() function indicate if the order is successfully accepted by the trade server. If it returns true or false, will not get a order without SL.
The key is the EA order set every ordert SL,but it get a no SL order. Your answer does not resolve the problem.
Thanks for your answer.
1.for() will end when PositionSelect(_Symbol) return false, or OrderCheck(request,result) return false ,or OrderSend(request,result1). If for() loops endless, The EA will run in the for(),and can not do other works, in fact , the EA works normally.
2 . The return value ofOrderSend() function indicate if the order is successfully accepted by the trade server. If it returns true or false, will not get a order without SL.
The key is the EA order set every ordert SL,but it get a no SL order. Your answer does not resolve the problem.
Thanks for your answer.
1.for() will end when PositionSelect(_Symbol) return false, or OrderCheck(request,result) return false ,or OrderSend(request,result1). If for() loops endless, The EA will run in the for(),and can not do other works, in fact , the EA works normally.
It is mistake. PositionSelect can return true if the client terminal didn't get yet information from trade server. In this case your EA will send new trade request to trade and open a new position without SL and TP because I see this in your code:
Read the article Trade Events in MetaTrader 5.
Advise: try to backtest your EA in with "Random Delay" Execution - https://www.metatrader5.com/en/terminal/help/algotrading/testing.
Even though I don't think it will help to make your mistake obvious.
Advise: try to backtest your EA in with "Random Delay" Execution - https://www.metatrader5.com/en/terminal/help/algotrading/testing.
Even though I don't think it will help to make your mistake obvious.
Thanks again for your answer.
The EA use ordersend() in two place . one is to close order(in this case, not set SL),another is to create new order(in this case ,set SL). The function you mentioned is to close the opened order ,not create new order , I think it is not necessary to set SL in this case.
Thanks again for your answer.
The EA use ordersend() in two place . one is to close order(in this case, not set SL),another is to create new order(in this case ,set SL). The function you mentioned is to close the opened order ,not create new order , I think it is not necessary to set SL in this case.
Thanks to incorrect approach your EA can open new position immediately after closing other one. In both matters you send request by OrderSend(). There is no special function to close open position in MQL5, just OrdersSend() to buy or to Sell. Is it clear?
I know there is no special function to close open position in MQL5.
My EA has two function: OpenOrder() and CloseAll().
If there are no order,I call OpenOrder() (has SL) to create new orders
If there are order, first I call CloseAll() (no SL) to close orders ,then call OpenOrder() (has SL) to create new orders.
for example,
if i want to buy order ,if there are buy orders, I will do nothing. if there are sell orders ,first I call CloseAll() (buy orders no SL) to close the sell orders, then call OpenOrder() ( buy orders with SL ) to create new orders.
Is it incorrect?
- www.mql5.com
if i want to buy order ,if there are buy orders, I will do nothing. if there are sell orders ,first I call CloseAll() (buy orders no SL) to close the sell orders, then call OpenOrder() ( buy orders with SL ) to create new orders.
Is it incorrect?

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
https://championship.mql5.com/2011/en/news/101#comments.
thanks.