Price Action With Python?

Discussion in 'App Development' started by Money Trust, Oct 1, 2019.

  1. Question for experienced Python programmers.

    I'm only a couple of weeks into learning Python and aside from using it for Web Development, I want to build technical trading systems as well (among other things). So, my question is whether or not it's possible to code discretionary technical analysis methods. By discretionary, I'm referring to price action methods such as trend lines/channels, support and resistance levels, volume and spread, etc. Basically, visual chart patterns that are usually charted by lines of some sort.

    Is it possible to not only code such discretionary methods and but also develop an automated trading system based on that sort of a strategy? I've saw the algorithmic trading libraries for Python, by the way, but I'm asking about a form of visual analysis.
  2. Surely the definition of discretionary is that it isn't systematic, and therefore can't be programmed?

    Having said that I suppose you could try training a neural network or similar to recognise chart patterns; essentially get thousands of charts, hand label them with the sort of feature you think you are seeing, and then try and train the network to recognise them.

    Would I do this? No, but then I am not a big fan of subjective methods that involve looking for weird patterns in charts...

  3. SteveH


    If you're into charting, Python is probably not the way to go, unless you want to create your own charting library from scratch, possibly using the pyQt libraries to interface with the Qt GUI.

    NinjaTrader is free for non-trading use (i.e. you can't issue buy/sell orders to a broker IN their software without purchasing it). You're better off trying to program what you want with that (in C#) and then, if you want to use Python to take the trades, simply send the buy/sell signals to a process running Python (I recommend using ZeroMQ, easy stuff). There's enough examples of free C# NT indicators on the Internet to where you can piece together what you want.

    There's also Amibroker AFL scripts out there to do similar things you want graphically and you can translate their drawing algorithms over to C# (or pyQt).
    Last edited: Oct 1, 2019
    sharpquant likes this.
  4. tommcginnis


    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.
    Heydrrich and nooby_mcnoob like this.
  5. pipeguy


    I think any pattern can be coded since it is expressed in candlesticks - units. Take for example Triangle. Draw corner cases of triangle in platform. Here you can assume that all triangle formations inside those limits are valid and should send the signal to open a trade.

    Even more complicated patterns can be coded but you have to specify the limits.
  6. gaussian


    Channels can be drawn, trendlines really cannot. They are far too subjective. You could trivially define a trendline as a line that intersects the points that are the successive lower/high highs for example - but what if it isn't perfect (pro tip: it never is)? Do you use a fudge factor? How much? Of course you could try - but you will likely waste a lot of time to discover the same thing I posted here.

    Supports and resistances are also fairly subjective. At what level do you draw across? The only difference between drawing a trendline and an S/R line is the angle made with the vertical axis. Other than that you will experience the same problems as trendlines.
  7. Axon


    I've been using Python with the Matplotlib charting library to run a trading bot I've been refining over the last couple of years. Did quite well in crypto and I'm currently adapting it to work with equities and futures. So I guess the answer is yes provided you have an edge already that you can program in. Without that latter part though all the automation in the world won't make a dime.
  8. pipeguy


    Did you need only matplotlib library or something else like Pandas? How did you handle the data?
    The purpose of this question to understand how many libraries I need to master.
  9. Axon


    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:
  10. tommcginnis


    One-word response: "Niiiiiice." :thumbsup::thumbsup::thumbsup:

    Two-word response: "Great summary." :D
    #10     Oct 4, 2019