Skip to content

Instantly share code, notes, and snippets.

@MilindRCodes
Created June 9, 2016 05:45
Show Gist options
  • Select an option

  • Save MilindRCodes/634bab99dc4d0bee7362f8d802c4f7b8 to your computer and use it in GitHub Desktop.

Select an option

Save MilindRCodes/634bab99dc4d0bee7362f8d802c4f7b8 to your computer and use it in GitHub Desktop.

Revisions

  1. MilindRCodes created this gist Jun 9, 2016.
    241 changes: 241 additions & 0 deletions Shorting at High.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,241 @@
    #####################################################################################
    # Strategy - Shorting at High #
    #####################################################################################

    # Upload the libraries and make other initial settings ##########################################################################################

    library(xlsx);library(zoo);library(quantmod);library(rowr);library(TTR);

    system.time({

    ######################################## Part 1 of the coDe ############################################

    # Set the parameters here ###########################################################################################################################################

    pc_up_level = 2 # Set the high percentage level here
    noDays = 2 # No of lookback days, Google finance can go back upto 15 days for 1-minute data.

    # Read the xlsx file that contains the Stock tickers that are to be backtested ##################################################################################################################################################################

    test_tickers = read.csv("F&O Stock List.csv")
    symbol = as.character(test_tickers[,4])


    leverage = as.character(test_tickers[,5])

    sig_finds = data.frame(0,0,0,0,0,0)
    colnames(sig_finds) = c("Ticker","Leverage","Previous Price","Current Price","Abs Change","% Change")

    for(s in 1:length(symbol)){

    print(s)

    source("Stock price data.R") # Custom function to download stock data
    intraday_price_data(symbol[s],noDays)

    dirPath = paste(getwd(),"/",sep="") # Specify the directory
    fileName = paste(dirPath,symbol[s],".csv",sep="") # Specify the filename
    data = as.data.frame(read.csv(fileName)) # Read the downloaded file

    # When the code is run on the trading day, the last price will point
    # to yesterday's closing price. This price is to be extracted to compute
    # the % change in today's current price.
    data_close =subset(data, select=(CLOSE), subset=(TIME == 1530))
    pdp = tail(data_close,1)[,1]

    # When the code is run on the trading day, the last price will point
    # to the latest price. Use this price to compute the latest percentage
    # change for each stock in the stock list.

    #(Please note: when the code is run during non-trading hours,
    #(both the pdp and the lp will be the same.)
    lp = tail(data,1)[,3]

    # Compute Percentage change for each stock & include in the dataframe only if significant #############################################

    # Compute absolute and the percentage change
    apc = round((lp - pdp),2)
    pc = round(((lp - pdp) / pdp) * 100,2)

    if (pc > pc_up_level ){

    # Create a empty data frame, only if the Percentage change is significant
    # fill the data frame with symbols price changes
    sig_finds_temp = data.frame(0,0,0,0,0,0)

    # Only if the change is significant, the respective stock data will be entered
    # in the data frame being created below.
    colnames(sig_finds_temp) = c("Ticker","Leverage","Previous Price","Current Price","Abs Change","% Change")
    sig_finds_temp = c(symbol[s],leverage[s],pdp,lp,apc,pc)
    sig_finds = rbind(sig_finds, sig_finds_temp) # combines the previous and current data frame
    rm(sig_finds_temp) # deletes the temporary data frame

    print(sig_finds)

    }

    # Deletes the downloaded stock price files for each ticker after processing the data
    unlink(fileName)

    } # Closes the for loop

    # Write the results in an excel sheet
    write.xlsx(sig_finds,"Shorting at High.xlsx")

    ######################################## Part 2 of the code ############################################

    # Compute the different metrics which will be used in evaluating the best stock to Short #################################################

    source("Stock price data.R") # Following lines pull NIFTY data
    daily_price_data("NIFTY",1) # Number of years to lookback into eg. 1 = 1 year

    dirPath = paste(getwd(),"/",sep="")
    fileName = paste(dirPath,"NIFTY",".csv",sep="")

    nifty_data = as.data.frame(read.csv(file = fileName))

    test_tickers = read.xlsx("Shorting at High.xlsx",header=TRUE, 1, startRow=1, as.data.frame=TRUE)
    t = nrow(test_tickers)

    test_tickers$Count = 0
    test_tickers$Avg_high = 0
    test_tickers$Avg_decline = 0
    test_tickers$Next_3d_Change = 0
    test_tickers$Low_1Yr = 0
    test_tickers$High_1Yr = 0
    test_tickers$Neg_Last15_days = 0
    test_tickers$RSI = 0
    test_tickers$NIFTY_Correlation = 0

    symbol = as.character(test_tickers[2:t,2])
    noDays = 1

    for (s in 1:length(symbol)){

    print(s)

    table_sig = data.frame(0,0,0,0,0,0,0)
    colnames(table_sig) = c("Date","Days High in %","Days Close in %","RSI","Entry Price","Exit Price","Profit/Loss")

    source("Stock price data.R")
    daily_price_data(symbol[s],noDays) # Using the built-in function
    dirPath = paste(getwd(),"/",sep="")
    fileName = paste(dirPath,symbol[s],".csv",sep="")
    data = as.data.frame(read.csv(file = fileName)) # Read the file
    N = nrow(data)

    # Initializing variables to zero
    nc = 0
    Cum_hc_diff = 0
    Cum_threshold_per = 0
    Cum_next_3d_change = 0

    diff = data$HIGH - data$CLOSE

    rsi_data = RSI(data$CLOSE, n = 14, maType="WMA", wts=data[,"VOLUME"])

    for (i in 2:N) {

    days_close = (data$CLOSE[i] - data$CLOSE[i-1])*100/data$CLOSE[i-1]
    days_high = (data$HIGH[i] - data$CLOSE[i-1])*100/data$CLOSE[i-1]

    condition = ((days_high > pc_up_level) == TRUE)
    if (condition)
    {
    nc = nc + 1

    date_table = data$DATE[i]

    high_per_table = round(days_high,2)
    close_price_per_table = format(round(days_close,2),nsmall = 2)

    rsi_table = format(round(rsi_data[i],2),nsmall = 2)

    entry_price = format(round(data$CLOSE[i-1]*(1+(pc_up_level/100)),2),nsmall = 2)
    exit_price = format(round(data$CLOSE[i],2),nsmall = 2)
    pl_trade = format(round(((data$CLOSE[i-1]*(1+(pc_up_level/100))) - data$CLOSE[i]),2),nsmall = 2)

    hc_diff = diff[i] # diff between day's high and day's close whenever the price crossed the threshold level.
    Cum_hc_diff = Cum_hc_diff + hc_diff # Cumulative of the diff for all trades
    Average_hc_diff = round(Cum_hc_diff / nc , 2) # Computing the average for diff between high and low.

    Cum_threshold_per = Cum_threshold_per + days_high # Computes the total of Day's high's whenever price crossed the threshold.
    Average_threshold_per = round(Cum_threshold_per / nc , 2) # Average of High's

    if ( i < (N - 5)){ # Captures the average next 3-day price movement
    next_3d_change = data$CLOSE[i+3] - data$CLOSE[i]
    Cum_next_3d_change = Cum_next_3d_change + (data$CLOSE[i+3] - data$CLOSE[i])
    Average_next_3d_change = round(Cum_next_3d_change / nc,2)
    }

    # Create a temporary dataframe to add values and then later merge with the final table
    table_temp = data.frame(0,0,0,0,0,0,0)
    colnames(table_temp) = c("Date","Days High in %","Days Close in %","RSI","Entry Price","Exit Price","Profit/Loss")
    table_temp = c(date_table,high_per_table,close_price_per_table,rsi_table,entry_price,exit_price,pl_trade)
    table_sig = rbind(table_sig, table_temp)
    rm(table_temp)

    }


    }

    table_sig = table_sig[order(table_sig$Date),]

    name = paste(symbol[s]," past performance",".xlsx",sep="")
    write.xlsx(table_sig,name) # Write the results in an excel sheet

    # load it back to format the excel sheet for column width
    wb = loadWorkbook(name)
    sheets = getSheets(wb)
    autoSizeColumn(sheets[[1]], colIndex=1:16)
    saveWorkbook(wb,name)

    # Price performance in the last 15 days, checks for many days the price change was negative.
    nc_last_15 = 0
    for ( p in (N-15):N){

    condition_1 = (((data$CLOSE[p] - data$CLOSE[p-1])< 0 ) == TRUE )
    if (condition_1)
    {
    nc_last_15 = nc_last_15 + 1
    }
    }

    # Correlation between the ticker and NIFTY
    Cor_coeff = round(cor(data$CLOSE , nifty_data$CLOSE),2)

    # Filling the Shorting at High.xlsx with the computed metrics

    e = s + 1
    test_tickers$Count[e] = nc
    test_tickers$Avg_high[e] = format(Average_threshold_per,nsmall = 2) # Indicates the Avg. percentage above the threshold in the past
    test_tickers$Avg_decline[e] = format(Average_hc_diff,nsmall = 2) # Indicates the Avg. decline in Absolute rupee terms from the Avg. high to the closing price in the past test_tickers$Next_3d_Change[e] = round(Average_next_3d_change,2) # Indicates the next 3 day price movement.
    test_tickers$Next_3d_Change[e] = format(Average_next_3d_change,nsmall = 2)
    test_tickers$Low_1Yr[e] = format(min(data$CLOSE),nsmall = 2) # 1 year low price
    test_tickers$High_1Yr[e] = format(max(data$CLOSE),nsmall = 2) # 1 year high price
    test_tickers$Neg_Last15_days[e] = nc_last_15 # Indicates how many times in the last 15 days has the price been negative.
    test_tickers$RSI[e] = round(tail(rsi_data,1),2)
    test_tickers$NIFTY_Correlation[e] = Cor_coeff # Computes the correlation between the Stock and NIFTY.

    unlink(fileName) # Deletes the downloaded stock price files for each ticker after processing the data

    rm("data","table_sig")

    } # This closes the for loop

    test_tickers = test_tickers[-1,-1]
    colnames(test_tickers) = c("Ticker", "Leverage","Previous Price","Current Price","Abs. Change","% Change","Count","Avg.High %","Abs Avg.Decline","Next 3-d price move","1-yr low","1-yr high","-Ve last 15-days","RSI","Nifty correlation")

    # Write and format the final output file ##########################################################################################

    write.xlsx(test_tickers,"Shorting at High.xlsx") # Write the final output back to the same file.

    wb = loadWorkbook("Shorting at High.xlsx")
    sheets = getSheets(wb)
    autoSizeColumn(sheets[[1]], colIndex=1:19)
    saveWorkbook(wb,"Shorting at High.xlsx")

    #options(warn = oldw)

    })