Program PythonShell3_SVR_21; //# -*- coding: utf-8 -*- http://127.0.0.1:8080 //import scrapy - precondition note: change pyscript path of 991_oma_chartregression2.py Const PYPATH='C:\Users\max\AppData\Local\Programs\Python\Python36-32\'; PYPATH64='C:\Users\max\AppData\Local\Programs\Python\Python36\'; PYCODE='C:\Users\max\SkyDrive\IBZ_Prozesstechnik_2016\hellomaxbox\.vscode\urlschemaload.py'; PYSCRIPT= 'C:\maXbox\mX46210\DataScience\confusionlist\991_oma_chartregression2.py'; PYFILE = 'input("prompt: ")'+CRLF+ 'def mygauss3(): '+CRLF+ '# i=0 '+CRLF+ ' return sum(i for i in range(101))'+CRLF+ '# i=sum(i for i in range(101))'+CRLF+ '# print(i) '+CRLF+ ' '+CRLF+ 'print(mygauss3())'+CRLF+ 'k=input("press close to exit") '+CRLF+ '#input("prompt: ")'; PYFILE1 =// "source": [ 'listOfNumbers = [1, 2, 3, 4, 5, 6]'+CRLF+ ''+CRLF+ 'for number in listOfNumbers:'+CRLF+ ' print(number)'+CRLF+ ' if (number % 2 == 0):'+CRLF+ ' print(''\"is even\"'')'+CRLF+ ' else:'+CRLF+ ' print(''\"is odd\"'')'+CRLF+ ' '+CRLF+ ' print (''\"All done.\"'')'+CRLF; PYFILE2 = ' def'+CRLF; PYFILE3 = 'sum(i for i in range(101))'; PYFILE4 = 'def mygauss3(): '+CRLF+ '# i=0 '+CRLF+ ' return sum(i for i in range(101))'+CRLF+ '# i=sum(i for i in range(101))'+CRLF+ '# print(i) '+CRLF+ ' '+CRLF+ 'print(mygauss3())'+CRLF+ ''; PYSCRIPT5 = 'import numpy as np'+CRLF+ 'import matplotlib.pyplot as plt'+CRLF+ 'import sys'+CRLF+ 'from sklearn import tree'+CRLF+ 'from sklearn.svm import SVC'+CRLF+ 'from sklearn.ensemble import RandomForestClassifier'+CRLF+ 'from sklearn.linear_model import LogisticRegression'+CRLF+ 'from sklearn.preprocessing import StandardScaler'+CRLF+ 'from sklearn.metrics import accuracy_score'+CRLF+ 'from sklearn.model_selection import train_test_split'+CRLF+ ' '+CRLF+ ' '+CRLF+ '# Quotes from Yahoo finance and find optimal moving average'+CRLF+ 'import pandas_datareader.data as web'+CRLF+ ' '+CRLF+ '#DataMax - Predict for 30 days; Predicted has data of Adj. Close shifted up by 30 rows'+CRLF+ 'forecast_len=80 #default oma is 5'+CRLF+ 'YQUOTES = "^SSMI"'+CRLF+ 'PLOT = "Y"'+CRLF+ 'try:'+CRLF+ ' forecast_len = int(sys.argv[1])'+CRLF+ ' #forecast_len= int(" ".join(sys.argv[1:]))'+CRLF+ ' YQUOTES = str(sys.argv[2])'+CRLF+ ' PLOT = str(sys.argv[3])'+CRLF+ 'except:'+CRLF+ ' forecast_len= forecast_len'+CRLF+ ' YQUOTES = YQUOTES'+CRLF+ ' '+CRLF+ '#YQUOTES = "BTC-USD" #^GDAXI" , "^SSMI" , "^GSPC" (S&P 500 ) - ticker="GOOGL"'+CRLF+ 'try: '+CRLF+ ' df= web.DataReader(YQUOTES, data_source="yahoo",start="09-11-2010")'+CRLF+ 'except:'+CRLF+ ' YQUOTES = "^SSMI"'+CRLF+ ' df= web.DataReader(YQUOTES, data_source="yahoo",start="09-11-2010")'+CRLF+ ' print("Invalid Quote Symbol got ^SSMI instead")'+CRLF+ ' '+CRLF+ '#data = " ".join(sys.argv[1:])'+CRLF+ 'print ("get forecast len:",forecast_len, "for ", YQUOTES)'+CRLF+ 'quotes = df'+CRLF+ 'print(quotes.info(5))'+CRLF+ 'print(quotes["Close"][:5])'+CRLF+ 'print(quotes["Close"][-3:])'+CRLF+ 'df["_SMI_20"] = df.iloc[:,3].rolling(window=20).mean()'+CRLF+ 'df["_SMI_60"] = df.iloc[:,3].rolling(window=60).mean()'+CRLF+ 'df["_SMI_180"] = df.iloc[:,3].rolling(window=180).mean()'+CRLF+ 'df["_SMI_OMA"] = df.iloc[:,3].rolling(window=forecast_len).mean()'+CRLF+ ' '+CRLF+ '#"""'+CRLF+ 'if PLOT=="Y":'+CRLF+ ' x_ax_time = quotes.index #range(len(df))'+CRLF+ ' plt.figure(figsize=[12,7])'+CRLF+ ' plt.grid(True)'+CRLF+ ' plt.title("Optimal Moving Average OMA: "+YQUOTES, fontsize=18)'+CRLF+ ' plt.plot(x_ax_time, quotes["Close"], label="Close")'+CRLF+ ' plt.plot(x_ax_time, df["_SMI_60"],label="MA 3 Month")'+CRLF+ ' plt.plot(x_ax_time, df["_SMI_180"],label="MA 9 Month")'+CRLF+ ' plt.plot(x_ax_time, df["_SMI_OMA"],label= "OMA "+str(forecast_len)+" D.")'+CRLF+ ' #plt.xlabel("days", fontsize=15)'+CRLF+ ' # plt.plot_date(quotes.index, quotes["Close"])'+CRLF+ ' plt.legend(loc=2)'+CRLF+ ' plt.show()'+CRLF+ '#"""'+CRLF+ ' '+CRLF+ 'dates = quotes.index'+CRLF+ 'dates = dates[1:]'+CRLF+ '#closing_values = np.array([quote[3] for quote in quotes])'+CRLF+ '#volume_of_shares = np.array([quote[5] for quote in quotes])[1:]'+CRLF+ 'closing_values = np.array(quotes["Close"])'+CRLF+ 'volume_of_shares = np.array(quotes["Volume"])'+CRLF+ ' '+CRLF+ '#Predict for 30 days; Predicted has Quotes of Close shifted up by 30 rows'+CRLF+ 'ytarget= quotes["Close"].shift(-forecast_len)'+CRLF+ 'ytarget= ytarget[:-forecast_len]'+CRLF+ 'Xdata= closing_values[:-forecast_len]'+CRLF+ '#print("Offset shift:",ytarget[:10])'+CRLF+ ' '+CRLF+ '# Feature Scaling'+CRLF+ '#sc_X = StandardScaler()'+CRLF+ '#sc_y = StandardScaler()'+CRLF+ '#Xdata = sc_X.fit_transform(Xdata.reshape(-1,1))'+CRLF+ '#You need to do this is that pandas Series objects are by design one dimensional.'+CRLF+ '#ytarget = sc_y.fit_transform(ytarget.values.reshape(-1,1))'+CRLF+ ' '+CRLF+ 'from sklearn.svm import SVR'+CRLF+ '# Split datasets into training and test sets (80% and 20%)'+CRLF+ 'print("target shape len2: ",len(ytarget),len(Xdata))'+CRLF+ 'x_train,x_test,y_train,y_test=train_test_split(Xdata,ytarget,test_size=0.2, \'+CRLF+ ' random_state= 72)'+CRLF+ 'print("xtrain shape len3: ",len(x_train),len(y_train))'+CRLF+ ' '+CRLF+ '# - Create SVR model and train it'+CRLF+ 'svr_rbf= SVR(kernel="rbf",C=1e3,gamma=0.1)'+CRLF+ 'x_train = x_train.reshape(-1,1)'+CRLF+ 'svr_rbf.fit(x_train,y_train)'+CRLF+ ' '+CRLF+ '# Predicting single value as new result'+CRLF+ 'print("predict old in :", forecast_len, svr_rbf.predict([quotes["Close"][:1]]))'+CRLF+ 'print("predict now in :", forecast_len, svr_rbf.predict([quotes["Close"][-1:]]))'+CRLF+ ' '+CRLF+ '#DBASTAr - Get score'+CRLF+ 'svr_rbf_confidence=svr_rbf.score(x_test.reshape(-1,1),y_test)'+CRLF+ 'print(f"SVR Confidence: {round(svr_rbf_confidence*100,2)}%")'; ACTIVESCRIPT = PYSCRIPT5; function CompareFilename(List: TStringList; Index1, Index2: Integer): Integer; var fn1, fn2: String; begin fn1 := List.Names[Index1]; fn2 := List.Names[Index2]; Result := CompareString(fn1, fn2); end; function CompareFileSize(List: TStringList; Index1, Index2: Integer): Integer; var sz1, sz2: Int64; begin sz1 := StrToInt(List.ValueFromIndex[Index1]); sz2 := StrToInt(List.ValueFromIndex[Index2]); Result := ord(CompareValueI(sz1, sz2)); end; Function GetValueFromIndex(R: TStringList; Index: Integer):String; var S: string; i: Integer; begin S := R.Strings[Index]; i := Pos('=', S); if I > 0 then result := Copy(S, i+1, MaxInt) else result := ''; end; Function dummy(Reqlist: TStringList):String; var i: Integer; RESULTv: string; begin for i := 0 to ReqList.Count-1 do RESULTv := RESULTv + Reqlist.Names[i] + ' -> ' + GetValueFromIndex(Reqlist, i); result := RESULTv; end; var fcast: integer; olist: TStringlist; dosout, theMaxOMA, QuoteSymbol: string; Yvalfloat: array[1..500] of double; //TDynfloatArray; //theMaxFloat: double; RUNSCRIPT: string; begin //@main //saveString(exepath+'mygauss.py',ACTIVESCRIPT); saveString(exepath+'991_oma_chartregression5.py',ACTIVESCRIPT); sleep(300) //if fileExists(PYPATH+'python.exe') then //if fileExists(PYSCRIPT) then begin if fileExists(exepath+'991_oma_chartregression5.py') then begin RUNSCRIPT:= exepath+'991_oma_chartregression5.py'; //ShellExecute3('cmd','/k '+PYPATH+'python.exe && '+PYFILE +'&& mygauss3()' // ,secmdopen); { ShellExecute3('cmd','/k '+PYPATH+ 'python.exe && exec(open('+exepath+'mygauss.py'').read())' ,secmdopen); } { ShellExecute3('cmd','/k '+PYPATH+ 'python.exe '+exepath+'mygauss.py', secmdopen); } // ShellExecute3(PYPATH+'python.exe ',exepath+'mygauss.py' // ,secmdopen); maxform1.console1click(self); memo2.height:= 205; // maxform1.shellstyle1click(self); // writeln(GetDosOutput(PYPATH+'python.exe '+PYSCRIPT,'C:\')); fcast:= 120; //first forecast with plot QuoteSymbol:= 'BTC-USD'; //'BTC-USD'; //^SSMI olist:= TStringlist.create; olist.NameValueSeparator:= '='; //olist.Sorted:= True; //olist.CustomSort(@CompareFileName) //GetDosOutput('py '+PYSCRIPT+' '+itoa(fcast)+' '+QuoteSymbol+' "Y"','C:\'); GetDosOutput('py '+RUNSCRIPT+' '+itoa(fcast)+' '+QuoteSymbol+' "Y"','C:\'); for it:= 20 to 130 do if it mod 5=0 then begin //(GetDosOutput('py '+PYSCRIPT+' '+itoa(it)+' "BTC-USD"'+ 'Plot?','C:\')); dosout:= GetDosOutput('py '+RUNSCRIPT+' '+itoa(it)+' '+QuoteSymbol+' "N"','C:\'); writeln(dosout) with TRegExpr.Create do begin //Expression:=('SVR Confidence: ([0-9\.%]+).*'); Expression:=('SVR Confidence: ([0-9\.]+).*'); if Exec(dosout) then begin PrintF('Serie %d : %s',[it, Match[1]]); olist.add(Match[1]+'='+itoa(it)); Yvalfloat[it]:= strtofloat(Copy(match[1],1,5)); //MaxFloatArray end; Free; end; end; writeln(CR+LF+olist.text) writeln('OMA from key value list2: '+floattostr(MaxFloatArray(Yvalfloat))) TheMaxOMA:= olist.Values[floattostr(MaxFloatArray(Yvalfloat))]; writeln('OMA for Chart Signal: '+TheMaxOMA); olist.Free; (GetDosOutput('py '+RUNSCRIPT+' '+(TheMaxOMA)+' '+QuoteSymbol+' "Y"','C:\')); end; End. ref: https://docs.scrapy.org/en/latest/topics/link-extractors.html https://doc.scrapy.org/en/latest/topics/architecture.html https://www.rosettacode.org/wiki/Cumulative_standard_deviation#Pascal https://www.kaggle.com/vsmolyakov/keras-cnn-with-fasttext-embeddings https://www.angio.net/pi/bigpi.cgi https://medium.com/analytics-vidhya/using-deep-learning-to-create-a-stock-trading-bot-a96e6351d31c This function changes all data into a value between 0 and 1. This is as many stocks have skyrocketed or nosedived. Without normalizing, the neural network would learn from datapoints with higher values. This could create a blind spot and therefore affect predictions. The normalizing is done as so: value = (value - minimum) / maximum if Exec(memo2.text) then for i:=0 to SubExprMatchCount do PrintF('Group %d : %s', [i, Match[i]]); class ExampleSpider(scrapy.Spider): name = 'example' #allowed_domains = ['www.ibm.ch'] allowed_domains = ['127.0.0.1'] #start_urls = ['http://www.ibm.ch/'] start_urls = ['http://127.0.0.1:8080'] def parse(self, response): pass "source": [ "import numpy as np\n", "\n", "A = np.random.normal(25.0, 5.0, 10)\n", "print (A)" ] { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "## Activity" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "Write some code that creates a list of integers, loops through each element of the list, and only prints out even numbers!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 0 }