Skip to content

Commit

Permalink
added spaceing
Browse files Browse the repository at this point in the history
  • Loading branch information
AxelGard committed Dec 1, 2021
1 parent 0e8ca96 commit 678106f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cira/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@ def __init__(self):
self._historical_data = {}
self._calendar = {}


@property
def is_open(self) -> bool:
""" returns if exchange is open """
self._is_open = alpaca.api().get_clock().is_open
return self._is_open


def calendar(self, start='2018-12-01', end='2018-12-01'):
self._calendar = alpaca.api().get_calendar(start=start, end=end)[0].__dict__["_raw"]
return self._calendar


def assets_raw(self):
""" returns a list of all avilabel stocks in exchanges list """
all_assets = []
Expand All @@ -44,6 +47,7 @@ def assets_raw(self):
self._assets = all_assets
return self._assets


@property
def symbols(self):
""" returns a list of all symbols """
Expand All @@ -52,6 +56,7 @@ def symbols(self):
self._symbols.append(asset.__dict__["_raw"]["symbol"])
return self._symbols


@property
def stocks(self):
""" returns a list of objects Stocks """
Expand All @@ -60,6 +65,7 @@ def stocks(self):
self._stocks.append(stock.Stock(sym))
return self._stocks


@property
def historical_data(self):
""" gathers all historical data on all stocks, {"sym":[data]} """
Expand Down
15 changes: 14 additions & 1 deletion cira/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ def __init__(self):
self._position = []
self._account = {}


@property
def orders(self):
""" returns a list of all open orders with all diffult args """
self._list_orders = alpaca.api().list_orders()
return self._list_orders


@property
def position(self): # PREV: get_position # TODO: change to positions
def position(self): # PREV: get_position # TODO: change to positions
""" create a list of all owned position """
portfolio = alpaca.api().list_positions()
self._position = []
Expand All @@ -37,11 +39,13 @@ def position(self): # PREV: get_position # TODO: change to positions
self._position.append(position_dict)
return self._position


def owned_stock_qty(self, stock): # maby shuld be in stock.Stock
""" returns quantity of owned of a stock Stock (obj) """
position = stock.position
return position["qty"]


@property
def owned_stocks(self):
""" returns a list of owned stocks """
Expand All @@ -62,49 +66,58 @@ def sell_list(self, lst):
# # BUG: fix, google has problem selling!
stock_.sell(qty)


@property
def account(self):
""" returns the dict of user account details"""
self._account = util.reformat_position(alpaca.api().get_account())
return self._account


@property
def buying_power(self):
""" returns the amount of current buying_power that the user have"""
self._buying_power = self.account["buying_power"]
return self._buying_power


def is_blocked(self):
""" checks if the users has been blocked from trading """
return self.account["trading_blocked"]


@property
def cash(self):
""" returns the amount of a available liquid chash in account """
self._cash = self.account["cash"]
return self._cash


@property
def equity(self):
""" returns the amount of equity that users has """
self._equity = self.account["equity"]
return self._equity


@property
def equity_yesterday(self):
""" returns the amount of equity that was
available at market close yesterday """
self._equity_yesterday = self.account["last_equity"]
return self._equity_yesterday


@property
def equity_change(self):
""" returns the change in equity from yesterday to now """
self._equity_change = self.equity - self.equity_yesterday
return self._equity


def __repr__(self):
return f"portfolio({self.equity})"


def __str__(self):
return f"{self.position}"
34 changes: 34 additions & 0 deletions cira/stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(self, symbol:str):
self._plpc = 0
self._is_open = False


@property
def price(self) -> float: # PREV: current_price
""" returns the current price of given symbol (str) """
Expand All @@ -34,6 +35,7 @@ def price(self) -> float: # PREV: current_price
self._price = self.barset(1)[self.symbol][0].c
return self._price


@property
def value(self) -> float: # prev: value_of_stock
""" takes a string sym. Gets and returns the stock value at close """
Expand All @@ -45,20 +47,23 @@ def value(self) -> float: # prev: value_of_stock
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 """
order_ = self.order(qty, "buy")
if config.IS_LOGGING:
logging.log(logging.format_log_action("buy", self.symbol, qty))
return order_


def sell(self, qty: int):
""" sells a stock. Takes int qty and a string sym"""
order_ = self.order(qty, "sell")
if config.IS_LOGGING:
logging.log(logging.format_log_action("sell", self.symbol, qty))
return order_


def order(self, qty: int, beh: str) -> float:
""" submit order and is a template for order """
if not self.is_tradable:
Expand All @@ -70,24 +75,28 @@ def order(self, qty: int, beh: str) -> float:
)
return order


@property
def is_shortable(self) -> bool:
""" checks if stock can be shorted """
self._is_shortable = alpaca.api().get_asset(self.symbol).shortable
return self._is_shortable


@property
def can_borrow(self) -> bool:
"""check whether the name is currently
available to short at Alpaca"""
self._can_borrow = alpaca.api().get_asset(self.symbol).easy_to_borrow
return self._can_borrow


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


def historical_data(self, nr_days=1000):
"""returns a list of the stocks closing value,
range of 1 to 1000 days"""
Expand All @@ -97,6 +106,7 @@ def historical_data(self, nr_days=1000):
lst.append(bar.c)
return lst


@property
def week_pl_change(self) -> float:
""" Percentage change over a week """
Expand All @@ -107,19 +117,22 @@ def week_pl_change(self) -> float:
self._week_pl_change = (week_close - week_open) / week_open
return self._week_pl_change


@property
def is_tradable(self) -> bool:
""" return if the stock can be traded """
self._is_tradable = alpaca.api().get_asset(self.symbol).tradable
return self._is_tradable


@property
def position(self):
""" returns position of stock """
pos = alpaca.api().get_position(self.symbol)
self._position = util.reformat_position(pos)
return self._position


@property
def today_plpc(self) -> float:
""" stock today's profit/loss percent """
Expand All @@ -128,21 +141,25 @@ def today_plpc(self) -> float:
]
return self._today_plpc


@property
def plpc(self) -> float:
""" stock sym (str) Unrealized profit/loss percentage """
self._plpc = self.position["unrealized_plpc"]
return self._plpc


@property
def exchange_is_open(self) -> bool:
""" returns if exchange is open """
self._is_open = alpaca.api().get_clock().is_open
return self._is_open


def __repr__(self):
return f"{self.symbol}@(${self.price})"


def __str__(self):
return f"{self.symbol}"

Expand All @@ -153,26 +170,31 @@ def __eq__(self, other):
return self.price == other
return self.price == other.price


def __ne__(self, other):
if isinstance(other,(int,float)):
return self.price != other
return self.price != other.price


def __lt__(self, other):
if isinstance(other,(int,float)):
return self.price < other
return self.price < other.price


def __le__(self, other):
if isinstance(other,(int,float)):
return self.price <= other
return self.price <= other.price


def __gt__(self, other):
if isinstance(other,(int,float)):
return self.price > other
return self.price > other.price


def __ge__(self, other):
if isinstance(other,(int,float)):
return self.price >= other
Expand All @@ -185,38 +207,47 @@ def __add__(self, other):
return self.price + other
return self.price + other.price


def __radd__(self, other):
return self.price + other


def __sub__(self, other):
if isinstance(other,(int,float)):
return self.price - other
return self.price - other.price


def __rsub__(self, other):
return self.price - other


def __mul__(self, other):
if isinstance(other,(int,float)):
return self.price * other
return self.price * other.price


def __rmul__(self, other):
return self.price * other


def __truediv__(self, other):
if isinstance(other,(int,float)):
return self.price / other
return self.price / other.price


def __rdiv__(self, other):
return self.price / other


def __floordiv__(self, other):
if isinstance(other,(int,float)):
return self.price // other
return self.price // other.price


def __rfloordiv__(self, other):
return self.price // other

Expand All @@ -227,11 +258,14 @@ def __abs__(self):
# be neg but might be good to have
return abs(self.price)


def __int__(self):
return int(self.price)


def __float__(self):
return float(self.price)


def __round__(self, nDigits):
return round(self.price, nDigits)

0 comments on commit 678106f

Please sign in to comment.