psemdel / py-trading-bot

Trading-bot in python using django, vertorbt lib and interactive-brokers
MIT License
157 stars 38 forks source link

Missing used_api_key #42

Closed Fun1628 closed 2 months ago

Fun1628 commented 2 months ago

Furthermore, under IB.py

if (_settings["USED_API"]["alerting"]=="IB" and\

which is always empty in setting.py and there is a comment saying

"USED_API":{ "orders": "", #don't modify

psemdel commented 2 months ago

Hi, There are 2:

Order settings

"USED_API_DEFAULT":{ "orders": os.environ.get("USED_API_FOR_ORDER_PERF","IB"), #"IB", "MT5", "TS" or "CCXT" (YF does not allow performing orders) "alerting":os.environ.get("USED_API_FOR_DATA_ALERTING","IB"), #"IB", "YF", "MT5", "TS" or "CCXT" "reporting":os.environ.get("USED_API_FOR_DATA_REPORTING","YF"), #"IB", "YF", "MT5", "TS" or "CCXT" }, "USED_API":{ "orders": "", #don't modify "alerting":"", #don't modify "reporting":"", #don't modify },

USED_API_DEFAULT is what you want to use by default, USED_API is a kind of global that will saved the value evaluated during the run. The reason for that, is that IB requires quite a lot of permission and it maybe that you would like to use IB, but you don't have the permission, so it will fallback on YF. I could write that in the comments.

psemdel commented 2 months ago

It is populated in orders.models.py

def check_ib_permission(symbols: list, verbose: bool=True): ''' Populate USED_API from USED_API_DEFAULT Check if IB can be used, otherwise YF is the fallback. For CCXT, MT5 and TS there is nothing to check.

Needs to be performed for each set of symbols reported

Arguments
----------
   symbols: list of YF tickers
'''
for k, v in _settings["USED_API_DEFAULT"].items():
    if v in ["CCXT","MT5","TS"]:
        _settings["USED_API"][k]=v
    elif v=="IB":
        if symbols is None:
            #then populate only if it is empty
            if _settings["USED_API"][k]=="": 
                _settings["USED_API"][k]="IB"
        else:
            for symbol in symbols:
                if symbol in _settings["IB_STOCK_NO_PERMISSION"]:
                    if verbose:
                        logger.info("symbol " + symbol + " has no permission for IB")
                    _settings["USED_API"][k]="YF"
                    break

                a=Action.objects.get(symbol=symbol)      
                if a.stock_ex.ib_auth==False:
                    if verbose:
                        logger.info("stock ex " + a.stock_ex.ib_ticker + " has no permission for IB for "+k + " impacting: "+symbol)
                    _settings["USED_API"][k]="YF"
                    break
    elif v=="YF":
        _settings["USED_API"][k]=v
Fun1628 commented 2 months ago

I think there is a bug in check_ib_permission.

I set "HEARTBEAT_IB":True in setting.py, and notice the _settings["USED_API"]["alerting"] will not set to "IB" even if it has permission etc.

if symbols is None:
                #then populate only if it is empty
                if _settings["USED_API"][k]=="": 
                    _settings["USED_API"][k]="IB"
            else:
                **_settings["USED_API"][k]="IB"** # set to IB initially

So I add the line above and it's now using IB.

psemdel commented 2 months ago

I think you are right, a _settings["USED_API"][k]=_settings["USED_API_DEFAULT"][k] is somehow missing... I will create a branch, because I don't know when I will be able to test.

psemdel commented 1 month ago

I added tests. As a comment, this permission topic was for me one of the most difficult to handle. IB uses very granular permissions for the stocks..and the index. As my strategies use both, I need to be sure that the permissions are on both. The solution that I used in the end with a global is maybe not the best. I just wanted to avoid calling check permission every 2 lines.