How a Simple Idea Sparked My First Trading Algo 🧠💹
When I first dipped my toes into trading, I noticed something curious about NIFTY: it kept bouncing up and down all day, but by the end of it, hadn’t really gone anywhere. That daily volatility felt like untapped opportunity. I thought to myself — if I can just time my entries and exits well, there’s money to be made in this sideways movement.
Back then, I didn’t have much technical knowledge. I wasn’t drawing support/resistance lines or looking at moving averages. I was just observing price behaviour and trying to make sense of it in the most intuitive way.
The Birth of a Simple Idea 🔄
That’s when I built one of my first algos — super simple but very logical at its core.
Here’s how it worked:
If the price went up by X points, the algo would sell Futures or Call Options (CEs).
If the price then dipped, it would buy them back, locking in the difference as profit.
The same logic applied on the downside:
If the market fell, the algo would sell Puts (PEs).
As the price recovered, it would buy them back.
The logic was rooted in one principle: always sell first, and buy back later — especially in options. This made the strategy theta positive, which means time decay was working in our favour. Every passing minute increased our chances of profit — provided price stayed within a range.
The Catch: One-Sided Trends 💥
As elegant as it sounds, the strategy wasn’t foolproof. It worked beautifully in choppy or range-bound markets, but there was a catch…
In a strong one-sided trend — say, when the market kept moving up or down without reversing — this strategy could quickly accumulate heavy losses. Why? Because it kept fighting the trend, expecting reversals that never came.
The Saviour Combo 🤝
To protect against this, I paired this strategy with the Saviour Algo — a trend-following system that thrives during strong directional moves. So if the market does start trending sharply in one direction, the losses from the range strategy are offset (and sometimes even overcompensated) by the gains from Saviour.
Think of it like this:
Range Strategy: Makes money in sideways markets.
Saviour Algo: Kicks in when the market breaks out of the range.
Together, they balance each other out — one zigging when the other zags. 🧘♂️
We are looking at making this code opensource for this algo as well, so stay tuned.
What AI has to say about the code 🤖
Let's decode the run command:
python3.8 ticker_single_scraper_new.py 30 30 <OPTION_SYMBOL> 75:75 <REQUEST_TOKEN>
Once you run this, the bot comes to life. It connects to the API, fetches the current price of your chosen symbol, and immediately places its first "duo order" using the place_duo_order function.
The Secret Sauce: The multiplier_scale
This is where the Scraper goes from being a simple bot to an intelligent trading machine. What happens if the market starts trending strongly in one direction? A simple bot might keep buying as the price falls, digging itself into a deeper and deeper hole.
The Scraper has a brilliant solution: the multiplier_scale.
This is a built-in "learning" mechanism that makes the bot more cautious when it's on the wrong side of a trend. It works by tracking its net position (the total number of buys minus the total number of sells).
If the bot has a net SELL position (e.g., it has sold 3 times but only bought back once), it means the market is trending up. To avoid fighting this powerful uptrend, it automatically makes it harder to sell again. It does this by multiplying the sell_gap by a factor (e.g., 1.3x or 1.7x). The sell order is now placed further away, requiring a much bigger price spike to get filled.
If the bot has a net BUY position, it means the market is trending down. The bot adapts by widening the buy_gap, making it more difficult to add to its losing position.
This dynamic adjustment is the bot's superpower. It allows it to be aggressive in a sideways market but automatically becomes defensive and cautious when a strong trend emerges. It widens its trading bracket to avoid getting run over by the trend.
Built-in Safety Features
Any good trading bot needs safety nets. The Scraper has a few key ones:
The Cool-Off Period (cool_off_time): Inside the place_duo_order function, there's a mandatory pause (e.g., 5 seconds) before a new bracket is placed. This is a critical circuit breaker. It prevents the bot from firing off dozens of trades in a few seconds during a "flash crash" or a sudden, volatile news event. It forces the bot to "take a breath" and re-assess before jumping back in.
The Guardian Loop: The main while True: loop in the script constantly asks one question: "Are there any active orders in the market?" If the answer is no (meaning a trade was just completed or canceled), it knows it's safe to place a new duo order. This ensures the bot is always working but never places new orders while old ones are still pending.
Delta-Based Restrictions: The bot is smart enough to consider the risk of your entire portfolio. Through the check_changes_in_restrictions function, it can check your portfolio's overall "delta" (its directional exposure). If your portfolio becomes too bullish (high positive delta), the bot will automatically be forbidden from placing new BUY orders, even if its own logic wants to. This is a powerful, portfolio-level safeguard that prevents one aggressive bot from unbalancing your overall strategy.
A Walkthrough: The Scraper in Action
Let's imagine you run the bot on Nifty Futures, currently at 23,000, with a 10-point gap.
Launch: The bot starts and places a BUY order at 22,990 and a SELL order at 23,010.
Market Dips: The price falls and hits 22,990. The BUY order is instantly filled.
Instant Reaction: The bot immediately cancels the pending SELL order at 23,010. Its net position is now +75.
Adapt: The bot consults its multiplier_scale. Since its position is positive, it widens the buy_gap for the next trade (e.g., from 10 points to 13 points).
Cool-Off: It waits for the 5-second cool-off period.
New Bracket: The market is now at 22,990. The bot places a new bracket: a SELL order at 23,000 (10 points above) and a new, more cautious BUY order at 22,977 (13 points below).
And the cycle continues, 24/7, for as long as the script is running.
Conclusion
The ticker_single_scraper_new.py script is a brilliant example of how a few hundred lines of code can create a sophisticated, adaptive, and automated trading strategy. It's more than just a simple buy/sell program; it's a dynamic system that brackets the market, manages its own orders, and intelligently adapts its behavior based on the market's flow.
While it's a powerful tool, remember that all trading, especially algorithmic trading, carries risks. Understanding every line of code and testing thoroughly in a simulated environment is the first step before ever letting a bot like this trade with real capital. Happy coding, and trade safe
💬 Your Turn — What Do You Think?
I'm curious to hear your thoughts:
Have you ever tried trading range-bound setups like this?
What would you do differently to handle one-sided trends?
Do you think combining strategies is the way forward in algo trading?
This strategy was born from pure observation, and over time, refined with logic and layering. But I know many of you reading this have your own approaches, experiences, and maybe even critiques — and I’d love to hear them.
👉 Drop your thoughts in the comments below, or connect with me directly on X (Twitter) — my DMs are open!
Let’s exchange ideas. That’s how we all grow 🚀
Amazing work sir. I had a question and a suggestion:
Question: If you do both manual analysis and then execute via Algorithm. Doesn't it make it harder to handle because of human biases and also harder to backtest for a long period.
Suggestion: I am not sure if you use a spread while selling a chppy sideways market. If you don't then you can do something like make a 1-2-4 lots spread. When markets do make a big move they still mostly have to follow the 2 std deviation BB range so they do calm down usually. So in a 1-2-4 spread if markets hit your first position you can exit that and hold the other 2 further OTMs where the lots r more because it is more probable that it will not breach it. In which case you do cover some of the losses. This along with the trend following strategy you have could make risk management easier.
Thanks a lot for all the efforts you put. Looking forward to learning more and more.
please share your code and the hyper-parameters also so I can backtest on my side also