Skip to content

Instantly share code, notes, and snippets.

@under0tech
Created May 6, 2023 08:38
Show Gist options
  • Save under0tech/9c0c9876e7fcb456a02f394ad0d35a27 to your computer and use it in GitHub Desktop.
Save under0tech/9c0c9876e7fcb456a02f394ad0d35a27 to your computer and use it in GitHub Desktop.
# Main function
def myrmidon_trader_go(request):
twitter_auth = tweepy.OAuthHandler(TWITTER_API_KEY, TWITTER_API_SECRET)
twitter_api = tweepy.API(twitter_auth)
api = tradeapi.REST(TRADER_API_KEY, TRADER_API_SECRET, TRADER_API_URL)
account = api.get_account()
clock = api.get_clock()
if bool(account) == True:
message = f'''{TRADER_ID}: 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:
portfolio = api.list_positions()
if float(account.non_marginable_buying_power) < CASH_LIMIT or \
len(portfolio) >= OPENED_POSITIONS_LIMIT:
message = f"{TRADER_ID}: there is no cash on the account or limit reached!"
send_message(message)
else:
# Screen stocks
assets = api.list_assets(status='active', asset_class='us_equity')
assets = [x for x in assets if x.shortable == True and x.exchange == 'NASDAQ']
stocks = [x.symbol for x in assets][:SCREENER_NASDAQ_COUNT]
screened = []
for st in stocks:
_stock = screen_stock(st)
if _stock != {}:
screened.append(_stock)
screened = [x for x in screened if abs(x['stop_loss'] - x['take_profit']) > min(x['stop_loss'], x['take_profit']) * TAKE_PROFIT_DELTA]
# Analyze, 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:
STOCK = item['stock']
SENTIMENT = twitter_analysis(twitter_api, STOCK)
if (SENTIMENT['sentiment'] == 'POSITIVE' and item['direction'] == 'UP') or \
(SENTIMENT['sentiment'] == 'NEGATIVE' and item['direction'] == 'DOWN'):
OPERATION = 'buy' if item['direction'] == 'UP' else 'sell'
STOP_LOSS = item['stop_loss']
TAKE_PROFIT = item['take_profit']
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 = api.list_positions()
if bool(portfolio) == True:
message = f'{TRADER_ID}: 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_ID}: the market is *CLOSED*, let's try later on!"
send_message(message)
return f'{TRADER_ID}: DONE!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment