Wednesday, December 28, 2011

Euro drops to near 10 year low

Dec. 28 (Bloomberg) -- The euro dropped against the yen to almost the lowest level since 2001 as the European Central Bank’s balance sheet soared to a record after it lent regional banks more money last week to keep credit flowing.
The 17-nation currency fell against the dollar to the lowest level since January as concern increased that the region’s sovereign-debt crisis will curb growth, even as rates fell at an Italian bill sale. The dollar gained as stocks dropped, boosting demand for haven assets. The yen gained on a safety bid after a U.S. Treasury report criticized Japan for intervening in the currency market and as economic reports signaled slowing economic growth.

S&P 500 Snaps 5-Day Rally Amid Europe Concern

U.S. stocks declined, halting a five- day advance in the Standard & Poor’s 500 Index, as the European Central Bank’s balance sheet increased to a record after a surge of bank lending to stem the region’s debt crisis.
All 10 groups in the S&P 500 fell as measures of commodity, financial and industrial shares slid at least 1.4 percent. Alcoa (AA) Inc. andCaterpillar Inc. (CAT) dropped more than 1.9 percent. Bank of America Corp. (BAC) sank 2.9 percent, extending yesterday’s slump.
The S&P 500 lost 1.1 percent to 1,252.03 at 11:33 a.m. New York time, after rallying 5 percent over the previous five days. The Dow Jones Industrial Average fell 120.41 points, or 1 percent, to 12,170.94. About 1.5 billion shares changed hands on U.S. exchanges, or 35 percent below the same time a week ago.

Monday, December 19, 2011

Repainting Indicator

Repainting indicators are one of the most common problems facing would-be EA designers. Sometimes it seems like an indicator really has its thumb on the market. It perfectly predicts when the market will go up or down. Making an EA run off of the indicator is a no-brainer.

If this sounds familiar, then you need to consider whether or not the indicator repaints.

What is a repainting indicator?

First consider a baseball analogy. Last week, the St. Louis Cardinals beat the Texas Rangers to win the World Series. A repainting indicator is like asking you today, "Would you bet on the Cardinals or Rangers?," then pretending like you placed your bet 3 weeks ago and not today. You had no way of knowing for certain who would actually win.

How normal indicators work

If that sounds stupid, that's because it is. Repainting indicators use knowledge of the "historical future" to "predict" which way the market was going to go. Indicators typically start with old bars on the left side of the chart, then walk forward to the right side until they obtain enough information to make a decision. Say, for example, that you want to create the RSI indicator on a daily chart and you want to start on October 25. You start at October 25, and make decisions based on information from Oct. 24, 23, etc., until you've collected 14 bars worth of data or however long you set the period. You then say definitively that the RSI on October 25 is X.

Now you move on to October 26. You look back at October 25, 24, and so on, until you get the final RSI value for October 26. All of the examples move forward into the future, then look back into the past to get the current value.

A hypothetical repainting indicator

Now let's make a hypothetical RSI indicator that repaints. Say our first date under consideration is October 25. We might start by looking into the past, which is what we did last time. But for the sake of making the example obvious, let's look the other way - into the future.

We gather data from October 26, 27, and so on. Now, having learned the future, we jump back to October 25 and pretend that we knew the future all along. The RSI looks amazing.

A Real Example

I receive copies of the 3_Level_ZZ_Semafor every once in a while and it tends to cause problems. It's based on the same idea as the zig zag indicator, which helps it to pick perfect tops and bottoms. Trend traders love looking at it. The information seems like it would be so valuable.

Making an EA off of it, however, presents numerous problems. The trades that the Expert Advisor makes do not match up with what the indicator looks like a few hours later. What actually happens is that the indicator changes its mind. It doesn't leave behind any evidence of its incorrect prediction. The novice EA designer naturally concludes that the programming is all wrong and that we need to fix it.

The first image below shows the value of the semafor on a EURSUD M5 chart at 00:00 on October 10, 2011. You'll notice that a purple dot with the number 2 appears on that bar. The market is moving strongly upwards. The indicator at this point predicts that the move is likely overextended.

The 3_Level_ZZ_Semafor before repainting

If we jump 2 bars into the future at 00:10, now magically a number 3 dot appears. What's worse is that the number 2 dot disappears entirely from the 00:00 bar! The indicator sees that the price jumped up, so it goes back in time and deletes the evidence of its incorrect prediction. If you weren't watching the indicator or taking screenshots, it is as though the number 2 dot was never there.

The 3_Level_ZZ_Semafor after repainting

Test your indicator and look for repainting

No automated process exists to determine whether or not your indicator repaints. It's something that you have to do by staring at the chart during a backtest to see if the values change or not.

I like to do this by selecting an Expert Advisor without caring too much about which one I pick. If you don't have any installed, you can grab one from the free Expert Advisor page for the sake of testing.

You'll need to select settings to get the backtester to run. The options that you choose are not important, so long as you have selected something. I highlighted the required items in red below. I personally prefer to use M5 EURUSD charts. M5 is good because the backtester moves over the data quickly on full speed without going so fast that I cannot control the process. It strikes a nice balance between speed and following what's actually happening. I like EURUSD simply because I always have EURUSD data loaded. I don't have to worry about the tester not working, whereas that would likely be an issue if I selected an exotic currency like TRY/ZAR.

MetaTrader Backtester Settings

When the visual backtest chart pops up, the first thing to do is to load your indicator onto the chart. As the test progresses, watch how the indicator behaves. Some repainting indicators like the Semafor are completely obvious. They repaint on most bars, so it's easy to tell that it is not consistent. You can safely assume that it repaints.

Other indicators are more tricky. They only plot once every 50 bars and sometimes even less frequently. You have to watch closely to make sure that where plots appear, that they don't subsequently dance onto another bar. I like to use the same technique in the first screenshot that I posted with a vertical red line. Whenever the indicator plots something on the chart, mark it with a vertical line. Continue doing so for about a day's worth of data on the M5 chart. Stop the test, then scroll back through the chart. If 100% of the plotted values match the vertical lines, then your indicator does not repaint.

Wednesday, December 14, 2011

Time based orders in MetaTrader 4

The short summary is that time based orders in MT4 are possible. You just need to keep in mind some of the technical limitations. MetaTrader 4 EAs work based off of incoming ticks. Whenever the bid/ask changes by a micro pip or more, that event triggers the EA to do something.

When the markets hum along at a rapid pace, this effect is not noticeable. When the quote flow slows down, however, it can cause the EA to sleep entirely through your trading window.

Many news traders want to bracket buy and sell stops around the price at 08:29 on NFP Friday. Everyone knows that a major news event is about to release. Trading slows down, as does the flow of quotes. The pending orders may not set in time if an incoming tick does not arrive in the 60 seconds between 08:29 and 08:30. This seems unlikely to many novice traders, but it happens frequently enough that we inevitably receive these types of questions whenever we program a time-based order placement EA.

The workaround typically satisfies most traders. EAs place an order at the first available tick within a certain time window. Using NFP as the example, the EA might seek to place bracket orders around the first tick between 08:29-08:32. The chances of making it 2 minutes past 08:30 without a single tick are low. And given that you are taking the first tick, the orders will succesfully bracket around the price the vast majority of the time. It is important to keep in mind how the backend works for those one-off events where the orders do not function as desired.

MetaTrader 5 addresses MT4′s time weakness by offering event driven programming. It is possible to rewrite MQL4 EAs into MQL5 and to demand an action at a precise moment in time. The MQL5 timer actively watches the clock. Regardless of what the markets do, the EA can know to wake up and do whatever action is needed. This is one of the few scenarios where converting from MQL4 into MQL5 comes with obvious advantages.

Wednesday, December 7, 2011

Automated Choice of Brokerage Company for an Efficient Operation of Expert Advisors


Very often we face situations when an Expert Advisor successfully operates with one brokerage company and is not profitable or even lossmaking on the other one. The reasons may be different. Different brokerage companies have different settings:
  • Quotes. They slightly differ because of two factors - different data feeds and different filtration that smooths quotes. For some Expert Advisors this may be relevant. Situations may occur when an EA trades often with one brokerage company and rarely on another one.
  • Slippage. It may differ much in different brokerage companies. This also may lead to worse characteristics of an EA because of lower expected profit. 
  • Requotes. In some brokerage companies they are oftener than in others. In this case an EA will miss successful entering points because of large number of requotes.

Color Changing Indicators in MetaTrader

Many custom indicators in MetaTrader use lines that change colors to indicate a change in trend or market condition. Those types of indicators are among the more common requests that we get for programming Expert Advisors. Unfortunately, these indicators often present problems. What you see on the chart is not necessarily what the indicator says.

Take a look at the image below or click the link to view it in full size. You'll notice that I included the data window for the indicator, which is SuperTrend. As we walk from left to right, the data window does not suddenly shift from Trend Up to Trend Down. Instead, it shifts with an in between period where the trend is both up and down.

MT4 Color Changing Indicator

While the visual effect is immediately obvious, the numbers do not clearly indicate the indicator's condition. In fact, it frequently happens where the indicator entirely misrepresents its true calculation. If the last bar was only green and the indicator says that the current bar is red-green, then we can safely assume that the indicator is switching from green to red.

When the indicator says it's red-green and the past bar was red-green, it gets more tricky. We are forced to keep looking back through time until a "clean" red or green value appears. This enables us to capture the indicator's real value.

It does not, however, make for a happy trader. Consider the case where the indicator plots red-green, red, red-green, red. Because of the way MetaTrader draws lines from point to point, the indicator actually appears as a solid red line - a long, beautiful sell signal, right?

In fact, its true calculated values are red, green, red, green. This glitch can make for some ugly surprises. This is especially so when traders expect to ride a down trend and the line appears as solid red, but the indicator (and thus EA) keeps flip flopping on the trade direction. You have to keep this in mind when building EAs around custom indicators.

Tuesday, December 6, 2011

Copyright Issues When Programming Expert Advisors

People frequently discover indicators on forums, trading groups, etc. Although most are bad, occasionally an indicator looks promising enough to use if for programming an expert advisor. Are you allowed to use the indicator that you found in an Expert Advisor?

The crux of the matter is what you intend to do with it. Intellectual property (IP) law has a concept called public domain. If you create an indicator and then share it on a public web site, then the file is presumably free for all to use. Essentially, you give away the copyright when you share it for all to see. The only caveat would be if the file or hosting page places an explicit restriction on how the file may or may not be used.

Personal use of an Expert Advisor does not pose a problem. This includes indicators that you purchased. The only legal reason that prevents you from using a commercial indicator in a personal EA is if the purchasing agreement for the indicator forbids creating derivative products, such as using the indicator to build a new EA.

Writing a commercial EA with a public domain custom indicator also meets the hurdle. You cannot take someone else's work and pass it off as your own; you must substantially alter or improve it before the work becomes yours. Turning an indicator into an automated strategy qualifies as a substantial improvement.

The thorniest issue pops up when you want to use a commercial indicator to create a commercial EA without the permission of the indicator's owner. Most people, myself included, would call this theft. The only way that you can do this legally and ethically is to secure an agreement from the indicator's copyright owner.

I am a programmer. If you're deeply concerned about this issue and want to ensure this information truly reflects US law, then I strongly suggest that you speak with an intellectual property attorney. The information above is for general information purposes and should not be construed as legal advice.

Here's a quick summary:

    Create a private EA from a public domain custom indicator - Almost always ok, unless the indicator owner forbids it

    Create a private EA from a commercial custom indicator - generally OK, unless your purchasing agreement forbids it

    Create a commercial EA from a public domain custom indicator - generally OK, unless the indicator's creator forbids it

    Create a commercial EA from a commercial indicator - never OK, unless you strike an agreement with the copyright owner

Thursday, December 1, 2011

Central Banks coordinated move - lower USD Swap lines by 50 bp

TOKYO: Asian shares extended gains on Thursday after the world's six major central banks moved to tame a liquidity crunch for European banks by providing cheaper dollar funding. 

MSCI's broadest index of Asia Pacific shares outside Japan jumped 1.3 percent, after U.S. stocks rallied 4 percent and European equities rose 2 percent on Wednesday. Japan's Nikkei opened up 1.7 percent on Thursday. 

The U.S. Federal Reserve, the European Central Bank and the central banks of Canada, Britain, Japan and Switzerland said on Wednesday they would lower the cost of existing dollar swap lines by 50 basis points from Dec. 5, and arrange bilateral swaps to provide liquidity for other currencies. 

A move by China on Wednesday to cut the percentage of cash banks must keep as reserves also boosted sentiment. 

The central banks' move aims to thaw severe funding strains for European banks as lenders had been extremely reluctant due to concerns over the eurozone's ability to quickly resolve its debt crisis, and could warm investor stance towards risk.