Created
September 26, 2022 18:27
-
-
Save under0tech/4c179e1c62ce11572648c2cd1083c88b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# MAIN script | |
def medium_trader_go(request): | |
trader_api = api.REST(TRADER_API_KEY, TRADER_API_SECRET, TRADER_API_URL) | |
account = trader_api.get_account() | |
clock = trader_api.get_clock() | |
if bool(account) == True: | |
message = f'''{TRADER_BOT_NAME}: for *{account.account_number}* | |
current capital is _{account.portfolio_value}$_ | |
and non marginable buying power is _{account.non_marginable_buying_power}$_''' | |
send_message(message) | |
if clock.is_open == True: | |
if float(account.non_marginable_buying_power) < CASH_LIMIT: | |
message = f"{TRADER_BOT_NAME}: there is no cash on the account or limit reached!" | |
send_message(message) | |
else: | |
# Screen stocks | |
screened = ScreenStocks(trader_api) | |
# Check limit and trade | |
if len(screened) > 0: | |
CASH_FOR_TRADE_PER_SHARE = (float(account.non_marginable_buying_power) - CASH_LIMIT) / len(screened) | |
for item in screened: | |
predictions = Predict(item['stock']) | |
STOCK = item['stock'] | |
OPERATION = 'buy' if item['direction'] == 'UP' else 'sell' | |
STOP_LOSS = min([item['stop_loss']] + predictions) if item['direction'] == 'UP' else max([item['stop_loss']] + predictions) | |
TAKE_PROFIT = max([item['take_profit']] + predictions) if item['direction'] == 'UP' else min([item['take_profit']] + predictions) | |
SHARE_PRICE = round(min(STOP_LOSS, TAKE_PROFIT), 2) | |
SHARES_TO_TRADE = int(CASH_FOR_TRADE_PER_SHARE / SHARE_PRICE) | |
try: | |
if abs(STOP_LOSS - TAKE_PROFIT) > SHARE_PRICE * TAKE_PROFIT_DELTA and SHARES_TO_TRADE > 0: | |
Trade(api, STOCK, OPERATION, SHARES_TO_TRADE, TAKE_PROFIT, STOP_LOSS) | |
print(f'\n{STOCK}: {STOP_LOSS}, {TAKE_PROFIT}, {OPERATION}, {SHARES_TO_TRADE}') | |
except: | |
pass | |
portfolio = trader_api.list_positions() | |
if bool(portfolio) == True: | |
message = f'{TRADER_BOT_NAME}: we have {len(portfolio)} opened positions.' | |
for i in portfolio: | |
message = message + f'\n\t*{i.symbol}*: qty {i.qty} {i.side} for _{i.market_value}$_ \n\t\t\tcurrent price _{i.current_price}$_ \n\t\t\tprofit _{i.unrealized_pl}$_' | |
send_message(message) | |
if clock.is_open == False: | |
message = f"{TRADER_BOT_NAME}: the market is *CLOSED*, let's try later on!" | |
send_message(message) | |
return f'{TRADER_BOT_NAME}: DONE!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment