Repository navigation
Expand file tree
/
Copy pathProfitExtensions.cs
More file actions
145 lines (123 loc) · 4.73 KB
/
Copy pathProfitExtensions.cs
File metadata and controls
145 lines (123 loc) · 4.73 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
namespace TSLab.Script.Handlers
{
internal class WholeProfitState
{
public IList<IPosition> Active { get; private set; } = new List<IPosition>();
public double ClosedProfitCache { get; private set; }
public TradeDirection2 Direction { get; }
private readonly IPositionsList m_list;
private int m_lastBar;
private int m_positionsCount;
public WholeProfitState(IPositionsList list, TradeDirection2 direction = TradeDirection2.All)
{
m_list = list;
Direction = direction;
}
public void ProcessBar(int barNum)
{
var currentPositionsCount = GetPositionsCount();
if (m_positionsCount != currentPositionsCount)
{
m_lastBar = 0;
}
else
{
foreach (var position in Active)
{
if (position.IsActiveForBar(barNum))
continue;
m_lastBar = 0;
break;
}
}
if (m_lastBar != 0 && m_lastBar <= barNum)
return;
ClosedProfitCache = 0;
Active.Clear();
var closedCount = 0;
foreach (var pos in m_list)
{
if (Direction == TradeDirection2.Buys && !pos.IsLong)
continue;
if (Direction == TradeDirection2.Sells && !pos.IsShort)
continue;
if (pos.EntryBarNum > barNum)
{
currentPositionsCount--; //GLSP-3176
continue;
}
if (pos.IsActive || pos.ExitBarNum > barNum)
{
Active.Add(pos);
continue;
}
closedCount++;
ClosedProfitCache += pos.Profit();
}
m_positionsCount = currentPositionsCount;
m_lastBar = Active.Count + (closedCount == m_positionsCount ? barNum : 0);
}
private int m_lastPositionsCount;
private int m_positionsCountByDirection;
private int GetPositionsCount()
{
switch (Direction)
{
case TradeDirection2.All:
return m_list.PositionCount;
case TradeDirection2.Buys:
if (m_lastPositionsCount != m_list.PositionCount)
{
m_positionsCountByDirection = m_list.Count(x => x.IsLong);
m_lastPositionsCount = m_list.PositionCount;
}
return m_positionsCountByDirection;
case TradeDirection2.Sells:
if (m_lastPositionsCount != m_list.PositionCount)
{
m_positionsCountByDirection = m_list.Count(x => x.IsShort);
m_lastPositionsCount = m_list.PositionCount;
}
return m_positionsCountByDirection;
default:
throw new NotImplementedException();
}
}
}
internal static class ProfitExtensions
{
public static double GetProfit(this WholeProfitState state, int barNum)
{
var result = state.Active.Sum(p => p.CurrentProfit(barNum)) + state.ClosedProfitCache;
return result;
}
public static double GetProfitMin(this WholeProfitState state, int barNum)
{
var result = state.Active.Sum(p => p.CurrentProfitMin(barNum)) + state.ClosedProfitCache;
return result;
}
public static double GetProfit(this IEnumerable<IPosition> positions, int barNum)
{
var result = positions.Sum(item => item.GetProfit(barNum));
return result;
}
public static double GetProfit(this IPosition position, int barNum)
{
var result = position.EntryBarNum > barNum ? 0 : position.IsActiveForBar(barNum) ? position.CurrentProfit(barNum) : position.Profit();
return result;
}
public static double GetAccumulatedProfit(this WholeProfitState state, int barNum)
{
var result = state.Active.Sum(p => p.GetAccumulatedProfit(barNum)) + state.ClosedProfitCache;
return result;
}
public static double GetAccumulatedProfit(this IPosition position, int barNum)
{
var result = position.EntryBarNum > barNum ? 0 : position.IsActiveForBar(barNum) ? position.GetAccumulatedProfit(barNum) : position.Profit();
return result;
}
}
}