DEV Community

Cover image for AWS Lambda - Getting Started with Dollar Cost Averaging
guutong
guutong

Posted on

AWS Lambda - Getting Started with Dollar Cost Averaging

AWS Lambda - Getting Started with Dollar Cost Averaging

Investing carries risks. Investors should study the information before making an investment decision.

The inspiration for this study came from seeing someone in a group trying to post about their investment journey every day. I wanted to try being diligent like them, as shown in the picture below ๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡

Inspiration

However, I'm too lazy to open the app and make a purchase every day like they do๐Ÿ˜ต So, I decided to automate๐Ÿค– the process using AWS Lambda Function, which allows us to run code serverlessly๐Ÿš€ and pay only for what we use. I think it's perfect for testing out dollar cost averaging as a beginner investor.

AWS Lambda Function Price


First, let's create an AWS Lambda Function

Log in to the AWS Management Console and search for Lambda:

Create function

  1. Click "Create function" and select "Author from scratch"
  2. Enter a function name, such as "MyDollarCostAveragingFunction"
  3. Choose a runtime for the function, such as Python or Node.js, Java, etc. (Python is used in this example)
  4. Create or select a new IAM Role for the Lambda Function
  5. Click "Create function" and wait for AWS to create the function...

Next, we will write or upload code for the Lambda Function

Lambda Function

1.Write code for the function using the initial language you chose. The lambda handler will be provided, and you can add the remaining code.
(Note: You can write the code on your machine and then .zip it to "Upload")
2.Once the code is complete, you can test it by clicking "Test". A dialog will pop up, allowing you to create a simulated event to call your Lambda function (in this case, we don't need to send any parameters).

Create event

Test the Lambda Function with the event you created, and if everything works correctly, you can deploy it.

Test event result

3.In this example, the Lambda Function will call an API to create a market price order at the current time, as the goal is to invest regularly with the same amount of money.

In dollar cost averaging, the investor decides only two parameters: the fixed amount of money to invest each time period (i.e. the amount that is available to invest) and how often the funds are invested. https://en.wikipedia.org/wiki/Dollar_cost_averaging

Completed code

Additional Information

For Python, if you want to add other libraries not provided by Lambda Function, you can do so on your machine as follows:
Open a terminal, create a folder called 'python', and install the requests library for API calls (make sure Python is installed on your machine first)

$ mkdir python
$ cd python
$ pip3 install requests -t .
$ zip -r python.zip .
Enter fullscreen mode Exit fullscreen mode

Create Layer

Upload the created .zip file, then "Create" a new Layer. Copy the ARN of the created Layer and add it to your Lambda Function by clicking "Add a layer" and selecting "Specify an ARN". Paste the copied Layer ARN and click "Verify".

Add Layer to Function

Result of layer added

Now you can use the requests library in your Lambda Function.


Lastly, we will set up an Event Trigger to call the function daily

Lambda function diagram<br>

Add event

  1. In the Lambda Function page, click "Add trigger"
  2. Select "EventBridge (CloudWatch Events)" from the list
  3. Create a new Event Rule by clicking "Create a new rule"
  4. Enter a name for the Rule, such as "DailyTrigger"
  5. Set the Schedule expression to call the function daily by entering cron(0 12 * * ? *), which means every day at 12:00 (UTC time)
  6. Click "Add" to add the Trigger to your Lambda function
  7. Click "Save" to save the new Trigger settings

Lambda function result

Once you've followed the steps above, your Lambda function will be called daily as per the schedule you've set.

For API specifications or how to make API calls, you can further explore the following resources:Bitkubย , Binance

Top comments (0)