EA made first trade then stop

 

I have made an EA. However it does just first trade and then the time is halted (not the test but it does not move forward). I have been searching the mistake for very long time but found none.

How it should work: First trade is closed by stoploss or takeprofit. If the first trade was buy and was stopped by take profit next trade should be buy also. If the first trade was stopped by stoploss next trade should be sell. 

Where I did the mistake?

//--- input parameters
input bool     MM=true;
input double   lot=0.1;
input int   percent=20;
input double      take_profit=0.0008;
input double      stop_loss=0.0008;
input bool   first_trade_buy=true;
input int   max_slippage=3;
input int magic=111;
double lot1;
int cnt;

void OnInit()
{
   if (MM==false) lot1 = lot;
   if (MM==true) lot1 = (AccountFreeMargin()*percent/100/AccountLeverage()) ;
   
   if (OrdersTotal()==0 && first_trade_buy==true && OrderMagicNumber()!=111){
     OrderSend(Symbol(),OP_BUY,lot1,Bid,max_slippage,Bid-stop_loss,Bid+take_profit,NULL,magic,0,clrBlue);
     Print("first trade buy");}
   else if (OrdersTotal()==0 && first_trade_buy==false && OrderMagicNumber()!=111){
     OrderSend(Symbol(),OP_SELL,lot1,Ask,max_slippage,Ask+stop_loss,Ask-take_profit,NULL,magic,0,clrRed);
     Print("first trade sell");};

     ;
  };
void OnTick ()
   {
   if (MM==false) lot1 = lot;
   if (MM==true) lot1 = (AccountFreeMargin()*percent/100/AccountLeverage()) ;
   
   
   for(cnt=0;cnt=OrdersHistoryTotal();cnt++)
   {
   if(OrderSymbol() == Symbol())
    if(OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY )){
         if(OrderType()==OP_BUY && OrdersTotal()<=1){

             if (OrderClosePrice()==OrderTakeProfit() && OrderClosePrice()!=OrderStopLoss()) {
                OrderSend(Symbol(),OP_BUY,lot1,Bid,max_slippage,Bid-stop_loss,Bid+take_profit,NULL,0,0,clrBlue);}
             else if (OrderClosePrice()==OrderStopLoss() && OrderClosePrice()!=OrderTakeProfit()) {
                 OrderSend(Symbol(),OP_SELL,lot1,Ask,max_slippage,Ask+stop_loss,Ask-take_profit,NULL,0,0,clrRed);};
          }
        else if(OrderType()==OP_SELL && OrdersTotal()<=1){
             if (OrderClosePrice()==OrderStopLoss() && OrderClosePrice()!=OrderTakeProfit()) {
                 OrderSend(Symbol(),OP_BUY,lot1,Bid,max_slippage,Bid-stop_loss,Bid+take_profit,NULL,0,0,clrBlue);}
             else if (OrderClosePrice()==OrderTakeProfit() && OrderClosePrice()!=OrderStopLoss()) {
                OrderSend(Symbol(),OP_SELL,lot1,Ask,max_slippage,Ask+stop_loss,Ask-take_profit,NULL,0,0,clrRed);};
          }
   }
   }
 }

 

I don't know if this applies to your problem or not

https://forum.mql4.com/66334

I am surprised that any trade is opened. You are trying to buy at Bid and Sell at Ask

Why is there no code to check for and report on errors? 

 

I don't see how this would work to calculate an acceptable lot size

   if (MM==true) lot1 = (AccountFreeMargin()*percent/100/AccountLeverage()) ;
 
GumRai:

I don't know if this applies to your problem or not

https://forum.mql4.com/66334

I am surprised that any trade is opened. You are trying to buy at Bid and Sell at Ask

Why is there no code to check for and report on errors? 

Simply said: I don't know what I should write there. "Print: "Order was not opened."? I could see it by myself.

I agree that It is better to have it.

 
GumRai:

I don't see how this would work to calculate an acceptable lot size

 

 AccountFreeMargin - to know how much money can be used from an account.

percent/100 - how much money you want to use in percents (the 100 is used for not making the trade "percent" times bigger but smaller)

AccountLeverage - it would differ when you have an leverage 100 or 500.


I chaned something around "for(cnt=0,cnt......)". It was =,> or something like that and it worked. However it did not follow the "if" rules. Of course I forgot what it was.

 
safarmichal:

Simply said: I don't know what I should write there. "Print: "Order was not opened."? I could see it by myself.

I agree that It is better to have it.

When an ordersend fails, you should include GetLastError() in the Print and also include the details of the trade. That way, you can see why the order failed and modify your code to prevent the error.
 

safarmichal:

 

   if (MM==true) lot1 = (AccountFreeMargin()*percent/100/AccountLeverage()) ;

 AccountFreeMargin - to know how much money can be used from an account.

percent/100 - how much money you want to use in percents (the 100 is used for not making the trade "percent" times bigger but smaller)

AccountLeverage - it would differ when you have an leverage 100 or 500.


 If the leverage is 100, the lot size would be calculated 5 times bigger than if the leverage is 500

So the higher the leverage, the smaller the lot size.

Is that really what you want?

Personally, I think it more sensible if lot size is calculated based on the stop loss, ie maximum loss if the stop loss is hit

 
Thank you, for an advice. I have made it work. However it have not followed the rulles made by ifs. When Sell order is stopped by SL the next trade should be Buy. However it somehow keep on placing sell orders.
input bool     MM=true;
input double   lot=0.1;
input int   percent=20;
input double      take_profit=0.0008;
input double      stop_loss=0.0008;
input bool   first_trade_buy=true;
input int   max_slippage=3;
input int magic=111;
double lot1;

void init()
{

  };
void OnTick ()
   {
   if (MM==false) lot1 = lot;
   if (MM==true) lot1 = (AccountFreeMargin()*percent*AccountLeverage()/10000000) ;
   
   for(int cnt=0;cnt<OrdersHistoryTotal();cnt++)
   {
     if(OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY ))
     if(OrderSymbol() == Symbol() && OrdersTotal()<=1){
         if(OrderType()==OP_BUY){

             if (OrderClosePrice()==OrderTakeProfit() && OrderClosePrice()!=OrderStopLoss()) {
                OrderSend(Symbol(),OP_BUY,lot1,Ask,max_slippage,Ask-stop_loss,Ask+take_profit,NULL,0,0,clrBlue); Print ("Order 1 send");}
             else if (OrderClosePrice()==OrderStopLoss() && OrderClosePrice()!=OrderTakeProfit()) {
                 OrderSend(Symbol(),OP_SELL,lot1,Bid,max_slippage,Bid+stop_loss,Bid-take_profit,NULL,0,0,clrRed);Print ("Order 2 send");};
                 Print ("Last error = " , GetLastError() );
          }
        else if(OrderType()==OP_SELL){
             if (OrderClosePrice()==OrderStopLoss() && OrderClosePrice()!=OrderTakeProfit()) {
                 OrderSend(Symbol(),OP_BUY,lot1,Ask,max_slippage,Ask-stop_loss,Ask+take_profit,NULL,0,0,clrBlue);Print ("Order 3 send");}
             else if (OrderClosePrice()==OrderTakeProfit() && OrderClosePrice()!=OrderStopLoss()) {
                OrderSend(Symbol(),OP_SELL,lot1,Bid,max_slippage,Bid+stop_loss,Bid-take_profit,NULL,0,0,clrRed);Print ("Order 4 send");};
                Print ("Last error = " , GetLastError() );
   };
   };
   };
   if (OrdersTotal()==0 && first_trade_buy==true){
     OrderSend(Symbol(),OP_BUY,lot1,Ask,max_slippage,Ask-stop_loss,Ask+take_profit,NULL,magic,0,clrBlue);};
   if (OrdersTotal()==0 && first_trade_buy==false){
     OrderSend(Symbol(),OP_SELL,lot1,Bid,max_slippage,Bid-stop_loss,Bid+take_profit,NULL,magic,0,clrRed);};
 } 

 
It was writen "Order 2 send" in journal.
 

Do not rely on OrdersHistory being in chronological order

Always check for the latest by OrderCloseTime.

In fact your code is probably returning the earliest order instead of the latest. So if the earliest is a Buy, only the condition for a if(Buy) will be executed as the earliest order will always be the same.

if (OrderClosePrice()==OrderTakeProfit() && OrderClosePrice()!=OrderStopLoss())

 If TP is hit, there is no guarantee that it will be closed precisely at the TP price. Either allow some margin or maybe it will be sufficient to just check that TradeProfit>0

 

I was searching the TradeProfit but found none. I found OrderProfint - so I used. However I have a problem with the OrderCloseTime. I searched throught the internet and forums and found:

https://forum.mql4.com/47314

OrderCloseTime () >= ArrayMaximum(Closed_time, WHOLE_ARRAY, 0)

and chaned to

OrderCloseTime () >= ArrayMaximum(OrderCloseTime(), WHOLE_ARRAY, 0)

it wrote: "OrderCloseTime" - variable expected.

Then I tried place it under OrderSelect

input bool     MM=true;
input double   lot=0.1;
input int   percent=20;
input double      take_profit=0.0008;
input double      stop_loss=0.0008;
input bool   first_trade_buy=true;
input int   max_slippage=3;
input int magic=111;
double lot1; int index;
double order_close_time;

void init()
{

  };
void OnTick ()
   {
   for (int cn; cn<OrdersHistoryTotal();cn++){
   if(OrderSelect(cn, SELECT_BY_POS, MODE_HISTORY))
   OrderCloseTime()=order_close_time;
   }
   ArraySetAsSeries(order_close_time,TRUE);
   
   
   if (MM==false) lot1 = lot;
   if (MM==true) lot1 = (AccountFreeMargin()*percent*AccountLeverage()/10000000) ;
   
   for(int cnt=0;cnt<OrdersHistoryTotal();cnt++)
   {
     if(OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY ))
     if(OrderSymbol() == Symbol() && OrdersTotal()<=1 && OrderCloseTime() >= ArrayMaximum(OrderCloseTime(),WHOLE_ARRAY,0)){
         if(OrderType()==OP_BUY){

               if (OrderProfit()>=0){
                OrderSend(Symbol(),OP_BUY,lot1,Ask,max_slippage,Ask-stop_loss,Ask+take_profit,NULL,0,0,clrBlue); Print ("Order 1 send");}
             else if (OrderProfit()<0) {
                 OrderSend(Symbol(),OP_SELL,lot1,Bid,max_slippage,Bid+stop_loss,Bid-take_profit,NULL,0,0,clrRed);Print ("Order 2 send");};
                 Print ("Last error = " , GetLastError() );
          }
        else if(OrderType()==OP_SELL){
             if (OrderProfit()>=0) {
                 OrderSend(Symbol(),OP_BUY,lot1,Ask,max_slippage,Ask-stop_loss,Ask+take_profit,NULL,0,0,clrBlue);Print ("Order 3 send");}
             else if (OrderProfit()<0) {
                OrderSend(Symbol(),OP_SELL,lot1,Bid,max_slippage,Bid+stop_loss,Bid-take_profit,NULL,0,0,clrRed);Print ("Order 4 send");};
                Print ("Last error = " , GetLastError() );
   };
   };
   };
   if (OrdersTotal()==0 && first_trade_buy==true){
     OrderSend(Symbol(),OP_BUY,lot1,Ask,max_slippage,Ask-stop_loss,Ask+take_profit,NULL,magic,0,clrBlue);};
   if (OrdersTotal()==0 && first_trade_buy==false){
     OrderSend(Symbol(),OP_SELL,lot1,Bid,max_slippage,Bid-stop_loss,Bid+take_profit,NULL,magic,0,clrRed);};
 } 

It wrote multiple errors. Which part should I change? I really do not know how to continue.


Reason: