PDA

View Full Version : Cann't open BuyStop order



nhasan
07-17-2013, 06:34 AM
Hi Dear

I am trying to open BuyStop order from server script
But it returns error message : InvelidSybolIDResult

My code is here:

Public Function OpenBuyStop()
Dim cPair as VTLGeneral.CSymbol
cPair = ClientCode.GetSymbolByName( "EUR/USD" )
Dim OrderResult As VTLServerApplication.CallingResultsEnum
Dim CPrice as Double
Dim iPoint as Double = 0.0001

CPrice = ClientCode.SymbolInfoDouble("EUR/USD",VTLGeneral.ENUM_SYMBOL_INFO_DOUBLE.SYMBOL_BID)

OrderResult = ClientCode.NewLimitOrder ( 2, LotsOptimized(), cPair.ID, CPrice + 100 * iPoint, CPrice - 100 * iPoint, CPrice + 200 * iPoint )

GUI.MsgDialog ( "Order Result: " & OrderResult.ToString )

Return OrderResult

End Function

Can anyone help me to overcome this issue?

Thanks
Naeem Hasan

Hassan-HS
07-17-2013, 07:29 AM
Dear Hasan,


I noticed that you are passing the returned value of LotsOptimized function In the position of SymbolID (http://www.hybrid-solutions.com/support/index.php?/Knowledgebase/Article/View/1849)argument.


LotsOptimized : It seems returns the amount of the deal, So make sure you are calling the function with correct order of arguments.

nhasan
07-17-2013, 07:49 AM
Yes, That's the problem
Thanks for quick solution

nhasan
07-17-2013, 07:53 AM
But now the error is - InvalidAmountResult when use LotsOptimized function to calculate lot size based on Money Management or % risk taken

It can open position with 1 lot size, but not for fractional lots like 0.1

Hassan-HS
07-17-2013, 09:03 AM
Dear Hasan,


It's Configuration, Please give me your account ID, To configure the minimum amount per deal parameter.

nhasan
07-17-2013, 09:07 AM
My Account ID is : USER12653

nhasan
07-17-2013, 09:22 AM
I am trying to open a pending order at a specific time

If I call OpenBuyStop() function from Public Sub OnTick() it open's a number of orders ( 15/16) at a time

I am trying to avoid multiple call of the function with a condition like:

If OrdersCondition = 0 then ' here OrdersCondition is a global variable
If OpenBuyStop() = 1 then OrdersCondition = 1
End If

But it not works.

I make OrdersCondition variable global and try to change it from Public Sub NewLimitOrderResult with a line of code like
If CINT(Result) = 1 Then OrdersCondition = 100

But still it's opening multiple orders.

Is there any way to avoid this condition?
Should we place this code other than On Tick function?

Hassan-HS
07-17-2013, 09:26 AM
Dear hasan,


Everything is OK now, Please check your account.

nhasan
07-17-2013, 09:36 AM
Yes, I can open 0.1 lot now

But the main problem is multiple orders at a time
Please advice how to avoid it.
Is there any Sleep or Stop function to stop execution of On Tick function after opening an order?

Hassan-HS
07-17-2013, 10:02 AM
This is sample describe how to avoid OnTick





Dim Flag As Boolean
Dim _vSymbol As VTLGeneral.CSymbol

Public Sub main()
_vSymbol = ClientCode.GetSymbolByName("GOLD")
ClientCode.NewLimitOrder(2, _vSymbol.ID.ToString, 1, 1299.1, 1280.8, 1313.65)

End Sub


Public Sub OnTick()
If Flag = True Then

ClientCode.NewLimitOrder(2, _vSymbol.ID.ToString, 1, 1299.1, 1280.8, 1313.65)

End If

End Sub


Public Sub NewLimitOrderResult(ByRef Result As VTLServerApplication.NewOrderResultEnum, ByRef OrderId As Integer, ByRef ServerTime As String)
If Result = 1 Then

Flag = False

End If
End Sub

nhasan
07-17-2013, 10:17 AM
Thanks for the code
I have tested it but not solved the problem

In this code you call NewLimitOrder function from Sub Main that makes Flag = false from NewLimitOrderResult function
So it's not opening any order from OnTick function

But if you don't open any order from sub main - it opens many orders from OnTick before the Flag changed to False
Please check it be deleting NewLimitOrder function from sub main()

As I need to open pending order in a specific time ( just before news time ) I have to open it from OnTick function and it's opening many orders at a time.

Hassan-HS
07-17-2013, 10:50 AM
Dear Hasan,



I got the point , please check this code





Dim Flag As Boolean
Dim _vSymbol As VTLGeneral.CSymbol


Public Sub main()

Flag=True
_vSymbol = ClientCode.GetSymbolByName("GOLD")

End Sub


Public Sub OnTick()
If Flag = True Then
Flag = False
ClientCode.NewLimitOrder(2, _vSymbol.ID.ToString, 1, 1299.1, 1280.8, 1313.65)


End If

End Sub


Public Sub NewLimitOrderResult(ByRef Result As VTLServerApplication.NewOrderResultEnum, ByRef OrderId As Integer, ByRef ServerTime As String)
If Result = 1 Then


Else
Flag = True

End If

End Sub

nhasan
07-17-2013, 03:56 PM
Is there any basic change in this code?
You are making the flag = false before calling the NewLimitOrder function and that prevent it to open more orders.
But does it confirm opening of the order?
If NewLimitOrder function call fails for any reason, it will stop calling it and the order will never open

The main problem is global variables are not updating in between successive OnTick() function call
We should confirm opening of a new order from NewLimitOrderResult server event - but the OnTick function is calling several times before execution of this event.

Hassan-HS
07-17-2013, 04:27 PM
Dear Hasan


Now this code handles all types of failure that can get



Dim Flag As Boolean
Dim _vSymbol As VTLGeneral.CSymbol
Dim OrderResult As VTLServerApplication.CallingResultsEnum
Dim lots as double

Public Sub main()
Lots=10000
Flag=True
_vSymbol = ClientCode.GetSymbolByName("GOLD")

End Sub


Public Sub OnTick()
If Flag = True Then
Flag = False
OrderResult =ClientCode.NewLimitOrder(2, _vSymbol.ID.ToString,Lots, 1299.1, 1280.8, 1313.65)
if OrderResult <> 1 then
Flag = True
GUI.MsgDialog("Test")
Lots=1

end if


End If

End Sub


Public Sub NewLimitOrderResult(ByRef Result As VTLServerApplication.NewOrderResultEnum, ByRef OrderId As Integer, ByRef ServerTime As String)
If Result = 1 Then


Else
Flag = True

End If

End Sub

nhasan
07-18-2013, 04:31 AM
Dear Abukrayem
Thanks for the code and your effort to solve the problem.
It seems working for this context of simple coding
But I am sorry to say that the problem is not addressed properly, let me explain in details

Public Sub OnTick() is a server event and it's calling frequently before completing all actions of one call
We may write hundreds of lines with frequently changing environmental variables like market price, indicator's value etc to implement complex trading logic in professional auto traders application.
Let, I assign a value in a global variable in the 1st line of my code in OnTick sub
I can use it many times within the code, say I use it in 998th line
To execute this 997 lines of code it may take some time say 0.50 seconds or more
If the event OnTick occurs many times within this duration and change the value of that global variable outside the current code - how can we build consistent logic?

Any function/sub call must complete all of it's actions before next call of that function/sub to make sure all of it's variable remain unchanged within the code block for consistence of programming logic.

Am I clear?

I am working on MQL for more than 6 years. Their main function equivalent to your OnTick is start() that executes once on each tick
But they avoid all successive ticks before completing all actions of one call, even we can use sleep() function to stop the code for some long time say 2-3 seconds.

Your given solution may not work for my current project ( It's a big conversion project from MQL to VTL ) and I afraid that your system may not meet basic criteria for professional programming specially for high risk currency trading.

If possible please arrange to implement the above most important criteria in your programming environment and let me know so that I can get more complex project for conversion from MQL to VTL from my client.

Thanks
Naeem Hasan

Thanks

Hassan-HS
07-18-2013, 05:54 PM
Dear Hasan

Why OnTick() rises and interrupt the Main() ?
Because the VTL server is using multi threading technique,I will post sample that describe to you how to avoid multithreding and force the code to run sequentially .




Dim EndMain as boolean = false
Dim Counter as long =0

Public Sub main()

While True

Counter=Counter+1
If Counter = 999999999 then
Exit While
end if

End While

EndMain=True
End Sub


Public Sub OnTick()
If EndMain= false then
Exit sub
Else
EndMain= false
GUI.MsgDialog("Counter: " & Counter )


'
'Your Code
'
end if

End Sub

nhasan
07-18-2013, 10:40 PM
So far as I understand this technique will run my code only once and then stop until OnTick() interrupt sub main again - is it?
But my point was running multiple instance of OnTick() function at a time - that makes difficult to maintain logical flow of code segments.
However I can manage my current project using different technique, but I think this should be updated from system end so that one OnTick() function must complete all of it's action before it's next call.

Thanks

Hassan-HS
07-19-2013, 06:02 AM
Dear Hasan,


Thank you very much for your interest, Please suggest anything you find from your point of view is useful, and we'll take your request into consideration immediately.

nhasan
07-21-2013, 04:47 AM
I am using a simple technique to avoid multiple call of OnTick function as follows

Dim Flag as Boolean = True ' A global variable
Public Sub OnTick()
If Flag = False then exit sub
Flag = False
-
-
-
- My Code here
-
-
Flag = True
End Sub

This will work as long as we have enough time to change global variable Flag to false before next call of OnTick
But if two call ( say one from EURUSD and another from GOLD ) comes exactly at the same time (It can happen in multithread system ) then all of my code will execute twice.

I think there should be a way to identify which instrument calls the function OnTicke().
You can include a parameter as OnTick( Instrument as String ) so that we can use relative ticks only.
Because when we have decided to trade Gold, ticks from other instrument should have no significant and executing the code on those ticks may be west of resource - isn't it?

I think this improvement is required to make your system more reliable and it also help to reduce use of server resources.

Thanks
Naeem Hasan

Hassan-HS
07-21-2013, 07:14 AM
Dear Hasan,


We have taken your request seriously, we will update you as soon as possible.