Automating your algo with Python in 15 mins with 50 lines of code

By: Sumit Raj

About Me:

  • Techie | Author | Trainer | Mentor | Startups
  • Working as Senior Solutions Architect at GeoSpark
  • Authored internationally published book "Building Chatbots with Python", translated in Portuguese, Korean.
  • Avid Quora writer - Over a million views so far.
  • Total newbie in trading.

Problem

  • Not being able to take quick action
  • Emotions play
  • Strategies needs more time
  • Seamless execution
  • Cost and Productivity

Challenge

  • I know the strategy
  • I have manually backtested it or got it backtested.
  • I can write the pseudo code, may be using Chartink or TradingView using an interface
  • But I don't know how to automate or I can't sit all day in front of the system

Serverless Solution

What is serverless?

  • Serverless architectures doesn't mean there are no servers.
  • 3rd-party "Backend as a Service" (BaaS) services or "Functions as a Service" (FaaS) platform
  • Removes the need for a traditional always-on server like EC2 or VMs.
  • No out of memory error unless you already know how much you needed.

Step 1

  • Create a Lambda Function for calculation part with our Python code

Step 2

  • Create Zerodha redirect url API
  • Webhook endpoint for receiving Chartink alerts

/set-token => Redirect URL

/webhook => Receiving Alerts from Chartink

Step 3

What if I do not want to place order but just want alerts?

Just couple of more lines of code with the same setup.

def send_message_bot(chat_id, text):
    url = 'https://api.telegram.org/bot1377736798:<TOKEN>/sendMessage'
    r = requests.post(url, {'chat_id': chat_id, 'text': text}).json()

Cost Perspective

  • 1M free requests per month and 400,000 GB-seconds of compute time per month for AWS Lambda
Price
Requests $0.20 per 1M requests
Duration $0.0000166667 for every GB-second

Average trading days: 21 * 6 hour/day = 126 hours
Average runtime: 800 ms ~1sec
126 hours= 126*3600 = 4,53,600 seconds

(126*3600*128)/1024 = 56,700 GB-seconds

 

56,700 GB-s - 400000 free tier GB-s = -343,300.00 GB-s

 

Lambda costs - With Free Tier (monthly): 0.00 USD

Let's do the maths

Number of Requests (per month) Price (per million)
First 300 million $1.00
300+ million $0.90
  • Amazon API Gateway free tier includes one million API calls per month for up to 12 months.
  • 5GB Data (ingestion, archive storage, and data scanned by Logs Insights queries)
  • $1.00 per million events

Productivity and Maintenance

Perspective

  • Setup cronjob with click of a button
  • Logging is a piece of cake
  • Search logs for history of your executions
  • Stopping execution using Throttle
  • Setup once and use forever

Why choose Serverless Lambda over EC2?

  • Highly reduced operational cost
  • Less complex to manage compared to VMs
  • Faster development time and productivity
  • Near zero maintenance

Questions?

Thank you.

Website: https://sumitraj.in

Twitter: @sumit12dec

Phone: +91 959 028 3524

Email: sumit786raj@gmail.com

deck

By sumit12dec

deck

  • 329