Invalid Stops,can't place an order - page 2

 
Bilal Said:

in the strategy tester ?

Forum on trading, automated trading systems and testing trading strategies

Invalid Stops,can't place an order

fxsaber, 2018.01.20 16:53

2018.01.15 12:00:00   market sell 1.00 Bra50Feb18 sl: 79695 (79595 / 79605 / 79595)
2018.01.15 12:00:00   deal #2 sell 1.00 Bra50Feb18 at 79595 done (based on order #2)
2018.01.15 12:00:00   deal performed [#2 sell 1.00 Bra50Feb18 at 79595]
2018.01.15 12:00:00   order performed sell 1.00 at 79595 [#2 sell 1.00 Bra50Feb18 at 79595]
2018.01.15 12:00:00   OrderSend(_Symbol,OP_SELL,1,Bid,0,Bid+100*_Point,0)
2018.01.15 12:00:00   Request.action = TRADE_ACTION_DEAL (1)
2018.01.15 12:00:00   Request.magic = 0
2018.01.15 12:00:00   Request.order = 0
2018.01.15 12:00:00   Request.symbol = Bra50Feb18
2018.01.15 12:00:00   Request.volume = 1.0
2018.01.15 12:00:00   Request.price = 79595.0
2018.01.15 12:00:00   Request.stoplimit = 0.0
2018.01.15 12:00:00   Request.sl = 79695.0
2018.01.15 12:00:00   Request.tp = 0.0
2018.01.15 12:00:00   Request.deviation = 0
2018.01.15 12:00:00   Request.type = ORDER_TYPE_SELL (1)
2018.01.15 12:00:00   Request.type_filling = ORDER_FILLING_FOK (0)
2018.01.15 12:00:00   Request.type_time = ORDER_TIME_GTC (0)
2018.01.15 12:00:00   Request.expiration = 1970.01.01 00:00:00
2018.01.15 12:00:00   Request.comment = 
2018.01.15 12:00:00   Request.position = 0
2018.01.15 12:00:00   Request.position_by = 0
This is the result of executing this MQL4-style code
#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006

#define Bid SymbolInfoDouble(_Symbol, SYMBOL_BID)

void OnTick()
{
  OrderSend(_Symbol, OP_SELL, 1, Bid, 0, Bid + 100 * _Point, 0);
  
  ExpertRemove();
}


Alternative Code

#define Bid SymbolInfoDouble(_Symbol, SYMBOL_BID)

void OnTick()
{
  MqlTradeRequest Request = {0};

  Request.action = TRADE_ACTION_DEAL;
  Request.symbol = _Symbol;
  Request.volume = 1;
  Request.price = Bid;
  Request.sl = Bid + 100 * _Point;
  Request.type = ORDER_TYPE_SELL;  
  
  MqlTradeResult Result;
  Print(OrderSend(Request, Result));
  
  ExpertRemove();
}
 
fxsaber:
This is the result of executing this MQL4-style code


Alternative Code

but this is doing an instant sell alright ?

i need to put a pending order


When using your code,the EA open position,but i need a pending order and when using pending order that i have this problem with invalid stops.

 
Bilal Said:

i need to put a pending order

Read this please

Forum on trading, automated trading systems and testing trading strategies

Invalid Stops,can't place an order

fxsaber, 2018.01.20 10:50

Tester's EA

#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006

#define TOSTRING(A)  #A + " = " + (string)(A) + "\n"
#define TOSTRING2(A) #A + " = " + EnumToString(A) + " (" + (string)(A) + ")\n"

string ToString( const MqlTradeRequest &Request )
{
  return(TOSTRING2(Request.action) + TOSTRING(Request.magic) + TOSTRING(Request.order) +
         TOSTRING(Request.symbol) + TOSTRING(Request.volume) + TOSTRING(Request.price) +
         TOSTRING(Request.stoplimit) + TOSTRING(Request.sl) +  TOSTRING(Request.tp) +
         TOSTRING(Request.deviation) + TOSTRING2(Request.type) + TOSTRING2(Request.type_filling) +
         TOSTRING2(Request.type_time) + TOSTRING(Request.expiration) + TOSTRING(Request.comment) +
         TOSTRING(Request.position) + TOSTRING(Request.position_by));
}

string ToString( const MqlTradeResult &Result )
{
  return(TOSTRING(Result.retcode) + TOSTRING(Result.deal) + TOSTRING(Result.order) +
         TOSTRING(Result.volume) + TOSTRING(Result.price) + TOSTRING(Result.bid) +
         TOSTRING(Result.ask) + TOSTRING(Result.comment) + TOSTRING(Result.request_id) +
         TOSTRING(Result.retcode_external));
}

#define Bid SymbolInfoDouble(_Symbol, SYMBOL_BID)
#define Ask SymbolInfoDouble(_Symbol, SYMBOL_ASK)

#define PRINT(A) A; Print(#A + "\n" + ToString(MT4ORDERS::LastTradeRequest) + ToString(MT4ORDERS::LastTradeResult));

void OnTick()
{
  PRINT(OrderSend(_Symbol, OP_BUYLIMIT, 1, Ask - 100 * _Point, 0, Ask - 200 * _Point, 0))
  
  ExpertRemove();
}

Result

Bra50Feb18: history synchronization started
Bra50Feb18: load 35 bytes of history data to synchronize in 0:00:00.000
Bra50Feb18: history synchronized from 2017.01.02 to 2018.01.19
Bra50Feb18,M1: history cache allocated for 394971 bars and contains 90702 bars from 2017.01.02 12:00 to 2018.01.12 21:09
Bra50Feb18,M1: history begins from 2017.01.02 12:00
Bra50Feb18,M1 (ActivTrades-Server): 1 minutes OHLC ticks generating
Bra50Feb18,M1: testing of Experts\Test2.ex5 from 2018.01.15 00:00 to 2018.01.20 00:00 started
USDBRL: history synchronization started
USDBRL: load 27 bytes of history data to synchronize in 0:00:00.093
USDBRL: history synchronized from 2017.01.02 to 2018.01.19
2018.01.15 12:00:00   buy limit 1.00 Bra50Feb18 at 79505 sl: 79405 (79595 / 79605 / 79595)
2018.01.15 12:00:00   OrderSend(_Symbol,OP_BUYLIMIT,1,Ask-100*_Point,0,Ask-200*_Point,0)
2018.01.15 12:00:00   Request.action = TRADE_ACTION_PENDING (5)
2018.01.15 12:00:00   Request.magic = 0
2018.01.15 12:00:00   Request.order = 0
2018.01.15 12:00:00   Request.symbol = Bra50Feb18
2018.01.15 12:00:00   Request.volume = 1.0
2018.01.15 12:00:00   Request.price = 79505.0
2018.01.15 12:00:00   Request.stoplimit = 0.0
2018.01.15 12:00:00   Request.sl = 79405.0
2018.01.15 12:00:00   Request.tp = 0.0
2018.01.15 12:00:00   Request.deviation = 0
2018.01.15 12:00:00   Request.type = ORDER_TYPE_BUY_LIMIT (2)
2018.01.15 12:00:00   Request.type_filling = ORDER_FILLING_FOK (0)
2018.01.15 12:00:00   Request.type_time = ORDER_TIME_GTC (0)
2018.01.15 12:00:00   Request.expiration = 1970.01.01 00:00:00
2018.01.15 12:00:00   Request.comment = 
2018.01.15 12:00:00   Request.position = 0
2018.01.15 12:00:00   Request.position_by = 0
2018.01.15 12:00:00   Result.retcode = 10009
2018.01.15 12:00:00   Result.deal = 0
2018.01.15 12:00:00   Result.order = 2
2018.01.15 12:00:00   Result.volume = 1.0
2018.01.15 12:00:00   Result.price = 0.0
2018.01.15 12:00:00   Result.bid = 79595.0
2018.01.15 12:00:00   Result.ask = 79605.0
2018.01.15 12:00:00   Result.comment = Request executed
2018.01.15 12:00:00   Result.request_id = 0
2018.01.15 12:00:00   Result.retcode_external = 0

MQL4-style

#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006

#define Ask SymbolInfoDouble(_Symbol, SYMBOL_ASK)

void OnTick()
{
  OrderSend(_Symbol, OP_BUYLIMIT, 1, Ask - 100 * _Point, 0, Ask - 200 * _Point, 0);
  
  ExpertRemove();
}


Alternative code

#define Ask SymbolInfoDouble(_Symbol, SYMBOL_ASK)

void OnTick()
{
  MqlTradeRequest Request = {0};

  Request.action = TRADE_ACTION_PENDING;
  Request.symbol = _Symbol;
  Request.volume = 1;
  Request.price = Ask - 100 * _Point;
  Request.sl = Request.price - 100 * _Point;
  Request.type = ORDER_TYPE_BUY_LIMIT;  
  
  MqlTradeResult Result;
  Print(OrderSend(Request, Result));
  
  ExpertRemove();
}
 
fxsaber:

Read this please


MQL4-style


Alternative code


The alternative Code works to put pending order BUY,but when i change it to sell,i have errors:

MqlTradeRequest Request = {0};

  Request.action = TRADE_ACTION_PENDING;
  Request.symbol = _Symbol;
  Request.volume = 1;
  Request.price = Ask - 100 * _Point;
  Request.sl = Request.price + 200 * _Point;
  Request.type = ORDER_TYPE_SELL;  
  
  MqlTradeResult Result;
  Print(OrderSend(Request, Result));

getting 4756 error 

 
Bilal Said:

The alternative Code works to put pending order BUY,but when i change it to sell,i have errors:

getting 4756 error 

#define Bid SymbolInfoDouble(_Symbol, SYMBOL_BID)

void OnTick()
{
  MqlTradeRequest Request = {0};

  Request.action = TRADE_ACTION_PENDING;
  Request.symbol = _Symbol;
  Request.volume = 1;
  Request.price = Bid + 100 * _Point;
  Request.sl = Request.price + 100 * _Point;
  Request.type = ORDER_TYPE_SELL_LIMIT;  
  
  MqlTradeResult Result;
  Print(OrderSend(Request, Result));
  
  ExpertRemove();
}

Good Luck!

 
fxsaber:

Good Luck!


now it's selling ! but i'm having a issue with stop loss that is blowing my mind .


if i put like your code :

  Request.sl = Request.price + 100 * _Point;

Works !

if i put :

  Request.sl = Request.price + 800 * _Point;

Works !


if i put :

  Request.sl = NormalizeDouble(stop_loss,_Digits) * _Point; // that return Request.price + ~400 points

doesn't work.


See the image please :


Now,using this,i get this image :

  Request.sl = Request.price + 300 * _Point;


if i use this :

 Request.sl = NormalizeDouble(stop_loss,_Digits) * _Point; 

i get :

the value of stop_loss  = 79406


What is wrong when i use the stop loss var ?


I think we are close to fix it ! Thanks for helping me !

 
Bilal Said:

The alternative Code works to put pending order BUY,but when i change it to sell,i have errors:

getting 4756 error 

ORDER_TYPE_SELL

That's not a pending order.

  Request.price = Bid + 100 * _Point;
  Request.sl = Request.price + 100 * _Point;

That's not a correct way to set price/sl, you need to normalize to tick size, not to points. 

On Bra50Feb18 prices vary by 5 points (1 tick).


 

Forum on trading, automated trading systems and testing trading strategies

Need help to round order last digit to 0 or 5

Alain Verleyen, 2016.10.23 18:47

Normalize your price like that :

//+------------------------------------------------------------------+
//| Normalize price according to tick size                           |
//+------------------------------------------------------------------+
double normalizePrice(double price)
  {
   double tickSize=SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
   return(MathRound(price/tickSize)*tickSize);
  }

 
Alain Verleyen:

That's not a pending order.

That's not a correct way to set price/sl, you need to normalize to tick size, not to points. 

On Bra50Feb18 prices vary by 5 points (1 tick).



WOW,After 1 day trying to solve the problem !


Thank you Very much Alain and fxsaber !!!!!!!!!!!!!!!!

 
Bilal Said:

WOW,After 1 day trying to solve the problem !


Thank you Very much Alain and fxsaber !!!!!!!!!!!!!!!!

As a side note, in general, you also have to check the stops level (15 currently for this symbol).

Reason: