PDA

View Full Version : How can I get SL/TP of an open position?



nhasan
07-18-2013, 06:29 AM
Dear Team

I am working on a MQL to VTL conversion project where I need to implement trailing stop loss facility.

But I don't find a way to read current stop loss value of an open position.
Data Type CPosition don't have Stop Loss or Take Profit option.

Can anyone help me in getting SL/TP from an open position?

Thanks
Naeem Hasan

Hassan-HS
07-18-2013, 07:18 AM
Dear Hasan,

Using this way you can get the SL & TP if you have ticket ID.





Public Sub main()

Dim COrder As VTLGeneral.Order
Dim i
Dim SL As Double
Dim TP As Double
Dim TicketID As integer
TicketID = 398630
For i = 1 To ClientCode.OrdersTotal()
COrder = ClientCode.OrderByIndex(i)
If COrder.OrderTicket =TicketID
SL=COrder.TP
TP=COrder.SL
GUI.MsgDialog("SL: " & SL & " TP: " & TP)
end if


Next
End Sub

nhasan
07-18-2013, 07:49 AM
Dear Abukrayem

Thanks for the code
It works on pending orders but not for open positions
I am looking for way to get SL and TP of an open position not pending orders

Thanks

Salah-HS
07-18-2013, 08:32 AM
Dear sir,
When you open a position there will not be a SL or TP values ;however you can manage that position to add these values
and that what will result in instantiation of a new pending order related to the managed position; so you can get the SL or TP from it
that's what was explained in the previous code in this sentence If COrder.OrderTicket =TicketID
where the TicketID is the ID of the position to which this order is related.

nhasan
07-18-2013, 08:36 AM
So how can we implement trailing stop facility for an open position from server script?

nhasan
07-18-2013, 08:53 AM
To test the platform I open a buy position manually
Then try to set a take profit - that opens another managed(sell) pending order with same lot size
But the ticket number of these two positions/orders are not same
So how can I manage that pending order from my server script?

If it's not possible in your platform - please say specifically, so that i can show it to my client and request him to remove this facility from his requirement.

Thanks

Salah-HS
07-18-2013, 09:15 AM
Dear,
Of course it's available in our platform and here is the way
after you setting the SL,TP values for certain position there will be a new (Manage) order (mapped)to that position
although that the TicketID of the Pending Order is different than the ID of the corresponding position but they are still
tied together since the TicketID of the position is stored as an attribute inside the Order object and you can retrieve it as shown in the above code.
kindly have a deeper look at the code delivered by hassan.

nhasan
07-18-2013, 09:46 AM
Yes, I can get the ticketID of managed orders separately - but how can I relate it with open position.
Say for example - I have many open positions (both buy and sell) for same currency pair, I want to set SL/TP on one of them and then update that as trailing stop
How can I identify the managed order which is related to the specific open position from server script?

Salah-HS
07-18-2013, 10:28 AM
Hi,
In VertexFX platform you may have multiple Manage orders (SL/TP orders) for the same position, i.e. if you have 10 lots position, you may have 1 lot SL at price 1 and 2 lots SL at price2 and 2 lots SL at price3,...

So to identify the Manage orders which are related to the same position you should loop over all the orders you have and compare the attribute value called TicketID (you can retrieve it as follow:Ordername.OrderTicket ) with the TicketID of the desired position.

Please let me know if you still need assistance

nhasan
07-18-2013, 04:04 PM
I afraid I can't express the problem clearly, let me try in details

Say I have 3 open positions for same currency pair with same lot size - A-buy, B-sell, C-buy
I also have some other pending orders (Not managed orders) for same currency pair with same lot size - D - Stop Buy, E-Limit Sell, F - Stop Sell

Now if I want to trail open position A with SL/TP - that will open another pending order G - Sell(Manage) with same lot size

Please explain how can I identify this order G is Manage order of A to set SL/TP from my server script? Remember all position/order have same lot size, same currency pair and I see TicketID is different for all of them.

Hassan-HS
07-18-2013, 05:01 PM
Dear hasan,

Which one of the positions that you managed it?,Let me suppose it's the second "B" and its ticket ID is 399111 .
See the following code , It's take each order form D - G and put it's object in COrder variable then,It get the value of OrderTicke property and when COrder.OrderTicket = 0 mean this order is not reference to any position in other word it is limit order and when COrder.OrderTicket = The t ticket ID of "B" we conclude the COrder is managed order for "B" position.



Public Sub main()

Dim COrder As VTLGeneral.Order
Dim i
Dim SL As Double
Dim TP As Double
Dim TicketID As integer ' The Ticket that you wlould to get it's SL & Tp
Dim OrderID 'The Order that mapped with desired ticket
TicketID = 399111 'The ID Of "B" position
For i = 1 To ClientCode.OrdersTotal() 'Loop from D to G order.
COrder = ClientCode.OrderByIndex(i)

' The OrderTicket property : descripe to which one ofthe positions this order is reference to.
'When COrder.OrderTicket = TicketID then I conclude -> the current order is refrence to desired position.
If COrder.OrderTicket =TicketID
SL=COrder.TP
TP=COrder.SL
GUI.MsgDialog("The Order " & COrder.OrderId & " is refrence to" & TicketID & " Ticket, SL: " & SL & " TP: " & TP)
end if


Next
End Sub

Hassan-HS
07-18-2013, 05:53 PM
Dear hasan

Every thing now is clear ?

nhasan
07-18-2013, 10:14 PM
Yes

If there is an example code in knowledge base for trailing stop - that will be very helpful for new comers to understand better and quickly

Thanks

Hassan-HS
07-19-2013, 07:01 AM
Dear,

Can you give me the flowchart or mql code describe to your idea?

nhasan
07-21-2013, 04:49 AM
This is an interesting project to convert a news trading MQL EA to VTL and then post that code on 15 different Forex forum!

I have almost completed it and will post it in this forum as part of the project.

Hassan-HS
07-21-2013, 07:45 AM
Good luck :D