|
|
|
@ -1,18 +1,27 @@
|
|
|
|
|
from core.exchanges import BaseExchange
|
|
|
|
|
from alpaca.trading.client import TradingClient
|
|
|
|
|
from alpaca.trading.requests import GetAssetsRequest
|
|
|
|
|
from alpaca.common.exceptions import APIError
|
|
|
|
|
from alpaca.trading.client import TradingClient
|
|
|
|
|
from alpaca.trading.enums import OrderSide, TimeInForce
|
|
|
|
|
from alpaca.trading.requests import LimitOrderRequest, MarketOrderRequest
|
|
|
|
|
from alpaca.trading.requests import (
|
|
|
|
|
GetAssetsRequest,
|
|
|
|
|
LimitOrderRequest,
|
|
|
|
|
MarketOrderRequest,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
from core.exchanges import BaseExchange
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AlpacaExchange(BaseExchange):
|
|
|
|
|
def connect(self):
|
|
|
|
|
self.client = TradingClient(
|
|
|
|
|
self.account.api_key, self.account.api_secret, paper=self.account.sandbox, raw_data=True
|
|
|
|
|
self.account.api_key,
|
|
|
|
|
self.account.api_secret,
|
|
|
|
|
paper=self.account.sandbox,
|
|
|
|
|
raw_data=True,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def get_account(self):
|
|
|
|
|
return self.client.get_account()
|
|
|
|
|
|
|
|
|
|
def get_supported_assets(self):
|
|
|
|
|
try:
|
|
|
|
|
request = GetAssetsRequest(status="active", asset_class="crypto")
|
|
|
|
@ -20,7 +29,7 @@ class AlpacaExchange(BaseExchange):
|
|
|
|
|
asset_list = [x["symbol"] for x in assets if "symbol" in x]
|
|
|
|
|
print("Supported symbols", asset_list)
|
|
|
|
|
except APIError as e:
|
|
|
|
|
log.error(f"Could not get asset list: {e}")
|
|
|
|
|
self.log.error(f"Could not get asset list: {e}")
|
|
|
|
|
# return False
|
|
|
|
|
return asset_list
|
|
|
|
|
|
|
|
|
@ -55,7 +64,11 @@ class AlpacaExchange(BaseExchange):
|
|
|
|
|
else:
|
|
|
|
|
raise Exception("Unknown direction")
|
|
|
|
|
|
|
|
|
|
cast = {"symbol": trade.symbol, "side": direction, "time_in_force": TimeInForce.IOC}
|
|
|
|
|
cast = {
|
|
|
|
|
"symbol": trade.symbol,
|
|
|
|
|
"side": direction,
|
|
|
|
|
"time_in_force": TimeInForce.IOC,
|
|
|
|
|
}
|
|
|
|
|
if trade.amount is not None:
|
|
|
|
|
cast["qty"] = trade.amount
|
|
|
|
|
if trade.amount_usd is not None:
|
|
|
|
@ -75,7 +88,7 @@ class AlpacaExchange(BaseExchange):
|
|
|
|
|
try:
|
|
|
|
|
order = self.client.submit_order(order_data=market_order_data)
|
|
|
|
|
except APIError as e:
|
|
|
|
|
log.error(f"Error placing market order: {e}")
|
|
|
|
|
self.log.error(f"Error placing market order: {e}")
|
|
|
|
|
return (False, e)
|
|
|
|
|
elif trade.type == "limit":
|
|
|
|
|
if not trade.price:
|
|
|
|
@ -85,7 +98,7 @@ class AlpacaExchange(BaseExchange):
|
|
|
|
|
try:
|
|
|
|
|
order = self.client.submit_order(order_data=limit_order_data)
|
|
|
|
|
except APIError as e:
|
|
|
|
|
log.error(f"Error placing limit order: {e}")
|
|
|
|
|
self.log.error(f"Error placing limit order: {e}")
|
|
|
|
|
return (False, e)
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|