Price Action With Python?

I may be a bit unorthodox and though I'm very familiar with it, I've never felt the need to use Pandas. The work flow is pretty straightforward so here's an example:
The feeds I'm familiar with use either FIX or web sockets and I'll assume the latter since it's more common. Check the data provider's documentation for the api endpoint you need for ticker data or whatever then use something like the lomond web socket library to connect to it and start pulling the data.

Then whether you're storing tick by tick or generating candle stick charts from the feed, you need to store it somewhere. My suggestion is either redis using the rejson library (in memory database so very fast and supports concurrent reads and writes), sqlite if you need the speed, want to save memory and don't mind having to fiddle to get concurrent reads and writes working, or something more traditional and heavy weight like mysql or postgresql. But put it somewhere. Personally while I store my data in a database, my scripts that actually perform the trading also keep all charts in memory to avoid the latency of hitting the database. The database is just there so the data is persistent.

So now you have data continuously being put in a database. Now you need to apply your edge to the data. My edge is statistical and doesn't make use of any traditional indicators so I don't use any libraries like ta-lib though if you are looking for that, ta-lib is quite good. For my edge, the scipy and built-in python statistics library are quite sufficient. So with those two, I have my algorithm do its thing and if it finds what I'm looking for, it sets an order.

After setting an order, I use the information generated with scipy and statistics to decide where to sell. This is all automated.

To actually see the charts is where matplotlib comes in and for candlesticks, you will specifically need the mpl_finance library. Matplotlib is very complex and has a steep learning curve but the upshot is it's extremely powerful and feature rich so you can use it to visually interact with the data however you want. You can set buys, sells, cancels, or whatever else you need by clicking directly on the chart, it updates in real time, etc. That's the most basic of what it does but when you learn it, you will be able to program it to do anything you can imagine a chart could do which in itself can give an edge since you will be able to see the data in ways maybe other people haven't thought of.

Lastly, if you're interested in crypto, check out the excellent ccxt library that abstracts the api for over a hundred exchanges and lets you interact with all of them the same way and makes things like cross exchange arbitrage easier. For a free stock feed that gives somewhat useful data, check out iexcloud (just google it). The free data is limited to orders ran through the IEX exchange which is a smaller one so it's not perfect but I've found it useful and it's free so there's that.

So a beginner's library list based on my experience:
lomond
rejson
scipy
statistics
ta-lib
matplotlib
mpl_finance
ccxt
Thanks it was extremely useful. You charged me with some optimism about creating my own EA since I have some math background but lack guidance about how to implement it quickly into code.
 
I Python to take the trades, simply send the buy/sell signals to a process running Python (I recommend using ZeroMQ, easy stuff).
I have dipped my toe in trying to learn how to do something like this and haven't gotten very far - do you have any open source code online?
 
Don't ask -- just go do it.
There is no better way to learn the guts of what's on a chart, than to reproduce it yourself.
It's all just numbers -- derived from one price stream -- two if you include volume.
You might practice with a spreadsheet, but the actual Python code will be trivial.

A hint: defining trade triggers from a graph, as explicitly as possible, ahead-of-time, is vital. To sit before a pool of numbers without prior (rules-based) guidance can be pretty intimidating. :confused:
And that's not to say that you must be *bound* by such rules, only that you have a firm starting point. Let the evidence (whether graphical or numerical) guide you.

This guy knows what he's talking about.
 
I been programming in java for 1 year or so. Never coded in anything else (vba yes) and now trying to get my hand on python. Read a few books but finding it difficult. Firstly, most python books tend to be of lower quality and less formal than java books (usually college texts). Secondly i find the python structure (or lack of it) confusing
 
I been programming in java for 1 year or so. Never coded in anything else (vba yes) and now trying to get my hand on python. Read a few books but finding it difficult. Firstly, most python books tend to be of lower quality and less formal than java books (usually college texts). Secondly i find the python structure (or lack of it) confusing

I was the same, coming from C++. It took me a long time to "get" Python. The zen of Python:

python3 -c 'import this'
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

Some applicability to trading as well.
 
I was the same, coming from C++. It took me a long time to "get" Python.

So you "get" it now? i can see it can be powerful due to looser syntax etc, but being used to a formal language, i find the python codes a bit too informal and too 'on-the-fly' making it difficult to understand
 
So you "get" it now? i can see it can be powerful due to looser syntax etc, but being used to a formal language, i find the python codes a bit too informal and too 'on-the-fly' making it difficult to understand

The perceived informality makes it much easier to ruthlessly refactor and allows you to simplify, simplify, simplify. This will be extremely valuable to you in the future. For example, I have price collection code that goes back 6 months that I didn't touch until recently and following the "Zen of Python", purely through practice, made it really easy to modify.

If your Python code looks like Java, run.
 
The perceived informality makes it much easier to ruthlessly refactor and allows you to simplify, simplify, simplify. This will be extremely valuable to you in the future. For example, I have price collection code that goes back 6 months that I didn't touch until recently and following the "Zen of Python", purely through practice, made it really easy to modify.

If your Python code looks like Java, run.

Basically, Python code eventually ends up looking like your thoughts about what should happen and less like Python. This takes years.

In comparison, C++ "refactoring" often looks like more C++. And from what I've seen of Java refactoring with Big Enterprise Systems (TM), Java refactoring looks more Enterprise Big System.

That's not to say there cannot be beautifully written C++ and Java. But idiomatic "beautiful" C++ and Java don't look like your thoughts. Maybe I just think in Python now.
 
I came across many good books when learning java (effective java, david eck, etc) but all python books i have come across thus far read like a soap opera. Schools teach programming using java, so can't find any textbooks on python.
 
I came across many good books when learning java (effective java, david eck, etc) but all python books i have come across thus far read like a soap opera. Schools teach programming using java, so can't find any textbooks on python.

I would probably agree with your assessment that the amount of information is limited. However, that is mostly due to the fact that the Python PEPs and documentation are enough. I read through this my first go around (though for version 2): https://docs.python.org/3/tutorial/index.html

Very little I have had problems with language-wise, most to do with use of libraries.
 
Back
Top