E-Book Content
Quantitative Trading with R Quantitative Trading with R Understanding Mathematical and Computational Tools from a Quant’s Perspective Harry Georgakopoulos QUANTITATIVE TRADING WITH R Copyright © Folk Creations, Inc., 2015. All right reserved. First published in 2015 by PALGRAVE MACMILLAN® in the United States—a division of St. Martins Press LLC, 175 Fifth Avenue, New York, NY 10010. Where this book is distributed in the UK, Europe and the rest of the world, his is by Palgrave Macmillan, a division of Macmillan Publishers Limited, registered in England, company number 785998, of Houndmills, Basingstoke, Hampshire RG21 6XS. Palgrave Macmillan is the global academic imprint of the above companies and has companies and representatives throughout the world. Palgrave® and Macmillan® are registered trademarks in the United States, the United Kingdom, Europe and other countries. ISBN: 978–1–137–35407–5 Library of Congress Cataloging-in-Publication Data Georgakopoulos, Harry. Quantitative trading with R : understanding mathematical and computational tools from a quant’s perspective / Harry Georgakopoulos. pages cm ISBN 978–1–137–35407–5 (hardback)— ISBN 1–137–35407–0 () 1. Stocks—Mathematical models. 2. Investment analysis—Mathematical models. 3. Corporations—Finance—Computer programs. 4. Commodity exchanges. I. Title. HG4661.G46 2015 332.640285’5133–dc23 2014028408 A catalogue record of the book is available from the British Library. Design by Newgen Knowledge Works (P) Ltd., Chennai, India. First edition: January 2015 10 9 8 7 6 5 4 3 2 1 Printed in the United States of America. To Pinelopi, Maria, and Anastasia Contents List of Figures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi List of Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii 1 An Overview . . . . . . . . . . . . . . . . The mission statement . . . . . . . . . Financial markets and instruments . Trading strategies . . . . . . . . . . . . . High-frequency trading . . . . . . . . About the orderbook . . . . . . . . . . Trading automation . . . . . . . . . . . Where to get data from . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 3 5 5 6 7 8 9 2 Tools of the Trade . . . . . . . . . The R language . . . . . . . . . . . Getting started with R . . . . . . The c() object . . . . . . . . . . . The matrix() object . . . . . . . The data.frame() object . . . The list() object . . . . . . . . . The new.env() object . . . . . . Using the plot() function . . . Functional programming . . . . Writing functions in R . . . . . . Branching and looping . . . . . . A recommended style guide . . A pairwise correlation example Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .