Download MetaTrader 5

OrderSend works on market, but not in backtests 134 error

To add comments, please log in or register
baku85
18
baku85  

Hi,

 I have some strange issue while using backtester.

Here is some code sample:

 

double ND(double val)
  {
   return(NormalizeDouble(val, Digits));
  }

void OnTick()
  {
   double price=Ask;
   double minstoplevel = MarketInfo(Symbol(),MODE_STOPLEVEL)+10;
   double stoploss=ND(Ask+minstoplevel*Point);
   double takeprofit=ND(Ask-minstoplevel*3*Point);
   OrderSend(Symbol(),OP_SELL,0.01, Bid,10,0,0,"My order",16384,0,clrGreen);  
  }


 when I add this strategy to market, on each tick new order is coming.

But while running backtests I have errors:

 

2017.01.20 11:02:16.863 2016.08.30 23:59  Tester: PrevBalance: 10000.00, PrevPL: 28.50, PrevEquity 10028.50, PrevMargin: 10057.91, NewMargin: 11142, FreeMargin: -1113.60
2017.01.20 11:02:16.863 2016.08.30 23:59  Tester: PrevBalance: 10000.00, PrevPL: 28.50, PrevEquity 10028.50, PrevMargin: 10057.91, NewMargin: 11142, FreeMargin: -1113.60
2017.01.20 11:02:16.863 2016.08.30 23:59  Tester: not enough money for sell 0.01 EURUSD at 1.11412 sl: 0.00000 tp: 0.00000 [2016.08.30 23:59]

 

can anyone tell me why is that ?

 

Thanks ! 

baku85
18
baku85  
btw. its metatrader 4 running on wine.
Jose Francisco Casado Fernandez
3241
Jose Francisco Casado Fernandez  

You have to check that there is enough free margin to trade your selected volume before sending an order.

Read the documentation, please.

https://docs.mql4.com/account/accountfreemargincheck

Regards.

AccountFreeMarginCheck - Account Information - MQL4 Reference
AccountFreeMarginCheck - Account Information - MQL4 Reference
  • docs.mql4.com
AccountFreeMarginCheck - Account Information - MQL4 Reference
Keith Watford
Moderator
10652
Keith Watford  
2017.01.20 11:02:16.863 2016.08.30 23:59  Tester: not enough money for sell 0.01 EURUSD at 1.11412 sl: 0.00000 tp: 0.00000
What do you expect if opening a new order every tick?
baku85
18
baku85  
Jose Francisco Casado Fernandez:

You have to check that there is enough free margin to trade your selected volume before sending an order.

Read the documentation, please.

https://docs.mql4.com/account/accountfreemargincheck

Regards.

ok thanks :) working code:

 

if (AccountFreeMarginCheck(Symbol(), OP_SELL, 0.01) > 0) {
      OrderSend(Symbol(),OP_SELL,0.01, Bid,10,0,0,"My order",16384,0,clrGreen);  
   }


 

whroeder1
15902
whroeder1  
Jose Francisco Casado Fernandez: You have to check that there is enough free margin to trade your selected volume before sending an order.
Which is sufficient to open but not prevent stop out.
  • You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
  • Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
  • Do NOT use TickValue by itself - DeltaPerLot
  • You must normalize lots properly and check against min and max.
  • You must also check FreeMargin to avoid stop out
Tony Chavez
236
Tony Chavez  
baku85:

Hi,

 I have some strange issue while using backtester.

Here is some code sample:

 

double ND(double val)
  {
   return(NormalizeDouble(val, Digits));
  }

void OnTick()
  {
   double price=Ask;
   double minstoplevel = MarketInfo(Symbol(),MODE_STOPLEVEL)+10;
   double stoploss=ND(Ask+minstoplevel*Point);
   double takeprofit=ND(Ask-minstoplevel*3*Point);
   OrderSend(Symbol(),OP_SELL,0.01, Bid,10,0,0,"My order",16384,0,clrGreen);  
  }


 when I add this strategy to market, on each tick new order is coming.

But while running backtests I have errors:

 

2017.01.20 11:02:16.863 2016.08.30 23:59  Tester: PrevBalance: 10000.00, PrevPL: 28.50, PrevEquity 10028.50, PrevMargin: 10057.91, NewMargin: 11142, FreeMargin: -1113.60
2017.01.20 11:02:16.863 2016.08.30 23:59  Tester: PrevBalance: 10000.00, PrevPL: 28.50, PrevEquity 10028.50, PrevMargin: 10057.91, NewMargin: 11142, FreeMargin: -1113.60
2017.01.20 11:02:16.863 2016.08.30 23:59  Tester: not enough money for sell 0.01 EURUSD at 1.11412 sl: 0.00000 tp: 0.00000 [2016.08.30 23:59]

 

can anyone tell me why is that ?

 

Thanks ! 

I would say you need to add more logic to your ordering.


Here is what I do to keep from getting killed by Margin Calls:


double MarginFree =  AccountInfoDouble(ACCOUNT_MARGIN_FREE);

double EQTY =  AccountInfoDouble(ACCOUNT_EQUITY);

double MarginCheck = (MarginFree/EQTY);

if(SomeOrderRule && MarginCheck > 0.30) { //Make sure you have more than 30% margin left before ordering (This can be adjusted depending on your comfort level)

      if(OrderCount < 6)

           OrderSend(Symbol(),OP_BUY,.10,Ask,3,0,Ask+.025,NULL,NULL,0,clrTeal); }


Jose Francisco Casado Fernandez
3241
Jose Francisco Casado Fernandez  
whroeder1:

Which is sufficient to open but not prevent stop out.

You can't ever prevent a StopOut.

  • 1. Your formula "DeltaPerLot" is variable (TickValue is variable, it changes with the prices movements). Therefore even if you calculate it at the moment of your entry, it doesn't assure that you are save from a StopOut later.
  • 2. You also may open other positions later (manual or with other EA's) which would affect your Margin.
  • 3. You also may change your Stop Loss later, or not to use a Stop Loss, and close your position with anyother rule (an opposite signal, for example).
  • 4. There may be a price gap that causes your Stop Loss not to trigger where you placed it.


It is why I consider it is useless to calculate that at the moment of your entry if your aim is only prevent a StopOut. I think that's only useful if your aim is to get a ROUGH IDEA of what you can lose in that trade, but it will not prevent a Stop Out.


Regards

honest_knave
Moderator
2326
honest_knave  
Tony Chavez:

Here is what I do to keep from getting killed by Margin Calls:


double MarginFree =  AccountInfoDouble(ACCOUNT_MARGIN_FREE);

double EQTY =  AccountInfoDouble(ACCOUNT_EQUITY);

double MarginCheck = (MarginFree/EQTY);

if(SomeOrderRule && MarginCheck > 0.30) { //Make sure you have more than 30% margin left before ordering (This can be adjusted depending on your comfort level)

      if(OrderCount < 6)

           OrderSend(Symbol(),OP_BUY,.10,Ask,3,0,Ask+.025,NULL,NULL,0,clrTeal); }


You're going to get a zero-divide error if this code runs before your terminal has connected e.g. restart.
To add comments, please log in or register