Algo Strategy: Trading Conditions Explained

Aa document ma algoStrategy.js ma vaprayeli badhi j conditions (Buy, Sell, and Enhanced Mode) ne Detail ma samjava ma aavyu che.


1. Entry Type Classifications (Market Mood)

Algo har divase market no 'Mood' check kare che. Aa logic Line 134-145 ma che:

Entry Type Condition (Logic) Interpretation
STRONG_BULL ema10 > ema20 && ema20 > ema50 && rsi > 50 && rsi < 70 Market full uptrend ma che.
BEAR Entry ema10 < ema20 && rsi < 45 Market niche jai rahyu che (Correction mode).
ETF Dip rsi < 35 Stock 'Oversold' zone ma che (Sasto bhav).
BULL Entry rsi > 50 && rsi < 70 Normal uptrend.
SIDEWAYS Default Market ek j range ma che.

2. Buy Trigger Conditions (Kyare kharidvu?)

Algo Line 148-151 ma 'Strong Buy Signal' check kare che. Jo niche mathi koi pan ek condition sachi pade, to Buy Order execute thay che:

  1. Uptrend Confirmation:
    • ema10 > ema20 && rsi > 40 && rsi < 75
    • Matlab: Trend up che and RSI pan support kare che.
  2. Oversold Dip (Bollinger Band):
    • rsi < 30 && close > bb.lower
    • Matlab: RSI 30 ni niche che (khub sasto) and price Bollinger Band na niche na level thi pachi upar aavi rahi che.
  3. MACD Bullish Cross:
    • macd.MACD > macd.signal && rsi > 45
    • Matlab: MACD e positive signal aapyo che.

3. Sell Trigger Conditions (Kyare vechvu?)

Algo Line 178-185 ma Sell logic check kare che:

  1. Peak Reversal (Overbought):
    • rsi > 85 || close > bb.upper * 1.02
    • Matlab: RSI 85 thi upar gayo (khub costly) ya price Bollinger Band ni boundary todi ne upar nikli gai.
  2. Bearish Trend Break:
    • ema10 < ema50 && rsi < 40
    • Matlab: Short term trend (EMA10) e long term trend (EMA50) ne niche ni taraf cross karyu.
  3. Stop Loss (Safety):
    • close < buyPrice * (1 - maxLossPercent / 100)
    • Matlab: Jo price kharid-bhav thi 5% (ya set kareli limit) niche jai, to loss book kari lo.
  4. End of Period (Backtest Closure):
    • i === data.length - 1
    • Matlab: Jo backtest no chhello divas aavi jay and position open hoy, to algo automatically current price par trade close kari de che jethi final calculation thai shake.

4. Enhanced Strategy (Optimal Swing Logic)

Jo Standard Strategy no return Buy & Hold return karta ocha ma ocho 5% vadhare na hoy (algoReturn <= bhReturn + 5), tyare જ Enhanced Strategy active thay che (Line 304). Aa logic 'Optimal Swings' shodhe che:

Phase 1: DIP Search (Buy)

Phase 2: Profit Check

Phase 3: PEAK Search (Sell)

Phase 4: Scanning Loop (Next Trade Search)


5. Corporate Events (Corporate Actions) Logic

Algo માત્ર ભાવ પર નહિ, પણ કંપનીના ઇવેન્ટ્સ પર પણ ધ્યાન આપે છે (Line 53-131), જેથી બેકટેસ્ટ એકદમ રિયલ-વર્લ્ડ (Real-world) જેવો જ રિઝલ્ટ આપે:

  1. DIVIDEND: જો કંપની ડિવિડન્ડ આપે, તો તે રકમ સીધી તમારા Balance માં એડ થાય છે. (Dividend = Shares × Amount).
  2. SPLIT: જો સ્ટોક સ્પ્લિટ થાય (દા.ત. 10:1), તો તમારા Shares ની સંખ્યા તે મુજબ વધી જાય છે અને Buy Price તે મુજબ ઘટી જાય છે.
  3. BONUS: બોનસના કિસ્સામાં પણ તમારા Shares માં બોનસના શેર ઉમેરાય છે.
  4. BUYBACK: જો કંપની બાયબેક કરે, તો અમુક શેર્સ કંપનીના બાયબેક ભાવે વેચાય છે અને તેના પૈસા તમારા Balance માં પાછા આવે છે.

6. Summary Table (Technical Indicators)

Indicator Buy Zone Sell Zone
RSI < 30 (Oversold) > 85 (Overbought)
EMA 10 vs 20 EMA 10 > EMA 20 (Golden Cross) EMA 10 < EMA 20 (Death Cross)
Price vs BB Near Lower Band 2% Above Upper Band
MACD MACD > Signal MACD < Signal

[!TIP] Aa badha indicators ma Price Action (45-day DIP) ne sau thi vadhare priority aapva ma aavi che, kemke bhav e badha indicators no priority zone che.


7. Technical Indicators Deep Dive (Kei rite kaam kare che?)

Har ek indicator ni basic samaj and real-world example niche mujab che:

1. EMA (Exponential Moving Average)

2. RSI (Relative Strength Index)

3. MACD (Moving Average Convergence Divergence)

4. Bollinger Bands (BB)


8. Technical Formulas & Logic (ગણતરી કેવી રીતે થાય છે?)

જો તમારે ઊંડાણમાં સમજવું હોય કે કોમ્પ્યુટર આ આંકડા કેવી રીતે લાવે છે:

1. EMA Calculation Logic

EMA માત્ર એવરેજ નથી, પણ તે નવા ભાવને વધુ વજન (Weight) આપે છે.

2. RSI Calculation Logic

RSI છેલ્લા 14 દિવસના વધારા (Gain) અને ઘટાડા (Loss) ને સરખાવે છે.

3. MACD Calculation Logic

MACD બે 'ટ્રેન્ડ લાઈન' વચ્ચેનું અંતર છે.

4. Bollinger Bands (BB) Logic (એકદમ સાદી ભાષામાં)

Bollinger Bands ને તમે એક "પાઈપ (Pipe)" અથવા "હાઇવેની બે બાજુની લાઈનો" તરીકે વિચારી શકો છો.

આ પાઈપ કેવી રીતે બને છે? તેના 3 ભાગ છે:

  1. Middle Band (વચ્ચેની લાઈન): આ છેલ્લા 20 દિવસની સાદી એવરેજ (SMA) છે. જો HDFC 20 દિવસથી ₹100 આસપાસ રહેતો હોય, તો વચ્ચેની લાઈન ₹100 બનશે.
  2. Standard Deviation (SD / σ - હલચલ/Volatility): આ માપે છે કે સ્ટોક રોજ કેટલો કૂદે છે.
    • Proper Formula (SD): √[ ∑(Price - SMA)² / n ] (જ્યાં n = 20 દિવસ, અને SMA = Middle Band)
    • Formula નું સાદી ભાષામાં બ્રેકડાઉન:
      1. (Price - SMA): રોજનો ભાવ વચ્ચેની લાઈનથી કેટલો દૂર હતો તે જુઓ (દા.ત. ૩ રૂપિયા ઉપર યા ૨ રૂપિયા નીચે).
      2. ² (સ્ક્વેર): માઈનસ વાળા આંકડા પ્લસ કરવા બધાનો વર્ગ (Square) કરો.
      3. ∑ અને / n: આવા 20 દિવસના આંકડાનું ટોટલ મારીને તેની એવરેજ કાઢો (જેથી એવરેજ હલચલ મળે).
      4. √ (રૂટ): પહેલા સ્ક્વેર કર્યો હતો તેની અસર કાઢવા ફાઇનલ જવાબનું વર્ગમૂળ કાઢો.
      • ટૂંકમાં: આ ફોર્મ્યુલા એક સિંગલ આંકડો આપે છે જે દર્શાવે છે કે "સ્ટોક રોજ એવરેજથી કેટલા રૂપિયાનો જંપ મારે છે." (દા.ત. SD = ₹5)
  3. Upper / Lower Bands (ઉપર-નીચેની બોર્ડર): ગણિતનો નિયમ છે કે વચ્ચેની લાઈન ± ડબલ હલચલ (2 × SD) કરીએ તો 95% ટાઈમ સ્ટોક એમાં જ રહે.
    • Proper Formula (Upper Band): SMA + (2 × SD)
    • Proper Formula (Lower Band): SMA - (2 × SD)
    • Example Calculation: જો SMA = ₹100 અને SD = ₹5 હોય, તો:
      • Upper Band: ₹100 + (2 × 5) = ₹110 (છત)
      • Lower Band: ₹100 - (2 × 5) = ₹90 (તળિયું)

અલ્ગોરિધમ આનો ઉપયોગ કેવી રીતે કરે છે? હવે આલ્ગોને ખબર પડી ગઈ કે HDFC ની નોર્મલ રેન્જ ₹90 થી ₹110 છે.