Classifier based on k nearest neighbors. - MetaTrader 4 Expert | MT4 EA Download - MetaTrader 4 Resources




Implementation of k -nearest neighbors for memory-based machines. This approach didn't produce the expected results, probably because too little time was spent fiddling with it (most likely), Maybe it's because of some lack of knowledge. Anyway, I decided to post this method for everyone to discuss, maybe it will be fruitful (I really hope so).
The Euclidean_Metric function is a classifier. Using an available vector basis that describes a transaction or market situation, this function determines whether the input vector belongs to any group in the basis. The vectors are grouped by user, which in the example is simple: class 1 if the transaction ends with a positive result, class 0 if the result is negative. The nearest neighbor search of multidimensional vectors is performed using the following command Euclidean distance. Then it counts how many of these k -vectors belong to category 1. After that, it divides this value by the total number of neighbors (i.e. by k ) and receives the probability that the given vector belongs to class 1. Due to the "wrong" choice of coordinates of the vectors, the classification cannot be fully trusted (simply above 0,5 or below 0,5). Therefore, an additional threshold is included, i.e. entering the market if the probability of a profitable future trade is higher than 0.7 (for example). The ratio of the moving average is considered as a measure (coordinate) of the vector, assuming that the ratio is static and after classifying the vector (trade) once, this classification can also be used for forward testing. But it's not that simple :). Why on earth are k - nearest neighbors? Simply because when considering the first nearest neighbor, it could be an unusual spike of random values that will be chosen for classification, regardless of the presence of nearby clusters with opposite (in-group) vectors. Detailed explanation of S. Haykin's « Neural Networks: Comprehensive Fundamentals » (Learning Methods).
There are two problems with classifiers:
1) Find static data that describes market conditions (or future transactions) with the required accuracy or the correct level of classification required.
2) A lot of math, so - the calculation takes a while... (btw, slightly smaller than a PN network )
In short, the problem is almost the same as traditional TS. The advantage is that the classifier can formalize conditions that the trader cannot see, but will intuitively feel that certain values of the indicator affect whether a trade should be made.
Now let’s talk about implementation in detail.
according to- Is it really wrong to write a file using a vector library? Classified transactions...
Buy_critical point =0.6 Everyone’s threshold buy Position
Sell Threshold = 0.6 Likewise for sale
inverse_position_open _?= True ; this is an interesting point . If the probability of a profitable trade is small, then why not enter the market as a reverse position? This flag enables such locations.
Countdown_buy_critical point = 0.3 threshold, the probability of profit at this time is to buy Take a smaller position and open a sell trade
invers_sell_threshold=0.3; Likewise ...
Fast=12 parameter smoothed moving average of similarity and difference
TP =40; take profit
Sl =30; stop loss
CLOSE_ORDER = FALSE ; flag for closing the position on the opposite signal is enabled only if the order is profitable...
Usage: First, the flag is based on - Really , Fang Si Er = TP and run on history (1 time!), writing vector file. The next setting time is based on - Wrong It is recommended to optimize the thresholds and I choose them based on the first report (without classification). If probability of success = 0,5 then Thresholds are 0,6 and 0,4 (reversed position)
This is yet another example, the classifier can be applied to other TS in the same way, using other input data
Before classification:

After that, optimize the threshold.


Attachment download
📎 Stat_Euclidean_Metric.mq4 (12.82 KB)
Source: MQL5 #8645
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •