Reading the fine print for Dukascopy JForex Strategy Contest

Update: I have since won the JForex strategy contest a few times. If you are looking to learn JForex programming, you can start with an explanation of my winning strategy and my long list of JForex posts. The Dukascopy JForex Strategy Contest began last week. Starting this month, Dukascopy offers \$15,000 total of cash prizes to the top 10 automated trading systems every month. However, before entering, there are a couple of clauses in their contest rules that caught my attention.

  1. Cash prizes are deposited into contestant's Dukascopy account (Ranking and Awarding Rules #5).
  2. All source code needs to be submitted and agrees to be published (Ranking and Awarding Rules #2 and #6).

#1 is troublesome because it means winners have to open an account with Dukascopy if they don't already have an account with them. But that isn't a big deal since they are actually one of the best forex brokers. I planned to open an account with them anyway. However, #2 will certainly turn many people away. The requirement to disclose source code in an automated trading contest is almost unheard of as far as I know. Are you willing to disclose your winning system just for that few hundred, or a even few thousand, dollars of prize money? In any case, I decided to enter this contest anyway just for fun. Yet, you can be sure that I won't be using any proprietary logic in my system. In fact, I just finished my automated trading strategy today and it is now running. I will discuss about my strategy after it completes its first trade. That might take a while as my system is expected to make fewer than 5 trades in a month.

From Object Oriented Programming to Object Oriented Design

The good news is that I have finished my data scraping module for extracting all the company data listed on the two Toronto Stock Exchanges (TSX and TSV). I presented an alpha version of this data scraping module last week. I added the functionality to download all the historical quotes from Yahoo for all the companies. These data are stored locally in a cool HDF5 format with PyTables. I am very happy about that part. The bad news is that my module looks more like a hacked job than a piece of software. There is no defined structure for my underlying codes. It is obvious to me that this module would be difficult to extend, which is the whole point of my project. In programming jargon, my code smells (Wikipedia). Then it finally dawned on me. I am way behind in my programming skills. For the past few years, my quant work has been confined to MatLab or some proprietary trading platforms. I wrote scripts. Not programs. My problem with programming is that I am still stuck in a procedural programming mentality because that is what I've always used. Even though I updated my knowledge recently by learning Python, learning to program with an object oriented programming language is only the first step. (I am also familiar with C++ and Java from way back) To use an analogy, programming a software is like constructing a building. The actual coding itself is akin to the construction work. I got that part. But coding without a well designed plan can only go so far. It is now clear to me that if I am to build the trading software to complement my trading as I have envisioned it, I need to step it up with my software design ability. Consequently, I am now catching up on object oriented design and analysis, as well as software design patterns with a couple of books. I will try not to get too carried away with these though. My main focus is trading. But this will be very beneficial for my trading in the long run if I can build a more robust trading software that can easily grow with my future needs. I have many big plans for this side project of mine. I posted a UML diagram showing an architectural overview of my project.

Data Scraping the Toronto Stock Exchange: Extracting 3,660 companies' data

One of the tasks that I've always wanted to make more efficient in my stock trading is the work of scanning for stocks to trade. One look at my trading strategy posts and you'll see that I have devised many stock scanning systems in the past few years. The most recent system that I've used is one that uses options data to filter stocks. However, it is not automated. So it takes a lot of time to gather and analyze the data. Furthermore, the set of tools that I use is limited to U.S. stocks. Now that I have taken an interest in the Canadian stock market, I can't seem to find any public tool that I like. Thus, I am biting the bullet now and taking my time to develop a custom system once and for all. Before we can analyze stock data, we need to extract them first. Where better else for that than go straight to the source at TMX.com, the parent company of Toronto Stock Exchange (TSX) and TSX Venture Exchange (TSXV). TMX.com provide a list of publicly traded companies in ten Excel files. The files are divided by sectors. Each contain a number of fundamental company data, such as market capitalization and outstanding shares. So step 1 is to extract those data. This is where I am at now. I attached the source code for an alpha/developmental release below for anyone interested. It is a working program to scrape the data from TMX.com's files. But it's still a work-in-progress. That's why I am calling it a version 0.1. The next milestone is to program a Stocks class to hold, organize, and manage all three thousand, six hundred, and sixty companies' data. This is an easy task to do by extending the built-in dictionary class in Python. However, I haven't gotten to that chapter yet in my scientific programming with Python learning book. I stopped at chapter 8 to work on this project. Chapter 9 is the inheritence and hierarchical material. The goal of this project is to build an automated data scraping program for TSX and TSXV data from various sources into my computer. Once I have my data, that's when the real fun starts. Regarding the code below, I know that source code is useless for most people. Once the project is complete, I will compile the code into a standalone application and post it on this site. Subscribe to my RSS feed so that you can keep up-to-date with the progress of this project and my other ramblings on trading. [python] # extractTMX.py # version: 0.1 alpha release # revision date: March, 2010 # by Paul, Quantisan.com """A data scraping module to extract company listing excel files from TMX.COM""" import xlrd # to read Excel file #import sys from finClasses import Stock # custom Stock class def _verify(): """Verification function for a rundown of the module""" pass # copy test block here when finished def findCol(sheet, key): """Find the column corresponding to header string 'key'""" firstRow = sheet.row_values(0) for col in range(len(firstRow)): if key in firstRow[col]: return col # return first sighting else: # not found raise ValueError("%s is not found!" % key) def scrapeXLS(book): """Data scraping function for TMX Excel file""" listingDict = {} # dict of ('ticker': market cap) for index in range(book.nsheets): sh = book.sheet_by_index(index) mcCol = findCol(sh, "Market Value") assert type(mcCol) is int, "mcCol is a %s" % type(mcCol) osCol = findCol(sh, "O/S Shares") assert type(osCol) is int, "osCol is a %s" % type(osCol) secCol = findCol(sh, "Sector") # multiple matches but taking first assert type(secCol) is int, "secCol is a %s" % type(secCol) hqCol = findCol(sh, "HQ\nRegion") assert type(hqCol) is int, "hqCol is a %s" % type(hqCol) for rx in range(1, sh.nrows): sym = str(sh.cell_value(rowx=rx, colx=4)) # symbol s = sh.cell_value(rowx=rx, colx=2) # exchange col. if s == "TSX": exch = "T" elif s == "TSXV": exch = "V" else: raise TypeError("Unknown exchange value") mc = sh.cell_value(rowx=rx, colx=mcCol) # market cap # check for empty market cap cell mc = int(mc) if type(mc) is float else 0 os = int(sh.cell_value(rowx=rx, colx=osCol)) # O/S shares sec = str(sh.cell_value(rowx=rx, colx=secCol)) # sector hq = str(sh.cell_value(rowx=rx, colx=hqCol)) # HQ region listingDict[sym] = Stock(symbol=sym,exchange=exch, mktCap=mc,osShares=os, sector=sec,hqRegion=hq) return listingDict def fetchFiles(fname): infile = open(fname, 'r') # text file of XLS file names listing = {} for line in infile: # 1 file name per line if line[0] == '#': continue # skip commented lines line = line.strip() # strip trailing \n print "Reading '%s' ..." % line xlsFile = "TMX/" + line # in TMX directory book = xlrd.open_workbook(xlsFile) # import Excel file listing.update(scrapeXLS(book)) # append scraped the data to dict return listing #if __name__ == '__main__': # verify block # if len(sys.argv) == 2 and sys.argv[1] == 'verify': # _verify() if __name__ == '__main__': # test block listing = fetchFiles('TMX/TMXfiles.txt') [/python]

Forex Trading: Income or Capital Gain Tax in Canada?

I've always known that foreign exchange trading is treated as capital gain tax in Canada. But just to be sure before filing my taxes soon, I've decided to double check the facts from Canada Revenue Agency. As you know, the difference between income tax and capital gain tax is substantial. Income tax is taxed at your marginal tax rate. Whereas capital gain tax is a generous half of your marginal tax rate. That works out to a 10% to 20% difference. Taxes in Canada is generally simple to do. The problem though, is sifting through the cacophony of information within the Canada Revenue Agency to find out the applicable rules. I've copy and pasted a couple of relevant excerpts from the 2010 CRA Income Tax Interpretation Bulletin for the record. Basically, forex trading can be treated as either income or capital gain tax in Canada (surprise). According to IT-95R Foreign exchange gains and losses.

  1. Where it can be determined that a gain or loss on foreign exchange arose as a direct consequence of the purchase or sale of goods abroad, or the rendering of services abroad, and such goods or services are used in the business operations of the taxpayer, such gain or loss is brought into income account. If, on the other hand, it can be determined that a gain or loss on foreign exchange arose as a direct consequence of thepurchase or sale of capital assets, this gain or loss is either a capital gain or capital loss, as the case may be. Generally, the nature of a foreign exchange gain or loss is not affected by the length of time between the date the property is acquired (or disposed of) and the date upon which payment (or receipt) is effected.

As you can see, it is very vague. That's why forex trading can be considered income or capital gain tax. It is up to you and your accountant to figure out which works for you. A noteworthy point in the above excerpt is that the holding period is not taken into account. So there's no 30-day rule like in the states whereby frequent trading would miss out the capital loss credit if they re-purchase the same asset within 30-day of disposal. Update: Looks like I have misconstrued the above article with regard to capital loss. As Olga pointed out in the comments, Chapter 5 of T4037 defines Superficial Loss. In which if you repurchase your property (e.g. stock) within 30-days after a sale at a loss, then that initial loss cannot be deducted as a capital loss. More about the Superficial Loss rules in Canada can be found at WhereDoesAllMyMoneyGo.com. Further down the page in IT-95R, we have the following bullet.

  1. A taxpayer who has transactions in foreign currency or foreign currency futures that do not form part of business operations, or are merely the result of sundry dispositions of foreign currency by an individual, will be accorded by the Department the same treatment as that of a "speculator" in commodity futures see 7 and 8 or IT-346R. However, if such a taxpayer has special "Inside" information concerning foreign exchange, he will be required to report his gains and losses on income account.

IT-346R Commodity Futures and Certain Commodities explains the tax treatment of speculation in the commodity markets.

  1. As a general rule, it is acceptable for speculators to report all their gains and losses from transactions in commodity futures or in commodities ascapital gains and losses with the result that only one-half the gain is taxable, and one-half the loss is allowable subject to certain restrictions, (hereinafter called "capital treatment") provided such reporting is followed consistently from year to year.

So there, we have it. Amateur forex traders, such as myself, can report our forex trading gain/loss as capital gains and losses. The reason being that forex trading isn't part of my business operation because I have another primary source of income (e.g. salary from another job).

Addendum via reader Lem:

I think you forgot to mention that in IT-346 bulletin it states the following,

8) If a speculator prefers to use the income treatment in reporting gains and losses in commodity futures or commodities, it may be done provided this reporting practice is followed consistently from year to year. If income treatment has been used by a speculator in 1976 or a subsequent taxation year, the Department will not permit a change in the basis of reporting. *Interpretation Bulletin CPP-3 discusses the effect of the income treatment and capital treatment on self-employed earnings for the purposes of the Canada Pension Plan.

so just like you said FOREX can be treated as a Capital or Income gains/losses. You just have to be consistent on your filing, exactly what CRA consultant told me....if you filed it as business in the very beginning yo can't change it to Capital Gains.

←   newer continue   →