Saeid Pourmoradi:
Expert Hedging works. When MetaTrader is closed or the server is rebooted, after launching, unfortunately, the previous orders that were open and if the Grid Distance reaches the specified value, it cannot open a new order and the error : invalid lots amount for OrderSend function is displayed in the section. Thank you for your advice, what code should be changed or added?
datetime da=D'2040.12.31 00:00'; extern string EA_Name="Test"; extern double lot=0.01; //Lot Size extern double dis=10; //Grid Distance extern double Multiplier=1.04; extern double Spreed=30; //Maximum Spread extern double maxlot1=1.28; //Maximum Lot Size extern bool tf=false; //Time Filter extern string StartTime="00:00"; //Start Time extern string StopTime="23:59"; //End Time extern string en1="Martingale"; //Engine 1 (Martingale) extern double tpp=8; //Take Profit (Pips) extern double tp=30; //Take Profit $$ extern int mag1=123; //Magic Number extern string en2="Anti Martingale"; //Engine 2 (Anti Martingale) extern int mhl=10; //Martingal Hedge Level extern int tsl=3; //Trailing SL (Pips) extern int ts=1; //Trailing Step (Pips) extern int sl=0; //Stop Loss (Pips) extern bool hr=true; //Hedge Reborn extern double maxlot2=1.28; //Maximum Lot Size extern double al=10000000; //Account Locking($) extern int mag2=456; //Magic Number //extern string Email="eamt4forex@gmail.com"; double point; bool en=true; double hbb,blb,hsb,bsl; double fbl,fsl,hbl,hsl; bool b2=false,s2=false; bool shb=false,shs=false; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- if(Digits==5 || Digits==3) { point=Point*10; } else { point=Point; } //--- RecoverState(); // rebuild martingale/hedge state after any restart return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Rebuild martingale/hedge state from currently open orders | //| Fixes "invalid lots amount" after a terminal/chart restart: | //| the state globals are lost on restart while positions remain. | //+------------------------------------------------------------------+ void RecoverState() { // Base grid lot = largest open lot on mag1 (fbl()/fsl() return the max lot) if(nbuy(mag1)>0) fbl=fbl(mag1); else fbl=lot; if(nsell(mag1)>0) fsl=fsl(mag1); else fsl=lot; // Hedge running totals = whatever is currently open on mag2 hsl=LS(mag2); // total hedge SELL volume hbl=LB(mag2); // total hedge BUY volume } //+------------------------------------------------------------------+ //| Snap a lot to the broker's lot step and min/max limits | //+------------------------------------------------------------------+ double NormalizeLots(double l) { double step=MarketInfo(Symbol(),MODE_LOTSTEP); double minl=MarketInfo(Symbol(),MODE_MINLOT); double maxl=MarketInfo(Symbol(),MODE_MAXLOT); if(step<=0.0) step=0.01; int digits=(int)MathCeil(-MathLog10(step)); if(digits<0) digits=0; l=MathFloor(l/step+0.5)*step; // snap to lot step if(l<minl) l=minl; if(l>maxl) l=maxl; return(NormalizeDouble(l,digits)); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { if(TimeCurrent()>da) { Alert("EA Expired. Please contact to x@gmail.com"); ExpertRemove(); return; } //--- if(((tf && TradeTime()) || tf==false) && (MarketInfo(Symbol(),MODE_SPREAD)/10<=Spreed)) { if(nbuy(mag1)==0) { fbl=lot; blb=0; hsl=0; hsb=0; shs=false; b2=false; } if(nsell(mag1)==0) { fsl=lot; bsl=0; hbb=0; hbl=0; shb=false; s2=false; } if(nbuy(mag1)==0 && nsell(mag1)==0) { BUY(lot,mag1,0); SELL(lot,mag1,0); } if(nbuy(mag1)>0 && Ask<=fbo(mag1)-dis*point) { fbl=fbl(mag1)*Multiplier; // derive from live order -> restart-safe BUY(fbl,mag1,0); if(nbuy(mag1)==mhl) { hsl=fbl(mag1); SELL(hsl,mag2,sl); } else if(nbuy(mag1)>mhl) { hsl+=fbl(mag1); if(hr) SELL(hsl-LS(mag2),mag2,sl); else SELL(fbl,mag2,sl); } if(nsell(mag1)==0) SELL(lot,mag1,0); } if(nsell(mag1)>0 && Bid>=fso(mag1)+dis*point) { fsl=fsl(mag1)*Multiplier; // derive from live order -> restart-safe SELL(fsl,mag1,0); if(nsell(mag1)==mhl) { hbl=fsl(mag1); BUY(hbl,mag2,sl); } else if(nsell(mag1)>mhl) { hbl+=fsl(mag1); if(hr) BUY(hbl-LB(mag2),mag2,sl); else BUY(fsl,mag2,sl); } if(nbuy(mag1)==0) BUY(lot,mag1,0); } } if(nsell(mag1)>0 && (NS(mag1)+NB(mag2)>=tp || ((NS(mag1)+NB(mag2))/(LS(mag1)+LB(mag2)))*Point>=tpp*point)) { CloseSell(mag1); if(nbuy(mag2)>0) CloseBuy(mag2); } if(nbuy(mag1)>0 && (NB(mag1)+NS(mag2)>=tp || ((NB(mag1)+NS(mag2))/(LB(mag1)+LS(mag2)))*Point>=tpp*point)) { CloseBuy(mag1); if(nsell(mag2)>0) CloseSell(mag2); } // if((NB(mag1)+NB(mag2)+NS(mag1)+NS(mag2))<=-al) if(AccountProfit()<=-al) { en=false; if((LB(mag1)+LB(mag2))>(LS(mag1)+LS(mag2))) SELL((LB(mag1)+LB(mag2))-(LS(mag1)+LS(mag2)),mag2,0); if((LB(mag1)+LB(mag2))<(LS(mag1)+LS(mag2))) BUY((LS(mag1)+LS(mag2))-(LB(mag1)+LB(mag2)),mag2,0); ExpertRemove(); return; } if(nbuy(mag2)>0) { for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) { if(OrderType()==0 && OrderSymbol()==Symbol() && OrderMagicNumber()==mag2) { if(OrderStopLoss()<Bid-tsl*point && Bid-OrderOpenPrice()>=tsl*point) { bool mb=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-tsl*point,OrderTakeProfit(),0,clrNONE); if(mb) { hbb=Bid-tsl*point; } } } } } } if(nsell(mag2)>0) { for(int j=0;j<OrdersTotal();j++) { if(OrderSelect(j,SELECT_BY_POS,MODE_TRADES)) { if(OrderType()==1 && OrderSymbol()==Symbol() && OrderMagicNumber()==mag2) { if((OrderStopLoss()>Ask+tsl*point || OrderStopLoss()==0) && (OrderOpenPrice()-Ask>=tsl*point)) { bool sb=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+tsl*point,OrderTakeProfit(),0,clrNONE); if(sb) { hsb=Ask+tsl*point; } } } } } } } //+------------------------------------------------------------------+ bool TradeTime() { bool result=false; datetime svt = StringToTime(TimeToString(TimeCurrent(), TIME_DATE) + " " + StartTime); datetime bvt = StringToTime(TimeToString(TimeCurrent(), TIME_DATE) + " " + StopTime); if(svt<bvt) { if(TimeCurrent()>=svt && TimeCurrent()<bvt) result=true; } else { if(TimeCurrent()>=svt || TimeCurrent()<bvt) result=true; } return(result); } //+------------------------------------------------------------------+ int nbuy(int m) { int buy=0; for(int i=0;i<OrdersTotal();i++) { int sb=OrderSelect(i,SELECT_BY_POS,MODE_TRADES); if(OrderType()==0 && OrderSymbol()==Symbol() && OrderMagicNumber()==m) buy++; } return buy; } //+------------------------------------------------------------------+ int nsell(int m) { int sel=0; for(int x=0;x<OrdersTotal();x++) { int ss=OrderSelect(x,SELECT_BY_POS,MODE_TRADES); if(OrderType()==1 && OrderSymbol()==Symbol() && OrderMagicNumber()==m) sel++; } return sel; } //+---------------------------------------------------------------------+ double fbo(int m) { double z=0,zz=0; for(int i=OrdersTotal()-1;i>=0;i--) { int a=OrderSelect(i,SELECT_BY_POS,MODE_TRADES); if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && OrderMagicNumber()==m) { z=OrderOpenPrice(); if(z<zz || zz==0)zz=z; } } return zz; } //+------------------------------------------------------------------+ double fso(int m) { double z=0,zz=0; for(int i=OrdersTotal()-1;i>=0;i--) { int a=OrderSelect(i,SELECT_BY_POS,MODE_TRADES); if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==m) { z=OrderOpenPrice(); } if(z>zz || zz==0)zz=z; } return zz; } //+------------------------------------------------------------------+ double fbl(int m) { double z=0,zz=0; for(int i=OrdersTotal()-1;i>=0;i--) { int a=OrderSelect(i,SELECT_BY_POS,MODE_TRADES); if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && OrderMagicNumber()==m) { z=OrderLots(); if(z>=zz || zz==0)zz=z; } } return zz; } //+------------------------------------------------------------------+ double fsl(int m) { double z=0,zz=0; for(int i=OrdersTotal()-1;i>=0;i--) { int a=OrderSelect(i,SELECT_BY_POS,MODE_TRADES); if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==m) { z=OrderLots(); } if(z>=zz || zz==0)zz=z; } return zz; } //+------------------------------------------------------------------+ void SELL(double ll,int m,double ssl) { if(ssl>0) { ssl=Bid+ssl*point; if(shs) hsb=ssl; } if(ll>maxlot1 && en && m==mag1) ll=maxlot1; else if(ll>maxlot2 && en && m==mag2) ll=maxlot2; ll=NormalizeLots(ll); if(ll<=0.0) { Print("SELL skipped: computed lot <= 0"); return; } int sell=OrderSend(Symbol(),OP_SELL,ll,Bid,0,ssl,0,EA_Name,m,0,clrNONE); if(sell<0) { Print("sell failed with error #",GetLastError()); } else { Print("sell placed successfully ",m); } } //////////////////////////////////////// void BUY(double ll,int m,double ssl) { if(ssl>0) { ssl=Ask-ssl*point; if(shb) hbb=ssl; } if(ll>maxlot1 && en && m==mag1) ll=maxlot1; else if(ll>maxlot2 && en && m==mag2) ll=maxlot2; ll=NormalizeLots(ll); if(ll<=0.0) { Print("BUY skipped: computed lot <= 0"); return; } int buy=OrderSend(Symbol(),OP_BUY,ll,Ask,0,ssl,0,EA_Name,m,0,clrNONE); if(buy<0) { Print("buy failed with error #",GetLastError()); } else { Print("buy placed successfully ",m); } } //+------------------------------------------------------------------+ void CloseBuy(int m) { for(int i=0;i<OrdersTotal();i++) { int a=OrderSelect(i,SELECT_BY_POS,MODE_TRADES); { if(OrderType()==OP_BUY && OrderMagicNumber()==m && OrderSymbol()==Symbol()) { int u=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),100,clrNONE); } } } if(nbuy(m)>0)CloseBuy(m); } //////////////////////////////////////// void CloseSell(int m) { for(int i=0;i<OrdersTotal();i++) { int a=OrderSelect(i,SELECT_BY_POS,MODE_TRADES); { if(OrderType()==OP_SELL && OrderMagicNumber()==m && OrderSymbol()==Symbol()) { int u=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),100,clrNONE); } } } if(nsell(m)>0)CloseSell(m); } //////////////////////////////////////// double NB(int m) { double tb=0; for(int i=0;i<OrdersTotal();i++) { double nb=0; if(OrderSelect(i,SELECT_BY_POS)==true) { if(OrderType()==OP_BUY && OrderMagicNumber()==m && OrderSymbol()==Symbol()) { nb=OrderProfit()+OrderSwap()+OrderCommission(); tb=nb+tb; } } } return tb; } //////////////////////////////////////// double NS(int m) { double ts1=0; for(int i=0;i<OrdersTotal();i++) { double ns=0; if(OrderSelect(i,SELECT_BY_POS)==true) { if(OrderType()==OP_SELL && OrderMagicNumber()==m && OrderSymbol()==Symbol()) { ns=OrderProfit()+OrderSwap()+OrderCommission(); ts1=ns+ts1; } } } return ts1; } //////////////////////////////////////// double LB(int m) { double tb=0; for(int i=0;i<OrdersTotal();i++) { double nb=0; if(OrderSelect(i,SELECT_BY_POS)==true) { if(OrderType()==OP_BUY && OrderMagicNumber()==m && OrderSymbol()==Symbol()) { nb=OrderLots(); tb=nb+tb; } } } return tb; } //////////////////////////////////////// double LS(int m) { double ts1=0; for(int i=0;i<OrdersTotal();i++) { double ns=0; if(OrderSelect(i,SELECT_BY_POS)==true) { if(OrderType()==OP_SELL && OrderMagicNumber()==m && OrderSymbol()==Symbol()) { ns=OrderLots(); ts1=ns+ts1; } } } return ts1; }
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
Expert Hedging works. When MetaTrader is closed or the server is rebooted, after launching, unfortunately, the previous orders that were open and if the Grid Distance reaches the specified value, it cannot open a new order and the error : invalid lots amount for OrderSend function is displayed in the section. Thank you for your advice, what code should be changed or added?