Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function yahooData(code,percent,flat,C1,C2,C3,C4) {
- var url='https://finance.yahoo.com/quote/'+code+'/key-statistics?p='+code
- var source = UrlFetchApp.fetch(url).getContentText()
- var jsonString = source.match(/(?<=root.App.main = ).*(?=}}}})/g) + '}}}}'
- var data = JSON.parse(jsonString)
- var result = []
- // Get the trailing PE
- let TPEe = 0
- let TPE1 = 0
- let TPE2 = data.context.dispatcher.stores.QuoteSummaryStore.summaryDetail.trailingPE.fmt
- try{TPE1 = data.context.dispatcher.stores.QuoteTimeSeriesStore.timeSeries.trailingPeRatio[0].reportedValue.fmt}
- catch(e) {TPE1 = 10000;TPEe = TPEe+1}
- if (Math.abs(TPE1-TPE2)<(TPE2/percent+flat)) {result.push(TPE1)}
- else {
- try{TPE1 = data.context.dispatcher.stores.QuoteTimeSeriesStore.timeSeries.trailingPeRatio[1].reportedValue.fmt}
- catch(e) {TPE1 = 10000}
- if (Math.abs(TPE1-TPE2)<(TPE2/percent+flat)) {result.push(TPE1)}
- else if (C1 == 1) {result.push(TPE2);TPEe = TPEe+1}
- else {result.push('');TPEe = TPEe+1}
- }
- // Get the forward PE
- let FPEe = 0
- let FPE1 = 0
- let FPE2 = data.context.dispatcher.stores.QuoteSummaryStore.summaryDetail.forwardPE.fmt
- try{FPE1 = data.context.dispatcher.stores.QuoteTimeSeriesStore.timeSeries.trailingForwardPeRatio[0].reportedValue.fmt}
- catch(e){FPE1 = 10000;FPEe = FPEe+1}
- if (Math.abs(FPE1-FPE2)<(FPE2/percent+flat)) {result.push(FPE1)}
- else {
- try{FPE1 = data.context.dispatcher.stores.QuoteTimeSeriesStore.timeSeries.trailingForwardPeRatio[1].reportedValue.fmt}
- catch(e) {FPE1 = 10000}
- if (Math.abs(FPE1-FPE2)<(FPE2/percent+flat)) {result.push(FPE1)}
- else if (C2 == 1) {result.push(FPE2);FPEe = FPEe+1}
- else {result.push('');FPEe = FPEe+1}
- }
- // Get the PEG Ratio
- let PEGe = 0
- let PEG1 = 0
- let PEG2 = data.context.dispatcher.stores.QuoteSummaryStore.defaultKeyStatistics.pegRatio.fmt
- try{PEG1 = data.context.dispatcher.stores.QuoteTimeSeriesStore.timeSeries.trailingPegRatio[0].reportedValue.fmt}
- catch(e){PEG1 = 10000;PEGe = PEGe+1}
- if (Math.abs(PEG1-PEG2)<(PEG2/percent+flat*2)) {result.push(PEG1)}
- else {
- try{PEG1 = data.context.dispatcher.stores.QuoteTimeSeriesStore.timeSeries.trailingPegRatio[1].reportedValue.fmt}
- catch(e) {PEG1 = 10000}
- if (Math.abs(PEG1-PEG2)<(PEG2/percent+flat*2)) {result.push(PEG1)}
- else if (C3 == 1) {result.push(PEG2);PEGe = PEGe+1}
- else {result.push('');PEGe = PEGe+1}
- }
- // Get the P/B Ratio
- let PBe = 0
- let PB1 = 0
- let PB2 = data.context.dispatcher.stores.QuoteSummaryStore.defaultKeyStatistics.priceToBook.fmt
- try{PB1 = data.context.dispatcher.stores.QuoteTimeSeriesStore.timeSeries.trailingPbRatio[0].reportedValue.fmt}
- catch(e){PB1 = 10000;PBe = PBe+1}
- if (Math.abs(PB1-PB2)<(PB2/percent+flat)) {result.push(PB1)}
- else {
- try{PB1 = data.context.dispatcher.stores.QuoteTimeSeriesStore.timeSeries.trailingPbRatio[1].reportedValue.fmt}
- catch(e) {PB1 = 10000}
- if (Math.abs(PB1-PB2)<(PB2/percent+flat)) {result.push(PB1)}
- else if (C4 == 1) {result.push(PB2);PBe = PBe+1}
- else {result.push('');PBe = PBe+1}
- }
- // Get the price values
- try{result.push(data.context.dispatcher.stores.QuoteSummaryStore.financialData.currentPrice.fmt)}
- catch(e){result.push('not found')}
- try{result.push(data.context.dispatcher.stores.QuoteSummaryStore.financialData.targetLowPrice.fmt)}
- catch(e){result.push('not found')}
- try{result.push(data.context.dispatcher.stores.QuoteSummaryStore.financialData.targetMeanPrice.fmt)}
- catch(e){result.push('not found')}
- try{result.push(data.context.dispatcher.stores.QuoteSummaryStore.financialData.targetHighPrice.fmt)}
- catch(e){result.push('not found')}
- try{result.push(data.context.dispatcher.stores.QuoteSummaryStore.summaryDetail.fiftyTwoWeekHigh.fmt)}
- catch(e){result.push('not found')}
- // Push the fundamental state
- result.push(TPEe)
- result.push(FPEe)
- result.push(PEGe)
- result.push(PBe)
- return [result]
- }
Advertisement
Add Comment
Please, Sign In to add comment