🔵 How to Get More Bitcoin [Coinbase x AlphaSquared]
Earlier this year I reviewed a product called AlphaSquared that provides a 0-100 risk number similar to the Fear and Greed index
Why AlphaSquared?
No one can perfectly time the market, but using products like AlphaSquared has allowed me to massively outperform a “dumb” dollar cost-averaging strategy over the last 5 years.
My favorite part of AlphaSquared is they have integrated backtesting that allows you to create your own strategy and see how it would have performed over any time horizon.
This allows you to customize a strategy that is totally unique to you and your risk tolerance.
If you want to learn even more about AlphaSquared and get free access check out this video here:
So AlphaSquared is great but it's kind of a pain to go check it everyday and the place a trade. It would be much better if we could just automatically place trades based on the custom strategies that we've defined within our AlphaSquared profiles.
Good news: the Coinbase Advanced Trade API exists!
Coinbase Advanced Trade x AlphaSquared
Over the last year I've refactored my Coinbase Advanced Trade wrapper to include even more functionality and even reference the new official wrapper built by Coinbase.
Because we already have this base of the Coinbase Advanced Trade API it is very simple to integrate with AlphaSquared.
Simply go to your AlphaSquared Member Area and click on API Access. This will show you your AlphaSquared API Token.
Next, go download the latest code from my Python wrapper (to use AlphaSquared you will need version 0.3.2 or later)
You can either download the source code if you want to run it locally or you can download the layer.zip to run the code in AWS
If you need help getting your code up and running in AWS check out this full video tutorial:
If you still have questions go join the Discord and someone should be able to help you
Using the Code
Here's some sample code that you can use up in AWS with your new lambda layer, your Coinbase API keys and your Alphasquared API keys to get you started:
from coinbase_advanced_trader import EnhancedRESTClient, AlphaSquaredTrader
from alphasquared import AlphaSquared
import logging
# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
def lambda_handler(event, context):
try:
# Initialize Coinbase client with obscured API keys
coinbase_api_key = "YOUR_COINBASE_API_KEY"
coinbase_api_secret = "YOUR_COINBASE_API_SECRET"
coinbase_client = EnhancedRESTClient(api_key=coinbase_api_key, api_secret=coinbase_api_secret)
# Initialize AlphaSquared client with obscured API key
alphasquared_api_key = "YOUR_ALPHASQUARED_API_KEY"
alphasquared_client = AlphaSquared(alphasquared_api_key, cache_ttl=60)
# Create AlphaSquaredTrader
trader = AlphaSquaredTrader(coinbase_client, alphasquared_client)
# Set trading parameters
product_id = "BTC-USDC"
strategy_name = "YOUR STRATEGY NAME"
# Execute strategy
logger.info(f"Executing strategy {strategy_name} for {product_id}")
trader.execute_strategy(product_id, strategy_name)
logger.info("Strategy execution completed")
return {
'statusCode': 200,
'body': 'Strategy execution completed successfully'
}
except Exception as e:
logger.error(f"An error occurred: {str(e)}")
logger.exception("Full traceback:")
return {
'statusCode': 500,
'body': f'An error occurred: {str(e)}'
}
The last variable you need to fill out in the script above is the strategy_name. This comes from your AlphaSquared account.
Note that this only works with Custom Strategies that you define (not with the pre-existing default AlphaSquared stratgies - Conservative/Moderate/Aggressive)
I have a strategy called "My Custom Strat" where I have defined some custom values:
Here's how to read the custom strategy above:
If the AlphaSquared risk is between 0-5 my strategy will buy $200
If the AlphaSquared Risk is between 60-65 my strategy will buy $5
If the AlphaSquared risk is between 85-90 my strategy will sell 5% of my current portfolio
If the AlphaSquared risk is between 95-100 my strategy will sell 12% of my current portfolio
Note: there is a big difference between buying and selling.
When buying I am defining a set fiat amount that I want to buy (could be any number >0)
When selling I am defining a percentage of my portfolio to sell (obviously can't be >100)
If I wanted to call bring "My Custom Strat" into my script I would use this code:
strategy_name = "My Custom Strat"
Make sure you respect the spacing and capitalization that you define in AlphaSquared
Behind the scenes the Coinbase API will handle the rest (buying a static fiat amount and selling a % of the portfolio you're currently trading in).
If you want to learn more about how to set up a sub-portfolio within your Coinbase account and track the profit and loss of this AlphaSquared strategy over time check out this video here:
If you have any questions join the Discord and I'll get you pointed in the right direction.