Coder Social home page Coder Social logo

autotrade's Introduction

autotrade.R

Stock/ETF/futures auto-trading code in R for use with Interactive Brokers' IBAPI

Why automate all your trading or portfolio rebalancing? Because I think it may improve your performance over the long run due to lower costs, obtaining better fills with IBALGO, increases strategy responsiveness to new data, and less human error. At the very least, automating your trading/rebalancing can set you free from having to manually enter trades.

This is the auto-trading code that I presented at R Finance 2018 in Chicago on June 2. It works with any backtesting platform because the code only requires 3 inputs - two, simple dataframes and one integer:

  1. the new, recommended allocation that's generated by your signal, backtest, AI, etc
  2. the current allocation including number of shares for each security (i.e. your current portfolio) that is downloaded via IBAPI
  3. your net liquidation value that's available to you to trade - also downloaded via IBAPI

You need 4 things to make autotrade.R work:

  1. Interactive Broker's API installed
  2. Python 3.6 installed
  3. Reticulate package
  4. ib_insync - a Python module to interface with IBAPI more easily

Once autotrade.R is working for you, then complete trading automation can occur by scheduling your scripts.

This is just a starter script to get you going. More code is needed to handle your unique trading situation - for example to handle expiring contracts or to add a new, different stock/ETF that was not part of the original dataframe of available symbols to trade or even just to avoid receiving an error if your current portfolio happens to hold no securities at all. However at its basic, autotrade.R will work as-is without additional code modifications.

I have tested a more developed version of the autotrade.R code below that traded the futures markets every hour on the hour; the result was that 100% of the trades occurred as planned and without any errors.

SharpeRatioDistribution.R

Based on Andrew Lo's paper, this is just a quick and dirty way to determine the variance of your empirical Sharpe Ratio without having to bootstrap.

DISCLAIMER: IF YOU USE ANY PART OF THIS AUTO-TRADING CODE, YOU ASSUME ALL RISK ASSOCIATED WITH ITS USE WHICH MAY INCLUDE BUT IS NOT LIMITED TO TOTAL LOSS OF PRICIPAL OR MORE.

PROVIDED AS-IS. FOR EDUCATIONAL USE ONLY. NOT FOR PRODUCTION USE. DO NOT USE FOR LIVE TRADING.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.