├── 1.png ├── how it works.png ├── Demonstration.gif ├── README.md └── FX_TradingBot.py /1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RetributionByRevenue/Preconfigured-Mt4-Python-Trading-Enviroment-/HEAD/1.png -------------------------------------------------------------------------------- /how it works.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RetributionByRevenue/Preconfigured-Mt4-Python-Trading-Enviroment-/HEAD/how it works.png -------------------------------------------------------------------------------- /Demonstration.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RetributionByRevenue/Preconfigured-Mt4-Python-Trading-Enviroment-/HEAD/Demonstration.gif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | follow my instagram: @slay_the_normies 2 | 3 | The purpose of this github is to provide a solution to the biggest problem currently in the forex market, and that issue is algo trading! 4 | 5 | I also made a youtube video tutorial with complete walkthrough how to operate the code: 6 | https://www.youtube.com/watch?v=qHsNMn1rEzI&lc=z23vulyhro3exp0khacdp43aj24gv4lscs31pfbwpklw03c010c 7 | 8 | ![Output Result](https://github.com/RetributionByRevenue/Preconfigured-Mt4-Python-Trading-Enviroment-/blob/master/Demonstration.gif?raw=true) 9 | 10 | 11 | Most forex brokers do not have their own custom api and only provide their own version of Metatrader 4 for traders to interface with. Metatrader 4/Metatrader 5 is an outdated windows xp era forex trading software that is rather difficult to use AND especially frustrating to program for!!! 12 | 13 | 14 | The journey of becoming a competent forex algo trader was brutal(devastating at times), required lots of trial and error, and is difficult. However, the problems I and many others in my shoes have faced can be solved to some degree when using Python as a base programing language instead of the garbage MQL4(Metatrader 4) programing language. Assuming you have a strategy in mind to test or have a profitable trading strategy written in Python, the downloaded files will allow users to place trades very simply to Metatrader 4. Furthermore, I’m assuming that most people I am helping here are new to programming or algo trading in general, so I organized everything to be simple to understand. 15 | 16 | ![Output Result](https://raw.githubusercontent.com/RetributionByRevenue/Preconfigured-Mt4-Python-Trading-Enviroment-/master/how%20it%20works.png) 17 | 18 | 19 | How it works? I have configured PORTABLE/CONTAINERIZED versions of Python3 and Metatrader 4 environments to demonstrate an easy solution to send orders to mt4 using Python. In simple terms, you just need to download the files and run the programs. Due to the nature of these portable environments I have configured, there is no need for any installing of any kind. It is simple to use and possibly a great way for new people to get into forex or algo trading. You can even run the files on a USB or in a Dropbox folder giving many computers you own the ability to algo trade without the need of installing python or metatrader. 20 | Just load spyder.exe and terminal.exe and you are ready to start algo trading. 21 | 22 | 23 | What is the catch? Only works on windows only. You can use wine to emulate on mac/linux but support is hit or miss. 24 | Download: https://mega.nz/#!j1V3SAJY!nefxmYUgvWRgemo7t5sJ5B1sBd96SNramou1RjbPPOc 25 | 26 | Click image below to watch Youtube video tutorial: 27 | [![Link to youtube video](https://raw.githubusercontent.com/RetributionByRevenue/Preconfigured-Mt4-Python-Trading-Enviroment-/master/1.png)](https://www.youtube.com/watch?v=qHsNMn1rEzI&lc=z23vulyhro3exp0khacdp43aj24gv4lscs31pfbwpklw03c010c) 28 | 29 | | Tech Stack Used | Description | Link | 30 | |----------|:-------------:|------:| 31 | | Oanda demo account api | A free and easy to use source to obtain candlestick data. Can also be used for order management. | https://developer.oanda.com/rest-live-v20/introduction/ | 32 | | WinPython | A powerful portable python enviroment with many useful tools preinstalled. | http://winpython.sourceforge.net/ | 33 | | MT4 Portable | A portable version of MT4 | http://projectreaper.pw/en/metatrader-4-portable/ | 34 | -------------------------------------------------------------------------------- /FX_TradingBot.py: -------------------------------------------------------------------------------- 1 | '''demonstration of a tradingbot that can place order to mt4, just need mt4 "Place_Order" expert advisor endabled 2 | and dll imports & allowed live trading to be enabled on the mt4 client. 3 | 4 | most forex brokers do not have a api and sadly we are stuck with mt4 to algo trade fx, at least this is a minor compermise 5 | that can bring the power of python for trading profibility :) 6 | 7 | this trading bot will produce a csv that an expert advisor can read and place orders from. the csv file is then deleted 8 | 9 | made by @slay_the_normies 10 | ''' 11 | import pandas as pd 12 | import csv 13 | import time 14 | import talib 15 | import numpy as np 16 | from pathlib import Path 17 | path = str(Path(__file__).parent.absolute()) 18 | path=path.replace("\\","\\\\") 19 | path=path[:path.find("Pythonic")] 20 | logs=''#fetch oanda current open orders data with your own api key, convert data to string and check if the pair is inside string object 21 | fx_pairs='''AUDJPY CADJPY CHFJPY EURJPY NZDJPY USDJPY GBPJPY AUDUSD EURUSD GBPUSD NZDUSD USDCAD USDCHF AUDCAD CADCHF EURCAD GBPCAD NZDCAD AUDCHF EURCHF GBPCHF NZDCHF EURAUD EURGBP EURNZD GBPNZD GBPAUD AUDNZD''' 22 | #fx_pairs='EURUSD' #you can trade specific pairs if you desire 23 | fx_pairs=fx_pairs.replace(' ','') 24 | counter1=(len(fx_pairs)/6) 25 | 26 | mt4_location="C:\\Users\\t\\AppData\\Roaming\\MetaQuotes\\Terminal\\61007F75C6EC7CED9A269B292061D7A1\\MQL4\\Files\\LastSignal.csv" 27 | print(mt4_location) 28 | while counter1>0: 29 | string=fx_pairs[-6:] 30 | fx_pairs=fx_pairs[0:-6] 31 | counter1=counter1-1 32 | df = pd.read_csv(path+'Pythonic Fx\\WinPython\\Forex OHLC\\'+string+'.csv') 33 | df=df.drop('Unnamed: 0', 1) 34 | df=df.drop('complete', 1) 35 | df=df.drop('time', 1) 36 | df=df.drop('volume', 1) 37 | #print(df.head) 38 | #print(df.tail) 39 | print('Obtained ', string,' data!') 40 | #####MATH####### 41 | MA_ARRAY_13=talib.EMA(np.array(df.close), timeperiod=13) 42 | RSI_ARRAY_13=talib.RSI(MA_ARRAY_13, timeperiod=13) 43 | MA_RSI_OMA_21=talib.EMA(RSI_ARRAY_13, timeperiod=21) 44 | MA_RSI_OMA_137=talib.EMA(RSI_ARRAY_13, timeperiod=137) 45 | print(RSI_ARRAY_13[-10:]) 46 | print(MA_RSI_OMA_21[-10:]) 47 | print(MA_RSI_OMA_137[-10:]) 48 | #time.sleep(5) 49 | ##### Trade conditions### 50 | current_price=df.tail(1) 51 | #print(klines.tail(1)) 52 | current_price=current_price.close 53 | current_price=current_price.tolist() 54 | #print(current_price) 55 | current_price=current_price[-1] 56 | print('current price is ', current_price) 57 | if 'JPY' in string: 58 | 59 | rounding=3 60 | Long_SL=current_price-0.50 61 | Long_TP=current_price+0.20 62 | Short_SL=current_price+0.50 63 | Short_SL=current_price-0.20 64 | else: 65 | rounding=5 66 | Long_SL=current_price-0.0050 67 | Long_TP=current_price+0.0020 68 | Short_SL=current_price+0.0050 69 | Short_TP=current_price-0.0020 70 | 71 | 72 | print('=====================================') 73 | 74 | print('====================================') 75 | if string[0:3]+'_'+string[3:6] not in logs: 76 | if MA_RSI_OMA_21[-1] < 15: 77 | if MA_RSI_OMA_21[-1] > RSI_ARRAY_13[-1] and MA_RSI_OMA_137[-1] > RSI_ARRAY_13[-1]:#LONG 78 | Pair=string 79 | signal='OP_BUY' 80 | SL=Long_SL 81 | TP=Long_TP 82 | place_order = pd.DataFrame({'': [Pair+','+signal+','+str(SL)+','+str(TP),',,,']}) 83 | #automatically determines file locations string to match your own system 84 | place_order = place_order.to_csv(path+"Pythonic Fx\\MT4 Portable\\MQL4\\Files\\LastSignal.csv", header=None, index=None, mode='w', sep=' ',quoting=csv.QUOTE_NONE, quotechar="-", escapechar="-") 85 | print(string+' pair has opened long') 86 | print('===============================') 87 | time.sleep(5) 88 | #insert optional oanda code here later for order tracking 89 | 90 | elif MA_RSI_OMA_21[-1] > 85: 91 | if MA_RSI_OMA_21[-1] < RSI_ARRAY_13[-1] and MA_RSI_OMA_137[-1] < RSI_ARRAY_13[-1]:#SHORT 92 | Pair=string 93 | signal='OP_SELL' 94 | SL=Short_SL 95 | TP=Short_TP 96 | place_order = pd.DataFrame({'': [Pair+','+signal+','+str(SL)+','+str(TP),',,,']}) 97 | #automatically determines file locations string to match your own system 98 | place_order = place_order.to_csv(path+"Pythonic Fx\\MT4 Portable\\MQL4\\Files\\LastSignal.csv", header=None, index=None, mode='w', sep=' ',quoting=csv.QUOTE_NONE, quotechar="-", escapechar="-") 99 | print(string+' pair has opened long') 100 | print('===============================') 101 | time.sleep(5) 102 | #insert optional oanda code here later for current order tracking 103 | else: 104 | print(string+' does not valid conditions to make trade') 105 | print('===============================') 106 | time.sleep(5) 107 | 108 | else: 109 | print('that particular pair is in trade') 110 | print('===============================') 111 | time.sleep(5) 112 | #this is a trading bot i made for a friend as a demonstration, it is not profitable. 113 | #it is best to place orders on your mt4 broker and oanda api demo simultaneously; so you can use the oanda api to track open orders for your mt4 broker 114 | #below is code that will place orders to oanda api 115 | 116 | #code that is used for shorting, insert in the shorting section of code 117 | #for longing, modify the varibiles and insert to longing section of code '' 118 | ''' 119 | api = oandapyV20.API(access_token='b1820d124857b58655e36eaa7f856b50-f9269344d4a6e789a9ce102df15af97d')#api key is expired 120 | 121 | mktOrder = MarketOrderRequest( 122 | instrument=str(string[0:3]+'_'+string[3:6]), 123 | units=-10000, #longing is positive number 124 | takeProfitOnFill=TakeProfitDetails(price=Short_TP).data, 125 | stopLossOnFill=StopLossDetails(price=Short_SL).data) 126 | r = orders.OrderCreate('101-002-10197028-005',data=mktOrder.data) 127 | try: 128 | rv = api.request(r) 129 | except oandapyV20.exceptions.V20Error as err: 130 | print(r.status_code, err) 131 | else: 132 | print(json.dumps(rv, indent=2)) 133 | ''' --------------------------------------------------------------------------------