automatic lot-size calculating function? - page 5

 

Hi Phillip, Shawn here back again. I made the minor changes that we discussed last time (I still didn't implement your latest version however) and the problems with lotsizes of JPY pairs are still cropping up. I let my EA run last night and it generated 3 trades (my account balance was US$2550, risk is 1%, so about $25 risk per trade):


(1) Sell NZDJPY, 0.89 lots

(2) Sell AUDJPY, 0.23 lots

(3) Sell CADJPY, 0.94 lots


The lotsize for the AUDJPY trade looks okay, but not the other two. I had some Print statements embedded around the calls to your stuff. First, here is the code of my EA for this part:


if(Bid > SellPrice && SellPrice != 0.00)
         {       
          CurrentOrderType=OP_SELL;
          CurrentStopLossPrice= FinalSellStopPrice;
          CurrentEquityAtRisk=(MaxPercentEquityAtRisk/100.0)*AccountBalance();
          CurrentLotSize=LotSize(CurrentEquityAtRisk,CurrentStopLossPrice,CurrentOrderType,CurrentSymbolType,CurrentCounterPairForCross);
          Print("SELL - ", Symbol(), "AccountBalance = ", DoubleToStr(AccountBalance(),2), " Max EquityAtRisk = $",DoubleToStr(CurrentEquityAtRisk,2)," and Max Lotsize = ",DoubleToStr(CurrentLotSize,4));
          CurrentLotSize=NormalizeLotSize(CurrentLotSize);
          Print("SELL - ", Symbol(), "Normalized LotSize is now = ", DoubleToStr(CurrentLotSize,4));
          if(CurrentLotSize<MarketInfo(Symbol(),MODE_MINLOT)) CurrentLotSize=MarketInfo(Symbol(),MODE_MINLOT);
          if(CurrentLotSize>MarketInfo(Symbol(),MODE_MAXLOT)) CurrentLotSize=MarketInfo(Symbol(),MODE_MAXLOT);
          CurrentEquityAtRisk=EquityAtRisk(CurrentLotSize,CurrentStopLossPrice,CurrentOrderType,CurrentSymbolType,CurrentCounterPairForCross);
          Print("SELL - ", Symbol(), "Current EquityAtRisk = $",DoubleToStr(CurrentEquityAtRisk,2)," and Current Lotsize = ",DoubleToStr(CurrentLotSize,4));
          Print("SELL - ", Symbol(), " MarketInfo(MODE_STOPLEVEL) = ",DoubleToStr(MarketInfo(Symbol(),MODE_STOPLEVEL),5));
          
          //Check free margin
          if (AccountFreeMargin() < (1000 * CurrentLotSize)) 
          {
             Print("We have no money. Free Margin = ", AccountFreeMargin());
             return(0);
          }
         
          Ticket=OrderSendReliable2Step(Symbol(),OP_SELLSTOP,CurrentLotSize,FinalSellPrice,Slippage,CurrentStopLossPrice,FinalSellProfTarg,Symbol()+DoubleToStr(MagicNumber,0),MagicNumber,0,Red);
          if(Ticket<=0) Print("Error = ",GetLastError());
          else 
          { 


And here is the output of the Print statements taken from the log, for each of the 3 trades above:


05:16:10 DelphiEA NZDJPY,H1: SELL - NZDJPYAccountBalance = 2550.90 Max EquityAtRisk = $25.51 and Max Lotsize = 0.8988
05:16:10 DelphiEA NZDJPY,H1: SELL - NZDJPYNormalized LotSize is now = 0.8900
05:16:10 DelphiEA NZDJPY,H1: SELL - NZDJPYCurrent EquityAtRisk = $25.26 and Current Lotsize = 0.8900
05:16:10 DelphiEA NZDJPY,H1: SELL - NZDJPY MarketInfo(MODE_STOPLEVEL) = 70.00000


05:16:56 DelphiEA AUDJPY,H1: SELL - AUDJPYAccountBalance = 2550.90 Max EquityAtRisk = $25.51 and Max Lotsize = 0.2376
05:16:56 DelphiEA AUDJPY,H1: SELL - AUDJPYNormalized LotSize is now = 0.2300
05:16:56 DelphiEA AUDJPY,H1: SELL - AUDJPYCurrent EquityAtRisk = $24.69 and Current Lotsize = 0.2300
05:16:56 DelphiEA AUDJPY,H1: SELL - AUDJPY MarketInfo(MODE_STOPLEVEL) = 50.00000


07:13:53 DelphiEA CADJPY,H1: SELL - CADJPYAccountBalance = 2550.90 Max EquityAtRisk = $25.51 and Max Lotsize = 0.9408
07:13:53 DelphiEA CADJPY,H1: SELL - CADJPYNormalized LotSize is now = 0.9400
07:13:53 DelphiEA CADJPY,H1: SELL - CADJPYCurrent EquityAtRisk = $25.49 and Current Lotsize = 0.9400

07:13:53 DelphiEA CADJPY,H1: SELL - CADJPY MarketInfo(MODE_STOPLEVEL) = 60.00000


Hope this helps!!

Oh Phillip, one other thing - I think I've found a wee bug in OrderSendReliable2Step. Would you know who I should report that to?


Thanks!

Shawn

 
Ricotter:

This guy does it using file history.

https://www.mql5.com/en/articles/1493

Since you are new here, it's worth mentioning that this limitation does not exist in MT5 Tester. FYI.

 
gordon:

Since you are new here, it's worth mentioning that this limitation does not exist in MT5 Tester. FYI.

Thanks for the tip. Is there any reason I should NOT be using MT5?

 
Ricotter:

Thanks for the tip. Is there any reason I should NOT be using MT5?

If you want to trade Live immediately then u are better off sticking to MT4, but if u plan on learning and doing Demo's for some months before going Live, then u might consider it. It's a new platform with many improvements over MT4, but it's also much more complicated and still not supported by many brokers. Here's some good info -> http://www.100forexbrokers.com/mt5-metatrader5. Also, u might want to check out MQL5.com.
 
shawnh:

Hi Phillip, Shawn here back again. I made the minor changes that we discussed last time (I still didn't implement your latest version however) and the problems with lotsizes of JPY pairs are still cropping up.

Everything "looks" ok in your code, still no idea why it is happening to you.

I recommend changing to the newer code. At this point you need to change something since whatever you've got now isn't working correctly.

I don't know who you should report the bugs to in regards to orderlib issues, sorry.
 
1005phillip:

Step 1: Place all the file attachments from this post into your include path (...\experts\include\*.mqh)

Step 2: Add the following to the top of your EA so it has access to the call functions contained in the attached files

Step 3: To compute the lotsize based on a budgeted amount of equity to risk add the following

Assuming you have defined MaxPercentEquityAtRisk somewhere in your EA to be the max allowed equity to put at risk of complete loss per trade in the event the stops are hit, this portion of code will first determine the maximum lotsize based on openprice and stoplossprice (not pips but actual market price, same thing you send in your order to the broker) and then it will determine the maximum position size that the broker will accept while not exceeding your budgeted risk equity.

Step 4: If you like having the results of the calculations printed in your log or added to the trade as an order comment you can also add the following

Step 5: Place your order (using the ordersendreliable method)

https://c.mql5.com/mql4/forum/2010/10/OrderReliable_2010.10.12.mqh


Hi 1005phillip,


Do you have a version of the 2 mqh (analyze..., and tradepositionmanage...) that has no imports of the below? This is because the two both have that imports, and is therefore not allowed by compiler.

//#include <stderror.mqh>
//#include <stdlib.mqh>


...so that I wont edit the files and keep them in read-only state. Or can I edit them and remove those imports?

 
jcadong5:

...so that I wont edit the files and keep them in read-only state. Or can I edit them and remove those imports?


Go ahead and edit them to your liking. The duplicity does not prevent compiling for me, the compiler logs a note regarding the duplicity but that is all it does.
 
Thanks.
 
1005phillip:
Everything "looks" ok in your code, still no idea why it is happening to you.

I recommend changing to the newer code. At this point you need to change something since whatever you've got now isn't working correctly.

I don't know who you should report the bugs to in regards to orderlib issues, sorry.


Hi there Phillip, Shawn here again. After having installed your latest version a few weeks ago, I'm happy to report that all appears well now and the calculated lot sizes appear bang-on. Guess I just needed the latest version!


Thanks again for all your help, you're a good man!


Cheers

Shawn

 

My pleasure! And thanks for letting me know it's working for you, I appreciate the feedback. Not sure what went awry with the earlier code. May well have been a bug of some sort that I unwittingly corrected between revisions.

I'll probably have an updated version within a week or two that will have some fallback features for use with backtesting in MT4 on cross-currency pairs. Right now if you try and backtest a cross-pair while using my lot-size code your backtest will bomb because the backtester can't access the market prices for the requisite counter and base pairs. (this isn't an issue in live or forward testing, purely a backtester limitation of MT4)

I'll post up the code when I cross that bridge perchance you or others find it helpful.

Have a good weekend,
- Phil

Reason: