res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Bid+(Stop_Loss*Point),0,"",0,0,MAGICMA);
Next time:
![](https://c.mql5.com/mql4/forum/2010/04/src_3.gif)
I realized, the reason, EA opens positions again and again, but I don't understand why, because there is a "return" condition in the EA!?
Every tick the conditions hold true the EA will attempt to send an order.
-
I understand, but how can i set, that EA opens position when there is no opened position, or when EA get a new buy or sell signal?
Thanks
Why isn't enough this?
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();
I think if there's no opened position, checks for open, and if there is, checks for close...
extern double Lots = 0.1; extern double MaximumRisk = 0.01; extern double DecreaseFactor = 0; extern double MovingPeriod = 13; extern double MovingShift = 0; extern double Stop_Loss = 70; extern double Take_Profit = 200; //+------------------------------------------------------------------+ //| Calculate open positions | //+------------------------------------------------------------------+ int CalculateCurrentOrders(string symbol) { int buys=0,sells=0; //---- for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA) { if(OrderType()==OP_BUY) buys++; if(OrderType()==OP_SELL) sells++; } } //---- return orders volume if(buys>0) return(buys); else return(-sells); } //+------------------------------------------------------------------+ //| Calculate optimal lot size | //+------------------------------------------------------------------+ double LotsOptimized() { double lot=Lots; int orders=HistoryTotal(); // history orders total int losses=20; // number of losses orders without a break //---- select lot size lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1); //---- calcuulate number of losses orders without a break if(DecreaseFactor>0) { for(int i=orders-1;i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; } if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue; //---- if(OrderProfit()>0) break; if(OrderProfit()<0) losses++; } if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1); } //---- return lot size if(lot<0.1) lot=0.1; return(lot); } //+------------------------------------------------------------------+ //| Check for open order conditions | //+------------------------------------------------------------------+ void CheckForOpen() { double ma; int res; //---- get Moving Average ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0); //---- sell conditions if(Close[1]<ma) { res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Bid+(Stop_Loss*Point*10),Bid-(Take_Profit*Point*10),"",0,0,MAGICMA); return(0); } //---- buy conditions if(Close[1]>ma) { res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,Ask-(Stop_Loss*Point*10),Ask+(Take_Profit*Point*10),"",0,0,MAGICMA); return(0); } //---- } //+------------------------------------------------------------------+ //| Check for close order conditions | //+------------------------------------------------------------------+ void CheckForClose() { double ma; //---- go trading only for first tiks of new bar if(Volume[0]>1) return; //---- get Moving Average ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0); //---- for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue; //---- check order type if(OrderType()==OP_BUY) { if(Close[1]<ma) OrderClose(OrderTicket(),OrderLots(),Bid,3,White); break; } if(OrderType()==OP_SELL) { if(Close[1]>ma) OrderClose(OrderTicket(),OrderLots(),Ask,3,White); break; } } //---- } //+------------------------------------------------------------------+ //| Start function | //+------------------------------------------------------------------+ void start() { //---- check for history and trading if(Bars<100 || IsTradeAllowed()==false) return; //---- calculate open orders by current symbol if(CalculateCurrentOrders(Symbol())==0) CheckForOpen(); else CheckForClose(); //---- } //+------------------------------------------------------------------+
//+------------------------------------------------------------------+
I understand, but how can i set, that EA opens position when there is no opened position, or when EA get a new buy or sell signal?
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen(); else CheckForClose();
Your CalculateCurrentOrders() function will return zero when there are no orders (that belong to symbol and magic number) so CheckForOpen() will only be called if there are no orders... I am not sure I understand what's the problem? Maybe u can explain again...
My problem is, after that I put SL and TP, my expert opens postition on every new tick, but I don't understand why!?
I would like to solve, that EA open position when there is no opened position.
Thanks!
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hey,
What does it mean... "String parameter expected"? I get this error message, when i test my EA with this StopLoss...
Tahnks
void CheckForOpen()
{
double ma;
int res;
//---- get Moving Average
ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//---- sell conditions
if(Close[1]<ma)
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Bid+(Stop_Loss*Point),"",0,0,MAGICMA);
return;
}
//---- buy conditions
if(Close[1]>ma)
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,Ask-(Stop_Loss*Point),"",0,0,MAGICMA);
return;
}