Quote from fullautotrading:
... that piece of code, but I will do it now
In terms of "pseudocode" the changes needed should be as follow.
To obtain the global results i am clearly looping over all the instruments and accumulating positions, PNL, values, realized, unrealized, and so on.
So where we had the statements like:
Position_Signed_tmp += Instrument.Position_Signed
Value_Signed_tmp += Instrument.ValueOfPosition_Multiplied_Signed
Position_Abs_tmp += Math.Abs(Instrument.Position_Signed)
Value_Abs_tmp += Math.Abs(Instrument.ValueOfPosition_Multiplied_Signed)
[...]
i would replace the last two statements by, first of all, creating a new class, say something like:
Class NeutralizedSignedQuantitiesForClones
Public Position_Signed_Clones (an Integer)
Public Value_Signed_Clones (a Decimal)
End Class
to store the "neutralized" values of each subset of clones (such as CL, CL_C1, CL_C2, ...) and by defining a new "dictionary" (clearly to be "reset" at each loop) where to store all of these objects for all subset of clones (es: set of CL clones, set of EUR clones, ...): NeutralizedPositionForCLones = New Dictionary(Of String, NeutralizedSignedQuantitiesForClones)
where the "string" arg would be name of the "original" instrument cloned (stored in all the instruments belonging to the same subset of clones).
Then the 2 above instructions in the loop to find the absolute sum of positions and values would be substituted with something like:
define: NeutralizedSignedQuantitiesForClones As NeutralizedSignedQuantitiesForClones = Null (or Nothing)
If Not NeutralizedSignedQuantitiesForClones_All.TryGetValue(Instrument.Security.SymbolInProgram_OriginalOrCloned, NeutralizedSignedQuantitiesForClones) Then
NeutralizedSignedQuantitiesForClones = New NeutralizedSignedQuantitiesForClones
NeutralizedSignedQuantitiesForClones_All.Add(Instrument.Security.SymbolInProgram_OriginalOrCloned, NeutralizedSignedQuantitiesForClones)
End If
NeutralizedSignedQuantitiesForClones.Position_Signed_Clones += .Position_Signed
NeutralizedSignedQuantitiesForClones.Value_Signed_Clones += .ValueOfPosition_Multiplied_Signed
so we can remove the two statements (which would be now incorrect) and, at the end of the loop of accumulation, we would add up the abs values of the (partially aggregated) signed values (by looping on the subsets of clones):
For Each NeutralizedSignedQuantitiesForClones As NeutralizedSignedQuantitiesForClones In NeutralizedSignedQuantitiesForClones_All.Values
Position_Abs_tmp += Math.Abs(NeutralizedSignedQuantitiesForClones.Position_Signed_Clones)
Value_Abs_tmp += Math.Abs(NeutralizedSignedQuantitiesForClones.Value_Signed_Clones)
Next NeutralizedSignedQuantitiesForClones
I have now also added full support to folio "superimposition" in Backtesting/Robustness Analysis/Calibration, besides trading. So the infrastructure is now "complete".
An open and interesting area of research are the algorithms to "coordinate" the overlaying clone folios, as to <b>maximize the reciprocal hedging action</b>. Say the "engagement rules" of the clone instruments ;-) And there is where we need to dig now...
This discussion should also make evident how there are options to the absurd "single trade stop (or reverse)" for a meaningful hedging. But clearly, nobody will tell you, as your money is needed ;-)
Tom