-
Notifications
You must be signed in to change notification settings - Fork 6
/
From_Broker_To_Strategy_MP_Synchronizer.Strategy.CS
101 lines (84 loc) · 3.54 KB
/
From_Broker_To_Strategy_MP_Synchronizer.Strategy.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
using System;
namespace PowerLanguage.Strategy
{
[IOGMode(IOGMode.Enabled)]
public class From_Broker_To_Strategy_MP_Synchronizer : SignalObject
{
private ITextObject m_textid;
private string m_sync_state;
private string m_diff_state;
private DateTime m_mp_diff_detected_time;
private bool m_mp_diff;
private bool m_mp_corrected;
private DateTime m_mp_diff_time_start;
private DateTime m_mp_corrected_time_start;
public From_Broker_To_Strategy_MP_Synchronizer(object ctx) :
base(ctx){
LatencyMS = 500;
TimeOutMS = 1000;
}
[Input]
public int TimeOutMS { get; set; }
[Input]
public int LatencyMS { get; set; }
protected override void StartCalc(){
m_textid = Environment.ApplicationCode == EApplicationCode.Portfolio ? null : DrwText.Create(new ChartPoint(Bars.Time[0], Bars.Close[0]), "CurrentState");
if (m_textid != null) {
m_textid.HStyle = ETextStyleH.Left;
m_textid.VStyle = ETextStyleV.Below;
}
m_sync_state = "MP is synchronized!";
m_diff_state = string.Format("MP syncronization.\nWait {0} seconds", TimeOutMS*0.001);
m_mp_diff = false;
m_mp_corrected = false;
}
protected override void Destroy() {}
protected override void CalcBar(){
if (!Environment.IsRealTimeCalc || !Environment.IsAutoTradingMode)
return;
{
var m_inner_mp = StrategyInfo.MarketPosition;
var m_broker_mp = StrategyInfo.MarketPositionAtBroker;
if (m_textid != null)
m_textid.Location = new ChartPoint(Environment.RightScreenTime, Environment.HighestScaleValue);
if (m_broker_mp != m_inner_mp){
m_mp_diff_detected_time = DateTime.Now;
if (!m_mp_diff && !m_mp_corrected){
m_mp_diff = true;
m_mp_diff_time_start = m_mp_diff_detected_time;
if (m_textid != null)
m_textid.Text = m_diff_state;
}
var _exit_price = .0;
var _place_order = false;
if (m_mp_diff && !m_mp_corrected){
_exit_price = Bars.Close[0];
if ((m_mp_diff_detected_time - m_mp_diff_time_start).TotalMilliseconds >= TimeOutMS){
_place_order = true;
m_mp_corrected = true;
m_mp_corrected_time_start = m_mp_diff_detected_time;
}
}
if (m_mp_corrected){
if ((m_mp_diff_detected_time - m_mp_corrected_time_start).TotalMilliseconds >= LatencyMS){
m_mp_corrected_time_start = m_mp_diff_detected_time;
m_mp_diff = false;
m_mp_corrected = false;
}
}
if (_place_order){
if (0 != m_broker_mp)
_exit_price = StrategyInfo.AvgEntryPriceAtBroker;
ChangeMarketPosition(m_broker_mp - m_inner_mp, _exit_price, "Sync Order");
}
}
else{
if (m_textid != null)
m_textid.Text = m_sync_state;
m_mp_corrected = false;
m_mp_diff = false;
}
}
}
}
}