Skip to content

Terraform provider for managing long term portfolio with Zerodha Kite

License

Notifications You must be signed in to change notification settings

mr-karan/terraform-provider-kite

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Terraform Provider Kite

Terraform plugin for managing stock portfolio with Zerodha Kite

asciicast

Why

This provider lets you declaratively manage your long term holdings with Terraform and Zerodha Kite API. You can model your entire portfolio as Terraform "resource" blocks and let it handle all the future updates to your portfolio. With Terraform you get to see how your portfolio evolved over time with point-in-time snapshots of your portfolio, you can do multiple changes to your portfolio with one click, you can easily extend it to make your own SIP scheduler with a simple cron job - Possibilities are endless.

Seriously why?

Well, honestly this is a fun project aimed at learning to build a Terraform Plugin. The Plugin SDK architecture is quite nice and I wanted to dig in some internals of how Terraform Provider works. This project helped me achieve that goal.

Installing

Download the latest binary from Releases and place it on your machine at $PLUGIN_DIRECTORY/$SOURCEHOSTNAME/$SOURCENAMESPACE/$NAME/$VERSION/$OS_$ARCH/. Follow the instructions to install Third-party Providers if you get stuck.

# Follow the steps if you are on Linux
cd /tmp
curl -sL https://github.com/mr-karan/terraform-provider-kite/releases/tag/v0.1.0 | tar xz
mv terraform-provider-kite ~/.terraform.d/plugins/mrkaran.dev/github/kite/0.1/linux_amd64/

You can view examples/versions.tf for the sample provider configuration.

Example Usage

# Buy 30 stocks of NSE:CASTROLIND
resource "kite_holding" "castrol" {
  tradingsymbol = "CASTROLIND"
  quantity      = 30
  exchange      = "NSE"
}

# Show the order id.
output "castrol_order_id" {
  value = kite_holding.castrol.order_id
}

Argument Reference

The following arguments are supported:

  • tradingsymbol - (Required, string) Tradingsymbol of the instrument.
  • quantity - (Required, int) Quantity to transact.
  • exchange - (Optional, string) Name of the exchange. Must be one of "NSE","BSE". Defaults to "BSE".

Attributes Reference

The following attributes are exported:

  • quantity - (int) Quantity held by the user. Includes the quantity in postion and holdings.
  • order_id - (string) Unique order ID.

Import

It is possible to import your existing holdings via terraform import command. This helps you to make your existing state file aware about the holdings bought without Terraform.

For eg, to import INFY from your holdings:

terraform import kite_holding.infy INFY

Configuring Provider

The provider requires the following environment variables to authenticate with Kite API:

  • KITE_API_KEY=<>
  • KITE_API_SECRET=<>
  • KITE_API_REQUEST_TOKEN=<>

You can generate these credentials using Kite Developer Console. To get KITE_API_REQUEST_TOKEN you need to visit the public Kite login endpoint at https://kite.zerodha.com/connect/login?v=3&api_key=xxx. You will get request_token as a URL parameter to the redirect URL registered for your app. This is a one time token and will expire after first succesful login attempt.

Please read point 3) in Warnings and Caveats section to know how the access token is persisted across multiple runs of terraform.

To initialise the provider:

provider "kite" {
}

Warnings and Caveats

  1. The author of this program is currently employed by Zerodha but this software isn't associated with Zerodha in any manner. This is a completely open and FOSS project.

  2. If you use this program and lose money, don't blame me. This software comes with absolutely no guarantees.

  3. Due to Indian Exchange regulations and guidelines the user(s) are expected to login to the trading platform every day before placing trades. To comply with that, the login cannot be automated. Since Terraform needs to call Kite API to get the latest state and modify the state, any kind of API call needs an access token. It is not possible to persist the Access Token across Terraform runs so this program persists it on the user's local path .tf-kite-secret. Future versions of this program will make the path to this file as a provider config. You are expected to keep this file private and not keep this open to shared environments. To repeat point 2), if you lose money while using/due to this program, don't blame me.

Develop

Test sample configuration

First, build and install the provider.

$ make install

Then, navigate to the examples directory.

$ cd examples

Run the following command to initialize the workspace and apply the sample configuration.

$ terraform init && terraform apply

Credits

LICENSE

See LICENSE for more details.

⭐️ Show your support

Give a ⭐️ if this project helped you!

Contributing

This is still an alpha release. For a full list of things to improve, see unchecked items in TODO. Contributions welcome!

About

Terraform provider for managing long term portfolio with Zerodha Kite

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published