forked from silahian/VisualHFT
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HelperCommon.cs
210 lines (179 loc) · 8.61 KB
/
HelperCommon.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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using VisualHFT.DataTradeRetriever;
namespace VisualHFT.Helpers
{
public class HelperCommon
{
public static DateTime GetCurrentSessionIni(DateTime? currentSessionDate)
{
currentSessionDate = currentSessionDate.ToDate().AddHours(HelperTimeProvider.Now.Hour).AddMinutes(HelperTimeProvider.Now.Minute);
if (!currentSessionDate.HasValue)
currentSessionDate = HelperTimeProvider.Now;
DateTime DateAt00 = currentSessionDate.ToDate();
DateTime DateAt500PM = DateAt00.AddHours(17);
DateTime DateAt530PM = DateAt00.AddHours(17).AddMinutes(30);
if (currentSessionDate > DateAt500PM && currentSessionDate < DateAt00.AddDays(1))
return DateAt530PM;
else if (currentSessionDate > DateAt00 && currentSessionDate < DateAt500PM)
return DateAt530PM.AddDays(-1);
else
return HelperTimeProvider.Now;
}
public static DateTime GetCurrentSessionEnd(DateTime? currentSessionDate)
{
currentSessionDate = currentSessionDate.ToDate().AddHours(HelperTimeProvider.Now.Hour).AddMinutes(HelperTimeProvider.Now.Minute);
if (!currentSessionDate.HasValue)
currentSessionDate = HelperTimeProvider.Now;
DateTime DateAt00 = currentSessionDate.ToDate();
DateTime DateAt500PM = DateAt00.AddHours(17);
DateTime DateAt530PM = DateAt00.AddHours(17).AddMinutes(30);
if (currentSessionDate > DateAt500PM && currentSessionDate < DateAt00.AddDays(1))
return DateAt500PM.AddDays(1);
else if (currentSessionDate > DateAt00 && currentSessionDate < DateAt500PM)
return DateAt500PM;
else
return HelperTimeProvider.Now;
}
public static int TimerMillisecondsToGetVariables = 1000 * 10; //10 seconds
public static IDataTradeRetriever EXECUTEDORDERS = new GenericTradesRetriever();
//public static IDataTradeRetriever EXECUTEDORDERS = new MSSQLServerTradesRetriever();
//public static IDataTradeRetriever EXECUTEDORDERS = new FIXTradesRetriever([path_to_fix_log_file], 1, "CME");
//public static HelperExposure EXPOSURES = new HelperExposure();
//public static HelperActiveOrder ACTIVEORDERS = new HelperActiveOrder();
public static HelperStrategy ACTIVESTRATEGIES = new HelperStrategy();
public static HelperStrategyParams STRATEGYPARAMS = new HelperStrategyParams();
public static Func<string, string, bool> GetPopup()
{
return (Func<string, string, bool>)((msg, capt) => MessageBox.Show(msg, capt, MessageBoxButton.OK, MessageBoxImage.Information) == MessageBoxResult.OK);
}
public static Func<string, string, bool> GetConfirmPopup()
{
return (Func<string, string, bool>)((msg, capt) => MessageBox.Show(msg, capt, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes);
}
public static Func<string, string, bool> GetValidationPopup()
{
return (Func<string, string, bool>)((msg, capt) => MessageBox.Show(msg, capt, MessageBoxButton.OK, MessageBoxImage.Asterisk) == MessageBoxResult.Yes);
}
public static Func<string, string, bool> GetErrorPopup()
{
return (Func<string, string, bool>)((msg, capt) => MessageBox.Show(msg, capt, MessageBoxButton.OK, MessageBoxImage.Error) == MessageBoxResult.Yes);
}
public static Dictionary<string, Func<string, string, bool>> GLOBAL_DIALOGS = new Dictionary<string, Func<string, string, bool>>()
{
{"popup", GetPopup() },
{"confirm", GetConfirmPopup() },
{"validation", GetValidationPopup() },
{"error", GetErrorPopup() }
};
public static void CreateCommonPopUpWindow(FrameworkElement ctrlToSendIntoNewWindow, Button butPopUp, object dataContext, string titleWindow = "New Window", int widthWindow = 800, int heightWindow = 600, ResizeMode resizeMode = ResizeMode.CanResize)
{
// Create a new window
Window window = new Window
{
Width = widthWindow, // Set the width of the window
Height = heightWindow, // Set the height of the window
Title = titleWindow // Set the title of the window
,
ResizeMode = resizeMode
};
Grid parent = (Grid)ctrlToSendIntoNewWindow.Parent;
// Create a placeholder control
var placeholder = new StackPanel
{
Orientation = Orientation.Vertical,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
// Handle the Loaded event of the new window
window.Loaded += (s, e3) =>
{
// Remove the chart from its current parent
parent.Children.Remove(ctrlToSendIntoNewWindow);
// Add a message to the placeholder
placeholder.Children.Add(new TextBlock
{
Text = "The chart is being displayed in a new window.",
TextAlignment = TextAlignment.Center
});
// Add a button to the placeholder
var button = new Button
{
Content = "Focus Window"
};
button.Click += (s1, e1) => window.Focus();
placeholder.Children.Add(button);
// Add the placeholder to the original parent
parent.Children.Add(placeholder);
// Add the chart to the window
ctrlToSendIntoNewWindow.DataContext = dataContext;
window.Content = ctrlToSendIntoNewWindow;
butPopUp.Visibility = Visibility.Collapsed;
};
// Handle the Closed event of the new window
window.Closed += (s, e3) =>
{
// Remove the chart from the window
window.Content = null;
// Remove the placeholder from the original parent
parent.Children.Remove(placeholder);
// Add the chart back to its original parent
parent.Children.Add(ctrlToSendIntoNewWindow);
butPopUp.Visibility = Visibility.Visible;
};
// Show the window
window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
window.Show();
}
public static string GetKiloFormatter(int num)
{
return GetKiloFormatter((double)num);
}
public static string GetKiloFormatter(decimal num)
{
return GetKiloFormatter((double)num);
}
public static string GetKiloFormatter(double num)
{
if (num < 500)
return num.ToString("N2");
if (num < 10000)
return num.ToString("N0");
if (num >= 100000000)
return (num / 1000000D).ToString("0.#M");
if (num >= 1000000)
return (num / 1000000D).ToString("0.##M");
if (num >= 100000)
return (num / 1000D).ToString("0.#k");
if (num >= 10000)
return (num / 1000D).ToString("0.##k");
return num.ToString("#,0");
}
public static string GetKiloFormatterTime(double milliseconds)
{
double num = milliseconds;
if (num >= 1000 * 60 * 60.0)
return (num / (60.0 * 60.0 * 1000D)).ToString("0.0 hs");
if (num >= 1000 * 60.0)
return (num / (60.0 * 1000D)).ToString("0.0 min");
if (num >= 1000)
return (num / 1000D).ToString("0.0# sec");
return num.ToString("#,0 ms");
}
public static string GetEnumDescription(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
return attributes[0].Description;
else
return value.ToString();
}
}
}