एल्गोरिदमिक ट्रेडिंग (algorithmic trading) Logo Light Mode

Kotak

Stockshaala

Module 4
कोडिंग और डिप्लॉयिंग एल्गोस (coding and deploying algos)
Course Index
Read in
English
हिंदी

Chapter 4 | 3 min read

पायथन में एक सैंपल एल्गो ट्रेडिंग स्ट्रेटेजी (A Sample Algo Trading Strategy in Python)

चलो एक सिंपल इंट्राडे स्ट्रेटेजी (simple intraday strategy) बनाते हैं:
खरीदें जब आरएसआई (RSI) < 30 और क्लोज (Close) > 20 ईएमए (EMA)
बेचें जब आरएसआई (RSI) > 70 और क्लोज (Close) < 20 ईएमए (EMA)

हम इस्तेमाल करेंगे:

  • yfinance डेटा के लिए
  • ta इंडिकेटर्स के लिए
  • पायथन लॉजिक स्ट्रेटेजी के लिए

यह सिर्फ लर्निंग/डेमो के लिए है। अभी लाइव ट्रेडिंग के लिए नहीं!

1. जरूरी लाइब्रेरीज इंस्टॉल करें (सिर्फ एक बार):
आप इसे गूगल कोलाब या अपने लोकल पायथन सेटअप में रन कर सकते हैं।

!pip install yfinance ta

नोट:

यह कमांड दो पायथन लाइब्रेरीज इंस्टॉल करता है जो मार्केट डेटा एनालिसिस और टेक्निकल इंडिकेटर कैलकुलेशन के लिए जरूरी हैं:

  • yfinance – जो याहू फाइनेंस से डायरेक्ट पायथन में हिस्टोरिकल और रियल-टाइम मार्केट डेटा लाने के लिए उपयोग होता है।

  • ta – एक टेक्निकल एनालिसिस लाइब्रेरी जो आरएसआई, ईएमए, एमएसीडी, बोलिंजर बैंड्स आदि जैसे इंडिकेटर्स की गणना के लिए रेडी-टू-यूज़ फंक्शन्स प्रदान करती है।

शुरुआत में ! इसे शेल कमांड के रूप में जुपिटर नोटबुक या गूगल कोलाब के अंदर रन करने की अनुमति देता है, ताकि पैकेजेस आपके करंट एनवायरनमेंट में इंस्टॉल हो जाएं।

2. लाइब्रेरीज इम्पोर्ट करें

import yfinance as yf
import ta
import pandas as pd

नोट:

यह कोड एल्गो ट्रेडिंग के लिए तीन आवश्यक पायथन लाइब्रेरीज इम्पोर्ट करता है:

  • yfinance (yf) याहू फाइनेंस से हिस्टोरिकल और रियल-टाइम मार्केट डेटा लाने के लिए।

  • ta टेक्निकल इंडिकेटर्स जैसे कि आरएसआई, ईएमए, एमएसीडी, बोलिंजर बैंड्स आदि की गणना के लिए।

  • pandas (pd) टाइम-सीरीज़ मार्केट डेटा को डेटा फ्रेम्स में स्टोर, प्रोसेस और एनालाइज करने के लिए।

ये सभी मिलकर आपको प्राइस डेटा डाउनलोड करने, टेक्निकल इंडिकेटर्स की गणना करने और स्ट्रेटेजी बिल्डिंग और बैकटेस्टिंग के लिए परिणामस्वरूप डेटा सेट को मैनीपुलेट करने की अनुमति देते हैं।

3. इंट्राडे प्राइस डेटा प्राप्त करें

data = yf.download("RELIANCE.NS", period="5d", interval="15m")
data.dropna(inplace=True)

यह आपको पिछले 5 दिनों के लिए रिलायंस के 15-मिनट कैंडल्स देता है। दिए गए सिक्योरिटीज एक उदाहरण हैं और सिफारिश नहीं हैं

4. आरएसआई और 20 ईएमए की गणना करें

data['rsi'] = ta.momentum.RSIIndicator(close=data['Close']).rsi()
data['ema20'] = ta.trend.EMAIndicator(close=data['Close'], 
window=20).ema_indicator()

नोट:

  1. ta.momentum.RSIIndicator: ta (टेक्निकल एनालिसिस) लाइब्रेरी से एक रिलेटिव स्ट्रेंथ इंडेक्स (आरएसआई) इंडिकेटर ऑब्जेक्ट बनाता है।

  2. close=data['Close']: आपके डेटा डेटा फ्रेम से 'क्लोज' प्राइस कॉलम का उपयोग आरएसआई की गणना के लिए करता है।

  3. .rsi(): प्रत्येक रो के लिए आरएसआई वैल्यूज की गणना करता है (डिफॉल्ट पीरियड आमतौर पर 14 होता है जब तक कि विशेष रूप से उल्लेखित न हो) और एक पैंडास सीरीज लौटाता है।

  4. data['rsi'] = ...: आपके डेटा फ्रेम में एक नए कॉलम 'rsi' में परिणामी आरएसआई वैल्यूज स्टोर करता है।

  5. ta.trend.EMAIndicator: एक्सपोनेंशियल मूविंग एवरेज (ईएमए) इंडिकेटर ऑब्जेक्ट बनाता है।

  6. close=data['Close']: फिर से, गणना के लिए डेटा स्रोत के रूप में 'क्लोज' प्राइस का उपयोग करता है।

  7. window=20: ईएमए लुकबैक पीरियड को 20 बार (जैसे, दैनिक डेटा के लिए 20 दिन) सेट करता है।

  8. .ema_indicator(): दिए गए पीरियड के लिए ईएमए वैल्यूज की गणना करता है।

  9. data['ema20'] = ...: आपके डेटा फ्रेम में एक नए कॉलम 'ema20' में गणना की गई ईएमए वैल्यूज सहेजता है।

बूम — आपके डेटा फ्रेम में इंडिकेटर्स जोड़ दिए गए।

5. खरीद/बिक्री लॉजिक को परिभाषित करें

def generate_signal(row):
    if row['rsi'] < 30 and row['Close'] > row['ema20']:
        return "BUY"
    elif row['rsi'] > 70 and row['Close'] < row['ema20']:
        return "SELL"
    else:
        return "HOLD"

data['signal'] = data.apply(generate_signal, axis=1)

यह फंक्शन प्रत्येक रो (यानी, प्रत्येक 15-मिनट कैंडल) को चेक करता है और एक सिग्नल असाइन करता है।

6. परिणाम देखें

print(data[['Close', 'rsi', 'ema20', 'signal']].tail(10))

नोट:

यह लाइन डेटा डेटा फ्रेम के चयनित कॉलम्स की अंतिम 10 रो को प्रिंट करती है:

  • 'क्लोज' – प्रत्येक पीरियड के लिए एसेट का क्लोजिंग प्राइस।

  • 'आरएसआई' – पहले से गणना किए गए रिलेटिव स्ट्रेंथ इंडेक्स वैल्यूज।

  • 'ईएमए20' – 20-पीरियड एक्सपोनेंशियल मूविंग एवरेज वैल्यूज।

  • 'सिग्नल' – एक ट्रेडिंग सिग्नल कॉलम (संभावित रूप से आपके स्ट्रेटेजी लॉजिक द्वारा उत्पन्न)।

.tail(10) फंक्शन आउटपुट को सबसे हालिया 10 रो तक सीमित करता है, जिससे नवीनतम मार्केट डेटा, इंडिकेटर्स और उत्पन्न ट्रेड सिग्नल्स की समीक्षा करना आसान हो जाता है, बिना पूरे डेटा सेट को प्रिंट किए।

आपको ऐसा आउटपुट दिखाई देगा:

यह आपको बैकटेस्ट करने में मदद करता है अगर लॉजिक नेत्रहीन समझ में आता है।

  • वास्तविक डेटा खींचा
  • इंडिकेटर्स की गणना की
  • खरीद/बिक्री सिग्नल्स की जाँच की
  • आपको एक रेडी "सिग्नल" कॉलम दिया

आप अब इस स्ट्रेटेजी में क्रमशः ऑर्डर निष्पादन, लॉगिंग और अलर्ट्स जोड़ सकते हैं।

क्या आप देखना चाहते हैं कितने खरीद/बिक्री सिग्नल्स आए?

print(data['signal'].value_counts())

This content has been translated using a translation tool. We strive for accuracy; however, the translation may not fully capture the nuances or context of the original text. If there are discrepancies or errors, they are unintended, and we recommend original language content for accuracy.

Is this chapter helpful?
Previous
कोडेड एल्गो ट्रेडिंग के लिए उपकरण (Tools for Coded Algo Trading)
Next
अपने एल्गो को लाइव ट्रेडिंग के लिए डिप्लॉय (Deploying Your Algo for Live Trading) करना

Disclaimer: This article is for informational purposes only and does not constitute financial advice. It is not produced by the desk of the Kotak Neo Research Team, nor is it a report published by the Kotak Neo Research Team. The information presented is compiled from several secondary sources available on the internet and may change over time. Investors should conduct their own research and consult with financial professionals before making any investment decisions. Read the full disclaimer here.

Investments in securities market are subject to market risks, read all the related documents carefully before investing. Brokerage will not exceed SEBI prescribed limit. The securities are quoted as an example and not as a recommendation. SEBI Registration No-INZ000200137 Member Id NSE-08081; BSE-673; MSE-1024, MCX-56285, NCDEX-1262.

Discover our extensive knowledge center

Explore our comprehensive video library that blends expert market insights with Kotak's innovative financial solutions to support your goals.

PreviousCourse IndexNext