Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
joanby committed Nov 27, 2022
1 parent 5c88557 commit 2d4e146
Showing 1 changed file with 66 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"\n",
"class MT5:\n",
"\n",
" def get_data(symbol, n, timeframe=mt5.TIMEFRAME_D1):\n",
" def get_data(symbol, n, timeframe=mt5.TIMEFRAME_D1):\n",
" \"\"\" Function to import the data of the chosen symbol\"\"\"\n",
"\n",
" # Initialize the connection if there is not\n",
Expand All @@ -44,45 +44,45 @@
" rates_frame = rates_frame.set_index('time')\n",
" return rates_frame\n",
"\n",
" def orders(symbol, lot, buy=True, id_position=None):\n",
" \"\"\" Send the orders \"\"\"\n",
" def orders(symbol, lot, buy=True, id_position=None):\n",
" \"\"\" Send the orders \"\"\"\n",
"\n",
" # Initialize the connection if there is not\n",
" if mt5.initialize() == False:\n",
" mt5.initialize()\n",
" # Initialize the connection if there is not\n",
" if mt5.initialize() == False:\n",
" mt5.initialize()\n",
"\n",
" # Get filling mode \n",
" filling_mode = mt5.symbol_info(symbol).filling_mode - 1\n",
" # Get filling mode \n",
" filling_mode = mt5.symbol_info(symbol).filling_mode - 1\n",
"\n",
" # Take ask price\n",
" ask_price = mt5.symbol_info_tick(symbol).ask\n",
" # Take ask price\n",
" ask_price = mt5.symbol_info_tick(symbol).ask\n",
"\n",
" # Take bid price\n",
" bid_price = mt5.symbol_info_tick(symbol).bid\n",
" # Take bid price\n",
" bid_price = mt5.symbol_info_tick(symbol).bid\n",
"\n",
" # Take the point of the asset\n",
" point = mt5.symbol_info(symbol).point\n",
" # Take the point of the asset\n",
" point = mt5.symbol_info(symbol).point\n",
"\n",
" deviation = 20 # mt5.getSlippage(symbol)\n",
" # **************************** Open a trade *****************************\n",
" if id_position == None:\n",
" deviation = 20 # mt5.getSlippage(symbol)\n",
" # **************************** Open a trade *****************************\n",
" if id_position == None:\n",
"\n",
" # Buy order Parameters\n",
" if buy:\n",
" type_trade = mt5.ORDER_TYPE_BUY\n",
" sl = ask_price*(1-0.01)\n",
" tp = ask_price*(1+0.01)\n",
" price = ask_price\n",
" if buy:\n",
" type_trade = mt5.ORDER_TYPE_BUY\n",
" sl = ask_price*(1-0.01)\n",
" tp = ask_price*(1+0.01)\n",
" price = ask_price\n",
"\n",
" # Sell order Parameters\n",
" else:\n",
" type_trade = mt5.ORDER_TYPE_SELL\n",
" sl = bid_price*(1+0.01)\n",
" tp = bid_price*(1-0.01)\n",
" price = bid_price\n",
" type_trade = mt5.ORDER_TYPE_SELL\n",
" sl = bid_price*(1+0.01)\n",
" tp = bid_price*(1-0.01)\n",
" price = bid_price\n",
"\n",
" # Open the trade\n",
" request = {\n",
" request = {\n",
" \"action\": mt5.TRADE_ACTION_DEAL,\n",
" \"symbol\": symbol,\n",
" \"volume\": lot,\n",
Expand All @@ -95,22 +95,22 @@
" \"comment\": \"python script order\",\n",
" \"type_time\": mt5.ORDER_TIME_GTC,\n",
" \"type_filling\": filling_mode,\n",
" }\n",
" # send a trading request\n",
" result = mt5.order_send(request)\n",
" result_comment = result.comment\n",
" }\n",
" # send a trading request\n",
" result = mt5.order_send(request)\n",
" result_comment = result.comment\n",
"\n",
" # **************************** Close a trade *****************************\n",
" else:\n",
" # Buy order Parameters\n",
" if buy:\n",
" type_trade = mt5.ORDER_TYPE_SELL\n",
" price = bid_price\n",
" type_trade = mt5.ORDER_TYPE_SELL\n",
" price = bid_price\n",
"\n",
" # Sell order Parameters\n",
" else:\n",
" type_trade = mt5.ORDER_TYPE_BUY\n",
" price = ask_price\n",
" type_trade = mt5.ORDER_TYPE_BUY\n",
" price = ask_price\n",
"\n",
" # Close the trade\n",
" request = {\n",
Expand All @@ -127,39 +127,40 @@
" \"type_filling\": filling_mode,\n",
" }\n",
"\n",
" # send a trading request\n",
" result = mt5.order_send(request)\n",
" result_comment = result.comment\n",
" return result.comment\n",
" # send a trading request\n",
" result = mt5.order_send(request)\n",
" result_comment = result.comment\n",
" return result.comment\n",
"\n",
" def resume():\n",
" \"\"\" Return the current positions. Position=0 --> Buy \"\"\"\n",
" # Initialize the connection if there is not\n",
" mt5.initialize()\n",
"\n",
" # Define the name of the columns that we will create\n",
" colonnes = [\"ticket\", \"position\", \"symbol\", \"volume\"]\n",
" def resume():\n",
" \"\"\" Return the current positions. Position=0 --> Buy \"\"\"\n",
" # Initialize the connection if there is not\n",
" mt5.initialize()\n",
"\n",
" # Go take the current open trades\n",
" current = mt5.positions_get()\n",
" # Define the name of the columns that we will create\n",
" colonnes = [\"ticket\", \"position\", \"symbol\", \"volume\"]\n",
"\n",
" # Create a empty dataframe\n",
" summary = pd.DataFrame()\n",
" # Go take the current open trades\n",
" current = mt5.positions_get()\n",
"\n",
" # Loop to add each row in dataframe\n",
" # (Can be ameliorate using of list of list)\n",
" for element in current:\n",
" element_pandas = pd.DataFrame([element.ticket,\n",
" # Create a empty dataframe\n",
" summary = pd.DataFrame()\n",
"\n",
" # Loop to add each row in dataframe\n",
" # (Can be ameliorate using of list of list)\n",
" for element in current:\n",
" element_pandas = pd.DataFrame([element.ticket,\n",
" element.type,\n",
" element.symbol,\n",
" element.volume],\n",
" index=colonnes).transpose()\n",
" summary = pd.concat((summary, element_pandas), axis=0)\n",
" summary = pd.concat((summary, element_pandas), axis=0)\n",
"\n",
" return summary\n",
" return summary\n",
"\n",
"\n",
" def run(symbol, long, short, lot):\n",
" def run(symbol, long, short, lot):\n",
"\n",
" # Initialize the connection if there is not\n",
" if mt5.initialize() == False:\n",
Expand Down Expand Up @@ -218,7 +219,7 @@
"\n",
" print(\"------------------------------------------------------------------\")\n",
"\n",
" def close_all_night():\n",
" def close_all_night():\n",
" result = MT5.resume()\n",
" for i in range(len(result)):\n",
" before = mt5.account_info().balance\n",
Expand Down Expand Up @@ -393,25 +394,25 @@
"def breakout(symbol):\n",
" global order_duration\n",
" \n",
" # Import / Features engineering\n",
" df = MT5.get_data(\"BTCUSD\", 50)\n",
" # Importar / Ingeniería de variables\n",
" df = MT5.get_data(symbol, 50)\n",
" df[\"resistance\"] = df[\"close\"].rolling(30).max().shift(3)\n",
"\n",
" distance = 0.5/100\n",
"\n",
" # Yersteday values\n",
" # Valores de ayer\n",
" close_y = df[\"close\"].shift(1).iloc[-1]\n",
" resistance_y = df[\"resistance\"].shift(1).iloc[-1]\n",
"\n",
" # Today values\n",
" # Valores de hoy\n",
" close = df[\"close\"].iloc[-1]\n",
" resistance = df[\"resistance\"].iloc[-1]\n",
"\n",
" # Signal\n",
" # Señal\n",
" buy = (close_y < resistance_y) and (close > resistance*(1+distance))\n",
" sell = False\n",
" \n",
" # Position modeling\n",
" # Modelizamos la posición\n",
" if buy:\n",
" order_duration = 7\n",
" else:\n",
Expand Down Expand Up @@ -534,7 +535,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -548,7 +549,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
"version": "3.8.8"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 2d4e146

Please sign in to comment.