Invalid ticket OrderClose error 4108 & invalid stoploss for OrderModoify

 

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();
     }
   
 } 
 

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.

 
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.

 
So i need to add RefreshRates after the Bid & RefreshRates after ask? to answer your question?
 
I've sorted out my problem thank you :)
 
Jack Buda:
I've sorted out my problem thank you :)

How did you solve it?

 
  1. Jack Buda: . I'm trying to open a order, modify it with 1 stoploss=100 but loop it. 

    If you select an order you continue, so eventually you exit the loop and you do nothing.

    Jack Buda: I've sorted out my problem thank you :)

    Don't do that. Someone searching might find this thread and still be clueless. What solved what?

    How To Ask Questions The Smart Way. 2004
         When You Ask.
              Follow up with a brief note on the solution.

  2.  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

    You buy at the Ask and sell at the Bid.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using the Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spread widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134.

Reason: