forked from PopovMP/FSB_Pro_Indicators
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomFilter.cs
More file actions
173 lines (154 loc) · 6.55 KB
/
RandomFilter.cs
File metadata and controls
173 lines (154 loc) · 6.55 KB
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
//==============================================================
// Forex Strategy Builder
// Copyright © Miroslav Popov. All rights reserved.
//==============================================================
// THIS CODE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE.
//==============================================================
using System;
using ForexStrategyBuilder.Infrastructure.Entities;
using ForexStrategyBuilder.Infrastructure.Enums;
using ForexStrategyBuilder.Infrastructure.Interfaces;
namespace ForexStrategyBuilder.Indicators.Store
{
public class RandomFilter : Indicator
{
public RandomFilter()
{
IndicatorName = "Random Filter";
PossibleSlots = SlotTypes.OpenFilter | SlotTypes.CloseFilter;
IsDeafultGroupAll = true;
IsGeneratable = false;
IndicatorAuthor = "Miroslav Popov";
IndicatorVersion = "2.0";
IndicatorDescription = "Bundled in FSB distribution.";
}
public override void Initialize(SlotTypes slotType)
{
SlotType = slotType;
// Setting up the indicator parameters
IndParam.IndicatorType = TypeOfIndicator.Additional;
// The ComboBox parameters
IndParam.ListParam[0].Caption = "Logic";
if (SlotType == SlotTypes.OpenFilter)
IndParam.ListParam[0].ItemList = new[]
{
"Gives a random entry signal"
};
else if (SlotType == SlotTypes.CloseFilter)
IndParam.ListParam[0].ItemList = new[]
{
"Gives a random exit signal"
};
else
IndParam.ListParam[0].ItemList = new[]
{
"Not Defined"
};
IndParam.ListParam[0].Index = 0;
IndParam.ListParam[0].Text = IndParam.ListParam[0].ItemList[IndParam.ListParam[0].Index];
IndParam.ListParam[0].Enabled = true;
IndParam.ListParam[0].ToolTip = "Logic of application of the indicator.";
// The NumericUpDown parameters
if (SlotType == SlotTypes.OpenFilter)
{
IndParam.NumParam[0].Caption = "Probability";
IndParam.NumParam[0].Value = 80;
IndParam.NumParam[0].Min = 0;
IndParam.NumParam[0].Max = 100;
IndParam.NumParam[0].Enabled = true;
IndParam.NumParam[0].ToolTip = "The probability to allow a new position opening in %.";
IndParam.NumParam[1].Caption = "Long vs short";
IndParam.NumParam[1].Value = 50;
IndParam.NumParam[1].Min = 0;
IndParam.NumParam[1].Max = 100;
IndParam.NumParam[1].Enabled = true;
IndParam.NumParam[1].ToolTip = "The probability to open Long vs. short in %.";
}
else if (SlotType == SlotTypes.CloseFilter)
{
IndParam.NumParam[0].Caption = "Probability";
IndParam.NumParam[0].Value = 20;
IndParam.NumParam[0].Min = 0;
IndParam.NumParam[0].Max = 100;
IndParam.NumParam[0].Enabled = true;
IndParam.NumParam[0].ToolTip = "The probability to close the position in %.";
}
}
public override void Calculate(IDataSet dataSet)
{
DataSet = dataSet;
// Reading the parameters
var iProbability = (int) IndParam.NumParam[0].Value;
var iLongShort = (int) IndParam.NumParam[1].Value;
var random = new Random();
// Saving the components
if (SlotType == SlotTypes.OpenFilter)
{
Component = new IndicatorComp[2];
Component[0] = new IndicatorComp
{
ChartType = IndChartType.NoChart,
FirstBar = 0,
Value = new double[Bars],
DataType = IndComponentType.AllowOpenLong,
CompName = "Is long entry allowed"
};
Component[1] = new IndicatorComp
{
ChartType = IndChartType.NoChart,
FirstBar = 0,
Value = new double[Bars],
DataType = IndComponentType.AllowOpenShort,
CompName = "Is short entry allowed"
};
// Calculation of the logic
for (int i = 0; i < Bars; i++)
{
if (random.Next(100) < iProbability)
{
int iRandNumb = random.Next(100);
Component[0].Value[i] = (iRandNumb <= iLongShort) ? 1 : 0;
Component[1].Value[i] = (iRandNumb > iLongShort) ? 1 : 0;
}
else
{
Component[0].Value[i] = 0;
Component[1].Value[i] = 0;
}
}
}
else
{
Component = new IndicatorComp[1];
Component[0] = new IndicatorComp
{
ChartType = IndChartType.NoChart,
FirstBar = 0,
Value = new double[Bars],
DataType = IndComponentType.ForceClose,
CompName = "Force Close"
};
for (int i = 0; i < Bars; i++)
{
Component[0].Value[i] = (random.Next(100) < iProbability) ? 1 : 0;
}
}
}
public override void SetDescription()
{
EntryFilterLongDescription = ToString() + " allows a long position";
EntryFilterShortDescription = ToString() + " allows a short position";
ExitFilterLongDescription = ToString() + " allows closing";
ExitFilterShortDescription = ToString() + " allows closing";
}
public override string ToString()
{
return IndicatorName + " (" +
IndParam.NumParam[0].ValueToString + ", " + // Probability
IndParam.NumParam[1].ValueToString + ")"; // Long vs Short
}
}
}