-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
BasketPortfolio.cs
213 lines (176 loc) · 6.09 KB
/
BasketPortfolio.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#region S# License
/******************************************************************************************
NOTICE!!! This program and source code is owned and licensed by
StockSharp, LLC, www.stocksharp.com
Viewing or use of this code requires your acceptance of the license
agreement found at https://github.com/StockSharp/StockSharp/blob/master/LICENSE
Removal of this comment is a violation of the license agreement.
Project: StockSharp.Algo.Algo
File: BasketPortfolio.cs
Created: 2015, 11, 11, 2:32 PM
Copyright 2010 by StockSharp, LLC
*******************************************************************************************/
#endregion S# License
namespace StockSharp.Algo
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Ecng.Common;
using Ecng.Collections;
using StockSharp.BusinessEntities;
/// <summary>
/// Basket portfolio.
/// </summary>
public abstract class BasketPortfolio : Portfolio
{
/// <summary>
/// Portfolios from which this basket is created.
/// </summary>
[Browsable(false)]
public abstract IEnumerable<Portfolio> InnerPortfolios { get; }
/// <summary>
/// Positions from which this basket is created.
/// </summary>
[Browsable(false)]
public abstract IEnumerable<BasketPosition> InnerPositions { get; }
}
/// <summary>
/// Portfolios basket based on the weights <see cref="Weights"/>.
/// </summary>
public class WeightedPortfolio : BasketPortfolio
{
private sealed class WeightsDictionary : CachedSynchronizedDictionary<Portfolio, decimal>
{
private sealed class WeightedPosition : BasketPosition
{
public WeightedPosition(WeightedPortfolio portfolio, IEnumerable<Position> innerPositions)
{
_innerPositions = innerPositions ?? throw new ArgumentNullException(nameof(innerPositions));
decimal? beginValue = null;
decimal? currentValue = null;
decimal? blockedValue = null;
foreach (var position in _innerPositions)
{
var mult = portfolio.Weights[position.Portfolio];
if (beginValue == null)
beginValue = mult * position.BeginValue;
else
beginValue += mult * position.BeginValue;
if (currentValue == null)
currentValue = mult * position.CurrentValue;
else
currentValue += mult * position.CurrentValue;
if (blockedValue == null)
blockedValue = mult * position.BlockedValue;
else
blockedValue += mult * position.BlockedValue;
}
BeginValue = beginValue;
BlockedValue = blockedValue;
CurrentValue = currentValue;
}
private readonly IEnumerable<Position> _innerPositions;
public override IEnumerable<Position> InnerPositions => _innerPositions;
}
private readonly WeightedPortfolio _parent;
private readonly IConnector _connector;
public WeightsDictionary(WeightedPortfolio parent, IConnector connector)
{
_parent = parent ?? throw new ArgumentNullException(nameof(parent));
_connector = connector;
}
public IEnumerable<BasketPosition> Positions
{
get
{
return CachedKeys
.SelectMany(pf => _connector.Positions.Where(pos => pos.Portfolio == pf))
.GroupBy(pos => pos.Security)
.Select(g => new WeightedPosition(_parent, g));
}
}
public override void Add(Portfolio key, decimal value)
{
base.Add(key, value);
((INotifyPropertyChanged)key).PropertyChanged += OnPortfolioChanged;
RefreshName();
}
public override bool Remove(Portfolio key)
{
if (base.Remove(key))
{
((INotifyPropertyChanged)key).PropertyChanged -= OnPortfolioChanged;
RefreshName();
return true;
}
return false;
}
public override void Clear()
{
foreach (var portfolio in CachedKeys)
Remove(portfolio);
}
private void OnPortfolioChanged(object sender, PropertyChangedEventArgs e)
{
RefreshParent();
}
private void RefreshName()
{
_parent.Name = CachedPairs.Select(p => $"{p.Value}*{p.Key}").JoinCommaSpace();
RefreshParent();
}
private void RefreshParent()
{
var currencyType = _parent.Currency;
var beginValue = 0m.ToCurrency(currencyType ?? CurrencyTypes.USD);
var currentValue = 0m.ToCurrency(currencyType ?? CurrencyTypes.USD);
var leverage = 0m.ToCurrency(currencyType ?? CurrencyTypes.USD);
var commission = 0m.ToCurrency(currencyType ?? CurrencyTypes.USD);
foreach (var pair in CachedPairs)
{
var portfolio = pair.Key;
var weight = pair.Value;
beginValue += Multiple(beginValue, weight, portfolio.BeginValue);
currentValue += Multiple(currentValue, weight, portfolio.CurrentValue);
leverage += Multiple(leverage, weight, portfolio.Leverage);
commission += Multiple(commission, weight, portfolio.Commission);
}
_parent.BeginValue = beginValue.Value;
_parent.CurrentValue = currentValue.Value;
_parent.Leverage = leverage.Value / Count;
_parent.Commission = commission.Value;
}
private static Currency Multiple(Currency currency, decimal weight, decimal? part)
{
if (currency is null)
throw new ArgumentNullException(nameof(currency));
if (part is null)
throw new ArgumentNullException(nameof(part));
return (currency.Value * weight * part.Value).ToCurrency(currency.Type);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="WeightedPortfolio"/>.
/// </summary>
/// <param name="connector">The connection of interaction with trade systems.</param>
public WeightedPortfolio(IConnector connector)
{
_weights = new WeightsDictionary(this, connector);
}
private readonly WeightsDictionary _weights;
/// <summary>
/// Instruments and their weighting coefficients in the basket.
/// </summary>
public SynchronizedDictionary<Portfolio, decimal> Weights => _weights;
/// <summary>
/// Portfolios from which this basket is created.
/// </summary>
public override IEnumerable<Portfolio> InnerPortfolios => _weights.CachedKeys;
/// <summary>
/// Positions from which this basket is created.
/// </summary>
public override IEnumerable<BasketPosition> InnerPositions => _weights.Positions;
}
}