victor-aunon / Oanda-trading-bot

A trading (forex and cryptocurrencies) bot based on backtrader to operate within the Oanda platform
MIT License
4 stars 3 forks source link

Bot is crashing on manual trade opening/closing in Oanda terminal. #1

Open batareikin123 opened 1 year ago

batareikin123 commented 1 year ago

Seems like a better bot than other examples available at Github and related to the same subject i.e. attempting to automate trading. But some issues present. Not quiet clear if this bot can open short positions and why bot is crashing if it catches manually opened position.

If a ticker is not in config (where let's say 5-6 tickers assigned) and you want to open manually yet another position with that ticker which is not in config, you have a bot crash with the following result:

Received external transaction MARKET_ORDER with id 17237. Skipping transaction. {'accountBalance': '207.8313', 'accountID': '001-......................, 'batchID': '17237', 'commission': '0.0', 'financing': '0.0', 'fullPrice': {'asks': [{'liquidity': '10000', 'price': 72.29}, {'liquidity': '10000', 'price': 72.295}], 'bids': [{'liquidity': '10000', 'price': 72.26}, {'liquidity': '10000', 'price': 72.255}], 'closeoutAsk': 72.295, 'closeoutBid': 72.255, 'type': 'PRICE'}, 'fullVWAP': 72.29, 'gainQuoteHomeConversionFactor': '1.3431903', 'guaranteedExecutionFee': '0.0', 'halfSpreadCost': '0.2025', 'id': '17238', 'instrument': 'WTICO_USD', 'lossQuoteHomeConversionFactor': '1.3566897', 'orderID': '17237', 'pl': '0.0', 'price': 72.29, 'reason': 'MARKET_ORDER', 'requestID': '187209137580606126', 'time': '1684463616.837673069', 'tradeOpened': {'guaranteedExecutionFee': '0.0', 'halfSpreadCost': '0.2025', 'initialMarginRequired': '99.5194', 'price': 72.29, 'tradeID': '17238', 'units': '10.0'}, 'type': 'ORDER_FILL', 'units': '10.0', 'userID': 9499008} Traceback (most recent call last): File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "/usr/lib/python3.10/runpy.py", line 86, in _run_code exec(code, run_globals) File "/home/user/.local/lib/python3.10/site-packages/oandatradingbot/main.py", line 4, in main() File "/home/user/.local/lib/python3.10/site-packages/oandatradingbot/oandabot.py", line 102, in main cerebro.run() File "/home/user/.local/lib/python3.10/site-packages/backtrader/cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) File "/home/user/.local/lib/python3.10/site-packages/backtrader/cerebro.py", line 1298, in runstrategies self._runnext(runstrats) File "/home/user/.local/lib/python3.10/site-packages/backtrader/cerebro.py", line 1528, in _runnext self._storenotify() File "/home/user/.local/lib/python3.10/site-packages/backtrader/cerebro.py", line 704, in _storenotify strat.notify_store(msg, *args, **kwargs) File "/home/user/.local/lib/python3.10/site-packages/oandatradingbot/strategies/base_strategy.py", line 105, in notify_store response = self.order_manager.manage_transaction(res_dict) File "/home/user/.local/lib/python3.10/site-packages/oandatradingbot/utils/order_manager.py", line 177, in manage_transaction return self.register_market_order(transaction) File "/home/user/.local/lib/python3.10/site-packages/oandatradingbot/utils/transaction_manager.py", line 142, in register_market_order self.orders[op_type][instrument]["MK"] = transaction

An advice on how to bypass this issue would be appreciated.

batareikin123 commented 1 year ago

Yet another example of the mentioned crash.

'batchID': '17243', 'id': '17243', 'instrument': 'WTICO_USD', 'longPositionCloseout': {'instrument': 'WTICO_USD', 'units': 'ALL'}, 'positionFill': 'REDUCE_ONLY', 'reason': 'POSITION_CLOSEOUT', 'requestID': '151180353626691405', 'time': '1684466731.028344999', 'timeInForce': 'FOK', 'type': 'MARKET_ORDER', 'units': '-11.0', 'userID': 9499008} 2023-05-18 23:25:31 - Received external transaction MARKET_ORDER with id 17243. Skipping transaction. {'accountBalance': '210.7551', 'accountID': '001-...................', 'batchID': '17243', 'commission': '0.0', 'financing': '-0.0026', 'fullPrice': {'asks': [{'liquidity': '10000', 'price': 72.559}, {'liquidity': '10000', 'price': 72.564}], 'bids': [{'liquidity': '10000', 'price': 72.529}, {'liquidity': '10000', 'price': 72.524}], 'closeoutAsk': 72.564, 'closeoutBid': 72.524, 'type': 'PRICE'}, 'fullVWAP': 72.529, 'gainQuoteHomeConversionFactor': '1.3421953', 'guaranteedExecutionFee': '0.0', 'halfSpreadCost': '0.2226', 'id': '17244', 'instrument': 'WTICO_USD', 'lossQuoteHomeConversionFactor': '1.3556847', 'orderID': '17243', 'pl': '1.3288', 'price': 72.529, 'reason': 'MARKET_ORDER_POSITION_CLOSEOUT', 'requestID': '151180353626691405', 'time': '1684466731.028344999', 'tradesClosed': [{'financing': '-0.0026', 'guaranteedExecutionFee': '0.0', 'halfSpreadCost': '0.2226', 'price': 72.529, 'realizedPL': '1.3288', 'tradeID': '17242', 'units': '-11.0'}], 'type': 'ORDER_FILL', 'units': '-11.0', 'userID': 9499008} Traceback (most recent call last): File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "/usr/lib/python3.10/runpy.py", line 86, in _run_code exec(code, run_globals) File "/home/user/.local/lib/python3.10/site-packages/oandatradingbot/main.py", line 4, in main() File "/home/user/.local/lib/python3.10/site-packages/oandatradingbot/oandabot.py", line 102, in main cerebro.run() File "/home/user/.local/lib/python3.10/site-packages/backtrader/cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) File "/home/user/.local/lib/python3.10/site-packages/backtrader/cerebro.py", line 1298, in runstrategies self._runnext(runstrats) File "/home/user/.local/lib/python3.10/site-packages/backtrader/cerebro.py", line 1528, in _runnext self._storenotify() File "/home/user/.local/lib/python3.10/site-packages/backtrader/cerebro.py", line 704, in _storenotify strat.notify_store(msg, *args, **kwargs) File "/home/user/.local/lib/python3.10/site-packages/oandatradingbot/strategies/base_strategy.py", line 105, in notify_store response = self.order_manager.manage_transaction(res_dict) File "/home/user/.local/lib/python3.10/site-packages/oandatradingbot/utils/order_manager.py", line 212, in manage_transaction return self.market_order_canceled(transaction) File "/home/user/.local/lib/python3.10/site-packages/oandatradingbot/utils/transaction_manager.py", line 282, in market_order_canceled trade_id = self.orders[op_type][instrument]["MK"]["id"] KeyError: 'WTICO_USD'

This ticker WTICO_USD is not in config. Trade opened manually. Bot crash example is above.