Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with close_all Method Not Properly Closing Positions #30

Open
r2roi opened this issue Jan 13, 2024 · 2 comments
Open

Issues with close_all Method Not Properly Closing Positions #30

r2roi opened this issue Jan 13, 2024 · 2 comments

Comments

@r2roi
Copy link

r2roi commented Jan 13, 2024

Hello ejtraderCT Team,

I am experiencing an issue with the close_all method in the Ctrader class, which is not functioning as expected in my implementation. The method is supposed to close all open positions, but it fails to do so. Here's the relevant snippet from my main.py:

python code
def process_close_all():
# ... code to update and log current positions ...
 update_positions_list()
current_positions = api.positions()
ctrader.close_all()
time.sleep(1)
# ... logging message ...

Despite calling ctrader.close_all(), the positions remain open. The close_all method is invoked after retrieving the current positions, but the subsequent positions check reveals that they are not closed. This issue occurs even though there are active positions present.

Could you please help me understand if there's a known issue with this method or if I might be missing something in my implementation? Any guidance or suggestions for troubleshooting this would be greatly appreciated.

Thank you for your assistance.

Copy link

We're glad you've opened your first issue. Please provide all the necessary details and any relevant code or screenshots to help us understand the problem better. Our team will review your issue and provide assistance as soon as possible. Thank you for contributing!

@r2roi
Copy link
Author

r2roi commented Jan 13, 2024

Thank you for the prompt response.
The method of buying and selling new positions is functioning properly.This is the only area where problems have arisen.Below is an excerpt from my code that is relevant and I would be happy if you could review it.

import json
from flask import Flask, request
import logging
from ejtraderCT import Ctrader

# Global instance of Ctrader
ctrader_instance = None

# Custom position list
position_list = {}

def get_ctrader_instance(force_relogin=False):
    global ctrader_instance
    if not ctrader_instance or force_relogin:
        credentials = get_ctrader_credentials()
        ctrader_instance = Ctrader(server=credentials["host"], account=credentials["sender_comp_id"], password=credentials["password"])
        LoopingCall(send_heartbeat).start(30) # send heartbeat every 30 seconds
    return ctrader_instance

# ・・・・・・Code to split processing between holding a buy/sell position or closing all positions (close_all) depending on the content of the webhook alert・・・

def update_positions_list():
    global ctrader_instance, position_list
    try:
        # Get current positions
        current_positions = ctrader_instance.positions()
        logger.debug(f"Acquired position data: {current_positions}")

        # Update own position_list
        position_list.clear() # clear existing position_list
        for pos in current_positions:
            position_list[pos['pos_id']] = pos

def process_st_off(retry_count=0):  #ST_OFF is a variable in my Alert Word that indicates the request to make close all
    try:
        update_positions_list() # update position list (call with no arguments)
        current_positions = ctrader.positions()
        logger.debug(f"Current positions: {current_positions}")

        logger.debug("Start process to close all positions.")
        ctrader.close_all()
        time.sleep(1) # wait 1 second
        logger.debug("Close process completed.")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant