Algorithmic trading in linux using python
"A couple options that I know of: Interactive Brokers offers a ""paper trading"" mode to its account holders that allows you to start with a pretend stack of money and place simulated trades to test trading ideas. They also provide an API that allows you to interface with their platform programmatically for retrieving quotes, placing orders, and the such. As you noted, however, it's not free; you must hold a funded brokerage account in order to qualify for access to their platform. In order to maintain an account, there are minimums for required equity and monthly activity (measured in dollars that you spend on commissions), so you won't get access to their platform without having a decent amount of skin in the game. IB's native API is Java-based; IbPy is an unofficial wrapper that makes the interface available in Python. I've not used IB at all myself, but I've heard good things about their API and its accessibility via IbPy. Edit: IB now supports Python natively via their published API, so using IbPy is no longer needed, unless you wish to use Python 2.x. The officially supported API is based on Python 3. TD Ameritrade also offers an API that is usable by its brokerage clients. They do not offer any such ""paper trading"" mode, so you would need to ""execute"" transactions based on quotes at the corresponding trade times and then keep track of your simulated account history yourself. The API supports quote retrieval, price history, and trade execution, among other functions. TDA might be more attractive than IB if you're looking for a low-cost link into market data, as I believe their minimum-equity levels are lower. To get access, you'll need to sign up for an API developer account, which I believe requires an NDA. I don't believe there is an official Python implementation of the API, but if you're a capable Python writer, you shouldn't have trouble hooking up to the published interfaces. Some caveats: as when doing any strategy backtesting, you'll want to be sure to be pessimistic when doing so, so your optimism doesn't make your trades look more successful than they would be in the real world. At a minimum, you'll want to ensure that your simulations transact at the posted bid/ask prices, not necessarily the last trade's price, as well as any commissions and fees associated with the trade. A more robust scheme would also take into account the depth of the order book (also known as level 2 quotes), which can cause additional slippage in the prices at which you buy/sell your security. An even more robust scheme would take into account the potential latency of trade execution, looking at all prices over some time period that covers the maximum expected latency and simulating the trade at the worst-possible price." "You can have a look at betabrokers. It's an simulated stock trading platform which is entirely email-based. You start with 10 000$ and you make transactions with commands in the subject line of the email (e.g. ""buy 250$ AAPL"" or ""cover 20 shares of AAPL""). It should be straightforward to add an email interface to your python script." I don't think any open source trading project is going to offer trial or demo accounts. In fact, I'm not clear on what you mean by this. Are you looking for some example data sets so you can see how your algorithm would perform historically? If you contact whatever specific brokers that you'd like to interface with, they can provide things like connection tests, etc., but no one is going to let you do live trades on a trial or demo basis. For more information about setting this sort of thing up at home, here's a good link: < http://www.stat.cmu.edu/~abrock/algotrading/index.html >. It's not Python specific, but should give you a good idea of what to do. In Japan, there's a competition well-lasting since 2004 or so where you can run your own software agent in a virtual market. Market data is updated from the real world everyday. And if your agent proves good, the organizer puts it into the real market. The language is unfortunately limited to Java only to my knowledge. OS is not limited since your agent is supposed to run on the organizer's environment. English might not be well supported on their web site...
"I'm just trying to visualize the costs of trading. Say I set up an account to trade something (forex, stock, even bitcoin) and I was going to let a random generator determine when I should buy or sell it. If I do this, I would assume I have an equal probability to make a profit or a loss. Your question is what a mathematician would call an ""ill-posed problem.""  It makes it a challenge to answer.  The short answer is ""no.""  We will have to consider three broad cases for types of assets and two time intervals. Let us start with a very short time interval.  The bid-ask spread covers the anticipated cost to the market maker of holding an asset bought in the market equal to the opportunity costs over the half-life of the holding period.  A consequence of this is that you are nearly guaranteed to lose money if your time interval between trades is less than the half-life of the actual portfolio of the market maker.  To use a dice analogy, imagine having to pay a fee per roll before you can gamble.  You can win, but it will be biased toward losing. Now let us go to the extreme opposite time period, which is that you will buy now and sell one minute before you die.  For stocks, you would have received the dividends plus any stocks you sold from mergers.  Conversely, you would have had to pay the dividends on your short sales and received a gain on every short stock that went bankrupt.  Because you have to pay interest on short sales and dividends passed, you will lose money on a net basis to the market maker.  Maybe you are seeing a pattern here.  The phrase ""market maker"" will come up a lot. Now let us look at currencies.  In the long run, if the current fiat money policy regime holds, you will lose a lot of money.  Deflation is not a big deal under a commodity money regime, but it is a problem under fiat money, so central banks avoid it.  So your long currency holdings will depreciate.  Your short would appreciate, except you have to pay interest on them at a rate greater than the rate of inflation to the market maker. Finally, for commodities, no one will allow perpetual holding of short positions in commodities because people want them delivered.  Because insider knowledge is presumed under the commodities trading laws, a random investor would be at a giant disadvantage similar to what a chess player who played randomly would face against a grand master chess player.  There is a very strong information asymmetry in commodity contracts.  There are people who actually do know how much cotton there is in the world, how much is planted in the ground, and what the demand will be and that knowledge is not shared with the world at large.  You would be fleeced. Can I also assume that probabilistically speaking, a trader cannot do   worst than random? Say, if I had to guess the roll of a dice, my   chance of being correct can't be less than 16.667%. A physicist, a con man, a magician and a statistician would tell you that dice rolls and coin tosses are not random.  While we teach ""fair"" coins and ""fair"" dice in introductory college classes to simplify many complex ideas, they also do not exist.  If you want to see a funny version of the dice roll game, watch the 1962 Japanese movie Zatoichi.  It is an action movie, but it begins with a dice game.   Consider adopting a Bayesian perspective on probability as it would be a healthier perspective based on how you are thinking about this problem.  A ""frequency"" approach always assumes the null model is true, which is what you are doing.  Had you tried this will real money, your model would have been falsified, but you still wouldn't know the true model. Yes, you can do much worse than 1/6th of the time.  Even if you are trying to be ""fair,"" you have not accounted for the variance. Extending that logic, then for an inexperienced trader, is it right to   say then that it's equally difficult to purposely make a loss then it   is to purposely make a profit? Because if I can purposely make a loss,   I would purposely just do the opposite of what I'm doing to make a   profit. So in the dice example, if I can somehow lower my chances of   winning below 16.6667%, it means I would simply need to bet on the   other 5 numbers to give myself a better than 83% chance of winning. If the game were ""fair,"" but for things like forex the rules of the game are purposefully changed by the market maker to maximize long-run profitability.  Under US law, forex is not regulated by anything other than common law.  As a result, the market maker can state any price, including prices far from the market, with the intent to make a system used by actors losing systems, such as to trigger margin calls.  The prices quoted by forex dealers in the US move loosely with the global rates, but vary enough that only the dealer should make money systematically.  A fixed strategy would promote loss. You are assuming that only you know the odds and they would let you profit from your 83.33 percentage chance of winning. So then, is the costs of trading from a purely probabilistic point of   view simply the transaction costs? No matter what, my chances cannot   be worse than random and if my trading system has an edge that is   greater than the percentage of the transaction that is transaction   cost, then I am probabilistically likely to make a profit? No, the cost of trading is the opportunity cost of the money.  The transaction costs are explicit costs, but you have ignored the implicit costs of foregone interest and foregone happiness using the money for other things. You will want to be careful here in understanding probability because the distribution of returns for all of these assets lack a first moment and so there cannot be a ""mean return.""  A modal return would be an intellectually more consistent perspective, implying you should use an ""all-or-nothing"" cost function to evaluate your methodology." Have you looked into GnuCash? It lets you track your stock purchases, and grabs price updates. It's designed for double-entry accounting, but I think it could fit your use case. Algo traders/quants/market backtesters/coders/et al.  I am looking to backtest basic things like the correlation of P/E, EV/EBITDA, etc. to market performance. I know a little R and Python.  What is the easiest way to learn to backtest this sort of stuff? I want to eventually get into algo trading sort of stuff.  I'd **greatly** appreciate it! "Just read the book Flash Boys by Michael Lewis.  It describes this process in detail, albeit a bit more dramatically than it has to.  Basically what ""HFTs"" (high frequency traders) do is they set up their line to the exchange so its microseconds faster than everyone else.  Then they test out the market with tiny orders, seeing how fast it's getting filled - if these are getting filled immediately, it probably means there's a big order coming in from an investor.  So the algorithm - and it's all algo-based obviously because no human can remotely hope to catch this - will detect that as soon as there's a spike in volume, it will buy all of the volume at the current price, and sell it back for higher, forcing the big investor to take on higher prices.    Another case is that some HFTs can basically buy the entire trade book from an exchange like Nasdaq.  So every time someone places a market order for 200 shares of a 6.5 stock, the HFT will see it, buy up all the current stock from say 6.5-6.6 and sell it back at 6.7.  Not rocket science if you already get info about the trade coming in, in fact this is basically market making but performed by an ""evil HFT"" instead of a ""trustworthy bank.""    But honestly there are a lot more ways to make money from HFT than front-running, which isn't even possible anymore because exchanges no longer sell their books to HFTs." You should consider Turbocash. It's a mature open-source project, installed locally (thick client). "The quantity are going to get booty raped when the cycle turns. The unprecedented bull run has lead to their models being skewed which is bad news for the quants who are the ""programmer funds"" you reference. I'm just an analyst and I use python regularly for my job, but I don't think AI can replace expertise." "In US public stock markets there is no difference between the actions individual retail traders are ""permitted"" to take and the actions institutional/corporate traders are ""permitted"" to take.  The only difference is the cost of those actions.  For example, if you become a Registered Market Maker on, say, the BATS stock exchange, you'll get some amazing rebates and reduced transaction prices; however, in order to qualify for Registered Market Maker status you have to maintain constant orders in the book for hundreds of equities at significant volumes.  An individual retail trader is certainly permitted to do that, but it's probably too expensive. Algorithmic trading is not the same as automated trading (algorithmic trading can be non-automated, and automated trading can be non-algorithmic), and both can be anywhere from low- to high-frequency.  A low-frequency automated strategy is essentially indistinguishable from a person clicking their mouse several times per day, so: no, from a legal or regulatory perspective there is no special procedure an individual retail trader has to follow before s/he can automate a trading strategy.  (Your broker, on the other hand, may have all sorts of hoops for you to jump through in order to use their automation platform.) Last (but certainly not least) you will almost certainly lose money hand over fist attempting bid-ask scalping as an individual retail trader, whether your approach is algorithmic or not, automated or not.  Why?  Because the only way to succeed at bid-ask scalping is to (a) always be at/near the front of the queue when a price change occurs in your favor, and (b) always cancel your resting orders before they are executed when a price change occurs against you.  Unless your algorithms are smarter than every other algorithm in the industry, an individual retail trader operating through a broker's trading platform cannot react quickly enough to succeed at either of those.  You would have to eschew the broker and buy direct market access to even have a chance, and that's the point at which you're no longer a retail trader. Good luck!" If you would like to use linux I suggest you to use KMyMoney http://kmymoney2.sourceforge.net/ It is based on gnucash but it is easier to use IMO Algorithmic trading essentially banks on the fact that a price will fluctuate in tiny amounts over short periods of time, meaning the volatility is high in that given time frame. As the time frame increases the efficiency of algorithmic trading decreases and proper investment strategies such as due diligence, stock screening, and technical analysis become the more efficient methods.  Algorithms become less effective as the time frame increases due to the smoothing effect of volatility over time. Writing an algorithm that could predict future long-term prices would be an impossible feat because as the time frame is scaled up there are far less price fluctuations and trends (volatility smooths out) and so there is little to no benchmark for the formulas.  An algorithm simply wouldn't make sense for a long-term position. A computer can't predict, say, the next quarter, an ousted CEO, a buyout, or anything else that could effect the price of the security, never mind the psychology behind it all. Vice versa, researching a company's fundamentals just to bank on a 0.25% daily swing would not be efficient. Tax advantages or not, it is the most efficient methods that are preferred for a given time-scale of trading. just start on quantopian. you wont know what you are doing. take someones algorithm, break it. figure out why its broken, fix it. figure out why it works. brainstorm ideas. code them. bugfix. realize you dont know enough. google search. . . profit  thats what I did. "Yes we lose some client business to algos - specifically the DMA (direct market access) side of things. I haven't seen much HFT impact on what I do.  I see the movement towards algos/automated trading and DMA over the next 5-10 years, and block traders like me going extinct eventually.  But this all assumes liquidity, which is not there in the product I trade.  So you still need a dealer who knows where the blocks ""live"".   You want to buy 5% of that issue? okay good-luck trying to do that in the market.  No algo will help you do that without moving the market against you." R/stockmarket is probably the best.  When I have some free time I can test that strategy back as long as you would like in quantstrat, although the only problem with testing futures is joining the consolidated contracts, however I can test it with SPY as SPY returns and ES_F track very closely. "If there is a very sudden and large collapse in the exchange rate then because algorithmic trades will operate very fast it is possible to determine “x” immediately after the change in exchange rate. All you need to know is the order book. You also need to assume that the algorithmic bot operates faster than all other market participants so that the order book doesn’t change except for those trades executed by the bot. The temporarily cheaper price in the weakened currency market will rise and the temporarily dearer price in the strengthened currency market will fall until the prices are related by the new exchange rate. This price is determined by the condition that the total volume of buys in the cheaper market is equal to the total volume of sells in the dearer market.   Suppose initially gold is worth $1200 on NYSE or £720 on LSE. Then suppose the exchange rate falls from r=0.6 £/$ to s=0.4 £/$.  To illustrate the answer lets assume that before the currency collapse the order book for gold on the LSE and NYSE looks like: GOLD-NYSE   Sell (100 @ $1310) Sell (100 @ $1300) <——— Sell (100 @ $1280) Sell (200 @ $1260) Sell (300 @ $1220) Sell (100 @ $1200) ————————— buy (100 @ $1190) buy (100 @ $1180)   GOLD-LSE Sell (100 @ £750) Sell (100 @ £740) ————————— buy (200 @ £720) buy (200 @ £700) buy (100 @ £600) buy (100 @ £550) buy (100 @ £530) buy (100 @ £520) <——— buy (100 @ £500)   From this hypothetical example, the automatic traders will buy up the NYSE gold and sell the LSE gold in equal volume until the price ratio ""s"" is attained. By summing up the sell volumes on the NYSE and the buy volumes on the LSE, we see that the conditions are met when the price is $1300 and £520. Note 800 units were bought and sold.   So “x” depends on the available orders in the order book. Immediately after this, however, the price of the asset will be subject to the new changes of preference by the market participants. However, the price calculated above must be the initial price, since otherwise an arbitrage opportunity would exist." **Here's a sneak peek of [/r/algotrading](https://np.reddit.com/r/algotrading) using the [top posts](https://np.reddit.com/r/algotrading/top/?sort=top&amp;t=year) of the year!**  \#1: [Python for Algorithmic Trading and Investing tutorial series](https://np.reddit.com/r/algotrading/comments/5selh0/python_for_algorithmic_trading_and_investing/)   \#2: [Stop calling it HFT](https://np.reddit.com/r/algotrading/comments/59syla/stop_calling_it_hft/)   \#3: [Introduction to Python for Econometrics, Statistics and Data Analysis](https://www.kevinsheppard.com/images/0/09/Python_introduction.pdf) | [6 comments](https://np.reddit.com/r/algotrading/comments/5kthsv/introduction_to_python_for_econometrics/)  ---- ^^I'm ^^a ^^bot, ^^beep ^^boop ^^| ^^Downvote ^^to ^^remove ^^| [^^Contact ^^me](https://www.reddit.com/message/compose/?to=sneakpeekbot) ^^| [^^Info](https://np.reddit.com/r/sneakpeekbot/) ^^| [^^Opt-out](https://np.reddit.com/r/sneakpeekbot/comments/5lveo6/blacklist/) I have done something similar to this myself.  What you are suggesting is a sound theory and it works.  The issues are (which is why it's the reason not everyone does it) : The initial cost is great, many people in their 20s or 30s cannot afford their own home, let alone buy second properties.  The time to build up a portfolio is very long term and is best for a pension investment.  it's often not best for diversification - you've heard not putting all your eggs in one basket? With property deposits, you need to put a lot of eggs in to make it work and this can leave you vulnerable. there can be lots of work involved. Renovating is a huge pain and cost and you've already mentioned tennants not paying!  unlike a bank account or bonds/shares etc.  You cannot get to your savings/investments quickly if you need to (or find an opportunity)  But after considering these and deciding the plunge is worth it, I would say go for it, be a good landlord, with good quality property and you'll have a great nest egg.  If you try just one and see how it goes, with population increase, in a safe (respectable) location, the value of the investment should continue to rise (which it doesn't in a bank) and you can expect a 5%+ rental return (very hard to find in cash account!)  Hope it goes well!