1 Imports System
2 Imports System.Drawing
3 Imports System.Collections.Generic
4 Imports RightEdge.Common
5 Imports RightEdge.Indicators
6
7 Public Class SystemMain
8 Inherits SystemBase
9 Public Overrides Sub Startup()
10 ' Perform initialization or set system wide options here
11
12 ' Send a warning to the system output on reversals instead of throwing an exception
13 PositionManager.PositionOverfilledAction = PositionAction.SendWarning
14
15 ' Add event handlers for filled orders and overfilled positions
16 AddHandler PositionManager.PositionOverfilled, AddressOf PositionReversed
17 AddHandler PositionManager.OrderFilled, AddressOf OrderFilled
18 End Sub
19
20 Sub PositionReversed(ByVal sender As Object, ByVal e As PositionOverfilledEventArgs)
21
22 Dim msg As String = "Position reversed. Original open date: " & e.OverfilledPosition.OpenDate.ToShortDateString() & _
23 " Reversal Date: " & e.OverfilledPosition.Info.CloseDate.ToShortDateString()
24 SystemData.Output.Add(OutputSeverityLevel.Informational, msg, e.OverfilledPosition.Symbol, "System")
25
26 ' The profit target is not automatically set for reversed positions (since it's considered an exceptional situation)
27 ' So set the profit target here
28 PositionManager.SetProfitTarget(e.OpenedPosition.ID, PositionManager.ProfitTarget, True)
29 End Sub
30
31 Sub OrderFilled(ByVal sender As Object, ByVal e As OrderFilledEventArgs)
32 If (e.Position.State = PositionState.Open)
33 ' First cancel any existing user-submitted orders for this position
34 Dim orders As List(Of PositionManager.TradeOrderAndOrder) = PositionManager.GetPendingOrdersForPosition(e.Position.ID)
35 Dim trade As PositionManager.TradeOrderAndOrder
36 For Each trade In orders
37 If (trade.TradeOrder.TradeType = TradeType.UserSubmitted)
38 PositionManager.CancelOrder(e.Position.ID, trade.TradeOrder.OrderID)
39 End If
40 Next
41
42 ' Submit our stop loss order
43 Dim myOrder As OrderSettings = New OrderSettings()
44 ' We want to close the position and open a position in the opposite direction
45 ' for twice the size. So the total transaction size is the current size times 3
46 myOrder.Size = e.Position.CurrentStats.CurrentSize * 3
47 ' The stop percentage will be the same as our profit target percent
48 myOrder.StopPrice = e.Position.Info.GetTargetPrice(-PositionManager.ProfitTarget)
49 myOrder.OrderType = OrderType.Stop
50 myOrder.TransactionType = e.Position.Info.ExitTransType
51
52 Dim NewOrder As Order
53 NewOrder = PositionManager.SubmitOrder(e.Position.ID, myOrder)
54
55 If (Not NewOrder.Error = Nothing)
56 SystemData.Output.Add(OutputSeverityLevel.Warning, NewOrder.Error, e.Position.Symbol)
57 End If
58 End If
59 End Sub
60
61
62 Public Overrides Sub NewSymbolBar(ByVal symbol As Symbol, ByVal bar As BarData)
63 ' This line of code runs the actions you have set up in in the Project Form
64 Actions.RunActions(symbol)
65
66
67
68 Dim positions As List(Of Position) = PositionManager.GetOpenPositions(symbol)
69
70 Dim hasPending As Boolean = False
71 Dim pending As List(Of Position) = PositionManager.GetPendingPositions()
72 Dim pos As Position
73 For Each pos In pending
74 If (pos.Symbol = symbol)
75 hasPending = True
76 End If
77 Next
78
79 ' Open a long position of size 1 if there are no open or pending positions for this symbol
80 If (positions.Count = 0 And Not haspending)
81 OpenPosition(symbol, PositionType.Long, OrderType.Market, 0, 1)
82 End If
83 End Sub
84 End Class
85