Full code.
#property copyright "Tarcísio Allyson" #property link "https://www.mql5.com" #property version "1.00" #property strict // Extern Variable extern double LotSize = 0.01; extern double TakeProfit = 5; extern double StopLoss = 15; extern int Slippage = 5; extern int MagicNumber = 123; extern int MAFastPeriodo = 21; extern int MAMainPeriodo = 42; extern int MASlowPeriodo = 66; // Global Variable int BuyTicket; int SellTicket; double UsePoint; int UseSlippage; double BuyStopLoss; double BuyTakeProfit; double SellStopLoss; double SellTakeProfit; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- Comment("EA Started"); UsePoint = PipPoint(Symbol()); UseSlippage = GetSlippage(Symbol(), Slippage); //--- return(0); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- double FastMAValue = iMA(NULL,0,MAFastPeriodo,0,MODE_EMA,PRICE_CLOSE,0); double MainMAValue = iMA(NULL,0,MAMainPeriodo,0,MODE_EMA,PRICE_CLOSE,0); double SlowMAValue = iMA(NULL,0,MASlowPeriodo,0,MODE_EMA,PRICE_CLOSE,0); //Buy Order if (FastMAValue > MainMAValue && MainMAValue > SlowMAValue && Bid > SlowMAValue && Bid < MainMAValue && BuyTicket == 0) { OrderSelect(SellTicket,SELECT_BY_TICKET); //Close order if(OrderCloseTime() == 0 && SellTicket > 0) { double CloseLots = OrderLots(); double ClosePrice = Ask; bool Closed = OrderClose(SellTicket,CloseLots,ClosePrice,UseSlippage,Red); } double OpenPrice = Ask; // Calculate stop loss and take profit if(StopLoss > 0) BuyStopLoss = OpenPrice - (StopLoss * UsePoint); if(TakeProfit > 0) BuyTakeProfit = OpenPrice + (TakeProfit * UsePoint); // Open buy order BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,OpenPrice,UseSlippage,BuyStopLoss,BuyTakeProfit,"Buy Order",MagicNumber,0,Green); } //Sell Order if (FastMAValue < MainMAValue && MainMAValue < SlowMAValue && Bid < SlowMAValue && Bid > MainMAValue && SellTicket == 0) { OrderSelect(BuyTicket,SELECT_BY_TICKET); //Close order if(OrderCloseTime() == 0 && BuyTicket > 0) { double CloseLots = OrderLots(); double ClosePrice = Bid; bool Closed = OrderClose(BuyTicket,CloseLots,ClosePrice,UseSlippage,Red); } double OpenPrice = Bid; // Calculate stop loss and take profit if(StopLoss > 0) SellStopLoss = OpenPrice + (StopLoss * UsePoint); if(TakeProfit > 0) SellTakeProfit = OpenPrice - (TakeProfit * UsePoint); // Open sell order SellTicket = OrderSend(Symbol(),OP_SELL,LotSize,OpenPrice,UseSlippage,SellStopLoss,SellTakeProfit,"Sell Order",MagicNumber,0,Red); } if (Ask < SellTakeProfit || Ask > SellStopLoss) { SellTicket = 0; } if (Bid > BuyTakeProfit || Bid < BuyStopLoss) { BuyTicket = 0; } //+-------------------------------------------------- //MARTINGALE int WinCount; int LossCount; for (int Count = OrdersHistoryTotal()-1; ; Count--) { OrderSelect(Count,SELECT_BY_POS, MODE_HISTORY); if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { if(OrderProfit() > 0 && LossCount == 0) WinCount++; else if (OrderProfit() < 0 && WinCount == 0) LossCount++; else break; } } } //+------------------------------------------------------------------+ // Pip Point Fuction double PipPoint(string Currency) { double CalcPoint; int CalcDigits = MarketInfo(Currency, MODE_DIGITS); if (CalcDigits == 2 || CalcDigits == 3) CalcPoint = 0.01; else if (CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001; return(CalcPoint); } // Get Slippage Function int GetSlippage(string Currency, int SlippagePips) { double CalcSlippage; int CalcDigits = MarketInfo(Currency,MODE_DIGITS); if(CalcDigits == 2 || CalcDigits == 4) CalcSlippage = SlippagePips; else if(CalcDigits == 3 || CalcDigits == 5) CalcSlippage = SlippagePips * 10; return(CalcSlippage);
- www.mql5.com
What's wrong with this?
EA crasches in this section.
try this
int WinCount = 0; int LossCount = 0; for (int Count = OrdersHistoryTotal()-1; Count >= 0; order--) { if(OrderSelect(Count,SELECT_BY_POS, MODE_HISTORY)) { if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { if(OrderProfit() > 0 && LossCount == 0) WinCount++; else if (OrderProfit() < 0 && WinCount == 0) LossCount++; else break; } } }
- There is no variable order. Mohamad meant count--. (Always prefer --count. Important with classes. Slight optimization with int types.)
- And once you subtract one, it is no longer a count but a position. Code what you mean, either A) rename the variable, or B) loop count>0 and select count-1.
- The break is wrong. If it is not a trade of the current symbol/magic number, ignore it. The break means other trades (on other charts) and manual trading breaks the code.
- No need for nested if's.
for (int Position = OrdersHistoryTotal()-1; Position >= 0; --Position) if( OrderSelect(Position,SELECT_BY_POS, MODE_HISTORY) && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { if(OrderProfit() > 0 && LossCount == 0) WinCount++; else if (OrderProfit() < 0 && WinCount == 0) LossCount++; }
alternatively#define COUNT uint // One based. #define INDEX uint // Zero based. for (COUNT count = OrdersHistoryTotal(); count > 0;){ --count; // count is now an INDEX if(OrderSelect(count,SELECT_BY_POS, MODE_HISTORY) ...
-
In the presence of multiple orders (one EA
multiple charts, multiple EAs, manual
trading,) while you are waiting for the current operation (closing, deleting,
modifying) to complete, any number of other operations on other orders could
have concurrently happened and changed the position indexing:
-
For non-FIFO (US
brokers,) (or the EA only opens
one order per symbol,) you can simply count down in a position
loop, and you won't miss orders. Get in the habit of always counting down.
Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.) -
and check OrderSelect in case earlier positions were deleted.
What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles - and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask) or (be direction independent and use) OrderClosePrice().
-
For non-FIFO (US
brokers,) (or the EA only opens
one order per symbol,) you can simply count down in a position
loop, and you won't miss orders. Get in the habit of always counting down.
- There is no variable order. Mohamad meant count--. (Always prefer --count. Important with classes. Slight optimization with int types.
- order variable is typo error, when copying form my code. thank you for that.
- Increment/decrement operators, and side effects.
- notice you post less now, on this forum :)
- www.learncpp.com
for(int i=x-1; i>=0; --i) //and for(int i=0; i<x; i++)
because it makes the indexing direction instantly stand out, thus making the code easier to mentally parse. I mostly agree with whoeder with the exception of a few points. *These are notes for the OP
Point 2: Agree. Variables should be named to explicitly identify it. By convention, the index iterator is usually named i (short for index or iterator) followed by the remaining (lower-case) ascii-alphabet for nested loops.
for(int i=0; i<x-1; i++) for(int j=i+1; j<x; j++)
Point 3: Is incorrect. The code will not break on a different symbol's position because the only way this block is entered is on a matching symbol&magic condition. That said, this should be refactored to use booleans instead of integers since it's checking for truth not quantity.
Point 4.1: Agreed. Although you should never begin a nested statement on the same line as the parent statement. This is a "code-smell" that will burn you in the debugging process and will generally make other programmers cringe in horror if they were to see your src. If you go by most c++ style guidelines then your code would look like something like this.
for(int i=OrdersHistoryTotal()-1; i>=0; --i) { if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) && OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC_NUMBER ){ ... } }
Point 4.2: There's no reason to go redefining types. This is otherwise known as code obfuscation (contrived complexity) and the only one who will be confused is yourself when you go to re-read your code a month later... You'll have to bounce around the page referencing the actual type so just use the actual type from the start. Your variable names (which should start with a lowercase letter, BTW) should describe what the variable is not a macro definition.
Point 5.1: Agree on all points except recursion is generally a better pattern instead of resetting the index var because of the potential to get stuck in an infinite loop on failing orders.
bool close_all() { RefreshRates(); for(int i=0; i<OrdersTotal(); i++) { if(OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC_NUMBER ){ if(OrderClose(...)) return close_all(); return false; } } return true; }
Jeez Northeden (Norbert).... do you even comprehend what you have asked....?
Try this.... "Focus"..... Ask one question.... focus....
Maybe take the "Break" out...
Also set the "Variables" to 0...
...take the "Else If"... out... make it a straight "if".....
...."Else If's" are not good.... in my opinion
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
What's wrong with this?
EA crasches in this section.