Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Break while loop for particular argument

I am in learning stage. Trying to create algo for trading.

Have a stock list
l = [ 's1', 's2' ]

Getting ltp through While Loop .
If stock s1 hit sl or target than break while loop for s1 to avoid multiple order and continue for s2 until condition not meet

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

stock = ["s1","s2"]

def sell(stock,ltp):
    while True:
        sl = ltp  - ltp*.01
        tgt =ltp + ltp*0.01

        # getting last price from api as Ltp
        if Ltp <=sl:
            # place sell order for S1
            break

            '''how to continue getting Ltp for stock s2 until condition not meet '''

def buy():
        for stock in stock:
            # buy order at ltp
            sell(stock,ltp)

>Solution :

You could try something like this:

# dictionary of stock:buy price
stock = {"s1":100,"s2":200}

def sell(stock):
    # create a dictionary of status for all stocks
    stock_sold = {sto:False for sto in stock}
    while True:
        for sto in stock:
           # getting last price from api as Ltp
           if (Ltp <= stock[sto]*0.9 or Ltp >= stock[sto]*1.1) and not stock_sold[sto]:
               # place sell order for S1
               stock_sold[sto] = True

        if all([stock_sold[sto] for sto in stock]:
            break
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading