Fix price extraction bug and remove debugging statements

master
Mark Veidemanis 9 months ago
parent cd32dff779
commit e10c6f5c46
Signed by: m
GPG Key ID: 5ACFCEED46C0904F

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

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

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

@ -1,6 +1,7 @@
from datetime import datetime from datetime import datetime
from django.conf import settings from django.conf import settings
from elastic_transport import ConnectionError
from elasticsearch import Elasticsearch from elasticsearch import Elasticsearch
from core.util import logs from core.util import logs
@ -27,6 +28,10 @@ def store_msg(index, msg):
client = initialise_elasticsearch() client = initialise_elasticsearch()
if "ts" not in msg: if "ts" not in msg:
msg["ts"] = datetime.utcnow().isoformat() 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": if not result["result"] == "created":
log.error(f"Indexing of '{msg}' failed: {result}") log.error(f"Indexing of '{msg}' failed: {result}")

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

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

Loading…
Cancel
Save