PDA

View Full Version : Sever Script Issues



Edakad
04-08-2015, 06:56 AM
Hello,

The code I am attaching is VTL server script I am developing. It opens 2 stop orders, one buy and sell at the same time. When one of them gets triggered, the script cancels the other order and trails the stop loss for the open position.

The issue I am facing now is regarding the cancelled order. The script cancels the order and terminal displays no order. However the server script still identifies the order and fails to open new trades after finishing the first set of trades.

ie. It works fine for the first time the script is run, but the second time it is run on the same symbol, it gets the cancelled orders as still existing (bt not visible in terminal) and do not send new set of orders.

When testing the script please set the parameter "hoursStart" to the present hour vale in chart to instantly send 2 orders.

Thanks.


'''############################################### ######################################'''#### Script Name: OCO Server - One Cancels Other ######
'''#### Author : Joy Sebastian ######
'''#### e-mail : joy.edakad@gmail.com ######
'''#### Date : 16/02/2015 16:34:46 ######
'''#### Description: OCO EA opens 2 orders, one stop buy and ome stop sell. #####
'''#### When one of these 2 orders is filled, it cancels the other ######
'''#### order and trails the open trade with trailing StopLoss. Specify #####
'''#### start hour to send orders with HourStart parameter and also the #####
'''#### take profit, stop loss and trail stoploss in pips. The script ######
'''#### can be used to manage 2 orders place manually as OCO, for this ######
'''#### add the OCO script to chart after manually placing the orders. ######
'''############################################### ######################################


'''################### INPUT PARAMETERS ################################################## ###############


dim closeAtDayEnd as Boolean = true
dim distance as double = 5
dim stoploss as double = 50
dim takeProfit as double = 100
dim lot as double = 0.1
dim trailingStop as double = 5
dim hoursStart as integer = 9
dim ocoSymbol as string = "GBP/USD"
dim Point as Double = 0.0001 ''' Point Value, set as per number of decimal places for the Symbol


'''############################################### ################################################## ######


dim orderSent as boolean = false
dim openCounterOrder as boolean = false






private function OpenPendingOrder(pType,pLots,pLevel,sl, tp)


dim xAsk as double
dim xBid as double


xAsk=ClientCode.SymbolInfoDouble(ocoSymbol, VTLGeneral.ENUM_SYMBOL_INFO_DOUBLE.SYMBOL_Ask )
xBid=ClientCode.SymbolInfoDouble(ocoSymbol, VTLGeneral.ENUM_SYMBOL_INFO_DOUBLE.SYMBOL_BID )


Select case pType


case VTLServerApplication.OperationTypeEnum.BuyType


OpenPendingOrder = ClientCode.NewLimitOrder( VTLServerApplication.OperationTypeEnum.BuyType , ClientCode.GetSymbolByName(ocoSymbol).ID, CDBL(pLots), CDBL(xAsk-pLevel*point), CDBL((xAsk-pLevel*Point)-sl*Point), CDBL((xAsk-pLevel*Point)+tp*Point))
exit function


case VTLServerApplication.OperationTypeEnum.BuyStopType

OpenPendingOrder = ClientCode.NewLimitOrder( VTLServerApplication.OperationTypeEnum.BuyStopType , ClientCode.GetSymbolByName(ocoSymbol).ID, CDBL(pLots), CDBL(xAsk+pLevel*point), CDBL((xAsk+pLevel*Point)-sl*Point), CDBL((xAsk+pLevel*Point)+tp*Point))
exit function


case VTLServerApplication.OperationTypeEnum.SellType


OpenPendingOrder = ClientCode.NewLimitOrder( VTLServerApplication.OperationTypeEnum.SellType , ClientCode.GetSymbolByName(ocoSymbol).ID, CDBL(pLots), CDBL(xAsk+pLevel*point), CDBL((xAsk+pLevel*Point)+sl*Point), CDBL((xAsk+pLevel*Point)-tp*Point))
exit function

case VTLServerApplication.OperationTypeEnum.SellStopTyp e


OpenPendingOrder = ClientCode.NewLimitOrder( VTLServerApplication.OperationTypeEnum.SellStopTyp e , ClientCode.GetSymbolByName(ocoSymbol).ID, CDBL(pLots), CDBL(xAsk-pLevel*point), CDBL((xAsk-pLevel*Point)+sl*Point), CDBL((xAsk-pLevel*Point)-tp*Point))
exit function


end select


end function


private function isNewSymbol()


dim i as integer


for i = 1 to ClientCode.OrdersTotal()


if ClientCode.OrderByIndex (CINT(i)).SymbolName = ocoSymbol then


isNewSymbol = false
exit function


end if


next


isNewSymbol = true


end function




private sub closeOposite(xType)


dim totalOrders as integer
totalOrders = ClientCode.ordersTotal()


dim i as integer
for i = 1 to totalOrders
if ClientCode.OrderByIndex (CINT(i)).SymbolName = ocoSymbol and ClientCode.OrderByIndex (CINT(i)).OrderType = xType then
ClientCode.DeleteLimitOrder(ClientCode.OrderByInde x (CINT(i)).OrderId)

end if


next


end sub






private sub CloseDeleteAll()


dim i as integer


for i = 1 to ClientCode.positionsTotal()
if ClientCode.OpenPositionByIndex(i).SymbolName = ocoSymbol then
ClientCode.CloseMarketOrder( ClientCode.OpenPositionByIndex(i).Ticket, CDBL(lot) )
end if
next




for i = 1 to ClientCode.OrdersTotal()
if ClientCode.OrderByIndex (CINT(i)).SymbolName = ocoSymbol then

ClientCode.DeleteLimitOrder(ClientCode.OrderByInde x (CINT(i)).OrderId)
end if

next


end sub






Public Sub main()


GUI.MsgDialog (ClientCode.OrdersTotal())


dim i as integer
for i = 1 to ClientCode.OrdersTotal()


GUI.MsgDialog (ClientCode.OrderByIndex (CINT(i)).OrderId)
GUI.MsgDialog (ClientCode.OrderByIndex (CINT(i)).SymbolName)


next








end sub






Public Sub OnTick()

dim curDate as System.DateTime
dim curHour as integer
curDate = System.DateTime.Now()
curHour = curDate.Hour


if (ClientCode.OrdersTotal() < 1 or isNewSymbol()) and curHour = hoursStart and not ordersent then


orderSent = true
OpenPendingOrder(VTLServerApplication.OperationTyp eEnum.BuyStopType ,lot,distance,stopLoss, takeprofit)
OpenPendingOrder(VTLServerApplication.OperationTyp eEnum.SellStopType ,lot,distance,stopLoss, takeprofit)

end if


if CloseAtDayEnd and curHour = hoursStart-1 then

CloseDeleteAll()


end if




dim i as integer
dim j as integer
dim xAsk as double
dim xBid as double


xAsk=ClientCode.SymbolInfoDouble(ocoSymbol, VTLGeneral.ENUM_SYMBOL_INFO_DOUBLE.SYMBOL_Ask )
xBid=ClientCode.SymbolInfoDouble(ocoSymbol, VTLGeneral.ENUM_SYMBOL_INFO_DOUBLE.SYMBOL_BID )




for i = 1 to ClientCode.PositionsTotal()

if ClientCode.OpenPositionByIndex(i).SymbolName = ocoSymbol and ClientCode.OpenPositionByIndex(i).BuySell = VTLServerApplication.OperationTypeEnum.BuyType then

closeOposite(VTLServerApplication.OperationTypeEnu m.SellStopType)
if ClientCode.OpenPositionByIndex(i).FloatingProfitLo ss > Point*trailingStop then
for j =1 to ClientCode.OrdersTotal()
if ClientCode.OrderByIndex (CINT(j)).SymbolName = ocoSymbol and ClientCode.OrderByIndex (CINT(j)).BuySell = -1 then
if ClientCode.OrderByIndex (CINT(j)).SL < xBid - Point*trailingStop Then
ClientCode.UpdateSLTPOrder(ClientCode.OrderByIndex (CINT(j)).OrderId, CDBL(lot), xBid - Point*trailingStop, ClientCode.OrderByIndex (CINT(j)).TP)
end if


end if


next


end if


end if


if ClientCode.OpenPositionByIndex(i).SymbolName = ocoSymbol and ClientCode.OpenPositionByIndex(i).BuySell = VTLServerApplication.OperationTypeEnum.SellType then
closeOposite(VTLServerApplication.OperationTypeEnu m.BuyStopType)


if ClientCode.OpenPositionByIndex(i).FloatingProfitLo ss > Point*trailingStop then
for j =1 to ClientCode.OrdersTotal()
if ClientCode.OrderByIndex (CINT(j)).SymbolName = ocoSymbol and ClientCode.OrderByIndex (CINT(j)).BuySell = 1 then
if ClientCode.OrderByIndex (CINT(j)).SL > xAsk + Point*trailingStop Then
ClientCode.UpdateSLTPOrder(ClientCode.OrderByIndex (CINT(j)).OrderId, CDBL(lot), xAsk + Point*trailingStop, ClientCode.OrderByIndex (CINT(j)).TP)
end if


end if


next


end if




end if


next


End Sub

Rawan-HS
06-21-2018, 12:19 PM
Dear Edakad

I'm glad to inform you that this issue is solved in version 11 which we made a lot of enhancement on the VTL server. But you have to modify your code by doing the following :


- Check if the orders Total is equal zero to open 2 Limits Orders, otherwise, check orders Type if Manage order or a Limit order by checking the open price for the order, If open price = 0 then its a managed order
- If all the orders was a managed order so the order total for you must be zero then you have to open a new 2 limit order, and so on

Let me know if you need anything .

Edakad
06-26-2018, 07:57 AM
Hi

Will check and come back soon. Thanks