Building a Twitter Bot to Tweet My Trades

I thought it would be great to share my trading activity through Twitter for a realtime view of all executions. So here is how to build a twitter bot to tweet my trades.

Building a Twitter Bot to Tweet My Trades

Alpaca’s trade API allows me to execute trades using a predetermined strategy. I thought it would be great to share my trading activity through Twitter for a realtime view of all executions. In this brief tutorial I will show you how to:

  • Trade with a simple script using Alpaca’s V2 API
  • Set up a Twitter developer application
  • Link your trading script to your Twitter application

Here is the full script for this project.

TradeTweeter.py
GitHub Gist: instantly share code, notes, and snippets.

The first step to creating this multi-platform integration was to create a simple script that trades. For this, I wrote a quick program in python that connects with Alpaca’s paper trading so that we can test before live-trading. My code can be seen to the left or can be copied from the gist here.

TradeTweeter.py
GitHub Gist: instantly share code, notes, and snippets.

For this test, my script connects to my paper trading account to purchase 1 share of AAPL at market price that is “good ’til canceled.”

After copying the code, make sure to replace the API key variables with your own keys that can be found on the Alpaca Dashboard.

After running the script, you should be able to see your positions and or orders within the paper trading dashboard. Next is setting up the twitter developer application. To accomplish this, navigate here and begin submitting your application. This is a tad tedious, but verification is instantaneous! After being approved for Developer Twitter, make your Twitter application, and allow the application to tweet from your Developer Twitter Account.

After completing that process, navigate to the “Keys and Tokens” tab under your application which can be seen below.

After generating those keys, we can now successfully integrate with the script you built earlier with just a few additional steps. Initialize your Twython object, and use your consumer key, consumer secret key, access token, and secret access token as parameters. Mine looks something like this:

As you can see above, I have added my keys, and parsed my order so I can tweet key information. The update_status function will now tweet with a string that includes the number of shares, the executed price, and the symbol. For a copy of my full script, please look here. Thank you for reading.

import alpaca_trade_api as tradeapi
from twython import Twython

base_url = 'https://paper-api.alpaca.markets'
api_key_id = 'REPLACE'
api_secret = 'REPLACE'

api = tradeapi.REST(
    base_url=base_url,
    key_id=api_key_id,
    secret_key=api_secret,
    api_version="v2"
)

account = api.get_account()


if account.trading_blocked:
    print('Account is currently restricted from trading.')


print('${} is available as buying power.'.format(account.buying_power))


order = api.submit_order(
    symbol='AAPL',
    qty=1,
    side='buy',
    type='market',
    time_in_force='gtc'
)

my_order = api.get_order(order.id)





twitter = Twython(
    "REPLACE",
    "REPLACE",
    "REPLACE",
    "REPLACE"
)

orderPrice = my_order.filled_avg_price
orderQty = my_order.filled_qty
orderSym = my_order.symbol

string = "Purchased " + str(orderQty) + " shares of " + str(orderSym) + " at $" + str(orderPrice) + "."

twitter.update_status(status=string)
print("Tweeted: %s" % string)


Technology and services are offered by AlpacaDB, Inc. Brokerage services are provided by Alpaca Securities LLC (alpaca.markets), member FINRA/SIPC. Alpaca Securities LLC is a wholly-owned subsidiary of AlpacaDB, Inc.

You can find us @AlpacaHQ, if you use twitter.