Exemple #1
0
def risk_sell(stock, api):
    account = api.get_account()
    money_on_account = float(account.buying_power)
    df, time = get_dataframe(stock, 100)
    atr = get_last_ATR(df)
    price = get_last_price(df, 'c')
    percent_risk = 0.05
    price_pos = 0
    general_risk = 0
    positions = api.list_positions()
    orders = api.list_orders()
    for stock in positions:
        price_pos = stock.avg_entry_price
        stop_price = list(filter(lambda x: x.symbol == stock.symbol, orders))[0].stop_price
        qty = stock.qty
        risk = (float(price_pos) - float(stop_price)) *int(qty)
        general_risk += risk
    if general_risk <= money_on_account * percent_risk:
        stop = price + 2 * atr
        onePercentFromTotal = money_on_account * 0.01
        return stop, onePercentFromTotal / (2 * atr)
    else:
        return 0,0 
Exemple #2
0
def stop_loss_sell(stock, api):
    df, time = get_dataframe(stock, 100)
    atr = get_last_ATR(df)
    price = get_last_price(df, 'c')
    stop = price - 2 * atr
    return stop
Exemple #3
0
def workplace():
    clock = api.get_clock()
    close = clock.next_close - clock.timestamp
    if clock.is_open:
        if close.total_seconds() > 900:
            try:
                replace_stop_loss(api)
            except Exception as error:
                print(error)
            watch_list = api.get_watchlists()
            watch_list = watch_list[0].id
            mylist = api.get_watchlist(watch_list)

            dif_symbols = len(mylist.assets)
            print('Number of symbols: ', dif_symbols)

            for each in mylist.assets:

                unit = each['symbol']
                print('Symbol: ', unit)
                account = api.get_account()

                df = get_dataframe(unit, 100)
                price = get_last_price(df[0], 'c')
                print('Current price: ', price)

                todo = check_indicator(df[0], 'hhll')

                trend = df[0].iloc[-1]['trend']
                if (trend == 'Buy' and todo == 'Buy'):
                    todo = 'Buy'
                elif (trend == 'Sell' and todo == 'Sell'):
                    todo = 'Sell'
                else:
                    todo = 'Skip'
                print('Action: ', todo)

                stop_price, qnty = risk(todo, unit, api)
                qnty = int(qnty)
                print('Stop price: ', stop_price)
                print('Quantity: ', int(qnty))

                total = qnty * price
                money = float(account.buying_power)

                if total > money:
                    qnty = int(money / price)
                if (qnty > 0):
                    try:
                        api.submit_order(symbol=unit,
                                         qty=int(qnty / dif_symbols),
                                         side=todo.lower(),
                                         type='market',
                                         time_in_force='gtc',
                                         order_class='oto',
                                         stop_loss={'stop_price': stop_price})
                    except Exception as error:
                        print(error)
        else:
            api.close_all_positions()
            api.cancel_all_orders()
    else:
        print('Рынок закрыт')