Quote from Murray Ruggiero:
Let's now discuss the concept of trading a larger portfiolo and limited the number of trades which can be open at one time to a given number. TradersStudio has two major concepts, session and tradeplans. Sessions are one system run on one or more markets. Tradeplans are a meta control of session, sessions run one bar at a time and then control is passed to the tradeplan. It has access to all the market and session objects. It can modify orders and place new orders.
You can see below an example tradeplan which trades a large stock portfiolo and limits open positions. It is also limited to a 1 position per day.
' This tradeplan limits the number of open positions based on ranking
'TradersStudio 2009-2011 all rights reserved
Sub TSPro_StockCustomPerAdvanced(MaxPositions)
Dim M As Integer
Dim Index As Integer
Dim SessCount As Integer
Dim DollarsPerTrade
Dim Rating
Dim CustPer As Array
If TradePlan.MarketType <> 3 Then
MsgBox ("This trade plan can only be run on a TradersStudio Stock Plan")
StopRun
End If
SessCount = TradePlan.SessionCount
For Index = 0 To SessCount - 1
DollarsPerTrade = TradePlan.SummEquity / MaxPositions
TradePlan.Session(Index).UnitSize = 1
CustPer = MyCustomPerformanceEx(Index)
TradePlan.Session(Index).SetCustomPerformanceEx(CustPer)
TradePlan.Session(Index).RankingType= Ordinal
' For each session Loop though the trading plans.
For M = 0 To TradePlan.Session(Index).MarketCount - 1
Rating = TradePlan.Session(Index).GetCustomPerformanceRankEx(TradePlan.Session(Index).Market(M).Symbol(0))
Dim iGlobalCount,iActiveOrderCount
TradePlan.Session(Index).SuperCustomRank(TradePlan.Session(Index).Market(M).Symbol(0),iGlobalCount,iActiveOrderCount)
If tradeplan.OpenTradeCount <MaxPositions Then
If iActiveOrderCount=1 And iGlobalCount<MaxPositions Then
TradePlan.Session(Index).Market(M).EntryNumUnits = Floor((DollarsPerTrade) / TradePlan.Session(Index).Market(M).Data(0, "TSCLose", 0))
Else
TradePlan.Session(Index).Market(M).EntryNumUnits = 0
End If
End If
TradePlan.Session(Index).Market(M).ExitNumUnits = TradePlan.Session(Index).Market(M).NumContractsHeld
Next
Next
End Sub
Here is a custom ranking function it used in the code above. It's also used so we only rank markets with a active entry.
Function MyCustomPerformanceEx(Index As Integer) As Array
Dim MCount As Integer
Dim CustomPer As Array
Dim count As Integer
MCount = TradePlan.Session(Index).MarketCount
ReDim(CustomPer, MCount, 3)
For count = 0 To MCount - 1
' If TradePlan.Session(Index).Market(count).HasActiveOrder = True Then
CustomPer[count, 0] = TradePlan.Session(Index).Market(count).Symbol(0)
CustomPer[count, 1] = TradePlan.Session(Index).Market(count).Data(0, "TSCLose", 0)/TradePlan.Session(Index).Market(count).data(0, "TSCLose", 40)
CustomPer[count, 2] =TradePlan.Session(Index).Market(count).IsActiveOrders(False)
'EndIf
Next
MyCustomPerformanceEx = CustomPer
End Function