PDA

View Full Version : Problem in appliation, account created for wrong client



zaid_86
10-01-2013, 04:47 PM
Hello,

I have written an application, that reads requests from XML file (on timer) and then creates V10 client and account via the API, but I have problem, that sometimes if two requests arrive one after another within several minutes, the account got created for the wrong /previous client,


here is the code:



Imports VertexFXBOAPI10
Imports System.IO
Imports System.Xml


Public Class Form1
Public WithEvents m_oBOAPI As CVertexFXBOAPI = New CVertexFXBOAPI
Public logged_in As login_values
Public client_created As client_creation_values = client_creation_values.IDLE
Public account_created As account_creation_values = account_creation_values.IDLE
Private xml_r As XmlReader
Private account_number As String
Private current_account_is_demo As Boolean

Public REQUEST_FILE_PATH As String = "C:\vertex_web_api\request.xml"
Public RESPONSE_DIR_PATH As String = "C:\vertex_web_api\responses\"

Enum login_values As Integer
CONNECTED = 1
DISCONNECTED = 2
CONNECTING_IN_PROCESS = 3
End Enum

Enum client_creation_values As Integer
IDLE = 1
IN_PROCESS = 2
End Enum

Enum account_creation_values As Integer
IDLE = 1
IN_PROCESS = 2
End Enum


Public Sub loginAsDealer()
Dim username As String = "XXXX"
Dim password As String = "XXXXX"
Dim server_ip As String = "XXXX.XXXX.XXXX.XXXX"
Dim server_port As Integer = 4401


m_oBOAPI.SetLoginInfo(username, password, server_ip, Val(server_port))
m_oBOAPI.Login()
logged_in = login_values.CONNECTING_IN_PROCESS

appendNewLog("login called")
End Sub


Public Sub createNewClientViaAPI()
client_created = client_creation_values.IN_PROCESS

Dim parent_account As String = "API INDIVIUAL"
xml_r.ReadStartElement("first_name")
Dim first_name As String = xml_r.ReadContentAsString()
xml_r.ReadEndElement()

xml_r.ReadStartElement("second_name")
Dim second_name As String = xml_r.ReadContentAsString()
xml_r.ReadEndElement()

xml_r.ReadStartElement("third_name")
Dim third_name As String = xml_r.ReadContentAsString()
xml_r.ReadEndElement()

xml_r.ReadStartElement("last_name")
Dim last_name As String = xml_r.ReadContentAsString()
xml_r.ReadEndElement()

xml_r.ReadStartElement("username")
account_number = xml_r.ReadContentAsString()
xml_r.ReadEndElement()

xml_r.ReadStartElement("password")
Dim password As String = xml_r.ReadContentAsString()
xml_r.ReadEndElement()

xml_r.ReadStartElement("email")
Dim email As String = xml_r.ReadContentAsString()
xml_r.ReadEndElement()


xml_r.ReadStartElement("phone")
Dim phone As Double = xml_r.ReadContentAsDouble()
xml_r.ReadEndElement()

xml_r.ReadStartElement("fax")
Dim fax As String = xml_r.ReadContentAsString()
xml_r.ReadEndElement()

xml_r.ReadStartElement("mobile")
Dim mobile As Double = xml_r.ReadContentAsDouble()
xml_r.ReadEndElement()


xml_r.ReadStartElement("address")
Dim address As String = xml_r.ReadContentAsString()
xml_r.ReadEndElement()

xml_r.ReadStartElement("show_as_demo")
current_account_is_demo = xml_r.ReadContentAsBoolean()
xml_r.ReadEndElement()

xml_r.ReadStartElement("pob")
Dim pob As String = xml_r.ReadContentAsString()
xml_r.ReadEndElement()


Dim tel_pass As String = "-"

xml_r.ReadStartElement("country")
Dim country As String = xml_r.ReadContentAsString()
xml_r.ReadEndElement()



Dim read_only As Boolean = False

Dim res As CreateClientResultEnum = m_oBOAPI.CreateClient(parent_account, first_name, second_name, third_name, last_name, account_number, password, phone, fax, mobile, tel_pass, pob, country, email, address, current_account_is_demo, read_only)

appendNewLog("Client " + account_number + "creation result: " + res.ToString)
If (res <> CreateClientResultEnum.SuccessfulClientResult And res <> CreateClientResultEnum.UsernameAlreadyExistClientR esult) Then
writeAccountOpeningResult("success=0&error_reason=" + res.ToString())
client_created = client_creation_values.IDLE
End If

End Sub

Public Sub createNewAccountViaAPI()
account_created = account_creation_values.IN_PROCESS
Dim acc_num As Integer
If (Not current_account_is_demo) Then
acc_num = Integer.Parse(account_number)
Else
acc_num = 0
End If


Dim res As CreateAccountResultEnum = m_oBOAPI.CreateAccount(account_number, acc_num, VertexGeneral10.AccountTypeEnum.NormalAccountType, 0.0, current_account_is_demo, False, False)

If (res <> VertexFXBOAPI10.CreateAccountResultEnum.Successful AccountResult) Then 'request not sent successfully
appendNewLog("Account#" + account_number + " creation result: " + res.ToString())
client_created = client_creation_values.IDLE
Else
appendNewLog("CreateAccount called successfully...")
End If
End Sub

Private Sub m_oBOAPI_AllDataAreLoaded() Handles m_oBOAPI.AllDataAreLoaded
logged_in = login_values.CONNECTED
appendNewLog("LOGGED IN!!!!")
End Sub


Private Sub m_oBOAPI_InvalidUsernamePassword() Handles m_oBOAPI.InvalidUsernamePassword
logged_in = login_values.DISCONNECTED
appendNewLog("INVALID USERNAME/PASSWORD")
End Sub

Private Sub m_oBOAPI_CreateClientResult(ByRef Result As VertexFXBOAPI10.CreateClientResultEnum) Handles m_oBOAPI.CreateClientResult
If (Result = CreateClientResultEnum.SuccessfulClientResult Or Result = CreateClientResultEnum.UsernameAlreadyExistClientR esult) Then
createNewAccountViaAPI()
Else
writeAccountOpeningResult("success=0&error_reason=" + Result.ToString())
End If
client_created = client_creation_values.IDLE
End Sub

Private Sub m_oBOAPI_CreateAccountResult(ByRef Result As VertexFXBOAPI10.CreateAccountResultEnum) Handles m_oBOAPI.CreateAccountResult
If (Result = CreateAccountResultEnum.SuccessfulAccountResult) Then
appendNewLog("Account#" + account_number + " has been created successfully!")
writeAccountOpeningResult("success=1")
Else
appendNewLog("Account#" + account_number + " not created, ERROR: " + Result.ToString)
writeAccountOpeningResult("success=0&error_reason=" + Result.ToString())
End If
account_created = account_creation_values.IDLE
End Sub

Private Sub m_oBOAPII_ReportDataReceived(ByRef NoRows As Boolean, ByRef data As System.Array) Handles m_oBOAPI.ReportDataReceived
MsgBox(data.ToString)
End Sub

Private Sub main_timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles main_timer.Tick
'TODO: check if there is new requests... (in the json file)
If (m_oBOAPI.LogedOn = False And logged_in <> login_values.CONNECTING_IN_PROCESS) Then
loginAsDealer()
appendNewLog("waiting for request....")
End If

If logged_in = login_values.CONNECTED Then
Dim sr As StreamReader = New StreamReader(REQUEST_FILE_PATH)
Dim request_content As String = sr.ReadToEnd()
sr.Dispose()

If request_content <> "" And client_created = client_creation_values.IDLE And account_created = account_creation_values.IDLE Then 'there is new request...
appendNewLog("new request is detected... will be handled now!")

xml_r = New XmlTextReader(REQUEST_FILE_PATH)
xml_r.Read()
xml_r.ReadStartElement("request")
xml_r.ReadStartElement("operation_type")
Dim operation_type As String = xml_r.ReadContentAsString()
xml_r.ReadEndElement()

If (operation_type = "open_account") Then
createNewClientViaAPI()
ElseIf (operation_type = "history") Then
readHistory()
cleanupRequests()
ElseIf (operation_type = "summary") Then
readSummery()
cleanupRequests()
Else
appendNewLog("Invalid request type: " + operation_type + ", REQUEST IGNORED!")
cleanupRequests()
End If
End If
End If


End Sub

Private Sub readHistory()
xml_r.ReadStartElement("account_id")
account_number = xml_r.ReadContentAsString()
xml_r.ReadEndElement()

appendNewLog("Retrieving account history for account#" + account_number)

m_oBOAPI.SelectAccount(account_number)

End Sub

Private Sub m_BOAPI_PendingOrdersLoaded() Handles m_oBOAPI.PendingOrdersLoaded
Dim positions_num As Integer = m_oBOAPI.OpenPositionsCount()

Dim ret As String = ""
For i = 1 To positions_num
'read all positions data and write them to file
Dim pos As COOpenPosition = m_oBOAPI.OpenPositionByIndex(i)

ret += "order[" + i.ToString + "]=" + pos.Ticket.ToString
ret += "&time[" + i.ToString + "]=" + pos.PosTime.ToString
ret += "&symbol[" + i.ToString + "]=" + pos.SymbolName.ToString
ret += "&lots[" + i.ToString + "]=" + pos.Lots.ToString
ret += "&price[" + i.ToString + "]=" + pos.Price.ToString
ret += "&ClosePrice[" + i.ToString + "]=" + pos.ClosePrice.ToString
ret += "&PL[" + i.ToString + "]=" + pos.FloatingProfitLoss.ToString
ret += "&BuySell[" + i.ToString + "]=" + pos.BuySell
ret += "&Commission[" + i.ToString + "]=" + pos.Commission
Next

Dim file_name As String = RESPONSE_DIR_PATH + "history_" + account_number + ".txt"
File.Create(file_name).Dispose()

Dim objWriter As New System.IO.StreamWriter(file_name)
objWriter.Write(ret)
objWriter.Close()

appendNewLog("Account history for account#" + account_number + " is Completed!")

writeSummaryToFile()
End Sub

Private Sub readSummery()
xml_r.ReadStartElement("account_id")
account_number = xml_r.ReadContentAsString()
xml_r.ReadEndElement()

appendNewLog("Retrieving account summary for account#" + account_number)

m_oBOAPI.SelectAccount(account_number)


End Sub

Private Sub writeSummaryToFile()
Dim balance As Double
Dim fltpl As Double
Dim credit As Double
Dim equity As Double
Dim margin_req As Double
Dim margin_level As Double
Dim eff_margin As Double

m_oBOAPI.AccountSummaries(account_number, balance, fltpl, credit, equity, margin_req, margin_level, eff_margin)

Dim file_name As String = RESPONSE_DIR_PATH + "summary_" + account_number + ".txt"
File.Create(file_name).Dispose()

Dim objWriter As New System.IO.StreamWriter(file_name)
objWriter.Write("Balance=" + balance.ToString + "&FloatingProfitLoss=" + fltpl.ToString + "&Credit=" + credit.ToString + "&Equity=" + equity.ToString + "&MarginRequirement=" + margin_req.ToString + "&MarginLevel=" + margin_level.ToString + "&EffectiveMargin=" + eff_margin.ToString)
objWriter.Close()

appendNewLog("Account summary for account#" + account_number + " is Completed!")
End Sub

Private Sub appendNewLog(ByVal msg)
operations_txt.Text = operations_txt.Text + vbNewLine + DateTime.Now.ToString + ": " + msg
End Sub

Private Sub writeAccountOpeningResult(ByVal msg As String)
Dim file_name As String = RESPONSE_DIR_PATH + account_number + ".txt"
File.Create(file_name).Dispose()

Dim objWriter As New System.IO.StreamWriter(file_name)
objWriter.Write(msg)
objWriter.Close()

cleanupRequests()
End Sub

Private Sub cleanupRequests()
'empty the request file...
xml_r.Close() 'once we write the result of the operation, we don't need the xml reader, destroy to prevent "process in use" exception
Dim filenum As Integer = FreeFile()
FileOpen(filenum, REQUEST_FILE_PATH, OpenMode.Output)
FileClose()

appendNewLog("------------------------------------------") 'separate the requests in the UI...
End Sub

Private Sub operations_txt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles operations_txt.TextChanged

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
End Class



and here is the format of the XML file



<?xml version="1.0" encoding="UTF-8" ?>
<request>
<operation_type>open_account</operation_type>
<first_name>real</first_name>
<second_name>real</second_name>
<third_name>real</third_name>
<last_name>real</last_name>
<username>333333111</username>
<password>XXXXX</password>
<email>zaid_real@wewebit.com</email>
<phone>0044444444</phone>
<fax></fax>
<mobile>0044444444</mobile>
<address>JO - amman</address>
<show_as_demo>1</show_as_demo>
<pob>0000</pob>
<country>JO</country>
</request>




As the code show, I write logs of the requests in a textarea, and here is a sample of the log when the problem appeared..




9/28/2013 2:19:09 PM: login called
9/28/2013 2:19:09 PM: waiting for request....
9/28/2013 2:19:15 PM: Account history for account# is Completed!
9/28/2013 2:19:15 PM: Account summary for account# is Completed!
9/28/2013 2:19:16 PM: LOGGED IN!!!!
9/28/2013 6:32:44 PM: new request is detected... will be handled now!
9/28/2013 6:32:44 PM: Client 3000006creation result: SuccessfulClientResult
9/28/2013 6:32:45 PM: CreateAccount called successfully...
9/28/2013 6:32:45 PM: Account#3000006 has been created successfully!
9/28/2013 6:32:45 PM: ------------------------------------------
9/28/2013 6:38:51 PM: new request is detected... will be handled now!
9/28/2013 6:38:51 PM: Client 3000007creation result: SuccessfulClientResult
9/28/2013 6:38:51 PM: CreateAccount called successfully...
9/28/2013 6:38:51 PM: Account#3000007 has been created successfully!
9/28/2013 6:38:51 PM: ------------------------------------------
9/28/2013 6:40:47 PM: new request is detected... will be handled now!
9/28/2013 6:40:47 PM: Client 3000008creation result: SuccessfulClientResult
9/28/2013 6:40:47 PM: CreateAccount called successfully...
9/28/2013 6:40:47 PM: Account#3000008 has been created successfully!
9/28/2013 6:40:47 PM: ------------------------------------------
9/28/2013 6:47:14 PM: new request is detected... will be handled now!
9/28/2013 6:47:14 PM: Client 3000009creation result: SuccessfulClientResult
9/28/2013 6:47:14 PM: CreateAccount called successfully...
9/28/2013 6:47:14 PM: Account#3000009 has been created successfully!


Although, the same code is applied for demo users, and no such problem appeared


Is there anything wrong with the code? or there is some kind of limitation on the API request queue time?
I attached a version of the app I wrote to this post

Appreciate your help,

Regards,

Hassan-HS
10-01-2013, 09:16 PM
Dear Zaid,

I will study this case, and i will update you soon please stand by.

b_morar
10-02-2013, 11:44 AM
Dear Zaid,

Please check the following sample, it's confirm that is no issue in our side.:D


Please check your code and "account_number" values that you are passing it to functions.

zaid_86
10-02-2013, 01:39 PM
Hello Morar,
thanks for your reply,
I checked the code several times and wasn't able to find anything wrong :confused:

as the log illustrated - please see above -, the requests are passed one after another with the correct account number each time,

should I do do something after creation of the account (for example call logout method), in order to clean the requests?? or anything else?

have you been able to reproduce the problem using the code I attached??

I have checked the code you attached, its a simple call but the case I have is for handling multiple requests with small time interval, my application also worked will for 2 weeks before I encountered this scenario

although I applied similar code for demo users (but with different application and different BO login information) and no problem appears at their side



Appreciate any help that will fix this problem

b_morar
10-02-2013, 02:53 PM
Dear Zaid welcome again,


1- "should I do do something after creation of the account (for example call logout method), in order to clean the requests?? or anything else?"

There is no need to do any such thing.

2-"have you been able to reproduce the problem using the code I attached??"

Sorry i don't able to run your code, but i have checked our API many times and it's work fine.

3-"I have checked the code you attached, its a simple call but the case I have is for handling multiple requests with small time interval, my application also worked will for 2 weeks before I encountered this scenario"

But you can create 10 or more clients and account in one minutes using my sample,right?

Regarding the code that it was working fine before, what kind of new things happen in order to face this? is there any new scenario came?

zaid_86
10-03-2013, 08:34 AM
Hello Morar,
thanks for your reply,

The only scenario that my client mentioned, is he tried to create multiple clients/accounts within small time interval..

anyway, I will try to gather more information from him, and from the log and keep you updated..


Regards,

b_morar
10-03-2013, 09:17 AM
Dear Zaid,

You are welcome any time.

zaid_86
10-23-2013, 08:52 AM
Hello again,
Eid mubarak, and sorry for the late response..

I have traced the problem, and I think I knew what is the cause,
my client specifies the account number he wants to create (so the first account has the same number as the client created)

so I amended your code a little to reflect this case (3rd line in the create account method):



Public Sub createNewAccountViaAPI()
Dim acc_num
acc_num = Integer.Parse(account_number) 'let the account number be specific according to data entered by the user
Dim res As CreateAccountResultEnum = m_oBOAPI.CreateAccount(txtclientusername.Text, acc_num, VertexGeneral10.AccountTypeEnum.NormalAccountType, CDbl(txtInitialPrice.Text), CheckBox2.Checked, CheckBox1.Checked, CheckBox3.Checked)
If (res <> VertexFXBOAPI10.CreateAccountResultEnum.Successful AccountResult) Then 'request not sent successfully
MsgBox("Account#" + account_number + " creation result: " + res.ToString())
Else
MsgBox("CreateAccount called successfully...")
End If
End Sub
and the issue appeared too in the app you provided after I changed this line,

any idea how to work around this problem?


Thanks,
zaid

b_morar
10-23-2013, 10:36 AM
Welcome Mr Zaid,

As an example, I made that the Username of the client shall be Numbers only, and I used the same number to be as the AccountID, below is the sample, and it worked fine for me:


Public Sub createNewAccountViaAPI()
Dim acc_num As Long
acc_num = Val(txtclientusername.Text)
Dim res As CreateAccountResultEnum = m_oBOAPI.CreateAccount(txtclientusername.Text, acc_num, VertexGeneral10.AccountTypeEnum.NormalAccountType, CDbl(txtInitialPrice.Text), CheckBox2.Checked, CheckBox1.Checked, CheckBox3.Checked, "TransTag")
If (res <> VertexFXBOAPI10.CreateAccountResultEnum.Successful AccountResult) Then 'request not sent successfully
MsgBox("Account#" + account_number + " creation result: " + res.ToString())
Else
MsgBox("CreateAccount called successfully...")
End If
End Sub
Please make sure that:
1) Your are logged in, All Data are Loaded, and the Client created successfully.
2) You passed the correct Username to the CreateAccount function.
3) The AccountID is unique (not exist), otherwise you will have an error code for that.

zaid_86
10-23-2013, 11:36 AM
Hello Mr. Morar,
I have copied your code, and oerformed a test but the same issue appeared,

can you connect to my machine via teamviewer, in order to see the problem? (I think this will save us some time)

here is my teamviewer info:
ID: 954 723 768
password: 2791

Hassan-HS
10-23-2013, 01:11 PM
Dear Zaid,


You need to re-login after creating new client as work around to solve this issue, i will check this case with our developers.

Hassan-HS
11-18-2013, 02:06 PM
Dear Zaid,

We are glade to inform you that we have released VertexFX Backoffice API 10.1.11, Now you can create many clients with accounts without any problem.

For more information click here (http://www.hybrid-solutions.com/support/index.php?/Knowledgebase/Article/View/3999).

Please contact our technical support to download it


let us know if you have any further inquiries.


:D

chal3oye
12-07-2013, 05:14 AM
Sorry i don't able to run your code, but i have checked our API many times and it's work fine.

3-"I have checked the code you attached, its a simple call but the case I have is for handling multiple requests with small time interval, my application also worked will for 2 weeks before I encountered this scenario"

But you can create 10 or more clients and account in one minutes using my sample,right?

Hassan-HS
12-07-2013, 07:09 AM
Dear sir,


How may i help you?

Hassan-HS
12-09-2013, 09:16 AM
Sorry i don't able to run your code, but i have checked our API many times and it's work fine.

3-"I have checked the code you attached, its a simple call but the case I have is for handling multiple requests with small time interval, my application also worked will for 2 weeks before I encountered this scenario"

But you can create 10 or more clients and account in one minutes using my sample,right?


Dear Sir, you have quoted " b_morar" reply that was posted in 2/10/2013.



Dear Zaid welcome again,

1- "should I do do something after creation of the account (for example call logout method), in order to clean the requests?? or anything else?"

There is no need to do any such thing.

2-"have you been able to reproduce the problem using the code I attached??"

Sorry i don't able to run your code, but i have checked our API many times and it's work fine.

3-"I have checked the code you attached, its a simple call but the case I have is for handling multiple requests with small time interval, my application also worked will for 2 weeks before I encountered this scenario"

But you can create 10 or more clients and account in one minutes using my sample,right?

Regarding the code that it was working fine before, what kind of new things happen in order to face this? is there any new scenario came?


Do you facing something incomprehensible?, anyway i will summarize the thread.

We have solved a case was occur when creating a client with account, then create another client with account, the second account will be assigned to the first client.


Note: this case solved in BOAPI 10.1.11