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 👇👇👇👇
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.
First, let's create an AWS Lambda Function
Log in to the AWS Management Console and search for Lambda:
- Click "Create function" and select "Author from scratch"
- Enter a function name, such as "MyDollarCostAveragingFunction"
- Choose a runtime for the function, such as Python or Node.js, Java, etc. (Python is used in this example)
- Create or select a new IAM Role for the Lambda Function
- Click "Create function" and wait for AWS to create the function...
Next, we will write or upload code for the 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).
Test the Lambda Function with the event you created, and if everything works correctly, you can deploy it.
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
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 .
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".
Now you can use the requests library in your Lambda Function.
Lastly, we will set up an Event Trigger to call the function daily
- In the Lambda Function page, click "Add trigger"
- Select "EventBridge (CloudWatch Events)" from the list
- Create a new Event Rule by clicking "Create a new rule"
- Enter a name for the Rule, such as "DailyTrigger"
- Set the Schedule expression to call the function daily by entering
cron(0 12 * * ? *)
, which means every day at 12:00 (UTC time) - Click "Add" to add the Trigger to your Lambda function
- Click "Save" to save the new Trigger settings
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)