Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Using Perl for writing Trading Systems

by defjukie (Sexton)
on Dec 23, 2004 at 00:22 UTC ( [id://416965]=perlquestion: print w/replies, xml ) Need Help??

defjukie has asked for the wisdom of the Perl Monks concerning the following question:

I wonder whether anyone has attempted to use Perl to write scripts to back-test various trading strategies?

I have written a simple program to test a simple interrelationship between sterling and dollars, and the strategy I tested (unsurprisingly) failed. Ah well, t'was merely a programming exercise...

Has anyone else attempted more complex trading systems using Perl and how did they find the compatibility of Perl with statistical testing? There are certainly programming languages (or basic programs) more designed to the task, but Perl is all I know (apart from awk and a little C) and surely anything can be done with Perl???

I have never put Perl to this use, so it is new territory for me. As an aside: Anyone know of any Perl scripts or modules people have written to determine regression equations? Seems highly complex, so I'll stick with STATA but I'd like to know if anyone else has done it.
~ defjukie

Replies are listed 'Best First'.
Re: Using Perl for writing Trading Systems
by Akhasha (Scribe) on Dec 23, 2004 at 03:08 UTC
    I haven't used Perl for this but have thought at length about the same problem domain. I'd be looking into Perl Data Language for dealing with numerically intensive tasks... (though it seems to be down at the moment)
    When it comes to regression, Statistics::Regression might be what you're after.
Re: Using Perl for writing Trading Systems
by xdg (Monsignor) on Dec 23, 2004 at 05:01 UTC

    Yes, sort of. I've used it for calculating a resampled efficient investment frontier. The great strength of perl for this task, I think, is the ability to munge input data quickly and easily and to quickly recode your program to test variations. It may not be the fastest simulation, but the speed of development/coding makes up for the speed of running, at least for the purposes I have for it. That said, PDL is certainly the way to go if you're dealing with any large data sets. On the statistics front, I wound up rolling my own so I knew exactly what was going on, so I'm not sure of a good recommendation there. (This from my early perl days -- now I'd probably look for a good module, or else would write my code in a way I could then release. It's on my "one of those days" list to revamp and release it.)

    I'm in the process of coding some other simulation stuff right now -- keep your eyes peeled on CPAN in the next month and you'll probably see it. It may or may not be useful to you, but I plan to include support for bootstrap resampling, etc., which is nice for backtesting investment returns.

    -xdg

    Code posted by xdg on PerlMonks is public domain. It has no warranties, express or implied. Posted code may not have been tested. Use at your own risk.

Re: Using Perl for writing Trading Systems
by aquarium (Curate) on Dec 23, 2004 at 04:24 UTC
    although some relationships exist between stocks, a lot depends on decisions made by large companies and governments, e.g. start a war and see the oil prices and gold go up. as you don't have this sort of data in computational form, it will not be considerred by your formulae. Besides all the above (unknown) factors, statistical analysis is confined to the realm of equations/formulae, which are not the best models for the (sometimes subtle) movements of stocks. A lot of effort has recently gone into picking market trends by using neural networks. there are already simple neural network perl modules around, but I haven't seen any such module specifically sculpted for trading analysis. Instead, there exists a number of commercially written ($$$) applications that do exactly the same thing, and display the result in fancy 3D graphs. If you just want to have fun analysing/guessing market trends and playing with the maths, then this would be the way to go....If you're serious about making money, just follow company/government decisions and the resulting market impact
    the hardest line to type correctly is: stty erase ^H
      Thanks for the reply.
      Firstly, I am primarily concerned with the currency, as opposed to the equity, markets but I must say I disagree with your claim that "if you're serious about making money, just follow company/government decisions and the resulting market impact". Alas, if it was that easy, there would be no problem. We would all be rich.

      Guys who make fundamental analysis profitable have a mass network of contacts within the handful of companies which they trade. Company reps visit with fund managers to attempt to persuade them, and thus a few other hundred sheep, to buy their stock. They have information that the stay-at-home trader doesn't have. I question the foundation of fundamental annalysis for anyone, but for guys without a wealth of contacts it is impossible. By the time the news comes out the moves have been made.

      Though most monks don't have networks of contacts, we do have data. The internet has made data freely available. This is why statistics can be the only way for the small investor to trade.

      People who believe every bit of market noise is reducible to a news snippet are, as Nassim Taleb would say, "fooled by randomness" - props to tphyahoo for giving this book a mention.

      ~ defjukie
Re: Using Perl for writing Trading Systems
by Tomte (Priest) on Dec 23, 2004 at 10:11 UTC

    I second the recommendation of PDL for meddling with numbers; on the statistics front I suggest R, I never used it with perl (I used it with python), but there are also perl-bindings. We were more than content, as it was easily capable of dealing with a very large data-set (30MB), SPSS couldn't even dream to work on (we had an SPSS license and tried it first, but it simply couldn't handle the amount of data per sample, let alone all samples...), STATS, which we considered switching to and had an evaluation license for, was of no use either; though I liked it a lot compared to SPSS...

    regards,
    tomte


    An intellectual is someone whose mind watches itself.
    -- Albert Camus

I've seen job adverts for this.
by tphyahoo (Vicar) on Dec 23, 2004 at 12:19 UTC
    I'm fairly certian I've seen job adverts for perl programmers to maintain software at proprietary trading corps. So the answer is probably, yes. Whether any of this code is in the public domain is another issue.

    Also, anybody who wants to build programs to autotrade should read Nassim Taleb's "Surprised By Randomness."

    And get a real job. :)

    thomas.

      Yes, there are jobs for programmers to maintain software at trading corps. That doesn't mean that those jobs are all about programming for trading...and even if the jobs are about programming for trading, then they're probably writing progs to sell them to suckers who think they'll make a fortune when they buy them. There's probably a warning on the program that says "the software will not make you money unless you combine the results with REAL market knowledge" The big money is to be made if you know ahead of time some important decision about the stock. The next best thing is to follow news closely....i heard it on the radio, a few months back, that gold was artificially low priced, and saw then the price climb and climb. I wouldn't bet my hard earned cash on it though.
      the hardest line to type correctly is: stty erase ^H
Re: Using Perl for writing Trading Systems
by dba (Monk) on Dec 23, 2004 at 16:17 UTC
    I searched for modules in CPAN, but couldn't find what I was looking for.
    I use WWW::Mechanize and LWP modules to get free data (Yahoo finance) and subscription data for specific equities based on pre-determined criteria and load them into a database( using DBI) and run various SQLs to test different strategies.
      Thanks for the reply.

      Have you found these strategies profitable? I don't want to know the actual strategy, that is your gospel and for you only, but I just wonder: have you found statistically significant patterns?

      Once I complete my MA (bout 1 1/2 years) I am going to work for a proprietary trading firm, so this is more than just an intellectual interest....

      ~ defjukie
        There are plenty of mathematics modules on CPAN...

        found this bundle of modules...

        Can't really comment on them right now... once I have my exams and essays out the way - roll along, January 19th!- I will spend a week sifting through the stuff (no doubt, I will inform the monastery of anything of interest)

        Anyway, thanks for your help dudes. This is a subject I will return to soon.

        peace out
        For the limited period (past 2 months), I was able to validate a pattern for a specific group of stocks. However, Iam not ready to place my hard earned cash yet :) I will apply the pattern for the next month or so on paper and then slowly start using it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://416965]
Approved by johnnywang
Front-paged by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-04-19 08:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found