Coder Social home page Coder Social logo

d-e-s-o / apcacli Goto Github PK

View Code? Open in Web Editor NEW
34.0 4.0 9.0 329 KB

A command line tool for trading stocks on Alpaca (alpaca.markets).

License: GNU General Public License v3.0

Rust 100.00%
alpaca-markets alpaca alpaca-trading-api alpaca-api rust rust-cli

apcacli's Introduction

pipeline crates.io rustc

apcacli

apcacli is a command line application for interacting with the Alpaca API at alpaca.markets. It provides access to the majority of Alpaca's functionality, including but not limited to:

  • inquiring account information
  • changing account configuration
  • retrieving account activity
  • accessing the market clock
  • submitting, changing, listing, and canceling orders
  • listing and closing open positions
  • listing and retrieving general asset information
  • streaming of account and trade events

It supports both the paper trading as well as the live API endpoints.

Usage

The program assumes environment variables representing the Alpaca key ID (APCA_API_KEY_ID) and secret (APCA_API_SECRET_KEY), so make sure that they are present. The program defaults to using the paper trading API. For live trading you will also need to change the URL to the API endpoint to use (APCA_API_BASE_URL).

export APCA_API_BASE_URL='https://api.alpaca.markets'; # We trade live
export APCA_API_KEY_ID='XXXXXXXXXXXXXXXXXXXX';
export APCA_API_SECRET_KEY='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';

With this setup, you can trade from the command line.

Submit an Order
$ apcacli order submit buy SPY --value 1000 --limit-price 200
> 40c13937-5969-48f0-92f8-2f1ef673517a
Lookup an Order
$ apcacli order get 40c13937-5969-48f0-92f8-2f1ef673517a
SPY:
  order id:         40c13937-5969-48f0-92f8-2f1ef673517a
  status:           accepted
  created at:       Sun, 10 May 2020 10:15:34 -0700
  submitted at:     Sun, 10 May 2020 10:15:34 -0700
  updated at:       Sun, 10 May 2020 10:15:34 -0700
  filled at:        N/A
  expired at:       N/A
  canceled at:      N/A
  quantity:         5
  filled quantity:  0
  type:             limit
  side:             buy
  good until:       canceled
  limit:            200.00 USD
  stop:             N/A
  extended hours:   false
List All Open Positions
$ apcacli position list
                                   |  Avg Entry  |     Today P/L      |      Total P/L
1 AAPL @  274.02 USD =  274.02 USD |  281.59 USD |  5.65 USD ( 2.11%) |  -7.57 USD (-2.69%)
1 AMZN @ 2359.86 USD = 2359.86 USD | 2426.79 USD | 31.74 USD ( 1.36%) | -66.93 USD (-2.76%)
1 BIDU @  101.75 USD =  101.75 USD |  107.33 USD |  0.34 USD ( 0.34%) |  -5.58 USD (-5.20%)
1 CTSO @    8.90 USD =    8.90 USD |    7.98 USD |  0.50 USD ( 5.95%) |   0.92 USD (11.53%)
1 EA   @  113.00 USD =  113.00 USD |  116.76 USD | -0.27 USD (-0.24%) |  -3.76 USD (-3.22%)
1 SPWR @    6.35 USD =    6.35 USD |    6.66 USD |  0.20 USD ( 3.25%) |  -0.31 USD (-4.65%)
3 XLK  @   87.00 USD =  261.00 USD |   80.10 USD |  8.55 USD ( 3.39%) |  20.70 USD ( 8.61%)
----------------------------------- ------------- -------------------- --------------------
                       3124.88 USD   3187.41 USD   46.71 USD ( 1.47%)   -62.53 USD (-1.96%)

More commands are available and can be discovered using the help.

The program is powered by the apca crate and written in Rust. It comes with shell completion support and automatic coloring of profit/losses.

Shell Completion

As mentioned earlier, apcacli comes with shell completion support (for various shells). A completion script can be generated via the shell-complete utility program and then only needs to be sourced to make the current shell provide context-sensitive tab completion support. E.g.,

$ cargo run --bin=shell-complete > apcacli.bash
$ source apcacli.bash

The generated completion script can be installed system-wide as usual and sourced through initialization files, such as ~/.bashrc.

Completion scripts for other shells work in a similar manner. Please refer to the help text (--help) of the shell-complete program for the list of supported shells.

apcacli's People

Contributors

d-e-s-o avatar dtolnay avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

apcacli's Issues

Format decimal values (and prices) as per the current locale

It would be nice if we were honoring the current locale for formatting of prices and other decimal values. Well, and potentially dates. And whatever else may be locale-dependent.

Unfortunately, last I checked there basically weren't any crates around that provided such functionality. Need to check first whether that has changed.

Don't use colors when input is not a TTY

We may not want to emit colors when the input is not a TTY. E.g., when redirecting the output to a file. I think we should add a switch to force usage of color, though. There is also NO_COLOR, though off hand I am not convinced that we need to follow it.

Implement extension support

It would be really nice to add extension support to the application. What I mean by that is basically having a way to write scripts/programs that can be invoked as an apcacli sub-command, similar to how you can write git or cargo sub-commands.

We have written something like that for nitrocli. From my perspective, most of that could be copied to apcacli. A discussion thread with most of the design discussions is d-e-s-o/nitrocli#50 (though not everything is applicable) and the main part of the implementation d-e-s-o/nitrocli#112 (likely more relevant). I don't recall all the details off the top of my head, so I don't know exactly how much is transferable and whether there needs to be some design work.

Anyway, this would be really nice to have and would allow us to move functionality that is currently stand-alone into the repository. E.g., I have a program that is useful for mostly manual trading, which basically ensures that stop orders are present for all positions that are above a certain percentage gain (which I see I haven't even published yet).

Switch from `structopt` to `clap` v3

We should convert the crate from using structopt to using clap v3, which is the new kid on the block for argument parsing (well, sort of, v2 has been around for ages and was the de-facto standard, but v3 does a few more things). I recall there being some kind of tutorial specifically aimed at conversions like this, though I couldn't find it off hand. May be useful to check for that first. My notes reference this, but I was under the impression there was a more formal document.

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.