Remove asset filter and begin implementing posting trades

This commit is contained in:
2022-11-10 07:20:14 +00:00
parent 47384aed5f
commit 40f6330a13
7 changed files with 164 additions and 86 deletions

View File

@@ -1,3 +1,5 @@
from decimal import Decimal as D
from alpaca.common.exceptions import APIError
from core.models import Strategy, Trade
@@ -33,11 +35,14 @@ def execute_strategy(callback, strategy):
base = callback.base
quote = callback.quote
direction = hook.direction
if quote not in ["usd", "usdt", "usdc", "busd"]:
log.error(f"Quote not compatible with Dollar: {quote}")
return False
quote = "usd" # TODO: MASSIVE HACK
symbol = f"{base.upper()}/{quote.upper()}"
if account.exchange == "alpaca":
if quote not in ["usd", "usdt", "usdc", "busd"]:
log.error(f"Quote not compatible with Dollar: {quote}")
return False
quote = "usd" # TODO: MASSIVE HACK
symbol = f"{base.upper()}/{quote.upper()}"
elif account.exchange == "oanda":
symbol = f"{base.upper()}_{quote.upper()}"
if symbol not in account.supported_symbols:
log.error(f"Symbol not supported by account: {symbol}")
@@ -53,32 +58,32 @@ def execute_strategy(callback, strategy):
# type = "limit"
type = "market"
trade_size_as_ratio = strategy.trade_size_percent / 100
trade_size_as_ratio = D(strategy.trade_size_percent) / D(100)
log.debug(f"Trade size as ratio: {trade_size_as_ratio}")
amount_usd = trade_size_as_ratio * cash_balance
amount_usd = D(trade_size_as_ratio) * D(cash_balance)
log.debug(f"Trade size: {amount_usd}")
price = callback.price
price = round(D(callback.price), 8)
if not price:
return
log.debug(f"Extracted price of quote: {price}")
# We can do this because the quote IS in $ or equivalent
trade_size_in_quote = amount_usd / price
trade_size_in_quote = D(amount_usd) / D(price)
log.debug(f"Trade size in quote: {trade_size_in_quote}")
# calculate sl/tp
stop_loss_as_ratio = strategy.stop_loss_percent / 100
take_profit_as_ratio = strategy.take_profit_percent / 100
stop_loss_as_ratio = D(strategy.stop_loss_percent) / D(100)
take_profit_as_ratio = D(strategy.take_profit_percent) / D(100)
log.debug(f"Stop loss as ratio: {stop_loss_as_ratio}")
log.debug(f"Take profit as ratio: {take_profit_as_ratio}")
stop_loss_subtract = price * stop_loss_as_ratio
take_profit_add = price * take_profit_as_ratio
stop_loss_subtract = D(price) * D(stop_loss_as_ratio)
take_profit_add = D(price) * D(take_profit_as_ratio)
log.debug(f"Stop loss subtract: {stop_loss_subtract}")
log.debug(f"Take profit add: {take_profit_add}")
stop_loss = price - stop_loss_subtract
take_profit = price + take_profit_add
stop_loss = D(price) - D(stop_loss_subtract)
take_profit = D(price) + D(take_profit_add)
log.debug(f"Stop loss: {stop_loss}")
log.debug(f"Take profit: {take_profit}")
@@ -90,15 +95,15 @@ def execute_strategy(callback, strategy):
symbol=symbol,
type=type,
# amount_usd=amount_usd,
amount=trade_size_in_quote,
amount=float(round(trade_size_in_quote, 2)),
# price=price,
stop_loss=stop_loss,
take_profit=take_profit,
stop_loss=float(round(stop_loss, 2)),
take_profit=float(round(take_profit, 2)),
direction=direction,
)
new_trade.save()
info = new_trade.post()
log.debug(f"Posted trade: {posted} - {info}")
log.debug(f"Posted trade: {info}")
def process_callback(callback):

View File

@@ -179,6 +179,106 @@ AccountDetailsSchema = {
}
{
"account": {
"marginCloseoutNAV": "35454.4740",
"marginUsed": "10581.5000",
"currency": "EUR",
"resettablePL": "-13840.3525",
"NAV": "35454.4740",
"marginCloseoutMarginUsed": "10581.5000",
"marginCloseoutPositionValue": "211630.0000",
"openTradeCount": 2,
"id": "101-004-1435156-001",
"hedgingEnabled": False,
"marginCloseoutPercent": "0.14923",
"marginCallMarginUsed": "10581.5000",
"openPositionCount": 1,
"positionValue": "211630.0000",
"pl": "-13840.3525",
"lastTransactionID": "2123",
"marginAvailable": "24872.9740",
"marginRate": "0.05",
"marginCallPercent": "0.29845",
"pendingOrderCount": 0,
"withdrawalLimit": "24872.9740",
"unrealizedPL": "0.0000",
"alias": "hootnotv20",
"createdByUserID": 1435156,
"marginCloseoutUnrealizedPL": "0.0000",
"createdTime": "2016-06-24T21:03:50.914647476Z",
"balance": "35454.4740",
},
"lastTransactionID": "2123",
}
class AccountSummaryNested(BaseModel):
marginCloseoutNAV: str
marginUsed: str
currency: str
resettablePL: str
NAV: str
marginCloseoutMarginUsed: str
marginCloseoutPositionValue: str
openTradeCount: int
id: str
hedgingEnabled: bool
marginCloseoutPercent: str
marginCallMarginUsed: str
openPositionCount: int
positionValue: str
pl: str
lastTransactionID: str
marginAvailable: str
marginRate: str
marginCallPercent: str
pendingOrderCount: int
withdrawalLimit: str
unrealizedPL: str
alias: str
createdByUserID: int
marginCloseoutUnrealizedPL: str
createdTime: str
balance: str
class AccountSummary(BaseModel):
account: AccountSummaryNested
lastTransactionID: str
AccountSummarySchema = {
"marginCloseoutNAV": "account.marginCloseoutNAV",
"marginUsed": "account.marginUsed",
"currency": "account.currency",
"resettablePL": "account.resettablePL",
"NAV": "account.NAV",
"marginCloseoutMarginUsed": "account.marginCloseoutMarginUsed",
"marginCloseoutPositionValue": "account.marginCloseoutPositionValue",
"openTradeCount": "account.openTradeCount",
"id": "account.id",
"hedgingEnabled": "account.hedgingEnabled",
"marginCloseoutPercent": "account.marginCloseoutPercent",
"marginCallMarginUsed": "account.marginCallMarginUsed",
"openPositionCount": "account.openPositionCount",
"positionValue": "account.positionValue",
"pl": "account.pl",
"lastTransactionID": "account.lastTransactionID",
"marginAvailable": "account.marginAvailable",
"marginRate": "account.marginRate",
"marginCallPercent": "account.marginCallPercent",
"pendingOrderCount": "account.pendingOrderCount",
"withdrawalLimit": "account.withdrawalLimit",
"unrealizedPL": "account.unrealizedPL",
"alias": "account.alias",
"createdByUserID": "account.createdByUserID",
"marginCloseoutUnrealizedPL": "account.marginCloseoutUnrealizedPL",
"createdTime": "account.createdTime",
"balance": "account.balance",
}
class PositionDetailsNested(BaseModel):
instrument: str
long: PositionLong