This may give you some things to consider.
if (Trading()) { if (Condition=="Buy") { CloseOrder(OP_SELL); if (OrdersTotal()==0) { int BUY = OrderSend(_Symbol,OP_BUY,0.1,Ask,2,0,Ask+300*_Point,eaName,MAGNUM,0,Blue); } } if (Condition=="Sell") { CloseOrder(OP_BUY); if (OrdersTotal()==0) { int SELL = OrderSend(_Symbol,OP_SELL,0.1,Bid,2,0,Bid-300*_Point,eaName,MAGNUM,0,Red); } } }
void CloseOrder(int cmd) { for (int x= OrdersTotal()-1; x>=0; x--) { if(OrderSelect(x,SELECT_BY_POS) && OrderSymbol()==_Symbol && OrderMagicNumber()==MAGNUM && OrderType()==cmd) if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0)) Print("error closing #",OrderTicket()," Error code ",GetLastError()); } }
This may give you some things to consider.
Thank you for the suggestion...
But after I'm trying, it's still same with current condition. it's still open in every candle.
there's any suggestion again? for my ea can open only once time per condition meet?
Hi Keith,
Thank you for the suggestion...
But after I'm trying, it's still same with current condition. it's still open in every candle.
there's any suggestion again? for my ea can open only once time per condition meet?
Show your full code.
Show your full code.
#property copyright "" #property link "" //+------------------------------------------------------------------+ //| Parameters | //+------------------------------------------------------------------+ string Signal =""; string eaName ="my eA" string trend; #define MAGNUM 010411 datetime DT; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- DT = Time[0]; //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //+------ MA Parameters double EMA1 = iMA(_Symbol,PERIOD_CURRENT, 5 ,0,MODE_EMA,PRICE_CLOSE,0); double EMA2 = iMA(_Symbol,PERIOD_CURRENT, 18 ,0,MODE_EMA,PRICE_CLOSE,0); double EMA3 = iMA(_Symbol,PERIOD_CURRENT, 90 ,0,MODE_EMA,PRICE_CLOSE,0); if (EMA1>EMA3 && EMA2>EMA3 && EMA1>EMA2) {Signal="Buy";} if (EMA1<EMA3 && EMA1<EMA2 && EMA2<EMA3) {Signal="Sell";} //+------------- Tringer Order if (Trading()){ if ((Signal=="Buy") !=0){ if (OrdersTotal()!=0) CloseOrder(); int BUY; BUY = OrderSend(_Symbol,OP_BUY,1.0,Ask,2,Ask-40*_Point,Ask+80*_Point,eaName,MAGNUM,0,Blue); } if ((Signal=="Sell") !=0){ if (OrdersTotal()!=0) CloseOrder(); int SELL; SELL = OrderSend(_Symbol,OP_SELL,1.0,Bid,2,Bid+40*_Point,Bid-80*_Point,eaName,MAGNUM,0,Red); } } } //----------------------------------------------------- void CloseOrder(){ int Ticket; Ticket = OrderSelect(0,SELECT_BY_POS); Ticket = OrderClose(OrderTicket(), OrderLots(),MarketInfo(Symbol(),MODE_BID+OrderType()),0); } bool Trading() { if (Time[0] != DT && OrdersTotal() <1){ DT = Time[0]; return true; } return false; }
Hi Keith, that's all my code...
it's simple, i'm just learn how to create a ea. . .
You should never use globalscope variables unless it is necessary.
I can see no reason for you to have declared the variable signal as a global variable.
Because it is a global variable it retains its value every tick. So if conditions for a buy are met, it will be set to "Buy", it will keep the value "Buy", no matter whether the conditions for a buy are met or not. It will only be reset when the conditions for a sell are met. Then of course, it will stay as "Sell" until the buy conditions are met.
As you are only placing trades when a new bar opens, is it necessary to check the MAs every tick?
It is more common to check MAs on bar 1, not bar 0 when checking conditions.
It doesn't make sense to close an open buy trade and then open a new buy when you have a buy signal. Look at the code that I posted earlier. You will see that it only closes sells on a buy signal.
You should never use globalscope variables unless it is necessary.
I can see no reason for you to have declared the variable signal as a global variable.
Because it is a global variable it retains its value every tick. So if conditions for a buy are met, it will be set to "Buy", it will keep the value "Buy", no matter whether the conditions for a buy are met or not. It will only be reset when the conditions for a sell are met. Then of course, it will stay as "Sell" until the buy conditions are met.
As you are only placing trades when a new bar opens, is it necessary to check the MAs every tick?
It is more common to check MAs on bar 1, not bar 0 when checking conditions.
It doesn't make sense to close an open buy trade and then open a new buy when you have a buy signal. Look at the code that I posted earlier. You will see that it only closes sells on a buy signal.
void OnTick() { if(isNewCandle()) { int signal = getDataFromBuffers(); if(limitOpenOrders() <= limitOrders) { if(signal == OP_BUY) { openBuy(); useTrailingStop(); } if(signal == OP_SELL) { openSell(); useTrailingStop(); } else { Print("Maximum Allowed Opened Orders Limit is Reached !!!"); } } } }can some one help me the EA keeps telling me maximum allowe opened order reached
If your EA compiles without any error but isn't doing what it should start the debugger:
https://www.metatrader5.com/en/metaeditor/help/development/debug // Code debugging
https://www.mql5.com/en/articles/2041 // Error Handling and Logging in MQL5
https://www.mql5.com/en/articles/272 // Tracing, Debugging and Structural Analysis of Source Code
https://www.mql5.com/en/articles/35 // scroll down to: "Launching and Debuggin"
- www.metatrader5.com
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi all,
Need your help, I have create a EA, but stuck on "Put limit / One Trade Only per Condition".... My target is, if a Condition meet EA will make order until take profit or stop loss, then wait until next condition meet. Hopefully you all can help me to resolve my stuck on this script.... Thank you...
Attached my script;