0-Bollinger-Symbol

Wed 12 November 2025
# Created: 20250104
import pyutil as pyu
pyu.get_local_pyinfo()
'conda env: py311; pyv: 3.11.9 (main, Apr 19 2024, 16:48:06) [GCC 11.2.0]'
print(pyu.ps2("requests"))
requests==2.32.3
import yfinance as yf
import pandas as pd
import matplotlib.pyplot as plt
# Step 1: Download data
start = "1976-01-01"
end = "2013-12-31"
symbol = "^GSPC"  # S&P 500

# Download historical data
data = yf.download(symbol, start=start, end=end)

# Step 2: Define a function to calculate Bollinger Bands
def bollinger_bands(price, window=20, num_sd=2):
    rolling_mean = price['Close'].rolling(window=window).mean()
    rolling_std = price['Close'].rolling(window=window).std()

    price['Upper Band'] = rolling_mean + (rolling_std * num_sd)
    price['Lower Band'] = rolling_mean - (rolling_std * num_sd)
    price['Moving Average'] = rolling_mean

    return price

# Apply the function
data = bollinger_bands(data)

# Step 3: Plot the data with Bollinger Bands
plt.figure(figsize=(12, 6))
plt.plot(data['Close'], label='Close Price', color='blue')
plt.plot(data['Upper Band'], label='Upper Band', color='red', linestyle='--')
plt.plot(data['Lower Band'], label='Lower Band', color='green', linestyle='--')
plt.plot(data['Moving Average'], label='Moving Average', color='orange')
plt.fill_between(data.index, data['Lower Band'], data['Upper Band'], color='gray', alpha=0.2)
plt.title(f'Bollinger Bands for {symbol}')
plt.legend(loc='best')
plt.show()
[*********************100%***********************]  1 of 1 completed

png

def show_bollinger_bands(symbol):

    # Step 1: Download data
    start = "1976-01-01"
    end = "2013-12-31"

    # Download historical data
    data = yf.download(symbol, start=start, end=end)

    # Step 2: Define a function to calculate Bollinger Bands
    def bollinger_bands(price, window=20, num_sd=2):
        rolling_mean = price['Close'].rolling(window=window).mean()
        rolling_std = price['Close'].rolling(window=window).std()

        price['Upper Band'] = rolling_mean + (rolling_std * num_sd)
        price['Lower Band'] = rolling_mean - (rolling_std * num_sd)
        price['Moving Average'] = rolling_mean

        return price

    # Apply the function
    data = bollinger_bands(data)

    # Step 3: Plot the data with Bollinger Bands
    plt.figure(figsize=(12, 6))
    plt.plot(data['Close'], label='Close Price', color='blue')
    plt.plot(data['Upper Band'], label='Upper Band', color='red', linestyle='--')
    plt.plot(data['Lower Band'], label='Lower Band', color='green', linestyle='--')
    plt.plot(data['Moving Average'], label='Moving Average', color='orange')
    plt.fill_between(data.index, data['Lower Band'], data['Upper Band'], color='gray', alpha=0.2)
    plt.title(f'Bollinger Bands for {symbol}')
    plt.legend(loc='best')
    plt.show()
show_bollinger_bands("ALLE")
[*********************100%***********************]  1 of 1 completed

png

show_bollinger_bands("AIZ")
[*********************100%***********************]  1 of 1 completed

png

show_bollinger_bands("ADSK")
[*********************100%***********************]  1 of 1 completed

png

show_bollinger_bands("AMZN")
[*********************100%***********************]  1 of 1 completed

png



Score: 10

Category: stockmarket


Access-Element

Wed 12 November 2025


def access_list_element(lst, index):
    try:
        print("Element:", lst[index])
    except IndexError:
        print("Error: Index out of range.")
    except TypeError:
        print("Error: Invalid index type.")
# Example usage
access_list_element([1, 2, 3], 1)
Element: 2
access_list_element([1, 2, 3], 5)
Error: Index out of range.
access_list_element([1, 2, 3], "a")
Error: Invalid …

Category: python-basics

Read More

Add-A-New-Column

Wed 12 November 2025

Add a New Column

import pandas as pd
data = {
    'city' : ['Toronto', 'Montreal', 'Waterloo'],
    'points' : [80, 70, 90]
}
data
{'city': ['Toronto', 'Montreal', 'Waterloo'], 'points': [80, 70, 90]}
type(data)
dict
df = pd.DataFrame(data)
df

Category: pandas-work

Read More

Advanced-For-Loop

Wed 12 November 2025
#  created : 20250125
#  https://www.scientecheasy.com/2022/10/for-loop-in-python.html/
num = int(input('Enter a number: '))
even = 0
odd = 0
for x in range(num):
    if x % 2 == 0:
        even = even + x
    else:
        odd = odd + x
print('Sum of even numbers = ',even)
print('Sum of odd numbers = ',odd)
Enter a …

Category: python-basics

Read More

Advanced-While-Loop

Wed 12 November 2025
#  created : 20250125
#  https://www.scientecheasy.com/2022/10/while-loop-in-python.html/
num = int(input('Enter a number: '))
sum = 0
remainder = 0
while num != 0:
    remainder = num % 10
    sum = sum + remainder
    num = int(num / 10)
print('Sum of all digits in number = ', sum)
Enter a number:  5


Sum of all digits in …

Category: python-basics

Read More

Age-Calculator

Wed 12 November 2025
from datetime import datetime
def get_age(d):
    d1 = datetime.now()
    months = (d1.year - d.year) * 12 + d1.month - d.month

    year = int(months / 12)
    return year
age = get_age(datetime(1991, 1, 1))
age
33


Score: 5

Category: basics

Read More

Arithmatic-Operators

Wed 12 November 2025
#  created : 20250113
#  https://www.scientecheasy.com/2022/10/operators-in-python.html/
#  python operators 
#  arithmetic operators
x = 20
y = 30
sum = x + y 
print("Sum: ", sum) 
Sum:  50
#  Type Conversion in Addition Calculation
x = 20 # an integer value.
y = 30.60 # a float value.
sum = x + y # Adding two numbers with + operator …

Category: python-basics

Read More

Armstrong-Num

Wed 12 November 2025

def is_armstrong(num):
    temp = num
    sum_of_cubes = 0
    while temp > 0:
        digit = temp % 10
        sum_of_cubes += digit ** 3
        temp //= 10
    if sum_of_cubes == num:
        print(num, "is an Armstrong number.")
    else:
        print(num, "is not an Armstrong number.")
is_armstrong(153)
153 is an Armstrong number.
is_armstrong(123)
123 is not an Armstrong number …

Category: python-basics

Read More

Assignment-Operators

Wed 12 November 2025
#  created : 20250113
#  https://www.scientecheasy.com/2022/10/assignment-operators-in-python.html/
# Assignment Operators in Python
# Simple Assignment
x = 20
y = 50
z = x + y + 2
print("Result = ", z)
Result =  72
# x += y  It is equivalent to x = x + y.
x = 20
y = 5
z = 10
x += y
print("Result of (x …

Category: python-basics

Read More
Page 1 of 10

Next »