Skip to content

Commit

Permalink
Added sklearn model to Jupyter sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmad Noman Musleh committed Jan 10, 2022
1 parent 792e6b1 commit a52d1f4
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
9 changes: 9 additions & 0 deletions samples/jupyter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Jupyter Sample

This is sample that will show you how to use the OpenApiPy Python package on a Jupyter notebook.

In the notebook we get the daily bars data from cTrader Open API, then we use it to train a sklearn model.

To use the sample you have to create a copy of "credentials.json" file and rename it to "credentials-dev.json".

Then fill the file with your Open API application credentials, access token, and a trading account ID.
91 changes: 88 additions & 3 deletions samples/jupyter/main.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
" highPrice = (trendbar.low + trendbar.deltaHigh) / 100000.0\n",
" lowPrice = trendbar.low / 100000.0\n",
" closePrice = (trendbar.low + trendbar.deltaClose) / 100000.0\n",
" return (openTime, openPrice, highPrice, lowPrice, closePrice, trendbar.volume)"
" return [openTime, openPrice, highPrice, lowPrice, closePrice, trendbar.volume]"
]
},
{
Expand Down Expand Up @@ -195,6 +195,8 @@
" dailyBars.clear()\n",
" dailyBars.extend(barsData)\n",
" print(\"\\ndailyBars length:\", len(dailyBars))\n",
" print(\"\\Stopping reactor...\")\n",
" reactor.stop()\n",
" \n",
"def symbolsResponseCallback(result):\n",
" print(\"\\nSymbols received\")\n",
Expand Down Expand Up @@ -292,7 +294,7 @@
"id": "a7835d65",
"metadata": {},
"source": [
"Now we have the price data, let'd do some cool stuff with it, we will transform it to a pandas DataFrame and then we will use it to train a sklearn model based on it."
"Now we have the price data, let's do some cool stuff with it, we will transform it to a pandas DataFrame:"
]
},
{
Expand All @@ -301,7 +303,90 @@
"id": "5a75f17c",
"metadata": {},
"outputs": [],
"source": []
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6f6bdcd7",
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame(np.array(dailyBars),\n",
" columns=['Time', 'Open', 'High', 'Low', 'Close', 'Volume'])\n",
"df[\"Open\"] = pd.to_numeric(df[\"Open\"])\n",
"df[\"High\"] = pd.to_numeric(df[\"High\"])\n",
"df[\"Low\"] = pd.to_numeric(df[\"Low\"])\n",
"df[\"Close\"] = pd.to_numeric(df[\"Close\"])\n",
"df[\"Volume\"] = pd.to_numeric(df[\"Volume\"])"
]
},
{
"cell_type": "markdown",
"id": "63eda941",
"metadata": {},
"source": [
"Now let's create a labels series, we will use it later for our ML model.\n",
"This will have a 1 if the close price was higher than open price and 0 otherwise.\n",
"We will use the today price data to predict the tomorrow bar type."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "02856070",
"metadata": {},
"outputs": [],
"source": [
"df[\"Labels\"] = (df[\"Close\"] > df[\"Open\"]).astype(int)\n",
"df[\"Labels\"] = df[\"Labels\"].shift(-1)\n",
"df.drop(df.tail(1).index,inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cb75ff4f",
"metadata": {},
"outputs": [],
"source": [
"df.head()"
]
},
{
"cell_type": "markdown",
"id": "5d5ce793",
"metadata": {},
"source": [
"Now let's create a sklearn LogisticRegression model and use our data to train and test it:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fc9f1fe5",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.metrics import accuracy_score\n",
"\n",
"model = LogisticRegression()\n",
"\n",
"x = df.loc[:, [\"Open\", \"High\", \"Low\", \"Close\", \"Volume\"]]\n",
"y = df.Labels\n",
"\n",
"x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.7)\n",
"\n",
"model.fit(x_train, y_train)\n",
"\n",
"y_pred= model.predict(x_test)\n",
"\n",
"print(\"Our Model accuracy score is: \", accuracy_score(y_test, y_pred))"
]
}
],
"metadata": {
Expand Down

0 comments on commit a52d1f4

Please sign in to comment.