forked from mlwin/SierraChart-Custom-Studies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySimOrderMgr.h
147 lines (115 loc) · 3.03 KB
/
MySimOrderMgr.h
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
#include <iostream>
#include <vector>
using namespace std;
// Simple Order Manager for sim testing without using built in one.
#define MY_DEBUG
class MySimOrderMgr
{
public:
static const int NO_ACTION = 0;
static const int LONG_TRADE = 1;
static const int SHORT_TRADE = 2;
static const int PROFIT_TARGET_HIT = 3;
static const int STOPPED_OUT = 4;
class TradePosition
{
public:
double m_EntryPrice;
int m_TradeType;
int Process(double lastTradedPrice, double ptTicks, double slTicks)
{
if (m_TradeType == LONG_TRADE)
{
if (lastTradedPrice > (m_EntryPrice + ptTicks))
{
#ifdef MY_DEBUG
cout << "PT hit on long trade @ " << m_EntryPrice << endl;
#endif
return PROFIT_TARGET_HIT;
}
else if (lastTradedPrice < (m_EntryPrice - slTicks))
{
#ifdef MY_DEBUG
cout << "SL hit on long trade @ " << m_EntryPrice << endl;
#endif
return STOPPED_OUT;
}
return NO_ACTION;
}
else if (m_TradeType == SHORT_TRADE)
{
if (lastTradedPrice < (m_EntryPrice - ptTicks))
{
#ifdef MY_DEBUG
cout << "PT hit on short trade @ " << m_EntryPrice << endl;
#endif
return PROFIT_TARGET_HIT;
}
else if (lastTradedPrice > (m_EntryPrice + slTicks))
{
#ifdef MY_DEBUG
cout << "SL hit on short trade @ " << m_EntryPrice << endl;
#endif
return STOPPED_OUT;
}
return NO_ACTION;
}
return NO_ACTION;
}
};
static MySimOrderMgr* Instance()
{
if (!m_pInstance)
{
m_pInstance = new MySimOrderMgr;
}
return m_pInstance;
}
void EnterLong(TradePosition tradeData)
{
tradeData.m_TradeType = MySimOrderMgr::LONG_TRADE;
m_pTradeDataVec->push_back(tradeData);
}
void EnterShort(TradePosition tradeData)
{
tradeData.m_TradeType = MySimOrderMgr::SHORT_TRADE;
m_pTradeDataVec->push_back(tradeData);
}
void Process(double lastTradedPrice)
{
int ret = 0;
for (vector<TradePosition>::iterator it = m_pTradeDataVec->begin(); it != m_pTradeDataVec->end(); )
{
ret = it->Process(lastTradedPrice, m_ProfitTargetTicks, m_StopLossTicks);
if (ret == PROFIT_TARGET_HIT)
{
// @@ save any data to analyze later
}
if (ret != NO_ACTION)
it = m_pTradeDataVec->erase(it);
else
it++;
}
#ifdef MY_DEBUG
cout << "Open positions: ";
for (unsigned i=0; i<m_pTradeDataVec->size(); ++i)
cout << m_pTradeDataVec->at(i).m_EntryPrice << ' ';
cout << "" << endl;
#endif
}
private:
static vector<TradePosition>* m_pTradeDataVec;
static MySimOrderMgr* m_pInstance;
double m_TickSize;
double m_ProfitTargetTicks;
double m_StopLossTicks;
MySimOrderMgr()
{
m_TickSize = 1.0;
m_ProfitTargetTicks = 15.0 * m_TickSize;
m_StopLossTicks = 15.0 * m_TickSize;
m_pTradeDataVec = new vector<TradePosition>();
}
};
MySimOrderMgr* MySimOrderMgr::m_pInstance = 0;
vector<MySimOrderMgr::TradePosition>* MySimOrderMgr::m_pTradeDataVec = 0;