Fix price extraction bug and remove debugging statements

This commit is contained in:
Mark Veidemanis 2023-08-26 11:05:28 +00:00
parent cd32dff779
commit e10c6f5c46
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
6 changed files with 10 additions and 25 deletions

View File

@ -12,8 +12,6 @@ def get_balance_hook(user_id, user_name, account_id, account_name, balance):
Called every time the balance is fetched on an account.
Store this into Elasticsearch.
"""
print("get_balance_hook start")
print("PARAMS", user_id, user_name, account_id, account_name, balance)
store_msg(
"balances",
{
@ -24,7 +22,6 @@ def get_balance_hook(user_id, user_name, account_id, account_name, balance):
"balance": balance,
},
)
print("get_balance_hook end")
def get_pair(account, base, quote, invert=False):

View File

@ -41,15 +41,10 @@ class OANDAExchange(BaseExchange):
def get_balance(self, return_usd=False):
r = accounts.AccountSummary(self.account_id)
print("r", r)
response = self.call(r)
print("response", response)
balance = float(response["balance"])
print("balance", balance)
currency = response["currency"]
print("currency", currency)
balance_usd = common.to_currency("sell", self.account, balance, currency, "USD")
print("balance_usd", balance_usd)
common.get_balance_hook(
self.account.user.id,
@ -58,11 +53,8 @@ class OANDAExchange(BaseExchange):
self.account.name,
balance_usd,
)
print("common.get_balance_hook", common.get_balance_hook)
if return_usd:
print("YES return_usd")
return balance_usd
print("NO return_usd")
return balance
def get_market_value(self, symbol):

View File

@ -67,6 +67,7 @@ def update_customer_fields(user):
name = expand_name(user.first_name, user.last_name)
stripe.Customer.modify(user.stripe_id, name=name)
def create_or_update_customer(user):
"""
Create or update a customer in Lago.

View File

@ -1,6 +1,7 @@
from datetime import datetime
from django.conf import settings
from elastic_transport import ConnectionError
from elasticsearch import Elasticsearch
from core.util import logs
@ -27,6 +28,10 @@ def store_msg(index, msg):
client = initialise_elasticsearch()
if "ts" not in msg:
msg["ts"] = datetime.utcnow().isoformat()
result = client.index(index=index, body=msg)
try:
result = client.index(index=index, body=msg)
except ConnectionError as e:
log.error(f"Error indexing '{msg}': {e}")
return
if not result["result"] == "created":
log.error(f"Indexing of '{msg}' failed: {result}")

View File

@ -376,33 +376,27 @@ def execute_strategy(callback, strategy, func):
# Check if we are trading against the trend
within_trends = checks.within_trends(strategy, symbol, direction)
if not within_trends:
print("NOT WITHIN TRENDS")
return
print("IS WITHIN TRENDS")
type = strategy.order_settings.order_type
# Get the account's balance in the native account currency
cash_balance = strategy.account.client.get_balance()
log.debug(f"Cash balance: {cash_balance}")
print("CASH", cash_balance)
# Convert the trade size, which is currently in the account's base currency,
# to the base currency of the pair we are trading
trade_size_in_base = get_trade_size_in_base(
direction, account, strategy, cash_balance, base
)
print("TRADE SIZE IN BASE", trade_size_in_base)
# Calculate TP/SL/TSL
protection = get_tp_sl(
direction, strategy, current_price, round_to=display_precision
)
print("PROTECTION", protection)
# Create object, note that the amount is rounded to the trade precision
amount_rounded = float(round(trade_size_in_base, trade_precision))
print("AMOUNT ROUNDED", amount_rounded)
new_trade = Trade.objects.create(
user=user,
account=account,
@ -418,7 +412,6 @@ def execute_strategy(callback, strategy, func):
direction=direction,
**protection,
)
print("NEW TRADE", new_trade)
new_trade.save()
if strategy.risk_model is not None:
@ -436,7 +429,6 @@ def execute_strategy(callback, strategy, func):
# Run the crossfilter to ensure we don't trade the same pair in opposite directions
filtered = crossfilter(account, symbol, direction, func)
print("FILTERED", filtered)
# TP/SL calculation and get_trade_size_in_base are wasted here, but it's important
# to record the decision in the Trade object. We can only get it after we do those.

View File

@ -49,13 +49,11 @@ class HookAPI(APIView):
log.error(f"HookAPI POST: {e}")
return HttpResponseBadRequest(e)
if hasattr(hook_resp, "market.price"):
if hasattr(hook_resp, "price"):
try:
price = float(hook_resp.market.price)
price = float(hook_resp.price)
except ValueError:
log.debug(
f"Could not extract price from message: {hook_resp.market.price}"
)
log.debug(f"Could not extract price from message: {hook_resp.price}")
return HttpResponseBadRequest("Could not extract price from message")
else:
price = extract_price(hook_resp.message)