Search code examples
pythonif-statementexceptionempty-list

Starting python if function with an empty value possibility


Automated stock bot in development here.

I'm trying to start an "if" function when an empty list value is returned as the argument, and end the script if a text (string?) value is returned. The reason for this is because I don't want to execute the if function if I currently have a trade open, only when I have no trades open. As far as I know the only modules from my broker's API(Oanda) useful to execute this task is one that outputs [] when I don't have a trade open. This was retrieved from the following list {'positions': [], 'lastTransactionID': '4765'} Part of me thinks what I'm trying to do is impossible, based on the research I've done, but I'm hoping someone has an idea. I put double asterisks around the important part of the script.

    from flask import Flask, request, abort
    import oandapyV20
    import oandapyV20.endpoints.positions as positions
    from exampleauth import exampleAuth


    # Create Flask object called app.
    app = Flask(__name__)


    # Create root to easily let us know its on/working.
    @app.route('/')
    def root():
        return 'online'


    @app.route('/webhook', methods=['POST'])
    def webhook():
        if request.method == 'POST':
            accountID, access_token = exampleAuth()
            api = oandapyV20.API(access_token=access_token)
            client=oandapyV20.API(access_token)
            r=positions.OpenPositions(accountID)
            x=client.request(r)
            **if x['positions']=='[]': #if empty execute if function, if not return 'okay'                      
            data = parse_webhook(request.get_data(as_text=True))
            sell=data['side']
            if data['side']=='sellEURAUD':
                exec(open("marketTPSLEURAUDv2sell.py").read())
            elif data['side']=='buyEURAUD':
                exec(open("marketTPSLEURAUDv2buy.py").read())
        else:
            return 'okay'**

    if __name__ == '__main__':
        app.run()

Solution

  • Just remove the qutos from empty list. Should be like this-

    if x['positions']==[]: