-
Notifications
You must be signed in to change notification settings - Fork 3
/
Rasmussen Martingale.cs
323 lines (276 loc) · 10.8 KB
/
Rasmussen Martingale.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
318
319
320
321
using System;
using System.Linq;
using System.Collections.Generic;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class RasmussenMartingale : Robot
{
[Parameter("Martingale Multiplier", DefaultValue = 2.0)]
public double Multiplier { get; set; }
[Parameter("Initial Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double InitialQuantity { get; set; }
[Parameter("Stop Loss", DefaultValue = 100)]
public double StopLoss { get; set; }
[Parameter("Take Profit", DefaultValue = 100)]
public double TakeProfit { get; set; }
[Parameter("Trailing Stop Loss", DefaultValue = 20.0)]
public double TrailingStop { get; set; }
[Parameter("Follow the Trend", DefaultValue = true)]
public bool FollowTrend { get; set; }
[Parameter("Number of Candles", DefaultValue = 5, MinValue = 1)]
public int CandlesNumber { get; set; }
[Parameter("Minutes between trades", DefaultValue = 0)]
public int MinutesBetweenTrades { get; set; }
[Parameter("Cycle Period (in hours)", DefaultValue = 24, MinValue = 1)]
public int Cycle { get; set; }
[Parameter("Martingale Overrides Cycle Reset?", DefaultValue = true)]
public bool OverrideCycle { get; set; }
[Parameter("Shut Down Amount (in $)", DefaultValue = 500)]
public double ShutDown { get; set; }
[Parameter("Use candle difference?", DefaultValue = false)]
public bool UseCandleDifference { get; set; }
[Parameter("Difference (in %)", DefaultValue = 50)]
public double DifferenceBetweenCandles { get; set; }
[Parameter("Filter tiny candles?", DefaultValue = true)]
public bool FilterTinyCandles { get; set; }
[Parameter("Minimum Last Candle Size (Pips)", DefaultValue = 3)]
public double MinCandleSize { get; set; }
private double BarArrayAverage;
private double VolumeMultiplier = 1;
private double MaxProfit = 0;
private double CycleProfit = 0;
private double InitialStopLoss;
private double InitialTakeProfit;
private Queue<double> BarArray = new Queue<double>();
private bool TakeProfitFlag = true;
private bool TrailingStopFlag = false;
private bool ShuttedDownFlag = false;
private bool CycleOverridenFlag = false;
private DateTime InitialTime;
private DateTime CycleEndTime;
private DateTime TimeForNewPosition;
private Position OpenPosition;
protected override void OnStart()
{
InitialStopLoss = StopLoss;
InitialTakeProfit = TakeProfit;
InitializeCycle();
for (int i = 1; i <= CandlesNumber; i++)
{
BarArray.Enqueue(GetBarHeight(i));
}
}
protected override void OnBar()
{
UpdateBars();
if (ShuttedDownFlag == false)
{
if (Server.Time >= TimeForNewPosition)
{
//Checking if entry condition has been met...
var OverZero = BarArray.Where(x => x > 0);
var SubZero = BarArray.Where(x => x < 0);
//...for a bullish trend.
if (OverZero.Count() == CandlesNumber && OpenPosition == null)
{
//Checking the trend or countertrend variable.
var _TradeType = FollowTrend ? TradeType.Buy : TradeType.Sell;
FilterAndExecuteTrades(_TradeType);
}
//...for a bearish trend.
if (SubZero.Count() == CandlesNumber && OpenPosition == null)
{
//Checking the trend or countertrend variable.
var _TradeType = FollowTrend ? TradeType.Sell : TradeType.Buy;
FilterAndExecuteTrades(_TradeType);
}
}
}
}
protected override void OnTick()
{
//Checking if the cycle has ended;
if (Server.Time >= CycleEndTime && CycleOverridenFlag == false)
{
CloseOpenPosition();
ResetFlags();
ResetVariables();
InitializeCycle();
}
if (ShuttedDownFlag == false)
{
//Checks if the TS is active.
if (TrailingStopFlag == true)
{
UpdateMaxProfit();
UpdateStopLoss();
}
//Checks if there's an open position.
if (OpenPosition != null)
{
if (OpenPosition.NetProfit >= TakeProfit && TakeProfitFlag == true)
{
ActivateTrailingStop();
}
if (OpenPosition.NetProfit <= -StopLoss)
{
CloseOpenPosition();
// If the TP was never reached, the position volume is multiplied.
if (TakeProfitFlag == true)
{
VolumeMultiplier = Multiplier * VolumeMultiplier;
StopLoss = InitialStopLoss * VolumeMultiplier;
TakeProfit = InitialStopLoss * VolumeMultiplier;
if (OverrideCycle == true)
CycleOverridenFlag = true;
}
else
{
ResetVariables();
}
ResetFlags();
}
else
{
//Checking the shut down properties.
if (CycleProfit + OpenPosition.NetProfit <= -ShutDown)
{
ShuttedDownFlag = true;
CloseOpenPosition();
Print("Shutted down until next cycle.");
}
}
}
}
}
private void ActivateTrailingStop()
{
TakeProfitFlag = false;
TrailingStopFlag = true;
MaxProfit = OpenPosition.NetProfit;
UpdateStopLoss();
}
private DateTime AddWorkDays(DateTime originalDate, int workDays)
{
DateTime tmpDate = originalDate;
while (workDays > 0)
{
tmpDate = tmpDate.AddDays(1);
if (tmpDate.DayOfWeek < DayOfWeek.Saturday && tmpDate.DayOfWeek > DayOfWeek.Sunday)
workDays--;
}
return tmpDate;
}
private DateTime AddWorkHours(DateTime originalDate, int workHours)
{
DateTime tmpDate = originalDate;
while (workHours > 0)
{
tmpDate = tmpDate.AddHours(1);
if (tmpDate.DayOfWeek < DayOfWeek.Saturday && tmpDate.DayOfWeek > DayOfWeek.Sunday)
workHours--;
}
return tmpDate;
}
private DateTime AddWorkMinutes(DateTime originalDate, int workMinutes)
{
DateTime tmpDate = originalDate;
while (workMinutes > 0)
{
tmpDate = tmpDate.AddMinutes(1);
if (tmpDate.DayOfWeek < DayOfWeek.Saturday && tmpDate.DayOfWeek > DayOfWeek.Sunday)
workMinutes--;
}
return tmpDate;
}
private void CloseOpenPosition()
{
if (OpenPosition != null)
{
var Result = ClosePosition(OpenPosition);
if (Result.IsSuccessful)
{
OpenPosition = null;
CycleProfit += Result.Position.NetProfit;
TimeForNewPosition = AddWorkMinutes(Server.Time, MinutesBetweenTrades);
}
}
}
private void ExecuteOrder(TradeType _TradeType)
{
var Result = ExecuteMarketOrder(_TradeType, Symbol, Symbol.NormalizeVolume(Symbol.QuantityToVolume(InitialQuantity * VolumeMultiplier)));
if (Result.IsSuccessful)
{
OpenPosition = Result.Position;
}
}
private void FilterAndExecuteTrades(TradeType _TradeType)
{
//Order execution when using difference between candles.
if (UseCandleDifference == true)
{
var Difference = BarArray.ToList()[CandlesNumber - 1] * 100 / BarArrayAverage - 100;
if (Difference >= DifferenceBetweenCandles)
FilterTinyCandlesAndExecute(_TradeType);
}
else
FilterTinyCandlesAndExecute(_TradeType);
}
private void FilterTinyCandlesAndExecute(TradeType _TradeType)
{
// Order execution when avoiding tiny candles.
if (FilterTinyCandles == true)
{
if (BarArray.ToList()[CandlesNumber - 1] > MinCandleSize * Symbol.PipSize)
ExecuteOrder(_TradeType);
}
else
ExecuteOrder(_TradeType);
}
private double GetBarHeight(int index)
{
double Close = MarketSeries.Close[MarketSeries.Close.Count - index - 1];
double Open = MarketSeries.Open[MarketSeries.Open.Count - index - 1];
return Close - Open;
}
private void InitializeCycle()
{
InitialTime = Server.Time;
CycleEndTime = AddWorkHours(InitialTime, Cycle);
ShuttedDownFlag = false;
CycleProfit = 0;
Print("A new cycle began. Ends on {0}", CycleEndTime);
}
private void ResetFlags()
{
TakeProfitFlag = true;
TrailingStopFlag = false;
}
private void ResetVariables()
{
VolumeMultiplier = 1;
StopLoss = InitialStopLoss;
TakeProfit = InitialTakeProfit;
CycleOverridenFlag = false;
}
private void UpdateBars()
{
BarArray.Dequeue();
BarArrayAverage = BarArray.Average();
BarArray.Enqueue(GetBarHeight(1));
}
private void UpdateMaxProfit()
{
MaxProfit = OpenPosition.NetProfit > MaxProfit ? OpenPosition.NetProfit : MaxProfit;
}
private void UpdateStopLoss()
{
StopLoss = -MaxProfit + TakeProfit * TrailingStop / 100;
}
}
}