hello

i am trying to make a code works with cross over 2 MA,

the idea is when the fast MA cross the slow one it start a buy order with volume 0.01 and wait till the other cross

in the second cross if the previous order have profit it will close it and start new sell order with volume 0.01

if not it will start new sell order with double volume 0.02

and go on until it get profit or limit loss then close all orders

the problem it make some kind of loop in the second cross and start many orders with double volumes not only one order

please if someone can help me with this

here is full code and thank you in advance

```//+------------------------------------------------------------------+
//|                                                        3oksh.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property version   "1.00"
#property strict

//Declare Fast Moving Average Properties
string S1 = "Fast MA Sttings";
int Fperiod = 9;
int Fshift = 0;
int Fmethod = 1;  //Or 1 for Exponential
int Fapplied = 0;
/* --------------------------------------------- */

//Declare Slow Moving Average Properties
string S2 = "Slow MA Sttings";
int Speriod = 20;
int Sshift = 0;
int Smethod = 0;  //Or 1 for Exponential
int Sapplied = 0;
/* --------------------------------------------- */

//Declare First order Properties
extern double StartingLot = 0.01;   //Lot Size
extern int PPeriod = 1;
extern double MStopLoss = -1.87;
int Multi = 2;
int Magic = 00020;
int ticket = 0;
double NewLot;
double MaxLots;
int ordcls =0;
string BS;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit(){

return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick(){

Comment("Now Lot = " + DoubleToString(MaxLots,2)
+ ", New Lot = " + DoubleToString(NewLot,2)
+ ", Profit = " + DoubleToString(ProfitAll(),2));

//Draw Fast MA
double FmaLast = iMA(
NULL,      //Symbol
PPeriod,   //TimeFrame
Fperiod,   //MA averaging period
Fshift,    //MA shift
Fmethod,   //Averaging method
Fapplied,  //Applied price
2          //Shift
);
double Fmanow = iMA(
NULL,      //Symbol
PPeriod,   //TimeFrame
Fperiod,   //MA averaging period
Fshift,    //MA shift
Fmethod,   //Averaging method
Fapplied,  //Applied price
1          //Shift
);

//Drow Slow MA
double SmaLast = iMA(
NULL,      //Symbol
PPeriod,   //TimeFrame
Speriod,   //MA averaging period
Sshift,    //MA shift
Smethod,   //Averaging method
Sapplied,  //Applied price
2          //Shift
);
double Smanow = iMA(
NULL,      //Symbol
PPeriod,   //TimeFrame
Speriod,   //MA averaging period
Sshift,    //MA shift
Smethod,   //Averaging method
Sapplied,  //Applied price
1          //Shift
);
if(OrdersTotal() == 0){             // I have no open orders on this chart
if(FmaLast<SmaLast && Fmanow>Smanow){
ticket = OrderSend(
Symbol(),                  //Symbol
StartingLot,               //Volume
3,                         //Slippage
0,                         //Stop loss
0,                         //Take profit
NULL,                      //Comment
Magic,                     //Magic number
0,                         //Pending order expiration
Green                      //Color
);
}
if(FmaLast>SmaLast && Fmanow<Smanow){//ok
ticket = OrderSend(
Symbol(),                  //Symbol
OP_SELL,                   //Operation
StartingLot,               //Volume
Bid,                       //Price
3,                         //Slippage
0,                         //Stop loss
0,                         //Take profit
NULL,                      //Comment
Magic,                     //Magic number
0,                         //Pending order expiration
Red                        //Color
);
}
}else{
for(int i = 0; i < OrdersTotal(); i++){
MaxLots = MathMax(OrderLots(), MaxLots);
}
}

NewLot = MaxLots * Multi;

if(FmaLast<SmaLast && Fmanow>Smanow){
if(ProfitAll() > 0.00){
CloseAll();
ticket = OrderSend(
Symbol(),                  //Symbol
StartingLot,               //Volume
3,                         //Slippage
0,                         //Stop loss
0,                         //Take profit
NULL,                      //Comment
Magic,                     //Magic number
0,                         //Pending order expiration
Green                      //Color
);
}else if(ProfitAll() < 0.00 && ProfitAll() > MStopLoss){
ticket = OrderSend(
Symbol(),                  //Symbol
NewLot,                    //Volume
3,                         //Slippage
0,                         //Stop loss
0,                         //Take profit
NULL,                      //Comment
Magic,                     //Magic number
0,                         //Pending order expiration
Green                      //Color
);
}
}else{
CloseAll();
}
}else if(FmaLast>SmaLast && Fmanow<Smanow){
if(ProfitAll() > 0.00){
CloseAll();
ticket = OrderSend(
Symbol(),                  //Symbol
OP_SELL,                   //Operation
StartingLot,               //Volume
3,                         //Slippage
0,                         //Stop loss
0,                         //Take profit
NULL,                      //Comment
Magic,                     //Magic number
0,                         //Pending order expiration
Green                      //Color
);
}else if(ProfitAll() < 0.00 && ProfitAll() > MStopLoss){
ticket = OrderSend(
Symbol(),                  //Symbol
OP_SELL,                    //Operation
NewLot,                    //Volume
3,                         //Slippage
0,                         //Stop loss
0,                         //Take profit
NULL,                      //Comment
Magic,                     //Magic number
0,                         //Pending order expiration
Green                      //Color
);
}
}else{
CloseAll();
}
}
}
}

//+------------------------------------------------------------------+

//Functions

double ProfitAll(){
double profit=0;
for(int i=OrdersTotal()-1; i >= 0; i--){
if(OrderMagicNumber()== Magic){
profit+=OrderProfit()+OrderCommission();
}
}
}
return profit;
}

void CloseAll(){
for(int i=OrdersTotal()-1; i >= 0; i--){
if(OrderMagicNumber()== Magic){
ordcls = OrderClose(
OrderTicket(),       // ticket
OrderLots(),         // volume
Bid,                 // close price
3,                   // slippage
Blue                 // color
);
}else if(OrderType() == OP_SELL){
ordcls = OrderClose(
OrderTicket(),       // ticket
OrderLots(),         // volume
3,                   // slippage
Blue                 // color
);
}
}
}
}
}

bool z = false;
for(int i=OrdersTotal()-1; i >= 0; i--){
if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic){
if(OrderType() == 0){
z = true;
}else if(OrderType() == 1){
z = false;
}
}
}
}
return(z);
}
```

If it's opening multiple orders then your position of if(OrdersTotal==0) is incorrect so make sure to pack each OrderSend() also to make sure there can always only be one position.

If it's not that, then you have a problem in the closing process where newly opened orders get closed immediately..

1. ` if(OrdersTotal() == 0){             // I have no open orders on this chart`
No open orders on any chart. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum
MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

2. `if(FmaLast<SmaLast && Fmanow>Smanow){`
You are checking a condition, will be true each tick. Only look once per bar or Act on a change of signal.
MQL4 (in Strategy Tester) - double testing of entry conditions - Strategy Tester - Expert Advisors and Automated Trading - MQL5 programming forum

about orderstotal its ok you right and i am not using manual work with experts

i dont even need magic

2- yes also you are right every tick it will check if the cross over happened or not

for that i put another condition its buyorsell

if the last order was buy u dont need to buy again but u can sell only

i also fixed the function of buy orsell but it works only one time

i need code check all existing orders and just get the last ordertype and its volume

here is the function i found but it work only once

```bool BuyOrSell(){
datetime TicketDate;
datetime StartTime;
int TicketNumber;
int TicketType;
bool z = false;
for(int i=0; i<OrdersTotal(); i++){
TicketDate = OrderOpenTime();
StartTime = TicketDate;
TicketNumber = OrderTicket();
z = true;
}else if(TicketType == OP_SELL){
z = false;
}
}

}
return (z);
}        ```

Marian85: about orderstotal its ok you right and i am not using manual work with experts i dont even need magic
If you had read the link you would understand that two filters required, MN and symbol. What part of "your code is incompatible with every EA (including itself on other charts and manual trading." was unclear?

why are you trying to re-event the wheel?

Top right there is the lens (=search) enter: "EA crossing ma" and you'll find 186 pages each with 10 (=1860) entries about Ea trading if two ma crosses?

And practically all are running!

Choose one and amend it according to your ideas! It's a lot faster!