<jupyter_start><jupyter_text>Basic trading- entry: do I use only the data for that stock endogenous, or also include the data from other stocks, exagenous.- entry and exit :- exit : take profit, stop loss, time out<jupyter_code>import pandas as pd
import numpy as np
import datetime as dt
import yfinance as yf  # for data
from pandas_datareader import data as pdr
yf.pdr_override()
def read_data(stock='AAPL',short = 26,long = 60):


    df = pdr.get_data_yahoo(stock,period = "60d",

            # fetch data by interval (including intraday if period < 60 days)
            # valid intervals: 1m,2m,5m,15m,30m,60m,90m,1h,1d,5d,1wk,1mo,3mo
            # (optional, default is '1d')
            interval = "2m",)


    emasUsed = [short,long]
    for ema in emasUsed:
        df['Ema_' + str(ema)] = round(df['Adj Close'].ewm(span = ema, adjust = False).mean(),2)
    df['Middle Band'] =df['Adj Close'].rolling(window=20).mean()
    df['Upper Band'] = df['Middle Band'] + 1.96*df['Close'].rolling(window=20).std()
    df['Lower Band'] = df['Middle Band'] - 1.96*df['Close'].rolling(window=20).std()
    df['status_lower'] = np.where(df['Close'] < df['Lower Band'],'below_ballinger','normal')
    df['status_upper'] = np.where(df['Close'] > df['Upper Band'],'above_ballinger','normal')
    
    return df
def print_results(percentchange):
    gains = 0
    ng = 0
    losses = 0
    nl = 0
    totallR = 1
    for i in percentchange:
        if i >0:
            gains +=i
            ng +=1
        else:
            losses +=i
            nl +=1
        totallR = totallR*((i/100)+1)
    totallR = round((totallR-1)*100)
    if ng > 0:
        avgGain = gains/ng
        maxR = str(max(percentchange))
    else:
        avgGain = 0
        maxR = 'undefined'

    if nl>0:
        avgLoss = losses/nl
        maxL = str(min(percentchange))
        ratio = str(-(avgGain/avgLoss))
    else:
        avgLoss = 0
        maxL = 'undefined'
        ratio = 'inf'

    if ng >0 and nl >0:
        bettingAvg = ng/(ng+nl)
    else:
        bettingAvg = 0


    print()
    print('Result for'+stock+"going back to "+str(df.index[0])+" Sample size: "+str(ng+nl)+"trades")
    #print('EMAs used : ',str(emasUsed))
    print('Batting Avg : '+str(bettingAvg))
    print('Gain/loss ratio: ' + ratio)
    print('Avg gain: ' + str(avgGain))
    print('Avg loss: '+ str(avgLoss))
    print('Max return: '+ str(maxR))
    print('Max loss: ' + str(maxL))
    print('Total return over '+str(ng+nl)+' trades: '+str(totallR)+'%')
    print()


stock = input('Enter stock tikcer symbol:')

short = int(input('Enter short moving avg'))
long = int(input('Enter long term movin avg'))


short_moving_avg = 'Ema_'+ str(short)
long_moving_avg = 'Ema_'+ str(long)
#### 1. STRATEGY CROSSING



strategies = [1,2,3,4]


for strategy in strategies:



    if strategy == 1:
        df = read_data(stock, short,long)
        pos = 0
        num = 0
        percentchange = []
        start_of_this_month = int((len(df.index[:])/60)*30)

        for i in df.index[start_of_this_month:]:

            cmin = df[short_moving_avg][i]
            cmax = df[long_moving_avg][i]
            close = df['Adj Close'][i]
            #if(status_lower=='below_ballinger'):
           # if (cmin>cmax) and (status_lower=='below_ballinger'):
            if (cmin>cmax):
               # print('red white blue')
                if pos ==0:
                    bp =close
                    pos=1
                    print('Buying now at'+ str(bp))
                    #print(i)


            elif(cmin<cmax):
                #print('blue white red')
                if pos ==1:
                    pos = 0
                    sp = close
                    print('Selling now at'+ str(sp))
                    #print(i)
                    pc = (sp/bp-1)*100
                    percentchange.append(pc)

            if num ==df['Adj Close'].count()-1 and pos==1:
                pos = 0
                sp = close
                print('Selling now at'+ str(sp))
                pc = (sp/bp-1)*100
                percentchange.append(pc)      
            num +=1
        print_results(percentchange)
    
    if strategy == 2:
        
        df = read_data(stock, short,long)
        pos = 0
        num = 0
        percentchange = []

        for i in df.index:
            cmin = df[short_moving_avg][i]
            cmax = df[long_moving_avg][i]
            status_upper = df['status_upper'][i]
            status_lower = df['status_lower'][i]


            close = df['Adj Close'][i]

            if(status_lower=='below_ballinger'):
               # print('red white blue')
                if pos ==0:
                    bp =close
                    pos=1
                    #print('Buying now at'+ str(bp))
                    #print(i)


            elif(status_upper=='upper_ballinger'):
                #print('blue white red')
                if pos ==1:
                    pos = 0
                    sp = close
                    #print('Selling now at'+ str(sp))
                    #print(i)
                    pc = (sp/bp-1)*100
                    percentchange.append(pc)

            if num ==df['Adj Close'].count()-1 and pos==1:
                pos = 0
                sp = close
                #print('Selling now at'+ str(sp))
                pc = (sp/bp-1)*100
                percentchange.append(pc)      
            num +=1
        print_results(percentchange)

    elif strategy == 3:
        df = read_data(stock, short,long)
        pos = 0
        num = 0
        percentchange = []

        for i in df.index:
            cmin = df[short_moving_avg][i]
            cmax = df[long_moving_avg][i]
            status_upper = df['status_upper'][i]

            close = df['Adj Close'][i]

            #if(status_lower=='below_ballinger'):
           # if (cmin>cmax) and (status_lower=='below_ballinger'):
            if (cmin>cmax):
               # print('red white blue')
                if pos ==0:
                    bp =close
                    pos=1
                    #print('Buying now at'+ str(bp))
                    #print(i)


            elif(status_upper=='upper_ballinger'):
                #print('blue white red')
                if pos ==1:
                    pos = 0
                    sp = close
                    #print('Selling now at'+ str(sp))
                    #print(i)
                    pc = (sp/bp-1)*100
                    percentchange.append(pc)

            if num ==df['Adj Close'].count()-1 and pos==1:
                pos = 0
                sp = close
                #print('Selling now at'+ str(sp))
                pc = (sp/bp-1)*100
                percentchange.append(pc)      
            num +=1
        print_results(percentchange)

        
    elif strategy == 4:
        df = read_data(stock, short,long)
        pos = 0
        num = 0
        percentchange = []

        for i in df.index:
            cmin = df[short_moving_avg][i]
            cmax = df[long_moving_avg][i]
            status_lower = df['status_lower'][i]

            close = df['Adj Close'][i]

            if(status_lower=='below_ballinger'):
           # if (cmin>cmax) and (status_lower=='below_ballinger'):
               # print('red white blue')
                if pos ==0:
                    bp =close
                    pos=1
                    #print('Buying now at'+ str(bp))
                    #print(i)


            elif(cmin<cmax):
                #print('blue white red')
                if pos ==1:
                    pos = 0
                    sp = close
                    #print('Selling now at'+ str(sp))
                    #print(i)
                    pc = (sp/bp-1)*100
                    percentchange.append(pc)

            if num ==df['Adj Close'].count()-1 and pos==1:
                pos = 0
                sp = close
                print('Selling now at'+ str(sp))
                pc = (sp/bp-1)*100
                percentchange.append(pc)      
            num +=1
        print_results(percentchange)


    #print(percentchange)



## print results<jupyter_output>Enter stock tikcer symbol: TSLA
Enter short moving avg 26
Enter long term movin avg 50
{'fraction_non_alphanumeric': 0.12807102894672828, 'fraction_numerical': 0.018608611043541716, 'mean_word_length': 1.4903089036947306, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '618357', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 2694, 'n_tokens_neox': 2452, 'n_words': 605, 'path': 'per_stock_all_strategies/test_all_strategies_per_stock.ipynb', 'repo_name': 'ajmal017/stock_price_prediction', 'size': '8222'}