Break while loop for particular argument

Question:

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

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)
Asked By: Shankar

||

Answers:

I modified the code based on the examples you gave in a comment; the code includes "sold" flags for each of the stock, and will sell if the Ltp (current stock price if I understood correctly) reaches buy_price – 10% (stoploss) or buy_price + 10% (target).

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
Answered By: Swifty
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.