Tester incorrectly generates error 134

 
I had a typo in my EA that resulted in opening many orders, so the margin exhaustion is expected. But since the code does check margin, the 134 was not.
RefreshRates();
...
    /* However, the broker doesn't care about the at.risk/account balance. They
     * care about margin. Margin used=lots used*marginPerLot and that must be
     * less than free margin available. */
            marginFree      = AccountFreeMargin()*0.95, // Allow some slack
            marginPerLot    = MarketInfo( Symbol(), MODE_MARGINREQUIRED ),
    // So I use, the lesser of either.
            size = MathMin(marginFree / marginPerLot, at.risk / maxLossPerLot);
...
    double  minLot  = MarketInfo(Symbol(), MODE_MINLOT);
Print(
"mf=",marginFree," mpl=",marginPerLot," lm=",marginFree/marginPerLot," ls=",size," min=",minLot
);
...
   double  LotStep     = MarketInfo(Symbol(), MODE_LOTSTEP);
   size =  MathMax( minLot, MathFloor(size*adjFact/LotStep)*LotStep )
The journal displays:

EURUSD,M1: OrderSend error 134
Tester: PrevBalance: 9455.32, PrevPL: 2958.73, PrevEquity 12414.05, PrevMargin: 12015.81, NewMargin: 12417, FreeMargin: -3.41
Tester: not enough money for buy 0.30 EURUSD at 1.23443
EURUSD,M1: mf=378.3239 mpl=1234.34 lm=0.3065 ls=0.3065 min=0.01


The output from the Print shows free margin was $378 and generated a maximum lot size of 0.30. But the tester doesn't agree with MarketInfo.


At the end of the routine, I added:

size =  MathMax( minLot, MathFloor(size*adjFact/LotStep)*LotStep );
double AFMC=AccountFreeMarginCheck(Symbol(), op.code, size);
Print("afmc=",AFMC);
and AFMC matches the tester's FreeMargin output.
 

Hey,

I am having the same problem as well. It seems that my code is being trigged and buying massive amounts of units over and over again on the same candle stick instead of being triggered by the trend happening within the candle stick. Since I am new to this there must be something I am missing. If I only want 1 indication of a buy/sell within a candle stick how do I properly exit out of the code once a signal has been triggered in the evaluation candle stick? I am currently using the Bars() as my counter which from my understand is counting the bars within a graph and not the ticks? But when I test this I dont think this is the case. Am I on the right track here?

 
No such thing as Bars(). Bars is unreliable, once you reach max bars on chart it won't change. Volume is unreliable, you can miss ticks. Always use time:
int start(){ static datetime Time0;
   if (Time0 == Time[0]) return; Time0 = Time[0];
   ...
My code
 
WHRoeder:
No such thing as Bars(). Bars is unreliable, once you reach max bars on chart it won't change. Volume is unreliable, you can miss ticks. Always use time: My code

Thanks the Answer on the other thread. I had this same issue and I traced it back to a error inside a Program at the dealing  desk. They Miss reported Server is busy.... At least that is my feeling. When a Order is sent the dealing desk for x amount of time they  lock out all other orders to prevent atomic conditions. When did this they display all the not enough money. They should say we are busy. Anyway  They lock out the funds and report out of money. This causes me to lower lots size and ALERT the USERS NOW.! .... It was really irritating ...... The Software. I was dealing with was in between Metatrader and the dealing desk and is called a MAT multi  access terminal ..it is for CTA's and CPO's so several access point had hit the dealings desk multiplexers. They place mat in front of the multiplexer. 
 

Use a mutex around your logic so that you get the lotsize calculation through opened order result. When you calculated the lotsize for the second order, you were using stale values because you were already in the process of opening the first order.

Server is busy only occurs when you send the orders to the server at the same time.

Reason: