
Chapter 5 | 3 min read
Algo Strategy: Sentiment Driven Trades
Ever seen a stock jump 8% within minutes of a breaking news alert?
That’s sentiment in action.
While traditional strategies focus on price, volume, and indicators — sentiment-based algos try to understand what the market is feeling and react faster than any human possibly can.
What is Sentiment Analysis?
In simple words — it’s about figuring out whether the tone of the news, tweets, articles, or announcements is positive, negative, or neutral.
Just like we say, “The vibes are good,” algos try to read market vibes from:
- News websites
- Social media (like Twitter/X)
- Company announcements
- Forums and blogs
How Algos Use Sentiment
Algos are programmed with Natural Language Processing (NLP) tools. These tools can:
-
Scan hundreds of headlines in seconds
-
Understand keywords and tone
-
Score the sentiment (e.g., +0.8 for very positive, -0.6 for negative) If a stock gets a sudden wave of positive news, the algo may:
-
Go long (buy the stock)
-
Place a tight stoploss (in case it's a false signal)
-
Exit when the euphoria cools down
Example
Let’s say a big news headline breaks: "Company XYZ bags ₹1,200 crore defence contract"
Algo scans the word “bags,” “₹1,200 crore,” and “defence” → assigns a positive score.
Stock is already up 2% in the pre-market.
Algo:
- Confirms spike in volume
- Confirms tweet storm building up
- Triggers a quick buy trade
import pandas as pd
import re
from datetime import datetime
# ---- Example inputs (exactly as described) ----
headline = "Company XYZ bags ₹1,200 crore defence contract"
tweets = [
"Big win for $XYZ as it bags massive defence order!",
"Volume building up fast. Watching XYZ closely."
]
premarket_change = 0.02 # +2% pre-market
volume_spike = True # confirmed
tweet_surge = True # confirmed
last_price = 100.0 # mock LTP
vwap_now = 100.8 # mock intraday VWAP
# ---- Tiny lexicon sentiment scorer (fast + transparent) ----
LEXICON = {
"bags": 0.9, "wins": 0.7, "secures": 0.7, "award": 0.6, "order": 0.5,
"contract": 0.7, "defence": 0.2, "crore": 0.2,
"probe": -0.6, "downgrade": -0.7, "loss": -0.6, "delay": -0.4,
"ban": -0.7, "default": -0.9, "fraud": -1.0, "penalty": -0.6
}
def score_text(text: str, lex=LEXICON) -> float:
tokens = re.findall(r"[a-zA-Z]+", text.lower())
hits = [lex[t] for t in tokens if t in lex]
if not hits:
return 0.0
return max(-1.0, min(1.0, sum(hits)/len(hits)))
# ---- Sentiment on the provided example ----
news_scores = [score_text(headline)]
tweet_scores = [score_text(t) for t in tweets]
sentiment = round(pd.Series(news_scores + tweet_scores).mean(), 3)
# ---- Trading rule (quick, sentiment-led scalp toward VWAP) ----
POS_THRESH = 0.25
PREMARKET_MIN = 0.02
conditions_ok = (
sentiment >= POS_THRESH and
premarket_change >= PREMARKET_MIN and
volume_spike and
tweet_surge
)
if conditions_ok:
entry = round(last_price, 2)
target = round(min(vwap_now, entry * 1.01), 2) # ~1% or VWAP, whichever lower
stop = round(entry * 0.985, 2) # ~1.5% tight SL
action = "BUY"
why = f"sentiment={sentiment}≥{POS_THRESH};
premarket={int(PREMARKET_MIN*100)}%+; volume spike; tweet surge"
else:
action = "NO TRADE"
entry = target = stop = None
why = "conditions not met"
# ---- Output ----
result = {
"time": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"headline": headline,
"sentiment": sentiment,
"premarket_change_%": round(premarket_change*100, 2),
"volume_spike": volume_spike,
"tweet_surge": tweet_surge,
"action": action,
"entry": entry, "target": target, "stop": stop,
"vwap_now": vwap_now,
"why": why
}
print(pd.Series(result))
These trades are usually short-term momentum plays, lasting from a few minutes to an hour.
The Challenges
- Sarcasm & slang: Hard for algos to detect (e.g., “Wow, what a brilliant loss!” might be negative but sounds positive)
- Fake news: Algos can’t always detect reliability
- Speed race: Many institutions are doing this, so latency (speed) matters a lot
How to Use it Safely
As a beginner:
- Don’t build a sentiment engine from scratch — it’s complex
- Use basic API-based sentiment data (if available)
- Combine sentiment with price action or volume confirmation
For example: “If sentiment score > 0.7 and price > previous day’s high, go long.”
Pro Tip
Sentiment algos are often used in event-based trading:
- Budget days
- Election results
- Quarterly earnings
- Global news (like Fed interest rate changes)
Here, news = trigger, and algos are always listening.
That wraps up our look at popular algo strategies!
Next, we’ll begin Module 6, where we shift from theory to action:
“Let’s Build a Simple Algo!”
Recommended Courses for you
Beyond Stockshaala
Discover our extensive knowledge center
Learn, Invest, and Grow with Kotak Videos
Explore our comprehensive video library that blends expert market insights with Kotak's innovative financial solutions to support your goals.













