Skip to content

Examples

Axel Gard edited this page Jul 23, 2024 · 13 revisions

Articles & code & orgs

Installation

You can install by pip

pip install cira

Usage

Because the alpca trade API needs a key.
You need to keep your api key in a json file. Cira needs the path to the file.

key.json

{
  "APCA-API-KEY-ID":"your_pub_key",
  "APCA-API-SECRET-KEY":"your_private_key"
}

then you can start using the lib

import cira
cira.alpaca.KEY_FILE = "../mypath/key.json"
stock = cira.Stock("TSLA")
stock.buy(1)
stock.sell(1)

full examples

In cira examples repository, you can find a bunch of different trading strategies and in grazing-cira repository you can find a production environment for strategies.

simple index fund

This index fund will just buy at random and sell at random thus putting your eggs in multiple baskets. This method will end up following the market pretty well. But it can be improved on, quite a lot but it is a great starting point.

import cira
import random
import time

cira.alpaca.KEY_FILE = "../mypath/key.json"

portfolio = cira.Portfolio()
exchange = cira.Exchange()

qty = 1 # choose how many stocks should be handled in one session 
while exchange.is_open:
    for stock in random.choices(exchange.stocks, k=qty):
        stock.buy(1)
    for stock in random.choices(portfolio.owned_stocks, k=qty):
        stock.sell(1)
    time.sleep(60*30) # 30 min timer    
Clone this wiki locally