/
FakeOutExample.cs
317 lines (242 loc) · 9.43 KB
/
FakeOutExample.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
using System.Collections.Generic;
using System.Drawing;
using OsEngine.Entity;
using OsEngine.Indicators;
using OsEngine.OsTrader.Panels;
using OsEngine.OsTrader.Panels.Attributes;
using OsEngine.OsTrader.Panels.Tab;
using OsEngine.Charts.CandleChart.Elements;
namespace OsEngine.Robots._MyRobots
{
[Bot("FakeOutExample")]
internal class FakeOutExample : BotPanel
{
public FakeOutExample(string name, StartProgram startProgram) : base(name, startProgram)
{
TabCreate(BotTabType.Simple);
_tab = TabsSimple[0];
// Basic setting
Regime = CreateParameter("Regime", "Off", new[] { "Off", "On", "OnlyClosePosition" }, "Base");
MinutsForExit = CreateParameter("MinutsForExit", 75, 5, 240, 1, "Base");
CandlesForPcLevel = CreateParameter("CandlesForPcLevel", 10, 3, 24, 1, "Base");
// Setting indicator
PeriodPC = CreateParameter("PeriodPC", 10, 5, 40, 1, "Base");
// Create indicator PC
_PC = IndicatorsFactory.CreateIndicatorByName("PriceChannel", name + "PC", false);
_PC = (Aindicator)_tab.CreateCandleIndicator(_PC, "Prime");
((IndicatorParameterInt)_PC.Parameters[0]).ValueInt = PeriodPC.ValueInt;
((IndicatorParameterInt)_PC.Parameters[1]).ValueInt = PeriodPC.ValueInt;
_PC.Save();
// Events
_tab.CandleFinishedEvent += _tab_CandleFinishedEvent;
ParametrsChangeByUser += FakeOutExample_ParametrsChangeByUser;
Description = "An example of a robot going short after a false upside breakout.";
}
public override string GetNameStrategyType()
{
return "FakeOutExample";
}
public override void ShowIndividualSettingsDialog()
{
}
private BotTabSimple _tab;
// Basic Settings
private StrategyParameterString Regime;
public StrategyParameterInt CandlesForPcLevel;
public StrategyParameterInt MinutsForExit;
// Indicator setting
public StrategyParameterInt PeriodPC;
// Indicator
Aindicator _PC;
private bool _signalSell;
private decimal _hLevelOne;
//Indicator Update event
private void FakeOutExample_ParametrsChangeByUser()
{
((IndicatorParameterInt)_PC.Parameters[0]).ValueInt = PeriodPC.ValueInt;
((IndicatorParameterInt)_PC.Parameters[1]).ValueInt = PeriodPC.ValueInt;
_PC.Save();
_PC.Reload();
}
//Line
LineHorisontal _lineOnPrimeChart;
//Lists of Extremums
List<decimal> methodsH = new List<decimal>();
List<decimal> localHighV = new List<decimal>();
List<int> localHighI = new List<int>();
// LOGIC
private void _tab_CandleFinishedEvent(List<Candle> candles)
{
// If the robot is turned off, exit the event handler
if (Regime.ValueString == "Off")
{
return;
}
// If there are not enough candles to build an indicator, we exit
if (candles.Count < PeriodPC.ValueInt + 30)
{
return;
}
if (CandlesForPcLevel.ValueInt == 0)
{
return;
}
// Method for Local Extremums
LocalExtremums(candles);
// Levels from PC Method Initialization
LevelsFromPc();
List<Position> openPositions = _tab.PositionsOpenAll;
// If there are positions, then go to the position closing method
if (openPositions != null && openPositions.Count != 0)
{
LogicClosePosition(candles);
}
// If the position closing mode, then exit the method
if (Regime.ValueString == "OnlyClosePosition")
{
return;
}
//If there are no positions, then go to the position opening method
if (openPositions == null ||
openPositions.Count == 0)
{
LogicOpenPosition(candles);
}
//Elements on chart
LineH1(); // Local High Level line
PointH1(); // Local High Marker
// Lines refresh
if (_lineOnPrimeChart != null)
{
_lineOnPrimeChart.TimeEnd = candles[candles.Count - 1].TimeStart;
_lineOnPrimeChart.Refresh();
}
}
private void LevelsFromPc()
{
List<decimal> pcHigh = _PC.DataSeries[0].Values;
var t = CandlesForPcLevel.ValueInt;
if (pcHigh.Count - CandlesForPcLevel.ValueInt <= 1)
{
return;
}
// PC value
decimal LastPcH = pcHigh[pcHigh.Count - 1];
decimal LastPcHPlus = pcHigh[pcHigh.Count - CandlesForPcLevel.ValueInt];
// If Last PC value and PC value minus variable equal - adding to List
if (LastPcH == LastPcHPlus)
{
if (methodsH.Count < 1)
{
methodsH.Add(LastPcH);
}
else
{
if (LastPcH != methodsH[methodsH.Count - 1])
{
methodsH.Add(LastPcH);
}
else
{
return;
}
}
try
{
if (methodsH.Count > 1)
{
_hLevelOne = methodsH[methodsH.Count - 1];
}
}
catch
{
}
}
}
private void LocalExtremums(List<Candle> candles)
{
localHighV.Clear();
localHighI.Clear();
int _candlesCountMinus = candles.Count - CandlesForPcLevel.ValueInt;
decimal _localHighV = candles[candles.Count - 1].High;
int j = 1;
for (int i = candles.Count - 1; i > _candlesCountMinus - 1; i--)
{
if (candles[i].High > _localHighV)
{
_localHighV = candles[i].High;
j = i;
}
}
if (j != 1)
{
localHighI.Add(j);
}
else
{
localHighI.Add(candles.Count - 1);
}
localHighV.Add(_localHighV);
}
// Chart Visual Elements
private void LineH1() // Local High Level line
{
List<Candle> candles = _tab.CandlesFinishedOnly;
if (candles.Count == 0 ||
candles.Count < 31)
{
return;
}
LineHorisontal line = new LineHorisontal("Line2", "Prime", false);
line.Value = _hLevelOne;
line.TimeStart = candles[0].TimeStart;
line.TimeEnd = candles[candles.Count - 1].TimeStart;
line.Color = Color.Green;
line.LineWidth = 2; // Толщина линии
_tab.SetChartElement(line);
_lineOnPrimeChart = line;
if (_lineOnPrimeChart != null)
{
_lineOnPrimeChart.TimeEnd = candles[candles.Count - 1].TimeStart;
_lineOnPrimeChart.Refresh();
}
}
private void PointH1() //Local High Marker
{
List<Candle> candles = _tab.CandlesFinishedOnly;
decimal _localHighV = localHighV[localHighV.Count - 1];
int _localHighI = localHighI[localHighI.Count - 1];
PointElement point = new PointElement("Some label", "Prime");
point.Y = _localHighV;
point.TimePoint = candles[_localHighI].TimeStart;
point.Color = Color.Red;
point.Style = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Star4;
point.Size = 12;
_tab.SetChartElement(point);
}
//Opening logic
private void LogicOpenPosition(List<Candle> candles)
{
// The last value of the indicators
Candle lastCandle = candles[candles.Count - 1];
decimal lastCandleClose = lastCandle.Close;
decimal _localHighV = localHighV[localHighV.Count - 1];
//Short
_signalSell = lastCandleClose < _hLevelOne &&
_localHighV > _hLevelOne;
if (_signalSell)
{
_tab.SellAtMarket(1);
}
}
private void LogicClosePosition(List<Candle> candles)
{
List<Position> openPositions = _tab.PositionsOpenAll;
Position pos = openPositions[0];
if (pos.TimeOpen.AddMinutes(MinutsForExit.ValueInt) <= candles[candles.Count - 1].TimeStart)
{
_tab.CloseAllAtMarket();
}
}
}
}