fxcm / ForexConnectAPI

Designed to trade, retrieve live/history price. Intended to be used to build auto-trading robots, custom trading on FXCM accounts.
132 stars 47 forks source link

How to calculate margin in vba? #9

Closed AVISHAY123 closed 3 years ago

AVISHAY123 commented 3 years ago

Also, some pairs giving wrong value, when using accountRow.UsableMargin accountRow.UsedMargin doesn't work at all in mt4 account

FXCMAPI commented 3 years ago

please provide more information, screenshot, error logs or sample code.

AVISHAY123 commented 3 years ago

I want to calcuate margin before opening position How can I do that? I use that code after openning position:

Dim accountRow As AccountTableRow Set accounts = tableManager.getTable(TableType_Accounts) Set accountRow = accounts.getRow(0) freeMargin = accountRow.UsableMargin UsedMargin=accountRow.Equity - accountRow.UsableMargin + accountRow.GrossPL

the values are not right freeMargin=3760.1 UsedMargin= 1099.86
accountrow.UsedMargin - getting an error

m1

fxcm-dhalpert commented 3 years ago

here is example code how to get margins per symbol:

Sub showMarginRequirements(instrument As String, text As String) Dim loginRules As ILoginRules Set loginRules = mSession.getLoginRules() Dim tradingSetting As ITradingSettingsProvider Set tradingSetting = loginRules.getTradingSettingsProvider() Dim accountsResponse As IResponse Set accountsResponse = loginRules.getTableRefreshResponse(TableType_Accounts) Dim responseReaderFactory As IResponseReaderFactory Set responseReaderFactory = mSession.getResponseReaderFactory() Dim accounts As IAccountTableReader Set accounts = responseReaderFactory.createAccountsTableReader(accountsResponse) Dim accountRow As accountRow Set accountRow = accounts.getRow(0) Dim MMR As Double, EMR As Double, LMR As Double Call tradingSetting.getMargins(instrument, accountRow, MMR, EMR, LMR) MsgBox text + " mmr=" + MMR + "; emr=" + EMR + "; lmr=" + LMR End Sub

AVISHAY123 commented 3 years ago

Thank you what is the difference between mmr emr lmr ? I get same data for them

Why is there a difference between the margins in the mt4 account and the ts2 account? ------------------------MT4--------------------------------TS2 aud/jpy------mmr=2.25 emr=2.25 lmr=2.25------mmr=9 emr=9 lmr=9 nzd/usd-----mmr=2 emr=2 lmr=2----------------mmr=8 emr=8 lmr=8

There are also pairs, which have no margins in TS2 and there in MT4 and vice versa -----------------------MT4-------------------------------TS2 us30-------- mmr=18.5 emr=18.5 lmr=18.5---mmr=-1 emr=-1 lmr=-1 nzd/jpy----- mmr=-1 emr=-1 lmr=-1--------- mmr=8 emr=8 lmr=8

Additionally, there are also pairs, which have no margins at all, in both TS2 and MT4

gbp/cad-----mmr=-1 emr=-1 lmr=-1------mmr=-1 emr=-1 lmr=-1 eur/nzd------mmr=-1 emr=-1 lmr=-1------mmr=-1 emr=-1 lmr=-1

fxcm-dhalpert commented 3 years ago

please try to subscribe for these symbols, you can enable them from Trading Station or from API.

AVISHAY123 commented 3 years ago

how to subscribe? And, When to unsubscribe and whether it is necessary?

fxcm-dhalpert commented 3 years ago

just open Trading Station desktop, and check if these symbols are visible, if not then enable them, than check in your code if it works.

AVISHAY123 commented 3 years ago

ok but, how do I do this from api?

FXCMAPI commented 3 years ago

please check GetOffers example form our package O2GRequest offersRequest = requestFactory.createRefreshTableRequest(O2GTableType.Offers); responseListener.SetRequestID(offersRequest.RequestID); session.sendRequest(offersRequest); if (!responseListener.WaitEvents()) { throw new Exception("Response waiting timeout expired"); } response = responseListener.GetResponse(); if (response != null) { responseListener.PrintOffers(session, response, null); }

AVISHAY123 commented 3 years ago

thank you but this is not in VBA and, I don't have GetOffers in my package is there a link?

FXCMAPI commented 3 years ago

you need to download ForexConnect for COM http://www.fxcodebase.com/wiki/index.php/Using_ForexConnect_in_COM you will find the example in VBA

AVISHAY123 commented 3 years ago

thank you I already have the new version but, there is no GetOffers in vba sample

AVISHAY123 commented 3 years ago

Can you show me GetOffers function for vba, please?

FXCMAPI commented 3 years ago

please try "Subscription" example in VBA samples folder

AVISHAY123 commented 3 years ago

Ok, thank you Now it works on mt4 account without some symbols like aus2000, usdzar, usdtry, eurtry, cooper, volx but in TS2 account it doesnt work for many symbols like audnzd us30 audchf and many others I tried TS2 account in VBA sample in excel, and those symbols did not change at all - what cam I do?

Additionally, there are significant differences in margins, between the mt4 account and the ts2 account, regardless of the API - why is that?

FXCMAPI commented 3 years ago

first make sure you see price update correctly on our GUI. say TS2 MT4 and TS2 accounts have different profiles that have different margin or price

AVISHAY123 commented 3 years ago

There is a small difference between the MT4 update and the TS2 update now, I subscribe audnzd in the GUI, and I can get its margin and update via API But I want to subscribe via API

If I open a new account, there are many non-subscribed symbols VBA sample not help for that I need the code for that, please

AVISHAY123 commented 3 years ago

Can you give VBA sample please for subscribe symbols?

FXCMAPI commented 3 years ago

please download com package http://www.fxcodebase.com/wiki/index.php/Using_ForexConnect_in_COM sample code under ForexConnectAPIx64\samples\com\VBA\Subscription