-
Notifications
You must be signed in to change notification settings - Fork 3
/
Tick Speed.cs
273 lines (238 loc) · 12.6 KB
/
Tick Speed.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
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("Tick Speed", TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class TickSpeed : Robot
{
[Parameter("Lots", DefaultValue = 0.1, MinValue = 0.01, Step = 0.01)]
public double Lots { get; set; }
[Parameter("Use Number of Ticks", DefaultValue = true)]
public bool ShowVolume { get; set; }
[Parameter("Use Price Variation", DefaultValue = false)]
public bool ShowVariation { get; set; }
[Parameter("Period (In seconds)", DefaultValue = 5, MinValue = 1)]
public int Period { get; set; }
[Parameter("Entry level", DefaultValue = 10, MinValue = 1)]
public int EntryLevel { get; set; }
[Parameter("Exit level", DefaultValue = 5, MinValue = 1)]
public int ExitLevel { get; set; }
[Parameter("Trend Strength (in %)", DefaultValue = 70, MinValue = 1, MaxValue = 100)]
public int TrendStrength { get; set; }
private Queue<DateTime> TimeQueue = new Queue<DateTime>();
private Queue<double> PriceQueue = new Queue<double>();
private Queue<double> DeltaPriceQueue = new Queue<double>();
private Queue<double> PositivePriceQueue = new Queue<double>();
private Queue<double> NegativePriceQueue = new Queue<double>();
private Stack<double> PriceStack = new Stack<double>();
private Colors Color = Colors.LightBlue;
private Position OpenPosition = null;
protected override void OnStart()
{
var price = MarketSeries.Close.LastValue;
TimeQueue.Enqueue(Server.Time);
PriceQueue.Enqueue(price);
PriceStack.Push(price);
DrawChart();
}
protected override void OnTick()
{
// Storing data for this tick.
var price = MarketSeries.Close.LastValue;
TimeQueue.Enqueue(Server.Time);
PriceQueue.Enqueue(price);
var deltaPrice = price - PriceStack.Peek();
DeltaPriceQueue.Enqueue(Math.Abs(deltaPrice));
if (deltaPrice >= 0)
{
PositivePriceQueue.Enqueue(Math.Abs(deltaPrice));
NegativePriceQueue.Enqueue(0);
}
else
{
NegativePriceQueue.Enqueue(Math.Abs(deltaPrice));
PositivePriceQueue.Enqueue(0);
}
PriceStack.Push(price);
// Updating chart and getting the last values.
var LastValue = DrawChart();
var LastPositivePeriod = GetTicksVariation(PositivePriceQueue, Server.Time.AddSeconds(-Period), Server.Time.AddSeconds(1));
var LastNegativePeriod = GetTicksVariation(NegativePriceQueue, Server.Time.AddSeconds(-Period), Server.Time.AddSeconds(1));
Print("+{0}, -{1}", LastPositivePeriod, LastNegativePeriod);
// Entry Logic
double PositivePercentage = 0;
double NegativePercentage = 0;
var TotalVariation = LastPositivePeriod + LastNegativePeriod;
if (TotalVariation > 0)
{
PositivePercentage = LastPositivePeriod / TotalVariation * 100;
NegativePercentage = LastNegativePeriod / TotalVariation * 100;
}
if (LastValue >= EntryLevel && PositivePercentage >= TrendStrength && OpenPosition == null)
{
ExecuteOrder(TradeType.Buy);
}
else if (LastValue >= EntryLevel && NegativePercentage >= TrendStrength && OpenPosition == null)
{
ExecuteOrder(TradeType.Sell);
}
else if (LastValue <= ExitLevel && OpenPosition != null)
CloseOpenPosition();
}
protected override void OnBar()
{
ClearQueues();
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
private void ClearQueues()
{
var sel = from t in TimeQueue
where t >= Server.Time.AddSeconds(-Period * 10)
select t;
if (sel.Count() > 0)
{
var selQueue = new Queue<DateTime>(sel.ToList());
var first = selQueue.Dequeue();
var firstIndex = TimeQueue.ToList().IndexOf(first);
//Clearing Queues.
PriceQueue = new Queue<double>(PriceQueue.ToList().Skip(firstIndex));
DeltaPriceQueue = new Queue<double>(DeltaPriceQueue.ToList().Skip(firstIndex));
PositivePriceQueue = new Queue<double>(PositivePriceQueue.ToList().Skip(firstIndex));
NegativePriceQueue = new Queue<double>(NegativePriceQueue.ToList().Skip(firstIndex));
PriceStack = new Stack<double>(PriceStack.ToList().Skip(firstIndex));
TimeQueue = new Queue<DateTime>(TimeQueue.ToList().Skip(firstIndex));
}
}
private void CloseOpenPosition()
{
var Result = ClosePosition(OpenPosition);
if (Result.IsSuccessful)
{
OpenPosition = null;
}
}
private int DrawChart()
{
if (ShowVolume)
return DrawTicksVolume();
else if (ShowVariation)
return DrawTicksVariation();
else
return 0;
}
private int DrawTicksVariation()
{
int PeriodB = Period * 2;
int PeriodC = Period * 3;
int PeriodD = Period * 4;
int PeriodE = Period * 5;
int PeriodF = Period * 6;
int PeriodG = Period * 7;
int PeriodH = Period * 8;
int PeriodI = Period * 9;
int PeriodJ = Period * 10;
int nA = GetTicksVariation(DeltaPriceQueue, Server.Time.AddSeconds(-Period), Server.Time.AddSeconds(1));
int nB = GetTicksVariation(DeltaPriceQueue, Server.Time.AddSeconds(-PeriodB), Server.Time.AddSeconds(-Period));
int nC = GetTicksVariation(DeltaPriceQueue, Server.Time.AddSeconds(-PeriodC), Server.Time.AddSeconds(-PeriodB));
int nD = GetTicksVariation(DeltaPriceQueue, Server.Time.AddSeconds(-PeriodD), Server.Time.AddSeconds(-PeriodC));
int nE = GetTicksVariation(DeltaPriceQueue, Server.Time.AddSeconds(-PeriodE), Server.Time.AddSeconds(-PeriodD));
int nF = GetTicksVariation(DeltaPriceQueue, Server.Time.AddSeconds(-PeriodF), Server.Time.AddSeconds(-PeriodE));
int nG = GetTicksVariation(DeltaPriceQueue, Server.Time.AddSeconds(-PeriodG), Server.Time.AddSeconds(-PeriodF));
int nH = GetTicksVariation(DeltaPriceQueue, Server.Time.AddSeconds(-PeriodH), Server.Time.AddSeconds(-PeriodG));
int nI = GetTicksVariation(DeltaPriceQueue, Server.Time.AddSeconds(-PeriodI), Server.Time.AddSeconds(-PeriodH));
int nJ = GetTicksVariation(DeltaPriceQueue, Server.Time.AddSeconds(-PeriodJ), Server.Time.AddSeconds(-PeriodI));
ChartObjects.DrawText("Title", "Price variation per period (in pipettes)", StaticPosition.TopLeft, Color);
ChartObjects.DrawText("A", GetStringToDraw(1, Period, nA), StaticPosition.TopLeft, Color);
ChartObjects.DrawText("B", GetStringToDraw(2, PeriodB, nB), StaticPosition.TopLeft, Color);
ChartObjects.DrawText("C", GetStringToDraw(3, PeriodC, nC), StaticPosition.TopLeft, Color);
ChartObjects.DrawText("D", GetStringToDraw(4, PeriodD, nD), StaticPosition.TopLeft, Color);
ChartObjects.DrawText("E", GetStringToDraw(5, PeriodE, nE), StaticPosition.TopLeft, Color);
ChartObjects.DrawText("F", GetStringToDraw(6, PeriodF, nF), StaticPosition.TopLeft, Color);
ChartObjects.DrawText("G", GetStringToDraw(7, PeriodG, nG), StaticPosition.TopLeft, Color);
ChartObjects.DrawText("H", GetStringToDraw(8, PeriodH, nH), StaticPosition.TopLeft, Color);
ChartObjects.DrawText("I", GetStringToDraw(9, PeriodI, nI), StaticPosition.TopLeft, Color);
ChartObjects.DrawText("J", GetStringToDraw(10, PeriodJ, nJ), StaticPosition.TopLeft, Color);
return nA;
}
private int DrawTicksVolume()
{
int PeriodB = Period * 2;
int PeriodC = Period * 3;
int PeriodD = Period * 4;
int PeriodE = Period * 5;
int PeriodF = Period * 6;
int PeriodG = Period * 7;
int PeriodH = Period * 8;
int PeriodI = Period * 9;
int PeriodJ = Period * 10;
int nA = GetNumberOfTicks(Server.Time.AddSeconds(-Period), Server.Time.AddSeconds(1));
int nB = GetNumberOfTicks(Server.Time.AddSeconds(-PeriodB), Server.Time.AddSeconds(-Period));
int nC = GetNumberOfTicks(Server.Time.AddSeconds(-PeriodC), Server.Time.AddSeconds(-PeriodB));
int nD = GetNumberOfTicks(Server.Time.AddSeconds(-PeriodD), Server.Time.AddSeconds(-PeriodC));
int nE = GetNumberOfTicks(Server.Time.AddSeconds(-PeriodE), Server.Time.AddSeconds(-PeriodD));
int nF = GetNumberOfTicks(Server.Time.AddSeconds(-PeriodF), Server.Time.AddSeconds(-PeriodE));
int nG = GetNumberOfTicks(Server.Time.AddSeconds(-PeriodG), Server.Time.AddSeconds(-PeriodF));
int nH = GetNumberOfTicks(Server.Time.AddSeconds(-PeriodH), Server.Time.AddSeconds(-PeriodG));
int nI = GetNumberOfTicks(Server.Time.AddSeconds(-PeriodI), Server.Time.AddSeconds(-PeriodH));
int nJ = GetNumberOfTicks(Server.Time.AddSeconds(-PeriodJ), Server.Time.AddSeconds(-PeriodI));
ChartObjects.DrawText("Title", "Number of ticks per period", StaticPosition.TopLeft, Color);
ChartObjects.DrawText("A", GetStringToDraw(1, Period, nA), StaticPosition.TopLeft, Color);
ChartObjects.DrawText("B", GetStringToDraw(2, PeriodB, nB), StaticPosition.TopLeft, Color);
ChartObjects.DrawText("C", GetStringToDraw(3, PeriodC, nC), StaticPosition.TopLeft, Color);
ChartObjects.DrawText("D", GetStringToDraw(4, PeriodD, nD), StaticPosition.TopLeft, Color);
ChartObjects.DrawText("E", GetStringToDraw(5, PeriodE, nE), StaticPosition.TopLeft, Color);
ChartObjects.DrawText("F", GetStringToDraw(6, PeriodF, nF), StaticPosition.TopLeft, Color);
ChartObjects.DrawText("G", GetStringToDraw(7, PeriodG, nG), StaticPosition.TopLeft, Color);
ChartObjects.DrawText("H", GetStringToDraw(8, PeriodH, nH), StaticPosition.TopLeft, Color);
ChartObjects.DrawText("I", GetStringToDraw(9, PeriodI, nI), StaticPosition.TopLeft, Color);
ChartObjects.DrawText("J", GetStringToDraw(10, PeriodJ, nJ), StaticPosition.TopLeft, Color);
return nA;
}
private void ExecuteOrder(TradeType _TradeType)
{
var Result = ExecuteMarketOrder(_TradeType, Symbol, Symbol.NormalizeVolume(Symbol.QuantityToVolume(Lots)));
if (Result.IsSuccessful)
{
OpenPosition = Result.Position;
}
}
private int GetTicksVariation(Queue<double> queue, DateTime time1, DateTime time2)
{
var sel = from t in TimeQueue
where t >= time1 && t < time2
select t;
if (sel.Count() > 0)
{
var selQueue = new Queue<DateTime>(sel.ToList());
var selStack = new Stack<DateTime>(sel.ToList());
var first = selQueue.Dequeue();
var last = selStack.Pop();
var firstIndex = TimeQueue.ToList().IndexOf(first);
var lastIndex = TimeQueue.ToList().IndexOf(last);
var deltas = queue.ToList().Take(lastIndex + 1).Skip(firstIndex);
return Convert.ToInt16(Math.Round(Enumerable.Sum(deltas) / (Symbol.PipSize / 10), 1));
}
return 0;
}
private int GetNumberOfTicks(DateTime time1, DateTime time2)
{
var sel = from t in TimeQueue
where t >= time1 && t < time2
select t;
return sel.Count();
}
private string GetStringToDraw(int NumberOfJumps, int Period, int NumberOfTicks)
{
var str = String.Concat(Enumerable.Repeat("\n", NumberOfJumps)) + string.Format("{0,-3} seconds: ", Period) + NumberOfTicks.ToString() + " " + String.Concat(Enumerable.Repeat("-", NumberOfTicks));
return str;
}
}
}