script to close orders once and only when new bar detected - page 2

 
deVries:  and why not do ...

MAKE    VALUE = 1    4 digit 

For 5 digit notation brokers VALUE = 10

Why not make it auto adjust. TP, SL, AND slippage
//++++ These are adjusted for 5 digit brokers.
int      pips2points;                  // slippage  3 pips  3=points 30=points
double   pips2dbl;                     // Stoploss 15 pips  0.0015   0.00150
int      Digits.pips;                  // DoubleToStr(dbl/pips2dbl, Digits.pips)
int      init(){                                            OptInitialization();
    if(Digits % 2 == 1){   // DE30=1/JPY=3/EURUSD=5 https://www.mql5.com/en/forum/135345
            pips2dbl = Point*10;    pips2points = 10;    Digits.pips = 1;
   } else { pips2dbl = Point;       pips2points =  1;    Digits.pips = 0;     }
   // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
//---- These are adjusted for 5 digit brokers.
   //{On ECN brokers you must open first and THEN set stops
   // int      ticket = OrderSend(..., 0,0,...)
   // if(ticket < 0)
   //    Alert("OrderSend failed: ", GetLastError());
   // else  if(!OrderSelect(ticket, SELECT_BY_TICKET))
   //    Alert("OrderSelect failed: ", GetLastError());
   // else  if(!OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0))
   //    Alert("OrderModify failed: ", GetLastError());
   //}
 
philtrader:
static int old_bars = 0; // remember the amount of bars already known 
if (old_bars != Bars) // if a new bar is received 
{
  1. Bars is unreliable. Once Bars exceeds Max Bars On chart it can be reduced back.
  2. Volume is unreliable. You can miss opening ticks.
  3. Always use time.
 
WHRoeder:
Why not make it auto adjust. TP, SL, AND slippage


You say it better William

ofcours this has to be auto adjust

I was trying to point out that this line

OrderClose(ticket,lots,Ask,3);

 "Ask"   and   "3"  was not chosen well to close every trade

 
WHRoeder:
  1. Bars is unreliable. Once Bars exceeds Max Bars On chart it can be reduced back.
  2. Volume is unreliable. You can miss opening ticks.
  3. Always use time.

datetime old_bar = 0;

int start()
{
  
  while(true) 
  { 
   
  
   if (Time[0] != old_bar)
See Post RaptorUK  the code is already using time  not Bars
 
philtrader:

No, they are just buy / sell orders executed at market. Could it be something with my charts or options settings ?
You can't close a Buy order at Ask . . .   if you want to be able to close Buy and Sell orders then use OrderClosePrice() instead of Ask . . .  or distinguish between Buys and Sells.
 
RaptorUK:
You can't close a Buy order at Ask . . .   if you want to be able to close Buy and Sell orders then use OrderClosePrice() instead of Ask . . .  or distinguish between Buys and Sells.

And when you have done that add code to check the return value from your OrderClose() and report the errors . . . 

read this:   What are Function return values ? How do I use them ?

 
deVries:
datetime old_bar = 0;

int start()
{
  
  while(true) 
  { 
   if (Time[0] != old_bar)
See Post RaptorUK  the code is already using time  not Bars
  1. This won't work at Time[0] will NEVER change - No RefreshRates() after Sleep();
  2. The loop is unnecessary. Just return. On the next tick start() will be called again.
 
WHRoeder:
  1. This won't work at Time[0] will NEVER change - No RefreshRates() after Sleep();
  2. The loop is unnecessary. Just return. On the next tick start() will be called again.

1.  good point

2.  it's a Script. 

Reason: