Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
4.8 kB
2
Indexable
Never
def check_buy_sell_signals(df):
    global in_position
    print("checking for buy and sell signals")
    last_row_index = len(df.index) - 1
    previous_row_index = last_row_index - 1
    if not df['in_uptrend'][previous_row_index] and df['in_uptrend'][last_row_index]:
        print("changed to uptrend, buy")
        if not in_position:
            order = exchange.create_market_buy_order(newSymbol, baseOrderSize)
            print(order)
            sleep(1)
            in_position = True
            info()
        if in_position:
            order = exchange.create_market_sell_order(newSymbol, baseOrderSize)
            print(order)
            sleep(1)
            in_position = True
            info()
        else:
            print("already in position, nothing to do")
    if df['in_uptrend'][previous_row_index] and not df['in_uptrend'][last_row_index]:
        if not in_position:
            print("changed to downtrend, sell")
            order = exchange.create_market_sell_order(newSymbol, baseOrderSize)
            print(order)
            sleep(1)
            in_position = True
            info()
        if in_position:
            print("changed to downtrend, sell")
            order = exchange.create_market_sell_order(newSymbol, baseOrderSize)
            print(order)
            sleep(1)
            in_position = True
            info()
        else:
            # order = exchange.create_market_sell_order(newSymbol, baseOrderSize)
            in_position = True
            info()
    return in_position
def info():
    global in_position,takeProfit,stopLoss
    poz = inpos()
    position = exchange.fetchAccountPositions()
    if poz == True:
        try:
            position
        except Exception as e:
            print("HATA".format(e))
            pass
        df = pd.DataFrame(position)
        df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
        tp = takeProfit
        st = stopLoss
        if len(df.loc[df['contracts'] > 0]) > 0:
            print("POZİSYON VAR")
            sleep(2)
            df = pd.DataFrame(position)
            df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
            newdf = df.loc[df['contracts'] > 0]
            newdf2 = pd.DataFrame(newdf, columns=["side", "unrealizedPnl", "contracts", "symbol", "entryPrice", "percentage"])
            # Convert the percentage column to a numeric type
            newdf2["percentage"] = pd.to_numeric(newdf2["percentage"], errors='coerce')
            kontrat = newdf2['contracts']
            # Iterate through each row of the new DataFrame
            for index, row in newdf2.iterrows():
                # Check if the percentage is greater than the take profit threshold
                if row["percentage"] > tp:
                    print("KAR VAR")
                    if row["side"] == "long":
                        print("LONGDA KAR TAKEPROFİT")
                        sat(kontrat)
                        in_position = False
                    elif row["side"] == "short":
                        print("ŞORTTA KAR TAKE PROFİT")
                        al(kontrat)
                        in_position = False
                    break
                # Check if the percentage is less than the stop loss threshold
                elif row["percentage"] < stopLoss:
                    print("ZARAR")
                    if row["side"] == "long":
                        print("STOP LOOSE")
                        sat(kontrat)
                        in_position = False
                    elif row["side"] == "short":
                        print("STOP LOSEEE")
                        al(kontrat)
                        in_position = False
                    break
    if in_position == False:
        df = pd.DataFrame(position)
        df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
        if len(df.loc[df['contracts'] < 0]) < 0:
            print("İŞLEM YOK VEYA BEKLENİYOR")
            run_bot()
    return position

def sat(kontrat):
    try:
        order = exchange.create_order(symbolName+'USDT', 'market', 'sell', kontrat, 0)
    except ccxt.InvalidOrder as e:
        print("Error: Invalid order - ", e)
    except ccxt.NetworkError as e:
        print("Error: Network error - ", e)
        print(order)
        sleep(5)
        in_position = False
    return order
def al(kontrat):
    try:
        order = exchange.create_order(symbolName+'USDT', 'market', 'buy', kontrat, 0)
    except ccxt.InvalidOrder as e:
        print("Error: Invalid order - ", e)
    except ccxt.NetworkError as e:
        print("Error: Network error - ", e)
        sleep(5)
        in_position = False
    return order