Skip to content

Commit

Permalink
added formating
Browse files Browse the repository at this point in the history
  • Loading branch information
AxelGard committed May 7, 2024
1 parent f086cee commit 1072df1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 52 deletions.
2 changes: 1 addition & 1 deletion cira/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from . import log
from . import strategy

# Assets
# Assets
from .asset import Asset
from .assset_cryptocurrency import Cryptocurrency
from .asset_stock import Stock
Expand Down
25 changes: 12 additions & 13 deletions cira/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import warnings

# Alpaca
# Alpaca
import alpaca
from alpaca.trading.requests import GetAssetsRequest
from alpaca.trading.enums import AssetClass, OrderType, AssetStatus
Expand All @@ -13,18 +13,18 @@
from alpaca.trading.requests import LimitOrderRequest, StopLimitOrderRequest
from alpaca.trading.client import TradingClient

# stock
# stock
from alpaca.data import StockHistoricalDataClient
from alpaca.data.requests import StockLatestQuoteRequest
from alpaca.data.requests import StockBarsRequest
from alpaca.trading.requests import MarketOrderRequest
from alpaca.data.live import StockDataStream

# crypto
from alpaca.data import CryptoHistoricalDataClient
# crypto
from alpaca.data import CryptoHistoricalDataClient
from alpaca.data.live.crypto import CryptoDataStream
from alpaca.data.requests import CryptoLatestQuoteRequest
from alpaca.data.requests import CryptoBarsRequest
from alpaca.data.requests import CryptoBarsRequest

import pandas as pd

Expand All @@ -38,11 +38,11 @@ class Asset:
def __init__(self, symbol: str) -> None:
"""Interface class"""
self.symbol = symbol
self.live_client:StockDataStream = None
self.history:StockHistoricalDataClient = None
self.trade:TradingClient = None
self.latest_quote_request:StockLatestQuoteRequest = None
self.bars_request:StockBarsRequest = None
self.live_client: StockDataStream = None
self.history: StockHistoricalDataClient = None
self.trade: TradingClient = None
self.latest_quote_request: StockLatestQuoteRequest = None
self.bars_request: StockBarsRequest = None

def price(self) -> float:
raise NotImplementedError
Expand All @@ -51,7 +51,6 @@ def price(self) -> float:
def get_all_assets(self):
raise NotImplementedError


def live_data(self, async_function_to_resolve_to, run: bool = True) -> None:
self.live_client.subscribe_quotes(async_function_to_resolve_to, self.symbol)
if run:
Expand Down Expand Up @@ -115,7 +114,7 @@ def buy_at(self, qty: int, price: float) -> None:
limit_order_data = LimitOrderRequest(
symbol=self.symbol,
limit_price=price,
qty = qty,
qty=qty,
side=OrderSide.BUY,
time_in_force=TimeInForce.FOK,
)
Expand Down Expand Up @@ -324,4 +323,4 @@ def __eq__(self, other):
return self.symbol == other.symbol

def __ne__(self, other):
return not self.__eq__(other)
return not self.__eq__(other)
3 changes: 2 additions & 1 deletion cira/asset_option.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import warnings


class OptionContract:
def __init__(self) -> None:
warnings.warn('Options have not been implemented, comming soon')
warnings.warn("Options have not been implemented, comming soon")
11 changes: 6 additions & 5 deletions cira/asset_stock.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pandas as pd


# Alpaca
# Alpaca
import alpaca
from alpaca.trading.requests import GetAssetsRequest
from alpaca.trading.enums import AssetClass, OrderType, AssetStatus
Expand All @@ -12,15 +12,15 @@
from alpaca.trading.client import TradingClient


# stock
# stock
from alpaca.data import StockHistoricalDataClient
from alpaca.data.requests import StockLatestQuoteRequest
from alpaca.data.requests import StockBarsRequest
from alpaca.trading.requests import MarketOrderRequest
from alpaca.data.live import StockDataStream


# cira
# cira
from .asset import Asset
from . import auth
from . import config
Expand All @@ -38,7 +38,6 @@ def __init__(self, symbol: str) -> None:
self.trade = TradingClient(APCA_ID, APCA_SECRET, paper=config.PAPER_TRADING)
self.latest_quote_request = StockLatestQuoteRequest
self.bars_request = StockBarsRequest


def price(self) -> float:
"""gets the asking price of the symbol"""
Expand All @@ -49,5 +48,7 @@ def price(self) -> float:
def get_all_assets(self):
APCA_ID, APCA_SECRET = auth.get_api_keys()
trade = TradingClient(APCA_ID, APCA_SECRET, paper=config.PAPER_TRADING)
search_params = GetAssetsRequest(asset_class=AssetClass.US_EQUITY, status=AssetStatus.ACTIVE)
search_params = GetAssetsRequest(
asset_class=AssetClass.US_EQUITY, status=AssetStatus.ACTIVE
)
return [a.symbol for a in trade.get_all_assets(search_params)]
58 changes: 26 additions & 32 deletions cira/assset_cryptocurrency.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import warnings

# Alpaca
# Alpaca
import alpaca
from alpaca.trading.requests import GetAssetsRequest
from alpaca.trading.enums import AssetClass, OrderType, AssetStatus
Expand All @@ -13,18 +13,18 @@
from alpaca.trading.requests import LimitOrderRequest, StopLimitOrderRequest
from alpaca.trading.client import TradingClient

# stock
# stock
from alpaca.data import StockHistoricalDataClient
from alpaca.data.requests import StockLatestQuoteRequest
from alpaca.data.requests import StockBarsRequest
from alpaca.trading.requests import MarketOrderRequest
from alpaca.data.live import StockDataStream

# crypto
from alpaca.data import CryptoHistoricalDataClient
# crypto
from alpaca.data import CryptoHistoricalDataClient
from alpaca.data.live.crypto import CryptoDataStream
from alpaca.data.requests import CryptoLatestQuoteRequest
from alpaca.data.requests import CryptoBarsRequest
from alpaca.data.requests import CryptoBarsRequest

import pandas as pd

Expand All @@ -38,80 +38,75 @@

class Cryptocurrency(Asset):
def __init__(self, symbol: str) -> None:
"""Exchange for trading cryptocurrencies"""
"""Exchange for trading cryptocurrencies"""
APCA_ID, APCA_SECRET = auth.get_api_keys()
self.symbol = symbol
self.live_client = CryptoDataStream(APCA_ID, APCA_SECRET)
self.history:CryptoHistoricalDataClient = CryptoHistoricalDataClient()
self.history: CryptoHistoricalDataClient = CryptoHistoricalDataClient()
if APCA_ID != "":
self.history = CryptoHistoricalDataClient(APCA_ID, APCA_SECRET)
self.trade = TradingClient(APCA_ID, APCA_SECRET, paper=config.PAPER_TRADING)
self.latest_quote_request = CryptoLatestQuoteRequest
self.latest_quote_request = CryptoLatestQuoteRequest
self.bars_request = CryptoBarsRequest


def price(self) -> float:
"""gets the asking price of the symbol"""
perms = self.latest_quote_request(symbol_or_symbols=self.symbol)
return float(self.history.get_crypto_latest_quote(perms)[self.symbol].ask_price)


def buy(self, qty: float) -> None:
"""Buy the asset,
qty is the number of the asset that you buy"""
market_order = MarketOrderRequest(
symbol=self.symbol,
qty = qty,
side = OrderSide.BUY,
type = OrderType.MARKET,
time_in_force = TimeInForce.GTC,
qty=qty,
side=OrderSide.BUY,
type=OrderType.MARKET,
time_in_force=TimeInForce.GTC,
)
if config.IS_LOGGING:
log.log("BUY", self.symbol, qty)
self.trade.submit_order(market_order)


def buy_within(self, qty:float, buy_at:float, sell_at:float) -> None:
def buy_within(self, qty: float, buy_at: float, sell_at: float) -> None:
req = StopLimitOrderRequest(
symbol = self.symbol,
qty = qty,
side = OrderSide.BUY,
time_in_force = TimeInForce.GTC,
limit_price = buy_at,
stop_price = sell_at
)
symbol=self.symbol,
qty=qty,
side=OrderSide.BUY,
time_in_force=TimeInForce.GTC,
limit_price=buy_at,
stop_price=sell_at,
)
self.trade.submit_order(req)


def buy_at(self, qty: int, price: float) -> None:
"""Buy the asset at a given price,
qty is the number of the asset that you buy"""
limit_order_data = LimitOrderRequest(
symbol=self.symbol,
limit_price=price,
qty = qty,
qty=qty,
side=OrderSide.BUY,
time_in_force=TimeInForce.GTC,
)
if config.IS_LOGGING:
log.log("BUY", self.symbol, qty)
self.trade.submit_order(order_data=limit_order_data)

def sell(self, qty: float) -> None:
"""Sell the asset,
qty is the number of the asset that you sell"""
market_order = MarketOrderRequest(
symbol=self.symbol,
qty=qty,
side=OrderSide.SELL,
type = OrderType.MARKET,
type=OrderType.MARKET,
time_in_force=TimeInForce.GTC,
)
logging.info(f"sell:{self.symbol}, qty:{qty}")
if config.IS_LOGGING:
log.log("SELL", self.symbol, qty)
self.trade.submit_order(market_order)


def sell_at(self, qty: int, price: float) -> None:
"""Sell the asset at a given price,
Expand All @@ -127,7 +122,6 @@ def sell_at(self, qty: int, price: float) -> None:
log.log("SELL", self.symbol, qty)
self.trade.submit_order(order_data=limit_order_data)


def _get_bars(self, start_date: datetime, end_date: datetime):
"""returns aplc bars from the given dates"""
params = self.bars_request(
Expand All @@ -138,12 +132,12 @@ def _get_bars(self, start_date: datetime, end_date: datetime):
adjustment="all",
)
return self.history.get_crypto_bars(params)


@classmethod
def get_all_assets(self):
APCA_ID, APCA_SECRET = auth.get_api_keys()
trade = TradingClient(APCA_ID, APCA_SECRET, paper=config.PAPER_TRADING)
search_params = GetAssetsRequest(asset_class=AssetClass.CRYPTO, status=AssetStatus.ACTIVE)
search_params = GetAssetsRequest(
asset_class=AssetClass.CRYPTO, status=AssetStatus.ACTIVE
)
return [a.symbol for a in trade.get_all_assets(search_params)]

0 comments on commit 1072df1

Please sign in to comment.