## Why I do not use Fibonacci for trading

What is Fibonacci?

To explain it succinctly, Fibonacci is a set of predetermined price levels of an instrument regarding a particular price range.

The previous figure shows an example of the Fibonacci levels. We take any lower low and any other highger high and draw a line connecting both points. The fibonacci levels between those two points are usually a set of percentages of the total heigth between the two points. Other levels may be printed, but the most common ones are 0, 50, 61.8. It is not unusual finding this default setup:

• 0%
• 23.6%
• 38.2%
• 50%
• 61.8%
• 78.6%
• 100%

Now, if you pay attention to the prior image, it happens that price usually bounces and ranges between those levels, therefore if we identify the level in which price is going to change its direction from, we can place an order exiting at the next Fibonacci level. In the last example, if we would have placed a buy order when the price crossed the 38.2 level the first time, we would have successfully sold with profit at the nearest Fibonacci level, 23.6.

I know traders who successfully use Fibonacci in their trades, if I am not using Fibonacci it is not to demerit the strategies that are profitable using it.

Why I do not use Fibos.

If I do not use Fibonacci is because to me, trading is:

• A matter of statistical price analysis.
• A matter of complete uncertainty about how price action will develop in the future.
• A matter of support and resistance game.

and because:

• Fibonacci establishes fixed levels not related to the actual price action.
• Fibonacci levels are not related to the actual price buildup.
• Fibonacci levels are a fixed structural deterministic approach and trading is dynamic and probabilistic.
• Fibonacci fails most of the time.

Now, look at the next image. The red circles mark the times in which Fibonacci trading failed. The problem is that you cannot predict when Fibonacci will work or how price will react at each level, just because Fibonacci levels are fixed no matter how price was moving before or how price is changing accross the different levels.

If  you place buy in the red circles number 1, 2 or 5, the trade is lost. The same happens when placing sell when price is in circles 3 and 4. Therefore, Fibonacci levels cannot be traded in a direct way.

The first red circle failed because the 38.2 level had been already retested, the second red circle failed because buying after a bearish breakout is a very poor setup. The third red circle failed because the 61.8 level represents antoher poor setup: selling next to a strong support from a resistance that was just broken by a breakout candle and therefore it became a new support (and no one should sell on a support if we want to stay safe). To understand the third cicle, look at the initial green huge breakout candle on the left side of tha chart. That candle has just transformed every resistance in a potential support. Selling in potential supports is not a safe wager.

Using Price Action.

As I mentioned, we can train our eyes and research how to identify the correct anchor points just by identifying them by analyzing the price history, chart patterns and price momentum. The exact same chart may be analyzed in a more realiable way by just understanding what each price move means.

So two immediate high probability trades can be depicted almost without too much effort within the same time range and without any indicator of course. I have drawn the dashed lines just by observing how the candles behave at the current scenario, without any predefined proportion nor shape.

One main rule here is: do not sell approaching near above a support and do not buy near below a resistance.

Conclusion.

The conclusion about all this, for me, is that the Fibonacci levels are good to spot potential price anchor points, but they also put the trader in a worse danger: having the conviction that something that is not there exists and losing the edge on the market.

Why should I use predetermined not trustworthy levels when I can train my eyes to identify support and resistance levels looking at the naked price?

If I draw my lines by observing the actual price I do know that each of my lines is actually an anchor point. And I can also draw diagonal (not necessarily horizontal) and vertical lines to mark high probability trade areas. I can identifiy where the price structures lay on and represent indeed high support and resistance areas.

And I do not need Fibonacci to understand that I have to sell only when price is high and buy only when price is low. The fibonacci levels may convince me that the price is near to a strong level and cloud my mind with that conviction so that I do not see what is actually happening by looking at the main picture and the signs that contradict that conviction. One of the worst enemies of traders is conviction because our brains are designed to fight for our own convictions and deny what denies those.

Do you think a boxer can win a round if he always protects himself against the same fixed punch type just because he thinks it is the most probable among the rest?

For these reasons, I think that Fibonacci and any other indicators are usually very dangerous tools, especially for those who have not developed yet a working trading strategy without need of using them.

## The key for making money trading Forex and other financial markets

At the beginning, I was overwhelmed like many others by the indecent numerous trading techniques that are published, because of those multiple websites repeating exactly the same slogans on how to be a trader that lead me to nowhere. During the last years I have tried one by one dozens of different trading strategies only to see these one after another. This path has been hard and full of failures to the point that many times I have doubted if there is really  a way for earning money by trading (Forex in my case). I have spent an important amount of  time and hundred, maybe thousands of euros acquiring paid software, books and spent money on trading tuition and rehearsal. Also whole years without holidays pursuing strategies to become a successful trader until I finally achieved my purpose.

Trading has several aspects I will try to expose next.

This is the most important part of trading. You can read Trading in the Zone by Mark Douglas to better understand the topic. But unless you do not deeply learn the psycological details of trading you will fail for sure.

Trading is probabilistic, so you repeat the same strategy again and again. You accept losses as a part on the wager. You accept that everything that may happen will happen when trading, everything you do not want to occur, it will occur eventually and repeteadely.

Trading is not a game, there are no rules. It is not safe and nothing you may predict.

Trading causes very strong emotional burden. You have to learn how to control it to avoid cutting your earnings and letting your losses grow. Your next trade will not be recover any previous loss, but to follow your system and earn money with it repeating the same steps of the same strategy.

Price is something hard to define when we speak about financial markets. I tend to think that price is more about expectations than value. People make money in any market by buying at low prices and selling at higher prices. Or by doing something equivalent.

Trading financial markets is about doing exactly it, finding when prices are either high or low and entering at the right moment. On the next figure there is an example of the strategy that beats the market which is very simple.

For doing it I use OCHL bars or japanese candlesticks. The next figure shows the same chart but using the candlesticks for displaying the price change.

The advantage is that with candlestick I can know which is the open, close high and low levels. I can now spot which will be the turning points of price analysing the current price momentum, trend and in particular the difference between high-low and open-close.

I know by observation that at the turning levels, called support and resistance levels, the candle’s wicks begin to be long compared to the candle bodies, so I can follow and predict the most probable price evolution by correct interpretation of the price changes. This is called price action trading.

Basically, with long compact candles there is no too much to do except they represent the current price momentum. If momentum and trend keep up with their direction, then we can take profit from it. Otherwise we must stay out. Look for instance the next figure, the colored part of the candle represents the open and close price levels while the wicks the maximum and minimum prices.

The first bar is a strong bar in which the range of prices has been respected during all the trading session. The second bar instead shows that the price of the asset even if it reached the same level as the first candle, it finally closed very low, almost at the same level of the opening price; meaning that traders perceive that the value of the instrumet is much lower than the highs of the session. In the first case we might expect some kind of price continuation or at least price slowdown. In the second case there is a chance that price will reverse within a bearish trend.

Therefore we are looking all the time longer wicks to enter and examining the price history to determine good entry points. How? Simply by confirming we are on a support or at a resistance depending if we want to either buy or sell. And that is all the story here. There is a number of patterns we can identify and use it to enter the markets. See an example below.

So, the key technique is identifying which are the turning points by observing the past and trading on the strongest levels found. On the figure above a resistance area is confirmed by notice it often acted as a strong resistance. Then we wait for both, momentum and trend to follow the same direction and we enter when a well-known pattern is produced. In this case a price reversal after a bearish breakout.

The goodness of this system is that we do not have only vague indications as almost all the trading resources, we do know the exact entry area according to what we can read from price itself, without using any indicator nor fixed pattern.

Fundamental analysis is not part of my trading, but I trade fundamentals however. A lot of profit can be obtained through trading fundamental announcements, with a higher risk, it is not difficult to get a 30% increase of your account in a single fundamental event.

Trading is a very risky activity and it is not suited to most of the people they say. Well, it may not be suited for most of the untrained people, but if one person is correctly prepared trading is not an issue riskier than any other economic activity.

But trading is not for novices alone, for unprepared people or for any person who would not be able to run a regular business successfuly.

Trading requires a lot of concepts to be assimilated and mastered before risking money and it takes months, otherwise and especially with leveraged financial instruments, it can blow up your account in a few days and even incur in debts due to the leverage levels.

I would say that trading is very difficult yet not riskier than other business. If we consider that 80% of people loss money when trading, we should take precautions as we would do with any other business.

Trading involves costs, requires investment, skill and hard work as any business needs. The rate of failure in any market will probably be similar to this 80%-90% reported by brokers regarding individuals lossing everything and more of their initial investments.

Money management is very important and it is key to make your account grow fast and limit your losses. I use compounding position sizing for instance.

If you are interested in straightforward learning experience, I offer a one-day seminar in trading to teach the basic principles that will allow you to become a trade once you master the practical strategies I can teach. It is not cheap but it will cost to you much less that it has costed to me. If you want a proof of it contact me to schedule a free demo session to show you how I beat the market you decide me to trade in Forex and other instruments at any past date you choose.

## Trading Forex Like a Pro

Behold, the secret to trade like a pro: become a pro.

### The past

It was during my college years at the Business School that I my interest in stock exchange trading emerged for the first time. A numer of subjects related to business and asset evaluation were taught within the financial, operationalorganizational and accounting areas of the official curriculum. One of the most remarkable topics covered was traditional charting theory; a bit of Elliott’s theories and some foundations in chartist patterns. At that time, Internet was still in the early stages and affordable access to financial data of acceptable quality was merely present only on daily press, so that in order to check stock charts and market analysis the time frame was indeed the daily reports that those publications delivered each morning. For those with a modern television set, the teletext service provided a listing of the companies trading at the Madrid Stock Exchange with their current share price updated every 15 minutes. The charts however were not included within the teletext information.

Operating in the markets was also not exent of pain. I used to have kind of a personal VIP bank card with a phone number contact printed on it for the bank’s brokerage department to call trades. Definitively decent for those interested in investing but completely insufficient for retail trading purposes.

### The Present Day

It could be written several hundred thousands of words only to describe the modern resources for teaching and allowing any individual to achieve a profound understanding and hands-on experience in trading. But beware of thinking that trading is easy and suitable for most people including yourself. Please, read before continuing the following notice and disclaimer:

Trading is an activity that involves high risk. Any person trying to trade has almost 100% chance of losing all the money on his/her trading accounts and additional high probability of incurring in additional debts when trading with leverage because margin calls may not be executed in time due to frequent particular market situations. The information herein is not a trading advice and must be solely used for educational purposes and never with live accounts with real money.

One of the places I enjoy buying most is El Corte Inglés, the biggest spanish retailer store chain. The key difference between El Corte Inglés and others is the customer satisfaction policy. If one client is not completely satisfied, El Corte Inglés completely refunds the purchase price without questions. They have followed this praxis since long ago when only a few used to. When a broker boasts about their cutting-edge technology and that with it you, a retailer, will be given an interesting if not realtively easy prospect of becoming a wealthy individual; when it happens I say: “Yeah, let’s prove it together!, let me buy your services and if I am not satisfied, please approve my reimbursement”.

It’s obvious at this stage that getting rich in one week or year is not the purpose of this post. If the purpose is becoming rich starting from scratch just look around you and realize that average people unfortunately stay on the average no matter how hard they try it, and that the easy routes usually lead to a bad end. Let’s keep our feet on the ground and stay focused towards something reasonable and absolutely work to give away any greed every time it speaks to you into the ear to weaken your good judgement. Sound preparation pays its cost and is not obtained without great effort, humility and discipline.

### Have a plan and take the first step

A plan with a preset deadline. If you do not succeed on it before the deadline, finish it immediately, never elongate the predefined time period. If the circumstances change notoriously midway through your plan execution, just cancel it. After cancelling a plan, it must be examined in detail and a new plan should come out after finishing assessing the current state of things. The plan that I present is a walk along a representative set of books and tools.

The books that will be mentioned next are used for guidance purposes and you should not be limited to them or even use any of them. There will be other good alternatives free and paid; on paper, digital support or even personal lessons. Make your own research and you will surely get many unexpected benefits from it. I encourage you to consult every related treatise to boarden your specific culture and to call into question whatever idea formerly developed. Babypips, Elite Trader, Forex Factory and other sites have a lot to offer to you.

The first part of the plan will consist on pure learning. The first book I would recommend is just Technical Analysis of the Financial Markets, by John M. Murphy.

It is a classic and you do not want to use it as a trading guide, you will lose everything if you would do it. Instead this book will teach you the basics on chart reading. I recommend however to skip all the chapters about indicators. Indicators never should be used for trading. All the strategies based on indicators, you should consider these just innefective. The only indicator that is woth using it is the moving average and purely to have a visual guide to help you determine if prices are above or below the mean and far away from equilibrium. But not even the rolling means should never be used to trigger any trading action.

While Murphy’s Technical Analysis of the Financial Markets has introduced the principles of charting theory, the next book could be Japanese Chandlestick Charting Techniques by Steve Nison.

This book will teach you tons of different price bar figures with exotic names. This information will prove to be essential to fine-tune your trading. Have this in mind in advance: japanese candlesticks should not be traded, instead, they will serve as a way to better gauge the tension within the market and how buyers and sellers toss around. You will encounter during your learning trip myriads of strategies that use japanese bars. Just ignore them all because they will empty your pocket.

Remember that indicators, signal providers and trading robots must be avoided without exception. Sooner or later you will run into the interesting area of algorithmic trading. Forget about strategies with indicators if you find them on the algorithmic trading books and you will see that there is no anything remaining to test. In a more elevated level you may come across researching about artificial intelligence. I have developed a bunch of IA systems under the umbrella of my master in Data Science and years of programming experience. Systems that learn by themselves, others that are trained upfront. Again, my adivce: first learn how to manually increase the size of your account, then find a hobby programming expert advisors if you are bored. The rule of thumb: never use a robot without knowing and understanding the underlying source code.

At this point you are at two thirds of your way of your trading preparation stage. Since you have already increased your prowess for financial markets, it is time to start testing that knowledge with real market data. You are still very far from being able to trade with real money and pressure; believe me, the pressure under real trading conditions is so huge it would crush you like a train in less time that it takes to say “hello world”. Financial markets are like a pool full of sharks where you are their aperitif. I am serious, stay away from risking one cent and from beginning being used to carry the loser’s backpack with you.

### Set the right mindset up

Trading without a proper psychological preparedness is like driving a car blindfolded.  The trader’s activity is characterised by unlimited risk and freedom to act, devoid of social considerations about what is good or fair. Markets change randomly. There are no rules about how many times traders have to be in the market, working hours, minimum or maximum position sizes and anyone can stay just looking at the charts searching odds. Anything that may happen can happen without prior notice, because the main principle of markets is free randomness.

There is a book praised by the community and describes in plain language the different psychological aspects of trading that the author has identified during his first 18 years trading and working as trading coach.

Trading In The Zone, by Mark Douglas is completely devoted to train you to become a trader. The way to success requires patience and time. Do not fear a break from charts to smother one’s natural tendency to act under emotions and hopes. Learn how to proceed with a consistent methodology consisting in putting the probabilities at your favour and waiting idle around  rough sea when the likelyhood of gain is against you.

Trading In The Zone takes off from the basics and climbs slowly to navigate across those main principles, which is good for people never been exposed in the past to those concepts, but it may result a bit tedious for the majority of persons who already have an average knowledge in psychology and self-help books. Anyway, in my opinion is worth reading it since it is quite unique to the trading area; maybe a bit expensive only because its popularity and because it could have been written in 30 pages instead of 100 (200 on A5 sheets). So not a big deal, but better having this in your toolbox and make the effort of paying what it costs.

### Overcome frustration

Through being hit you will learn to not being paralysed in combat or what is even worse, driven by fear or greed when realizing that no matter how well you follow what you have learnt you fail to achieve success trading repeteadely. You will learn how to avoid the punches of your adversaries; and finally if you have what takes, do not hold out a lot of hope on it, you will start winning on the ring. Your bedside books this time will be (1) Reading Price Charts Bar by Bar, by Al Brooks and (2) Naked Forex: High-Probability Techniques for Trading Without Indicators, by Alex Nekritin and Walter Peters.

This time practical trainig is also recommended. While you learn the concepts and tactics on these books including chart reading, entries and exits and risk management you may find yourself starting to win some wagers in the Forex arena. Remember, you are still just winning, nothing really similar to earning money by trading. You are not even an amateur at this stage. Please, consider keeping training in the manner I will tell you next for one or two months, reviewing the books and concepts one time after another until you really fully comprenhend everything.

Open a free demo Metatrader account on a Forex broker with a simulated maximum leverage of 50. In any case stay between 20 and 50 leverage level. Try https://tickmill.com, they won’t bother you with stupid phone calls trying to steal your money by suggesting you to open a real account as many or most of the brokers do. Remember: never ever accept opening a trading account from a phone call. Brokers will insist you are ready to trade with real money with their support. That is false, that is a thief trying to steal your hard earned money. Treat him/her accordingly and ask yourself why someone having the ability to earn money in that way would have such a horrible work instead of being on a beach in Bahamas trading with the laptop while having margaritas and other good experiences.

So once you install Metatrader find out the market opening hours and try to trade within the previous and the later hours. Avoid always trading news. Look for one or two major pairs, let’s say EURUSD, USDJPY or so. Find spreads lower than 3-5 pips and avoid the rest. Never trade bars that are higher than average and too far from equilibrium (10-30 EMA level). You can create a demo account with Metatrader itself. Do it and set it with the parameters above (\$500 of capital and 50 leverage). Test your knowledge and the new books risking no more than 50-60 pips on the 5M or 15M charts until your account peters out several times. Learn from it. Do it again and again for a few weeks or months. Avoid improvisation, stick to your strategy. See how it fails. Get over it and avoid acting by fear or greed and stay calm and cold.

### Join the amateur’s wagon

It is unlikely but not impossible that trading correctly support/resistance and round number levels you have started to see that finally your trades seem to anticipate market moves. Less probable that you even have been making profit from it for months. This is the next milestone in the plan you have been devoted to during the last months, maybe the last year until you got here.

First, stay out while news are released. This page contains a calendar that you shall use: https://www.forexfactory.com/calendar.php. Those long bars, horrible spreads and slippage levels are not seldom cause of news.

I would have one main concern at this point of the post: what about fundamental analysis?

“HA-HA”, yes, millions of operators accross the world with different purposes and reasons to act, each one of these operators ignoring the reasons of the others. Millions of plans which their trades respond to, private internal information that will never be shared with the public, outside the organizations. It is impossible to now how every agent is going to act today. With technical analysis, at least, you are whatching at what all of them are doing at this very moment and since ever and how securities are reacting to that. In Forex, wait for price to react to fundamentals and keep safe from news releases.

Now, check the next figure and see how the platform installed in the previous step with your demo account looked like, being indistinguishable from a live account platform. The platform simulates any trading operation in real-time on the Forex market, it is free and it does not use real money for demo accounts.

From here on, there is no one strict way to proceed. The last books I will add to your toolbox is will leverage your odds when choosing the right side to shoot from, Understanding Price Action by  Bob Volman.

Do not venture to such readings until you have mastered the prior lessons, meaning that even if you are still not being profitable in average, at least you have reviewed everything several times, enough to have already a nitid understanding of price action and psychological aspect of trading subtetlies. We have not been aiming to earn money yet, just to understanding price action and to get calm and stick to the plan when facing frustration within the session.

The other book, Forex Price Action Scalping, is also written by Bob Volman. In my opinion Bob Volman is one of those writers that excels over the rest in their field. If the first book is about understanding price action, this second book is more about grabbing the best of every situation in the market. I recommend reading first Undertanding Price Action. I think that the ideas presented on both are universal for any time frame. Be advised that this book is based upon the 70-bars chart, but I have seen the same patterns working, adjusting the settings to the volatility within that timeframe, in the higher timeframes due to the fractal nature of price which patters are repeated over and over again at any scale.

Fractals are structures that repeat themselves across any possible scale (size). The most known example is the one named “the Mandelbrot set”. But, please, do not rush into your book store to buy stuff about fractals and trading, there is no need for it. Just catch this idea of price patters being always present at any time scale and enjoy how a fractal set looks like whatching the image below.

By combining all the teachings since now and the lessons learnt within those live trading rooms, now continue with the demo trading and see if you are able to get consistent earnings for months.

If you are able, then you are one step out from catching the amateur’s wagon and start putting some real money. But not your money, find a broker offering a free welcome bonus account. Here you may be forced to raise the leverage level since those accounts are very small. However, with \$30 you have a lot to do with 0.01 lot position size risking no more than 50 cents per trade if you pick very well your trades. If the bonus account runs out of money, find another broker and start again with a bonus account. There are tens.

Once you are doing well with your bonus account and making it grow for weeks or months, start considering risking your first \$100 at a 30-50 leverage live account. But before doing it, carefully review all the consequences, legal and financial, of real trading and how it can affect you. Have in mind that the risk is very high and that you will probably have an average probability of losing all the money you put in higher than 90% or 99%. This is a very difficult and financially dangerous endeavour, be advised.

Finally, look for multiple resources, the net is full of them and you will find priceless articles, forum threads and videos. Do not let yourself being convinced by apparently logic and obvious winning techniques. Test everything always for months before deciding if it works or not.

Again, remember that this post is not a recommendation for trading. This post is meant to give support to those that, being interested on learning trading, have not found yet a clear guide of valid steps to follow and work on to improve or adquire in some way their trading skills. The followed results are not guaranteed and this post represents solely the subjective view of the author. The methods described may not be suitable for other persons and the author is not an expert trader nor holds any certification on the matter. This text is also subject to errors, misconceptions and interpretation; it could be completely wrong in every aspect.

### Final words

One of the most useful tools I have been using for backtesting is a paid tool called Forex Tester. It is not very cheap but it is not costly for what it has to offer to you. It has the same aspect as Metatrader and similar user interface. It also comes with real market data and can also import data from other tools and brokers.

The purpose of this tool is training and you can trade any past period simulating that is the present time. The tool shows you the market status and generates new bars for you so that the feeling is very similar to demo and real trading. You can also freeze the time and decide when the next bar will be added by just clicking one key.

Of course the typical trading operations are implemented such as market long/short orders, sell and buy stop/limit, trailing stops, etc. Look at this screenshot:

The developers offer a free trial of the tool on https://www.forextester.com/ so you can evaluate it.

I would like the reader appreciates the fact that I am not affiliated to any of the resources and companies mentioned on this post, including the ones I shared links to, like Tickmill or Forex Tester. I could have used affiliate Amazon links for the books and same thing for the other stuff, but I want the reader to be sure my opinion is not constrained nor directed by economical interests, recommending books and resources I am paid for.

Please, be careful, stay calm, act wisely and be patient. Best luck.

## Predicting Stock Exchange Prices with Machine Learning

This article will describe how to get an average 75% prediction accuracy in next day’s average price change. The target magnitude is the 2-day simple moving average. The reason is that if we do not apply smoothing to daily prices, the forecasts are much harder to get. The minimum possible smoothing is two days, and that will be the target: altering actual prices as little as possible.

I have selected randomly a company from the New York Stock Exchange, it was “CNH Industrial NV”. No reason for that, it has been a completely random choice among a couple thousand files I have generated extracted from either Yahoo! or Google finance, I do not remember the source. The files are uploaded here: https://drive.google.com/open?id=18DkJeCqpibKdR8ezwk9hGjdHYSGwovWH.

The method is valid for any financial data as long as it has the same structure. I have also tested it with Forex data getting similar accuracy levels with currencies such as EURUSD, GBPUSD or USDJPY. The interesting point of forecasting those quotes is that by examining where it fails, I think you will improve your price action trading skills and your understanding of the market and what matters.

## Data Collection and Variable Configuration

There are millions of possible variable candidates that may seem valid to be analyzed. And which will be the target value we will try to aim? I like thinking that price is like any other object subject to physical laws. It reacts to market forces, it has an inertia, velocity, acceleration, etc.

The forces may be volume, it may have a potential energy depending if it is very high or very low, the rate of change may be important and so on. There are other many factors we could analyze such as gaps, breakouts, technical patterns, candlestick analysis or price distribution within space just to mention a few. For this example we will only be focused on price action and volume.

I have the files saved in csv format to be used with Excel, so let’s start loading the csv file into a DataFrame object using Python.

```# Importing all the libraries that we will use.

import pandas as pd
import matplotlib.pyplot as plt
import xgboost as xgb
from sklearn.metrics import accuracy_score

#Load the data from a csv file.

The previous code will, after extracting, remove a column that won’t be used (“Adj Close”) and creating an index using the “Date” column. The date is not a variable we may use for forecasting, so there is no need to keep it as a column of the dataset.

The data now has the typical structure of the financial data: Date, Open, High, Low and Close. The first three rows are shown in the next table:

 Date Open High Low Close Volume 2013-09-30 2.75 13.08 12.5 12.5 352800 2013-10-01 12.76 13.16 12.75 12.92 1477900 2013-10-02 13.02 13.08 12.87 12.9 1631900

### Predictors

We are going to omit High, Low and Open, using only Open and Volume for the study. Let’s start preparing the data for the analysis. The predictors (X variables) to be used to predict the target magnitued (y variable) will be the following ones:

• Two day simple moving average (SMA2). The formula is (Ct – Ct-1)/2, being Ct equal to current day’s open price and Ct-1 to previous day’s open price. This formula is applied to each row of the data set.
`Predictors = pd.DataFrame({"sma2":CNHI["data"].Open.rolling(window=2).mean()})`
• 1 day window SMA2. The previous day’s SMA2 value.
`Predictors["sma2_1"] = Predictors.sma2.shift(1)`

And the other predictors will be:

• Current day SMA2 increment. (SMA2t – SMA2t-1).
• 1 day window SMA2 increment. (SMA2t-1 – SMA2t-2).
• Current day volume increment. (Volt – Volt-1).
• Current day volume rate of change. (Volt – Volt-1)/Volt
• 1 day window open price. (Ct-1)
• Current day open price increment. Ct – Ct-1
• Current day open price. Ct.
```Predictors["sma2_increment"] = Predictors.sma2.diff()

Predictors["sma2_1_increment"] = Predictors.sma2_1.diff()

Predictors["vol_increment"] = CNHI["data"].Volume.diff()

Predictors["vol_rel_increment"] = CNHI["data"].Volume.diff() / CNHI["data"].Volume

Predictors["open_1"] = CNHI["data"].Open.shift(1)

Predictors["open_incr"] = CNHI["data"].Open - CNHI["data"].Open.shift(1)

Predictors["open"] = CNHI["data"].Open

# The rows with nulls generated by rolling values will be removed.
Predictors = Predictors.dropna()```

A sample of the first 5 rows:

 Date sma2 sma2_1 sma2_increment sma2_1_increment vol_increment vol_rel_increment open_1 open_incr open 2013-10-03 12.895 12.89 0.005 0.135 -495500 -0.436026047 13.02 -0.25 12.77 2013-10-04 12.765 12.895 -0.13 0.005 -21800 -0.019558586 12.77 -0.01 12.76 2013-10-07 12.59 12.765 -0.175 -0.13 -400 -0.000359002 12.76 -0.34 12.42 2013-10-08 12.42 12.59 -0.17 -0.175 104600 0.08582212 12.42 0 12.42 2013-10-09 12.5 12.42 0.08 -0.17 -232400 -0.235604217 12.42 0.16 12.58

### Target Variable

This will be a classification variable, if the average price will go either up or down the next day.  The target will be forecasting the difference between today’s price and tomorrow’s price (which is unkonwn).

`target = pd.DataFrame({"value":Predictors.sma2.shift(-1) - Predictors.sma2}).dropna()`

After calculating the data to predict, the three first rows look like this:

 Date value 2013-10-03 -0.13 2013-10-04 -0.175 2013-10-07 -0.17

Finally we will match predictors and target values by date and remove those rows without counterpart in the other table.

```X = pd.merge(Predictors, target,left_index=True,right_index=True)[Predictors.columns]
y = pd.merge(Predictors, target,left_index=True,right_index=True)[target.columns]```

X now contains the predictors and y the target values. The table contains 1,059 records at this moment.

The extreme gradient boosting is an exceptional machine learning technique for many reasons. It is based on decision trees and it has nice features such as residuals analysis, non-linear regression, feature selection tools, overfitting avoidance and many other more. Other machine learning alternative techniques commonly used for this type of analysis are Support Vector Machines, Neural Networks and Random Forest. I have used all of those for predicting market prices and the Extreme Gradient Boosting is always my first choice.

We will setup the regression model using the 65% of the data and with that model, the next 35% of the data will be used to predict future values. This simulates the actual scenario in which we have past data to train our model and we want to predict how a future datum will be with the data we currently have on hand. The data will be split in two sets: the training set to preconfigure the model and the testing set that won’t be used to build the model, but only to test if it works as expected with new data.

```train_samples = int(X.shape[0] * 0.65)

X_train = X.iloc[:train_samples]
X_test = X.iloc[train_samples:]

y_train = y.iloc[:train_samples]
y_test = y.iloc[train_samples:]```

After applying the data splitting, the test data set contains:

• Train records: 688.
• Test records: 371.

The target variables will be transformed for binary classification. A positive change in the value of prices will be classified as 1 and a non-positive change as 0.

```def getBinary(val):
if val>0:
return 1
else:
return 0

# and the transformation is applied on the test data for later use.
# The train data will be transformed while it is being fit.
y_test_binary = pd.DataFrame(y_test["value"].apply(getBinary)```

And next, the model is trained and the test data predicted to verify the accuracy of the system:

```regressor = xgb.XGBRegressor(gamma=0.0,n_estimators=150,base_score=0.7,colsample_bytree=1,learning_rate=0.01)

xgbModel = regressor.fit(X_train,y_train.value.apply(getBinary))

y_predicted = xgbModel.predict(X_test)
y_predicted_binary = [1 if yp >=0.5 else 0 for yp in y_predicted]

print (accuracy_score(y_test_binary,y_predicted_binary))

Out: 0.76010781671159033```

So, the initial accuracy without optimizing the model is 76% predicting the daily average price change for each of the the next 371 trading days.

The model can be optimized, I have just used a few parameters to avoid overfitting with the training data and adjusting the learning rate.

The features used should also be analyzed to avoid using redundant variables and to discard those with no correlation. New features should be added to try improved approaches and, to sum up, there is a lot of work that could be done around this basic model.

XGBOOST has also ways to study features. Let’s take a look at their importance:

```fig = plt.figure(figsize=(8,8))
plt.xticks(rotation='vertical')
plt.bar([i for i in range(len(xgbModel.feature_importances_))], xgbModel.feature_importances_.tolist(), tick_label=X_test.columns, color="chocolate")
plt.show()```

It is obvious that the field extension is huge and especially interesting.