Invalid ticket OrderClose error 4108 & invalid stoploss for OrderModoify

To add comments, please log in or register
Jack Buda
208
Jack Buda  

Hi there, I have minor experience in MQL4, i have encountered a 2 problems with Invalid ticket Order Close error 4108 & invalid stop loss for OrderModoify but for BUY ORDER. Below i have posted the code & picture for the  If anyone can help, your help will be appreciated a lot.

extern int StopLoss=100;
extern double Profit=1;
extern double Slippage=2;
extern bool Symbols=true;
//extern int ProfitTarget=1;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart() 
 {
   for(int Loop=-1;Loop<100;Loop++)
   { 
    if (Digits==3||Digits==5)
     {
      Slippage=Slippage*10;
     }
     
    if (OrderSelect(Loop,SELECT_BY_POS,MODE_TRADES))continue;
    if (OrderType()==OP_BUY&&OrderType()==OP_SELL)continue;
    if (Symbols&&OrderSymbol()==Symbol())continue;   
    
    double StopLossBid,StopLossAsk;
    StopLossBid=Bid+StopLoss*Point;
    StopLossAsk=Ask-StopLoss*Point;
                                                                                                               
    int OpenBuy=OrderSend(Symbol(),OP_BUY,0.1,Ask,0,0,0,":)",0,0,clrBlue);
    if (OpenBuy>0)
    {
     bool ModBuy=OrderModify(OpenBuy,OrderOpenPrice(),OrderOpenPrice()-StopLossAsk,0,0,clrNONE);//
    }
    
    int OpenSell=OrderSend(Symbol(),OP_SELL,0.1,Bid,0,0,0,":)",0,0,clrRed);
    if (OpenSell>0)
    {
     bool ModSell=OrderModify(OpenSell,OrderOpenPrice(),OrderOpenPrice()+StopLossBid,0,0,clrNONE);
    }
         
    for(int Loop2=OrdersTotal()-1;Loop2>=0;Loop2--)
    {
     if (OrderSelect(Loop2,SELECT_BY_POS))
     bool CloseOrder=false;
     int Type=OrderType();
     switch(Type)
      {          
       case OP_BUY:if(OrderProfit()>=Profit) CloseOrder=OrderClose(OpenBuy,0.1,MarketInfo(Symbol(),MODE_BID),0,clrGreen);break;
       
       case OP_SELL:if(OrderProfit()>=Profit) CloseOrder=OrderClose(OpenSell,0.1,MarketInfo(Symbol(),MODE_ASK),0,clrGreen);break;
      } 
     }
      Sleep(10000);
      RefreshRates();
     }
   
 } 
Keith Watford
Moderator
21754
Keith Watford  

Why all the continues?

If the order is selected then the loop jumps to next iteration so the other continues will not do anything.

It is not clear what you are trying to do.

Seems like if there are less than 100 trades open you will try to open more.

You sleep for 10 seconds between each iteration so if you are using Bid and Ask, you must RefreshRates().

In the second loop, you check if the order's profit is >= Profit yet you don't try to close that order, you try to close the order with the ticket# OpenBuy or OpenSell.

Jack Buda
208
Jack Buda  
Keith Watford:

Why all the continues?

If the order is selected then the loop jumps to next iteration so the other continues will not do anything.

It is not clear what you are trying to do.

Seems like if there are less than 100 trades open you will try to open more.

You sleep for 10 seconds between each iteration so if you are using Bid and Ask, you must RefreshRates().

In the second loop, you check if the order's profit is >= Profit yet you don't try to close that order, you try to close the order with the ticket# OpenBuy or OpenSell.

1. I'm trying to open a order, modify it with 1 stoploss=100 but loop it. The first loop i tried adding for(int Loop=0;Loop<OrdersTotal();Loop++) it does nothing when it comes to send an order.

2. The OrderBuy is giving me invalid stoploss for OrderModify.

3. I want to close my trade in profit, hence OrderProfit>=Profit.

Jack Buda
208
Jack Buda  
So i need to add RefreshRates after the Bid & RefreshRates after ask? to answer your question?
Jack Buda
208
Jack Buda  
I've sorted out my problem thank you :)
To add comments, please log in or register