strike project django utlis

 import pytz

import json

import os

from datetime import datetime, timedelta

from multistrikeoi.get_nearest_strike import UserSelectedPrice as usp

from django.db import connection

import pandas as pd

from rest_framework.request import Request

from rest_framework.response import Response


from rest_framework import status

today_string=None

yesterday_string=None

tomorrow_string=None


def detect_day(today):

    global today_string, yesterday_string, tomorrow_string

    date_string = today

    parsed_date = datetime.strptime(date_string, "%Y-%m-%d")

    day_of_week = parsed_date.weekday()

    day_of_week_string = parsed_date.strftime("%A")

    # print(day_of_week)

    # print(day_of_week_string)


    date_string = today

    parsed_date = datetime.strptime(date_string, "%Y-%m-%d")


    yesterday = parsed_date - timedelta(days=1)

    yesterday_string = yesterday.strftime("%Y-%m-%d")

    # print("Yesterday's date:", yesterday_string)


    if day_of_week == 0:

        yesterday = parsed_date - timedelta(days=3)

        yesterday_string = yesterday.strftime("%Y-%m-%d")

        today_string = today

        tomorrow_string = parsed_date + timedelta(days=1)

        tomorrow_string = tomorrow_string.strftime("%Y-%m-%d")


    if day_of_week == 1:

        yesterday = parsed_date - timedelta(days=1)

        yesterday_string = yesterday.strftime("%Y-%m-%d")

        today_string = today

        tomorrow_string = parsed_date + timedelta(days=1)

        tomorrow_string = tomorrow_string.strftime("%Y-%m-%d")


    if day_of_week == 2:

        yesterday = parsed_date - timedelta(days=1)

        yesterday_string = yesterday.strftime("%Y-%m-%d")

        today_string = today

        tomorrow_string = parsed_date + timedelta(days=1)

        tomorrow_string = tomorrow_string.strftime("%Y-%m-%d")


    if day_of_week == 3:

        yesterday = parsed_date - timedelta(days=1)

        yesterday_string = yesterday.strftime("%Y-%m-%d")

        today_string = today

        tomorrow_string = parsed_date + timedelta(days=1)

        tomorrow_string = tomorrow_string.strftime("%Y-%m-%d")


    if day_of_week == 4:

        yesterday = parsed_date - timedelta(days=1)

        yesterday_string = yesterday.strftime("%Y-%m-%d")

        today_string = today

        tomorrow_string = parsed_date + timedelta(days=1)

        tomorrow_string = tomorrow_string.strftime("%Y-%m-%d")


    if day_of_week == 5:

        yesterday = parsed_date - timedelta(days=2)

        yesterday_string = yesterday.strftime("%Y-%m-%d")

        today_string = today

        today_string = datetime.strptime(today_string, "%Y-%m-%d")

        today_string = today_string - timedelta(days=1)

        today_string = today_string.strftime("%Y-%m-%d")

        tomorrow_string = parsed_date + timedelta(days=1)

        tomorrow_string = tomorrow_string.strftime("%Y-%m-%d")


    if day_of_week == 6:

        yesterday = parsed_date - timedelta(days=3)

        yesterday_string = yesterday.strftime("%Y-%m-%d")

        today_string = today

        today_string = datetime.strptime(today_string, "%Y-%m-%d")

        today_string = today_string - timedelta(days=2)

        today_string = today_string.strftime("%Y-%m-%d")

        tomorrow_string = parsed_date + timedelta(days=1)

        tomorrow_string = tomorrow_string.strftime("%Y-%m-%d")



class utils:




    def round_to_nearest_5_minutes(ist_time):

        # Calculate the remainder of minutes when divided by 5

        minutes_remainder = ist_time.minute % 5


        # Subtract the remainder from the current minutes to round to the nearest 5 minutes

        rounded_ist_time = ist_time - timedelta(minutes=minutes_remainder)


        # Set seconds to 00

        rounded_ist_time = rounded_ist_time.replace(second=0)


        # Format the time with the desired format using str.format()

        formatted_time = "{}-{:02d}-{:02d} {}:{}:{}".format(

            rounded_ist_time.year,

            rounded_ist_time.month,

            rounded_ist_time.day,

            rounded_ist_time.hour,

            rounded_ist_time.minute,

            rounded_ist_time.second,

        )

        return formatted_time


    def convert_utc_to_ist(utc_time):

        utc_time_str = utc_time.strftime("%Y-%m-%d %H:%M:%S")

        utc_time = datetime.strptime(utc_time_str, "%Y-%m-%d %H:%M:%S")

        utc_time = pytz.utc.localize(utc_time)

        ist_time = utc_time.astimezone(pytz.timezone("Asia/Kolkata"))

        return ist_time.strftime("%Y-%m-%d %H:%M:%S")


    def custom_round(prices, symbol):

        rounded_prices = []


        for price in prices:

            if symbol == "NIFTY":

                last_two_digits = price % 100

                if last_two_digits < 25:

                    rounded_price = (price // 100) * 100

                elif last_two_digits < 75:

                    rounded_price = (price // 100) * 100 + 50

                else:

                    rounded_price = (price // 100 + 1) * 100

            elif symbol == "BANKNIFTY":

                last_two_digits = price % 100

                if last_two_digits < 50:

                    rounded_price = (price // 100) * 100

                else:

                    rounded_price = (price // 100 + 1) * 100

            else:

                rounded_price = price  # Handle the "else" case


            rounded_prices.append(rounded_price)


        return rounded_prices[0]


    def User_Selected_Strike(symbol, instrument, strikeprice, option_type, expiery,start_date):


        global today_string, yesterday_string, tomorrow_string


        detect_day(today=start_date)


        df_selected_strike = usp.combined_oi_calculation(

            symbol=symbol,

            instrument=instrument,

            strikeprice=strikeprice,

            option_type=option_type,

            expiery=expiery,

            today=today_string,

            tomorrow=tomorrow_string


        )

        df_eod_data = usp.get_historical_eod_data(

            symbol=symbol,

            instrument=instrument,

            strikeprice=strikeprice,

            option_type=option_type,

            expiery=expiery,

            yesterday_string=yesterday_string

        )



        df_eod_data['expiry_date'] = df_eod_data['expiry_date'].apply(lambda x: x.strftime('%Y-%m-%d'))


        merged_df = df_selected_strike.merge(

            df_eod_data[["expiry_date", "open_interest", "Combined OI EOD"]],

            on="expiry_date",

            how="left",

        )

        merged_df.rename(

            columns={"open_interest": "Yesterday Eod OI"}, inplace=True

        )

        merged_df["Change In Oi"] = (

                merged_df["open_interest_x"] - merged_df["Yesterday Eod OI"]

        )

        merged_df["Change In Combined Oi"] = (

                merged_df["Combined OI"] - merged_df["Combined OI EOD"]

        )


        merged_df_json = merged_df.to_json(orient="records", date_format="iso")



        return merged_df_json


    # return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


    def User_Selected_Strike_df(symbol, instrument, strikeprice, option_type, expiery,start_date):


        global today_string, yesterday_string, tomorrow_string


        detect_day(today=start_date)


        df_selected_strike = usp.combined_oi_calculation(

            symbol=symbol,

            instrument=instrument,

            strikeprice=strikeprice,

            option_type=option_type,

            expiery=expiery,

            today=today_string,

            tomorrow=tomorrow_string


        )

        df_eod_data = usp.get_historical_eod_data(

            symbol=symbol,

            instrument=instrument,

            strikeprice=strikeprice,

            option_type=option_type,

            expiery=expiery,

            yesterday_string=yesterday_string

        )



        df_eod_data['expiry_date'] = df_eod_data['expiry_date'].apply(lambda x: x.strftime('%Y-%m-%d'))


        merged_df = df_selected_strike.merge(

            df_eod_data[["expiry_date", "open_interest", "Combined OI EOD"]],

            on="expiry_date",

            how="left",

        )

        merged_df.rename(

            columns={"open_interest": "Yesterday Eod OI"}, inplace=True

        )

        merged_df["Change In Oi"] = (

                merged_df["open_interest_x"] - merged_df["Yesterday Eod OI"]

        )

        merged_df["Change In Combined Oi"] = (

                merged_df["Combined OI"] - merged_df["Combined OI EOD"]

        )


        # merged_df_json = merged_df.to_json(orient="records", date_format="iso")



        return merged_df


Comments

Popular posts from this blog

MQL5 : Add time to current time in mins

MQL5: Closed order detail