Skip to content

Commit

Permalink
bug fix and error fix
Browse files Browse the repository at this point in the history
  • Loading branch information
AxelGard committed Feb 4, 2021
1 parent addd4d7 commit e611ad2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
26 changes: 15 additions & 11 deletions cira/stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ def price(self): # PREV: current_price
def value(self): # prev: value_of_stock
""" takes a string sym. Gets and returns the stock value at close """
nr_days = 1
barset = self.barset(nr_days)
if barset is None:
bars = self.barset(nr_days)
if bars is None:
self._value = 0
else:
self._value = barset[self.symbol][0].c # get stock at close
self._value = bars[self.symbol][0].c # get stock at close
return self._value



def buy(self, qty: int):
""" buys a stock. Takes int qty and a string sym """
Expand All @@ -58,11 +60,14 @@ def sell(self, qty: int):

def order(self, qty: int, beh: str):
""" submit order and is a template for order """
order = alpaca.api().submit_order(
symbol=self.symbol, qty=qty, side=beh,
type="market", time_in_force="gtc"
)
return order
if not self.is_tradable:
raise Exception(f"Sorry, {self.symbol} is currantly not tradable on https://alpaca.markets/")
else:
order = alpaca.api().submit_order(
symbol=self.symbol, qty=qty, side=beh,
type="market", time_in_force="gtc"
)
return order

@property
def is_shortable(self):
Expand All @@ -77,10 +82,9 @@ def can_borrow(self):
self._can_borrow = alpaca.api().get_asset(self.symbol).easy_to_borrow
return self._can_borrow

def barset(self, limit: int):
def barset(self, limit):
""" returns barset for stock for time period lim """
self._barset = alpaca.api().get_barset(self.symbol,
"day", limit=int(limit))
self._barset = alpaca.api().get_barset(self.symbol, "minute", limit=int(limit))
return self._barset

def historical_data(self, nr_days=1000):
Expand Down
11 changes: 9 additions & 2 deletions temp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
import random
import time

cira.alpaca.KEY_FILE = "./tests/test_key.json"
cira.alpaca.KEY_FILE = "../paper-trader/key.json"


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

print(portfolio.owned_stocks)

for stk in exchange.stocks:
print(f"{stk}@{stk.price }")


stk = cira.Stock("TSLA")
print(stk.barset)

0 comments on commit e611ad2

Please sign in to comment.