Auto Stop Loss and Take Profit is a server side VTL Script. This is a VTL expert adviser that applies a stop loss and take profit to all open positions. It runs on the server and monitor all open positions. This makes it a handy tool for scalpers, where execution speed is critical. The EA will add stop loss and take profit when new positions are open
[You must be registered and logged in to see this image.]
- Code:
'''#####################################################################################
'''#### Script Name: Ronz Auto Stop and Take Profit ######
'''#### Author : ######
'''#### e-mail : ######
'''#### Date : 19-07-2015 10:34:18 ######
'''#### Description: This script adds a stop loss and take profit to all open ######
'''#### Open Positions without stop loss and take profit. By setting the ######
'''#### 'ChartSymbolSelection' parameter to 0, the script works on the ######
'''#### symbol specified by the Symbol Parameter. The stop and target ######
'''#### can be customized with SL and TP parameters. Useful to set stop ######
'''##### and take profit on all trades automatically. ######
'''#####################################################################################
'''#################### PARAMETERS #####################################################
Dim TakeProfit As Integer = 500
Dim StopLoss As Integer = 250
Dim ChartSymbolSelection = 1 ''' 0 - Symbol, 1 - All Symbols
Dim Symbol As String = "EUR/USD"
Dim Point As Double = 0.00001 ''' Set point Value as per number of digits used by broker.
''' The point value also affects the TP and SL parameter.
'''#####################################################################################
Private Function CalculateCurrentOrders() As Integer
Dim Buys As Integer = 0
Dim Sells As Integer = 0
Dim i As Integer
dim cPos As VTLGeneral.CPosition
For i = 1 to ClientCode.PositionsTotal()
cPos = ClientCode.OpenPositionByIndex(i)
if ChartSymbolSelection = 0 and cPos.SymbolName <> Symbol then
''' Do nothing
else
if cPos.BuySell > 0 then ''' Buy Position
buys = buys +1
end if
if cPos.BuySell < 0 then
sells = Sells + 1
end if
end if
next
if buys > 0 then
CalculateCurrentOrders = buys
else
CalculateCurrentOrders = -Sells
end if
End Function
Function SetSLnTP() As Boolean
Dim SL As Double = 0.0
Dim TP As Double = 0.0
Dim i As Integer
dim cPos As VTLGeneral.CPosition
For i = 1 to ClientCode.PositionsTotal()
cPos = ClientCode.OpenPositionByIndex(i)
if ChartSymbolSelection = 0 and cPos.SymbolName <> Symbol then
''' Do nothing
else
if cPos.BuySell > 0 then
SL = cPos.Price - StopLoss*Point
TP = cPos.Price + TakeProfit*Point
''' Find Manage order for position
Dim j As Integer
Dim cOrd As VTLGeneral.Order
Dim res As Integer = 0
Dim ManageOrderFound As Boolean = false
for j = 1 to ClientCode.OrdersTotal()
cOrd = ClientCode.OrderByIndex(j)
if cPos.Ticket = cOrd.OrderTicket then
ManageOrderFound = true
''' ManageOrders track is there is Manage order for the position. If not
''' It creates a new manage SLTP order
if cOrd.TP = 0.0 then
res = ClientCode.UpdateSLTPOrder(cOrd.OrderId, cOrd.Lots, cOrd.SL, CDBL(TP))
else
if cOrd.SL = 0.0 then
res = ClientCode.UpdateSLTPOrder(cOrd.OrderId, cOrd.Lots, CDBL(SL), cOrd.TP)
end if
end if
end if
next
if ManageOrderFound = false then
res = ClientCode.NewSLTPOrder(cPos.Ticket,cPos.Lots, CDBL(SL), CDBL(TP))
end if
else
if cPos.BuySell < 0 then
SL = cPos.Price + StopLoss*Point
TP = cPos.Price - TakeProfit*Point
''' Find Manage order for position
Dim j As Integer
Dim cOrd As VTLGeneral.Order
Dim res As Integer
Dim ManageOrderFound As Boolean = false
for j = 1 to ClientCode.OrdersTotal()
cOrd = ClientCode.OrderByIndex(j)
if cPos.Ticket = cOrd.OrderTicket then
ManageOrderFound = true
if cOrd.TP = 0.0 then
res = ClientCode.UpdateSLTPOrder(cOrd.OrderId, cOrd.Lots, cOrd.SL, CDBL(TP))
else
if cOrd.SL = 0.0 then
res = ClientCode.UpdateSLTPOrder(cOrd.OrderId, cOrd.Lots, CDBL(SL), cOrd.TP)
end if
end if
end if
next
if ManageOrderFound = false then
res = ClientCode.NewSLTPOrder(cPos.Ticket,cPos.Lots, CDBL(SL), CDBL(TP))
end if
end if
end if
end if
next
SetSLnTP = false
End Function
''' <summary>
''' The main function is the entry point for any Script program
''' </summary>
Public Sub main()
End Sub
''' <summary>
''' The function is generated when a new tick is received for any symbol
''' </summary>
Public Sub OnTick()
if CalculateCurrentOrders() <> 0 then
SetSLnTP()
end if
End Sub
''' <summary>
''' Generatd after calling the CloseMarketOrder and receiving its result from the server
''' </summary>
Public Sub CloseOrderResult(ByRef Result As VTLServerApplication.NewOrderResultEnum, ByRef AtPrice As Double, ByRef PosOrOrder As Short, ByRef OrderId As Integer, ByRef BuySell As VTLServerApplication.OperationTypeEnum, ByRef Lots As Double, ByRef Symbol As VTLServerApplication.CSymbol, ByRef ServerTime As String, ByRef OpenTicket As Integer)
'''TODO: Implement the handler
End Sub
''' <summary>
''' Generatd after calling the NewMarketOrder and receiving its result from the server
''' </summary>
Public Sub NewOrderResult(ByRef Result As VTLServerApplication.NewOrderResultEnum, ByRef AtPrice As Double, ByRef PosOrOrder As Short, ByRef OrderId As Integer, ByRef BuySell As VTLServerApplication.OperationTypeEnum, ByRef Lots As Double, ByRef Symbol As VTLServerApplication.CSymbol, ByRef ServerTime As String)
'''TODO: Implement the handler
End Sub
''' <summary>
''' Generatd after calling the NewLimitOrder and receiving its result from the server
''' </summary>
Public Sub NewLimitOrderResult(ByRef Result As VTLServerApplication.NewOrderResultEnum, ByRef OrderId As Integer, ByRef ServerTime As String)
'''TODO: Implement the handler
End Sub
''' <summary>
''' Generatd after calling the NewSLTPOrder and receiving its result from the server
''' </summary>
Public Sub NewSLTPOrderResult(ByRef Result As VTLServerApplication.NewOrderResultEnum, ByRef OrderId As Integer, ByRef ServerTime As String)
End Sub
''' <summary>
''' Generatd after calling the UpdateLimitOrder and receiving its result from the server
''' </summary>
Public Sub UpdateLimitOrderResult(ByRef Result As VTLServerApplication.NewOrderResultEnum, ByRef OrderId As Integer, ByRef ServerTime As String)
'''TODO: Implement the handler
End Sub
''' <summary>
''' Generatd after calling the DeleteLimitOrder and receiving its result from the server
''' </summary>
Public Sub DeleteLimitOrderResult(ByRef Result As VTLServerApplication.NewOrderResultEnum, ByRef OrderId As Integer)
'''TODO: Implement the handler
End Sub
''' <summary>
''' Generatd after calling the DeleteSLTPOrder and receiving its result from the server
''' </summary>
Public Sub DeleteSLTPOrderResult(ByRef Result As VTLServerApplication.NewOrderResultEnum, ByRef OrderId As Integer)
'''TODO: Implement the handler
End Sub
''' <summary>
''' Generatd after calling the UpdateSLTPOrder and receiving its result from the server
''' </summary>
Public Sub UpdateSLTPOrderResult(ByRef Result As VTLServerApplication.NewOrderResultEnum, ByRef OrderId As Integer, ByRef ServerTime As String)
End Sub