Position size code (formula) is not working on indexs?

 

Here is what I am using (see below).

Now keep in mind this works on metals and FX markets...? During back testing these are the parameters:

2014.06.11 22:53:35.016 2008.09.25 10:00  Trend Fishing XAUUSD UK100,H1:  Account # 16384 -- leverage is 1:100 -- Account currency is USD -- Minium Lots are: 0.1 -- Min Lot Step is: 0.1 -- Tick Size is: 1 -- Tick Value is: 0.1

 

Anyone help me adjust where I am going wrong? I am trying to back-test my strategy on FTSE (UK100 - FXCM) but position size calculation is evidently wrong but I cannot figure it out? 

//+------------------------------------------------------------------+
//| Order Enter Function                                             |
//+------------------------------------------------------------------+
void OrderEntry(int direction)
{
   //Padding for the stop and padding for the entry too. 
   double ATR_Pad = iATR(NULL,60,14,1)/2; 
   double Buy_Pad = NormalizeDouble(ATR_Pad,Digits);
   double Sell_Pad = NormalizeDouble(ATR_Pad,Digits);
   
   //Stop calculations.    
   double ATR = iATR(NULL,60,14,1);
   double MA = iMA(NULL,60,MA_Period,0,1,0,1);
  
   //Lot calculation 
   double risk_amount = AccountBalance( )*RiskPercent/100;
   double Lot_Step = MarketInfo(Symbol(), MODE_LOTSTEP);
   double ts = MarketInfo(Symbol(), MODE_TICKSIZE);
   double tv = MarketInfo(Symbol(), MODE_TICKVALUE);
   double minlot = MarketInfo(Symbol(), MODE_MINLOT);
   
         
//+-------------------------------------------------------------------------------------+
//| Order Buy Function                                                                  |
//+-------------------------------------------------------------------------------------+   

//Place a pending buystop if no orders exists. Pending or otherwise.
if(direction==0)
{ 
      

      double buyPrice = NormalizeDouble(Ask,Digits);
            
      double BuyStopPriceMath = (MA - ATR); // < Stop loss is the low of the previous bars close minus 14atr.
      double BuyStopPrice = NormalizeDouble(BuyStopPriceMath,Digits);

      //get our buystop price from below the ma and our takeprofit based on our r:r ratio.
      double pips_to_bsl = buyPrice - BuyStopPrice;
      double buy_tp_price = (pips_to_bsl*RewardRatio)+buyPrice;
      double buy_takeprofit_price= NormalizeDouble(buy_tp_price, Digits);
      
      double loss_for_1_lot = pips_to_bsl / ts * tv ;
      double LotSize_Buy = MathFloor( risk_amount / loss_for_1_lot / Lot_Step) * Lot_Step ;

      double btp = buy_takeprofit_price;

...
 
May not be anything to do with it, but you don't check that your calculated lot size is >= to the minimum lot size allowed.
 

I have found that tickvalue is wrong in some instruments in some brokers:

27278115

2014.06.06 12:50:10

buy

0.01

xauusdf

1253.85

0.00

1254.85

2014.06.06 15:20:53

1254.85

0.00

0.00

0.00

1.00

 

Lots 0.01

Ticks 100

Profit 1

 

Do the maths:

1/100/0.01=1

 

Therefore real

MarketInfo(Symbol(),MODE_TICKVALUE) is 1

 

But the system gives

MarketInfo(Symbol(),MODE_TICKVALUE)=0.01

 

So it is wrong.

 
DomGilberto: help me adjust where I am going wrong?
  1. Don't use NormalizeDouble
  2. Normalize LotsSize and check the limits
  3. Do not use TICKVALUE by itself.
  4. What are Function return values ? How do I use them ? - MQL4 forum
 

Thanks everyone for your inputs.

 I am getting 4107 errors (invalid price) and 131 (Invalid trade volume).

 My little brain can't understand how it's invalid price...? Trade volume; well, we already knew that lol :P - Maths was never my strong point! 

 

I've tested price and it seems fine? 

2014.06.12 21:28:42.397 2009.05.11 04:00  TF-index UK100,H1:  buyPrice is: 4524

 (EDIT)

Ok, it's because I was trying to exceed max lots as I put too many 000's on the size of the starting balance lol.

 It is pushing trades out but I am still receiving error 4107?

2014.06.12 21:34:46.946 2008.11.24 10:00  TF-index UK100,H1: First Sell Order FAILED: 4107 On: UK100
2014.06.12 21:34:46.946 2008.11.24 10:00  TF-index UK100,H1: OrderSend error 4107
2014.06.12 21:34:46.946 2008.11.24 10:00  TF-index UK100,H1: invalid takeprofit for OrderSend function
2014.06.12 21:34:46.946 2008.11.24 10:00  TF-index UK100,H1:  LotSize_Sell is: 5.4
2014.06.12 21:34:46.946 2008.11.24 10:00  TF-index UK100,H1:  sellPrice is: 3706
 

Ok I think i have sorted it.

 My takeprofit was a multiple of the distance from Entry - Stop price... this multiple was set to 20x which in short positions equated to something less than 0!

 So all errors are now gone and it works!

Thanks for everyones input none the less :) - it definitely provoked me to put some logic into what I am doing! 

 

Hi, could you please tell me how did you manage to get the position size to work on all instruments ?

Or how you made it work on indices ?

Thank you!

Reason: