Custom lot calculation vs AccountFreeMarginCheck()

General discussion about the Tickstory Lite software package.
Post Reply
kentzversky
Posts: 4
Joined: Tue Apr 14, 2015 2:49 pm

Custom lot calculation vs AccountFreeMarginCheck()

Post by kentzversky »

Hi, first of all thanks for providing us such a powerful tool for free!

My problem is that following lot calculation check code:

Code: Select all

// lot calc for buy:
   onelotprice=Ask*(MarketInfo("EURUSD",MODE_LOTSIZE)/leverage);
   double volume, moneytospend; 
   moneytospend=AccountFreeMargin()*lot_mult;
   volume=MathFloor(   (moneytospend/onelotprice)  *100)*0.01;
               Print(mk+mk+"CustomMarginCheck (Single order):", DoubleToStr(AccountFreeMargin()-volume*onelotprice,22));
               Print(mk+mk+"MODE_MARGINREQUIRED (Single order):", DoubleToStr(AccountFreeMargin()-volume*MarketInfo(Symbol(),MODE_MARGINREQUIRED),22));
               Print(mk+mk+"AccountFreeMarginCheck(OP_BUY):        ", DoubleToStr(AccountFreeMarginCheck(Symbol(),OP_BUY,volume),22));
returns differrent results
from tester on REAL account:

Code: Select all

0	16:42:58	2015.01.29 09:14  jumper_72.6 EURUSD,M1: --- --- NO open orders!
0	16:42:58	2015.01.29 09:14  jumper_72.6 EURUSD,M1: --- Lot calculations: 
0	16:42:58	2015.01.29 09:14  jumper_72.6 EURUSD,M1: --- --- AccountLeverage: 500.00000000
0	16:42:58	2015.01.29 09:14  jumper_72.6 EURUSD,M1: --- --- AccountBalance: 100.00000000
0	16:42:58	2015.01.29 09:14  jumper_72.6 EURUSD,M1: --- --- AccountEquity: 100.00000000
0	16:42:58	2015.01.29 09:14  jumper_72.6 EURUSD,M1: --- --- AccountFreeMargin: 100.00000000
0	16:42:58	2015.01.29 09:14  jumper_72.6 EURUSD,M1: --- --- AccountMargin: 0.00000000
0	16:42:58	2015.01.29 09:14  jumper_72.6 EURUSD,M1: --- --- CustomMarginCheck (Single order):32.2450000000000188
0	16:42:58	2015.01.29 09:14  jumper_72.6 EURUSD,M1: --- --- MODE_MARGINREQUIRED (Single order):32.2480000000000047
0	16:42:58	2015.01.29 09:14  jumper_72.6 EURUSD,M1: --- --- AccountFreeMarginCheck(OP_BUY):        32.2486000000000104
from tester on SAME account with tickstory data:

Code: Select all

0	16:47:13	2015.01.15 13:31  jumper_72.6 EURUSD,M1: --- --- NO open orders!
0	16:47:13	2015.01.15 13:31  jumper_72.6 EURUSD,M1: --- Lot calculations: 
0	16:47:13	2015.01.15 13:31  jumper_72.6 EURUSD,M1: --- --- AccountLeverage: 500.00000000
0	16:47:13	2015.01.15 13:31  jumper_72.6 EURUSD,M1: --- --- AccountBalance: 100.00000000
0	16:47:13	2015.01.15 13:31  jumper_72.6 EURUSD,M1: --- --- AccountEquity: 100.00000000
0	16:47:13	2015.01.15 13:31  jumper_72.6 EURUSD,M1: --- --- AccountFreeMargin: 100.00000000
0	16:47:13	2015.01.15 13:31  jumper_72.6 EURUSD,M1: --- --- AccountMargin: 0.00000000
0	16:47:13	2015.01.15 13:31  jumper_72.6 EURUSD,M1: --- --- CustomMarginCheck (Single order):32.0762000000000001
0	16:47:13	2015.01.15 13:31  jumper_72.6 EURUSD,M1: --- --- MODE_MARGINREQUIRED (Single order):42.0000000000000071
0	16:47:13	2015.01.15 13:31  jumper_72.6 EURUSD,M1: --- --- AccountFreeMarginCheck(OP_BUY):       42.0000000000000000
As you can see MODE_MARGINREQUIRED and AccountFreeMarginCheck differs alot from custom calculations on tickstory.
Tickstory is set up with 99.9 quality, all broker data imported with provided script.

What am i doin wrong?

tickstory
Posts: 4899
Joined: Sun Jan 06, 2013 12:27 am

Re: Custom lot calculation vs AccountFreeMarginCheck()

Post by tickstory »

Hi Kentzversky,

The only setting that Tickstory embeds with regard to Free Margin is the 'Free Margin Mode' where the setting can be:

Dont Use = 0
Use All = 1
Use Profit = 2
Use Loss = 3

All that I can suggest is that you ensure this matches your broker. It may also be worthwhile referring to the MT4 forums as they may be better placed to assist. See http://forum.mql4.com/67106 for example.

Hope this helps.

kentzversky
Posts: 4
Joined: Tue Apr 14, 2015 2:49 pm

Re: Custom lot calculation vs AccountFreeMarginCheck()

Post by kentzversky »

Thank's for reply tickstory!

How do Tickstory obtain this values? I didn't find any straight analogs in mql4

MarketInfo():
MODE_PROFITCALCMODE: Profit calculation mode. 0 - Forex; 1 - CFD; 2 - Futures
MODE_MARGINCALCMODE: Margin calculation mode. 0 - Forex; 1 - CFD; 2 - Futures; 3 - CFD for indices

are not coresponding to your's;
Dont Use = 0
Use All = 1
Use Profit = 2
Use Loss = 3


If the is no analog - can you explain calculation method of each position.
Thank's!

tickstory
Posts: 4899
Joined: Sun Jan 06, 2013 12:27 am

Re: Custom lot calculation vs AccountFreeMarginCheck()

Post by tickstory »

Hi Kentzversky,

The 'Free margin mode' can be set in the 'Metatrader Info' tab when exporting to MT4. The setting is just to the right of the 'Leverage' setting.
The other two settings you mention - profit calc mode & margin calc mode - can also be set on this screen in the 'Symbol Settings' section.

Hope this helps.

kentzversky
Posts: 4
Joined: Tue Apr 14, 2015 2:49 pm

Re: Custom lot calculation vs AccountFreeMarginCheck()

Post by kentzversky »

Thanks you kindly, I know where to enter this settings but..

the question was:
How do Tickstory obtain this values for 'Free Margin Mode'?

As this values are obtained via tickstory script, so its done by some mql4 commands.
Can you provide us with the line of code with this calculations. Seems there is a error for my provider so i cant find out where.

I've got closer to the problem:
MarketInfo(Symbol(),MODE_MARGINREQUIRED) returns different results:

from tester on REAL account (EURUSD Balance $100):
221.61
from tester on SAME account with tickstory data (EURUSD Balance $100):
200
(like if i had initial deposit in EUR)

tickstory
Posts: 4899
Joined: Sun Jan 06, 2013 12:27 am

Re: Custom lot calculation vs AccountFreeMarginCheck()

Post by tickstory »

Hi Kentzversky,

Tickstory performs no calculations on margin - it simply exports broker stated values and MT4 does the rest. In the case of 'Free Margin Mode', it acquires this information from the MQL command AccountFreeMarginMode() (see http://docs.mql4.com/account/accountfreemarginmode).

I'm not sure if you can find some help in the discussion here: http://forum.mql4.com/5911 although this relates to an older MT4 build.

Regards.

kentzversky
Posts: 4
Joined: Tue Apr 14, 2015 2:49 pm

Re: Custom lot calculation vs AccountFreeMarginCheck()

Post by kentzversky »

Ok, I'm getting closer to solution.
A like problem was described here:
http://www.tickstory.com/forum/viewtopic.php?f=4&t=275:
The MT4 tester, following the hack by your program, issues a wrong number in response to the query MarketInfo(EURUSD, MODE_MARGINREQUIRED)). For EURUSD, the lot price (100000 EUR) should depend on the Euro's current price (136000 as of now) while with Tickstory, MT4 issues 100000.


Above is true:

Code: Select all

0	18:55:06	2015.04.01 00:00  lotcalculations EURUSD,M1: --- --- AccountCurrency: USD
0	18:55:06	2015.04.01 00:00  lotcalculations EURUSD,M1: --- --- Symbol: EURUSD
0	18:55:06	2015.04.01 00:00  lotcalculations EURUSD,M1: --- --- AccountLeverage: 500.00000000
0	18:55:06	2015.04.01 00:00  lotcalculations EURUSD,M1: --- --- AccountBalance: 100.00000000
0	18:55:06	2015.04.01 00:00  lotcalculations EURUSD,M1: --- --- [u]MODE_MARGINREQUIRED: 200[/u]
Should be:

Code: Select all

0	18:58:02	2015.04.01 00:00  lotcalculations EURUSD,M1: --- --- [u]MODE_MARGINREQUIRED: 214.99[/u]
I found a possible workaround:
-we want to trade EURUSD,
-we have account currency: USD
-Tools>Export to MT4>Metatrader Info: "Margin ccy:" field is set to USD by default (or by TS script), but

if you change "Margin ccy:" field to EUR (for AUDUSD to AUD aso) you will get results pretty close to real.

Notable that formerly this field was called "Currency:" and depicted account currency.

So what exactly "Margin ccy:" means?

tickstory
Posts: 4899
Joined: Sun Jan 06, 2013 12:27 am

Re: Custom lot calculation vs AccountFreeMarginCheck()

Post by tickstory »

Hi Kentzversky,

'Margin Currency' is a separate field used by the MT4 export (FXT) file. It essentially the base currency of the instrument and yes, you are correct - it should be EUR in the case of EURUSD (i.e. the base currency). In the case of non-FOREX instruments (such as GOLD), it should be USD.

This issue has already been addressed in the next major version of Tickstory and we will also put a note to change the TickstoryExpert file so it correctly sets this value. Thanks for taking the time to detail your issue.

Regards.

Post Reply