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:
- Uptrend Confirmation:
ema10 > ema20 && rsi > 40 && rsi < 75
- Matlab: Trend up che and RSI pan support kare che.
- 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.
- 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:
- 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.
- Bearish Trend Break:
ema10 < ema50 && rsi < 40
- Matlab: Short term trend (EMA10) e long term trend (EMA50) ne niche ni taraf cross karyu.
- 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.
- 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)
- Window: Current divas thi aagal na 45 divas.
- Condition:
currentPrice === minPrice
- Logic: Algo aagal na 45 divas no data scan kare che. Jo aaj no price aakhi window ma 'Lowest' hoy, to te tya Buy kare che.
Phase 2: Profit Check
- Condition:
maxPrice > minPrice * 1.04 (Line 338)
- Logic: Algo check kare che ke jo hu aahiya buy karu, to aagal na 100 divas ma mane ocha ma oacho 4% profit malse? Jo haa, to j trade lese.
Phase 3: PEAK Search (Sell)
- Window: Kharidya pachi na 100 divas.
- Logic: Algo 100 divas scan kare che and 'Maximum' bhav par sell order execute kare che.
Phase 4: Scanning Loop (Next Trade Search)
- Logic: જો એકવાર ટ્રેડ પૂરો થાય (sell થઈ જાય) તો આલ્ગો ત્યાંથી આગળ નવો ટ્રેડ સ્કેન કરવાનું ચાલુ કરે છે (
currentIdx = maxIdx + 1). પણ જો 4% પ્રોફિટ વાળો ટ્રેડ ના મળે, તો આલ્ગો 7 દિવસ (1 અઠવાડિયું) સ્કીપ કરીને ફરીથી નવો swing શોધે છે (currentIdx += 7). આનાથી ટાઈમ બચે છે અને optimal entry મળે છે.
5. Corporate Events (Corporate Actions) Logic
Algo માત્ર ભાવ પર નહિ, પણ કંપનીના ઇવેન્ટ્સ પર પણ ધ્યાન આપે છે (Line 53-131), જેથી બેકટેસ્ટ એકદમ રિયલ-વર્લ્ડ (Real-world) જેવો જ રિઝલ્ટ આપે:
- DIVIDEND: જો કંપની ડિવિડન્ડ આપે, તો તે રકમ સીધી તમારા Balance માં એડ થાય છે. (Dividend = Shares × Amount).
- SPLIT: જો સ્ટોક સ્પ્લિટ થાય (દા.ત. 10:1), તો તમારા Shares ની સંખ્યા તે મુજબ વધી જાય છે અને Buy Price તે મુજબ ઘટી જાય છે.
- BONUS: બોનસના કિસ્સામાં પણ તમારા Shares માં બોનસના શેર ઉમેરાય છે.
- 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)
- Work: Aa chhela divaso na bhav ne vadhare mahatva aape che. Tena thi trend ni jaldi khabar pade che.
- EMA 10: Short-term trend (Chhela 10 divas).
- EMA 20: Medium-term trend (Chhela 20 divas).
- EMA 50: Long-term trend (Chhela 50 divas).
- Example: Jo EMA 10 (₹712) e EMA 20 (₹700) ne niche thi upar cross kare, to matlab ke trend 'Positive' thai rahyu che (Buy signal).
2. RSI (Relative Strength Index)
- Work: Aa stock 'Sasto' che ke 'Mocho' te batave che. Tena point 0 thi 100 ni vache hoy che.
- RSI < 30: Oversold (Stock khub sasto thai gayo che, have vadhi shake).
- RSI > 70/80: Overbought (Stock khub mocho thai gayo che, have padi shake).
- Example: RSI 32.5 matlab stock khub 'cheap' zone ma che. Aahiya buy karvu safety bali ganay.
3. MACD (Moving Average Convergence Divergence)
- Work: Aa 'Momentum' batave che. Har MACD ma ek MACD Line hoy and ek Signal Line hoy.
- MACD Line > Signal Line: Bullish Momentum (Bhav vadhvani taqat).
- MACD Line < Signal Line: Bearish Momentum (Bhav ghatvani taqat).
- Example: Jo MACD -5 hoy and Signal -7 hoy, to MACD upar che, matlab momentum sudhari rahyu che.
4. Bollinger Bands (BB)
- Work: Aa Price ni 'Range' batave che. Tena tran bhag hoy: Upper Band, Middle Band, and Lower Band.
- Price Lower Band ne tade: Stock sasto che (Support level).
- Price Upper Band ne tade: Stock mocho che (Resistance level).
- Example: Lower Band ₹650 par che and HDFC no bhav ₹648 thayo. Aa ek perfect "DIP" entry point che.
8. Technical Formulas & Logic (ગણતરી કેવી રીતે થાય છે?)
જો તમારે ઊંડાણમાં સમજવું હોય કે કોમ્પ્યુટર આ આંકડા કેવી રીતે લાવે છે:
1. EMA Calculation Logic
EMA માત્ર એવરેજ નથી, પણ તે નવા ભાવને વધુ વજન (Weight) આપે છે.
- Formula:
[Current Price × Multiplier] + [Yesterday's EMA × (1 - Multiplier)]
- Multiplier:
2 / (Period + 1)
- Logic: જો આજે ભાવ અચાનક વધી જાય, તો EMA ઝડપથી ઉપર જશે, જ્યારે સાદી એવરેજ (SMA) ધીમી રહેશે.
2. RSI Calculation Logic
RSI છેલ્લા 14 દિવસના વધારા (Gain) અને ઘટાડા (Loss) ને સરખાવે છે.
- Formula:
100 - [100 / (1 + RS)]
- RS:
Average Gain / Average Loss
- Logic: જો છેલ્લા 14 દિવસમાં સ્ટોક રોજ પડતો જ હોય, તો
Average Loss વધી જશે અને RSI 30 ની નીચે આવી જશે, જે ખરીદીનો સંકેત છે.
3. MACD Calculation Logic
MACD બે 'ટ્રેન્ડ લાઈન' વચ્ચેનું અંતર છે.
- Formula:
12-day EMA - 26-day EMA
- Signal Line: MACD લાઈનની પોતાની 9-દિવસની એવરેજ.
- Logic: જ્યારે MACD લાઈન તેની સિગ્નલ લાઈનને નીચેથી કાપીને ઉપર જાય, ત્યારે સમજવું કે તેજીની શરૂઆત થઈ રહી છે.
4. Bollinger Bands (BB) Logic (એકદમ સાદી ભાષામાં)
Bollinger Bands ને તમે એક "પાઈપ (Pipe)" અથવા "હાઇવેની બે બાજુની લાઈનો" તરીકે વિચારી શકો છો.
આ પાઈપ કેવી રીતે બને છે? તેના 3 ભાગ છે:
- Middle Band (વચ્ચેની લાઈન): આ છેલ્લા 20 દિવસની સાદી એવરેજ (SMA) છે. જો HDFC 20 દિવસથી ₹100 આસપાસ રહેતો હોય, તો વચ્ચેની લાઈન ₹100 બનશે.
- Standard Deviation (SD / σ - હલચલ/Volatility): આ માપે છે કે સ્ટોક રોજ કેટલો કૂદે છે.
- Proper Formula (SD):
√[ ∑(Price - SMA)² / n ] (જ્યાં n = 20 દિવસ, અને SMA = Middle Band)
- Formula નું સાદી ભાષામાં બ્રેકડાઉન:
- (Price - SMA): રોજનો ભાવ વચ્ચેની લાઈનથી કેટલો દૂર હતો તે જુઓ (દા.ત. ૩ રૂપિયા ઉપર યા ૨ રૂપિયા નીચે).
- ² (સ્ક્વેર): માઈનસ વાળા આંકડા પ્લસ કરવા બધાનો વર્ગ (Square) કરો.
- ∑ અને / n: આવા 20 દિવસના આંકડાનું ટોટલ મારીને તેની એવરેજ કાઢો (જેથી એવરેજ હલચલ મળે).
- √ (રૂટ): પહેલા સ્ક્વેર કર્યો હતો તેની અસર કાઢવા ફાઇનલ જવાબનું વર્ગમૂળ કાઢો.
- ટૂંકમાં: આ ફોર્મ્યુલા એક સિંગલ આંકડો આપે છે જે દર્શાવે છે કે "સ્ટોક રોજ એવરેજથી કેટલા રૂપિયાનો જંપ મારે છે." (દા.ત. SD = ₹5)
- 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 છે.
- જ્યારે ભાવ ₹90 આવે (Lower Band): આલ્ગો સમજી જશે કે સ્ટોક હદ કરતા વધારે ઘટી ગયો છે (DIP). આ સૌથી સસ્તો ભાવ છે -> BUY.
- જ્યારે ભાવ ₹110 આવે (Upper Band): આલ્ગો સમજી જશે કે સ્ટોક લિમિટ કરતા વધારે મોંઘો થઈ ગયો છે (PEAK) -> SELL.