Version 2 MTT Intern Bot Task/Overview The goal of the MTT Intern Bot is to truly automate a user’s trading without having to even check the charts constantly. Setting up trade alerts is one thing but if the user has the Martin Trend Trader indicator, life is simplified. The trader merely needs to configure the indicators and alerts on a TradingView chart and set up the same chart on the same time frame in MT4. The trades will execute on their own and the user would not have to be logged in to either platform. A user will have custom alerts set up on TradingView.com that are sent to the email account that receives the alerts. The MT4 advisor needs to be able to receive email notifications based on a user’s email provider’s SMTP settings (preferably a dedicated Gmail, ProtonMail, or something similar), and convert specific parts of the email message into code to execute a trade in MT4 – if not just execute trades based on keywords within the alert message. A user will enter a specific set of criteria for the parameters of the expert advisor in MT4. Once the parameters are set, the advisor is ready. The advisor requires different email notifications to execute a trade (1 for a market order entry, 1 for a stop loss, and 1 for a take profit/market close). When the notifications are received, the advisor will place a market entry order and a stop loss only with the user parameters taken into account. The trade is exited when one more email notifications are sent to the advisor. <> Option 1: A user will have custom alerts set on indicators through TradingView.com that are sent to the email account that is associated with both TradingView.com and MT4. An email extractor, Message Export, will periodically export the alert emails into .txt format for the bot in MT4 to read and ultimately make trades. The MT4 advisor needs to be able to receive the text files and convert specific parts of the email message to execute a trade in MT4 after validating the user inputs on the bot done beforehand. The advisor requires 3 email notifications/text files to execute a trade all coming from 2 indicators on TradingView (1 for market entry, 1 for stop loss or trailing stop loss position, and 1 market exit). When the notifications are received, the advisor will place a market entry order and a stop loss only with the user parameters taken into account. The trade is exited when one more email notification is sent to the advisor. MTT Bot needs to go through this baseline process of trades: Receive “MTT” alert for market entry à Receive “Stop loss indicator” alert for stop loss or trailing stop loss value à Receive “MTT” alert for market exit A separate task may need to run after the market entry alert is triggered in order to set the stop loss value as an alternative to setting stop loss based on TV alerts. Once the market entry alert is triggered an HTML scraping tool needs to run, push the value from TradingView, and export that value to a file, or some similar process, that value through email or another medium to the MT4 trading bot. The suggestion here would be to use Python + Beautiful Soup to create webhooks on the “Martin Trend Trader” entries to push the stop loss value into a valid format for the MT4 bot. Option 2: A user will have a custom strategy established through TradingView.com on a demo account set to whatever account balance desired (will most likely need to have the account balance equal to the account balance in MT4). The TradingView strategy itself will make simulated trades and plot them on the TradingView graph. An API or some black box will need to pull these trade position values as soon as they are made. Market entry and stop loss values will occur simultaneously on TradingView. At this time the API will pull the values and plot them on the graph in MT4. At a later time the TradingView strategy will get a signal to close the position and at that point the data would be transmitted to MT4. <>. Terms · Expert Advisor and advisor used interchangeably · Exit and Take Profit used interchangeably · TradingView/Trading View/TV – charting website where alert email notifications will be triggered from · SL – Stop Loss · TP – Take Profit · Advisor – the bot that parses the email’s data and converts that into code to make a trade on MT4. · MT4 – Trading platform that is enabled for bot trading. · Exit/Market Exit = Take Profit = Close/Market Close Requirements · Ability for the expert advisor to make a trade at the time that the data was received. This will always be a market order entry for a long or short position, a static stop loss or trailing stop loss (depending on user-bot configuration), and a take profit/close/exit always as a market order. All trade types depend on user inputs and validation by the bot. · Ability for the advisor to work along with manual trades by the user through MT4 as long as the account balance permits the trade. · Ability for the advisor to run multiple copies on various timeframes of the same or different Forex pairs. · Ability for a manual, adjustable stop loss/trailing stop loss. · Ability for the bot to receive the text files and make trades while the user is offline/not logged in. · (Future requirement) Ability for the advisor/bot to work with different trading vehicles not only specific to Forex – more CFDs, Cryptocurrencies, Equities, Futures, etc. · (Future requirement) Ability to scale in and scale out orders – pyramiding. Possible requirements depending on option taken: · Ability for an HTML scraping tool to push a value from TradingView after the market entry signal has been transmitted to MT4. · Ability for an API mechanism or some black box to transmit position price values from TradingView directly to MT4. · Ability for the user’s expert advisor to read text files exported from an email exchange originating from a TradingView alert. Assumptions · User has a TradingView account set up with a dedicated email account to receive custom alerts triggered based on specific indicators for each pair on a chart with a given timeframe · User has an MT4 account where the expert advisor exists · User has filled out the advisor’s parameters on MT4 · The pair on TV’s alerts’ timeframe match the MT4 chart and advisor parameter. (Ex. TV EURUSD 24H matches MT4’s chart and advisor at 24H). Possible Assumptions · User has configured HTML scraping tool to push data from TV after the market signal has been triggered · User has configured email exporter tool on email exchange to run periodically and has set the file export type to .txt. · User has configured API/black box to transmit the position data from TV to MT4. Preconfiguration The following preconfiguration steps are with a EURUSD chart (for the first two images I was not able to get the SL TRY indicator to display on a non-trading day so imagine this image is for EURUSD) and alerts on the 24 hour timeframe. Note that steps 1-3’s order do not need to be in this sequence. Note that the steps for 4-7’s order do not need to be in this sequence. Trading View: 0. The user has logged into trading view and opens a chart for EURUSD at the 24H timeframe. 1. Martin Trend Trader – the user applies the indicator with the time and trend code through the indicator’s inputs. 2. SL TRY – the user applies the indicator and selects the source input as “Martin Trend Trader”. 3. The user creates an alert for Martin Trend Trader so that the email alert forwarded to MT4 can be read by the advisor in MT4. (Market entry) 4. The user creates an alert for SL TRY so that the email alert forwarded to MT4 can be read by the advisor in MT4. (Stop Loss) 5. The user creates an alert for Martin Trend Trader so that the email alert forwarded to MT4 can be read by the advisor in MT4. (Profit point) Example alert condition set up. “Send Email” is a mandatory requirement. Other elements of the alert will be discussed further in the document. Email Exporter (MessageExporter used as example) 0. Navigate to create new profile option on the MessageExporter tab found on Microsoft Exchange (edit profile if one is existing). 1. Set the export file format to “Text” 2. Set the export history so that files that are already sent are not sent again to the expert folder. 3. Set the output predefined folder to MT4’s expert folder – this will be the file repository. 4. Enable scheduler and set the batch job to run periodically as desired by the user – for instance if the user is running TV charts on the 4 hour it may be best to run the batch job every hour. 5. Optional – send emails specific to TV alerts to a separate Outlook folder. 6. Optional – set the keep exported messages properties to true. 7. MT4 0. Open MT4 and login. 1. Navigate to Files > Open Data Folder > MQL4 > Experts to ensure that the txt files received via email are being placed in the correct folder. Advisor: 0. The user opens a EURUSD chart on any time frame and clicks on the advisor from the Expert Advisor list under “Navigator”. This is mandatory. 1. The user makes the following inputs shown in the image above (example). Process Flow Diagram Logic SMTP Email Notifications Exported as .txt Files When the advisor on MT4 receives email text files the advisor must parse/read (no JSON or any coding of the message required?) keywords from the email in order to set market orders, stop loss orders, and take profit orders. After the data is parsed from the email, then the advisor can check the user inputs on the bot in MT4. Bold words in each description represent the keywords to be parsed. These keywords are tentative and not final. These keywords can be changed into code in order to be parsed as long as the code is provided to the trader so that he/she is able to configure on the message in TV alert accordingly. The following sequence must be strictly adhered to: 1 Market Order Entry à 2 Stop Loss à 3 Take Profit. Each stage in the sequence cannot occur before the other. Each email alert sent to the user’s email account and ultimately to MT4 will be sent through an email address similar to . (1) Market Order Entry ‘Martin Stochastic Trend’ description keywords to parse: (Text in parenthesis is just a description of the keyword and not necessarily a part of the description) “EURUSD (pair) Short Trade (Martin Trend Trader), 24H (timeframe)” or “EURUSD (pair) Long Trade (Martin Trend Trader), 24H (timeframe)” or “EURUSD (pair) Short Reentry (Martin Trend Trader), 24H (timeframe)” or “EURUSD (pair) Long Reentry (Martin Trend Trader), 24H (timeframe)” When the advisor checks the user parameters it will confirm: -type of trade – entry, SL, TP -Timeframe – does the timeframe on the email alert match the timeframe on the chart/advisor parameters? -Account Balance/Margin required/Percent Lot – does the account have enough to make a trade? (2) Stop Loss (SL) The advisor will receive email forwarded from the email account associated with Trading View (the original recipient) to parse. In order to create a SL position, the advisor must receive one Trading View emails for the ‘SL TRY’ indicator. ‘SL TRY’ description keywords to parse: (Text in parenthesis is just a description of the keyword and not necessarily a part of the description) <> The SL will always close 100% of the market order if triggered. If the value of the stop loss position cannot be transmitted to MT4 and the advisor, then the stop loss would need to be set 1% away from the market order. 1% is based on the price at which the market order was set. So if the price for USDJPY was set at 113.586 for a short and the stop loss value cannot be sent to MT4 and a stop loss at 1% needed to be made the formula would look like this: SL = 113.586 + (113.586 * .01) For a long position it would look like this: SL = 113.586 - (113.586 * .01) Note if the following combinations are made they must be strictly adhered to: If ‘Martin Trend Trader’ market entry has been generated And the value of the stop loss is sent from TV to MT4 Then create a stop loss order for the value provided Else create a stop loss order 1% distant away from the market order entry price (less than or greater than the market order entry price) <> When the advisor checks the user parameters it will confirm: -Trailing Stop Instead? – does the user want a trailing stop or a static stop? If the box is checked then a trailing stop will be created. If the box is not checked then a static stop will be created. (3) Take Profit (TP) When the criteria has been met to take profit on the market order entry, the advisor will close 100% of the trade upon the triggered alerts for the TP. For example, if the market order entry email that was sent for step 1 is for a short entry then the email notification that the advisor should be waiting for to take profit is: “EURUSD (pair) Short Profit Point (Martin Trend Trader), 24H (timeframe)” Alternatively for a long market entry TP: “EURUSD (pair) Long Profit Point (Martin Trend Trader), 24H (timeframe)” Note if the following combinations are made they must be strictly adhered to: For a short TP: If ‘Martin Trend Trader’ for short trade or short reentry was triggered in stage 1 Then close 100% of the trade For a long TP: If ‘Martin Trend Trader’ for short trade or short reentry was triggered in stage 1 Then close 100% of the trade -Time Zone/Trade From/Trade To – is the advisor allowed to close a position at this time? TradingView Indicator Source code and Alert Configuration Martin Trend Trader Description: Martin Trend Trader indicator trades based on trend. The trend code applied on various timeframes per pair is most likely the bar periods back to determine trend start based around OHLC. The time code input allows the user to manually set the trend indicator, but the author provides trend codes for tried-and-true tested pairs. At the beginning of each trend change the bar will alternate between green or red colors. The default settings are green for uptrend and red for downtrend and remain at each color until a trend change. Upon the beginning of each trend change the color will change colors. When this happens the user is able to set an alert for the trend change (i.e. Short Trade or Long Trade see details in the next section). When a trend changes and an entry is still live we want to close that trade. For every short/long trade opened there must be a “P” label or a trend change in order to close the trade. We want the bot to make trades upon trend change. Along the downtrend or uptrend, the indicator provides reentry points for a market entry. Every entry is signaled by an “E” label on top or bottom of the bar (bottom for long, top for short). A take profit point is signaled by a “P” label on top or bottom of the bar depending on the market entry type made (bottom for short, top for long). For every “E” label there must be at least one “P” label or trend change in order to close the trade. These signals will continuously flash during the duration of the bar, but for the purposes of our bot we are only concerned about entries or exits where the signal is printed after the bar close. We want the bot to make trades upon the reentry or exit points. Source Code: <> Mandatory Alert Configurations: - Martin Trend Trader - Live Alert - Short Trade, Short Reentry, Long Trade, Long Reentry - Once Per Bar Close – the provides the most optimal entry and exit when weighing r/r - Expiration Time open ended if possible else as far into the future as possible - Send Email - Message: <> SL TRY Source Code: //@version=3 study("SL TRY", overlay = true) SL = input(defval = close, title = 'Stop Loss', type = source) plot(SL, title = "CURRENT", color = black) plot(SL + 1, title = "Future on Uptrend", color = orange, linewidth = 3, trackprice = true, offset = +1) plot(SL - 1, title = "Future on Downtrend", color = orange, linewidth = 3,trackprice = true, offset = -1) Note that the user must always select the source on the chart to be “Martin Trend Trader: Stop Loss” on the chart as it always defaults to “close”. There is no way to set the default value to “Martin Trend Trader: Stop Loss” so this step must be taken into consideration. This is mandatory. Mandatory Alert Configurations <> The goal of this alert will be to plot the value of the stop loss. The point of this “SL TRY” indicator is to take the source, “Martin Trend Trader: Stop Loss”, and attempt to get that value. Currently TradingView has limitations on dynamic values being placed in an alert condition function nor is it allowed in something like a “toString” function. If the value of the stop loss can be pulled a different way from the Martin Trend Trader, then this indicator may not be utilized in the final product. See HTML scraper for more information. Alternatives/Additions to Option 1 or Option 2 HTML Scraper for Stop Loss Value – alternative to SL alert in option 1 TradingView has a widget titled “Data Window” that will actually show us the value of the Martin Trend Trader stop loss. Within the HTML divs we can see that value:
1.9698 == $0 The div may only be selected when the widget on the site is closed as seen in the element styles section on developer tools. The HTML scraper would need to run its requests.get function, or similar, after the alert for the market entry has been received by the MTT bot in MT4. The web scrape job would have to be sent to MT4 in a readable format for the MTT bot. TV strategy + API – option 2 <> The basis behind the strategy would be to use the indicator alerts locally within the website to set the values and then immediately send that information directly to MT4 upon execution. The thought process here would be API integration between TV and MT4 in order to transmit the data which requires more research to determine if this route is even possible. Conditions/User parameters on MTT Intern Bot The following inputs and their descriptions set by the user: Timeframe – this is a read-only field that is set to the timeframe on the chart. This timeframe MUST MATCH the TradingView email alert that is being pushed to the advisor on MT4. This does not mean that the user has to necessarily have the chart on TV open at all times but simply that the alert with the timeframe exists and is pushed to MT4. This will not throw an error message if the chart is different between TV and MT4 but the advisor will not consider the chart on MT4 to match and thus the bot will not run. Time Zone – time zone the user is trading in that is set based on the user’s default account settings. The user may change this setting. This field is displayed in UTC notation. Trade From – a required UTC time start that must be set by the user in the format HH:MM. Trade To – a required UTC time end that must be set by the user in the format HH:MM. Account Balance – a read-only field that displays the current value of the user’s current account balance. Leverage – a read-only field that displays the user’s leverage determined by the settings on the broker’s platform. Margin Required – a read-only field that will display the margin required from the user’s account in order to fulfill the order. This will automatically default to one lot size unless otherwise changed by the user in the lot percent field. If the account balance current value is down to only 5% of it’s original value an error will be thrown that the user cannot make a trade and must deposit more funds or close a trade to continue. Lot Percent – a field that will display the percent desired for one lot. For instance, if the user wants 5% of 1 lot then the user will enter “5”. The number of lots will always be “1” so you cannot trade more than 1 lot at a time on 1 chart, however you can open this advisor on multiple charts and trade 1 lot for each. This also means that you can have the same pair on two different charts on two different time frames (ex. a chart with the advisor for EURUSD 24H and a chart for EURUSD 4H). <> So “5.15%” would look like “5.15” in the field. The user may only input positive integers with decimals to the hundredths value. This field is required. See the calculation below to see what the calculation will be (assuming 100x leverage and the price for EURUSD 1.3900): Position volume: 1(Lot) = Balance / Margin Required * Lot Percent / 100 Position volume: 1(Lot) = 10,000 / 1390 * 5 / 100 = .36 Note that if the lot percent is changed to something that the user’s account balance is underbudget, then an error message will be thrown to convey that the advisor will not run. If the advisor receives notifications for a market entry at the same time, then the advisor will consider the pair that precedes the other in alphabetical order. For example, if emails are sent to the advisor from both EURUSD and GBPUSD at the same time and the account balance is 10,000, then the advisor will consider EURUSD first. The pair will be considered and traded first thus decreasing the account balance. Then, the advisor will make the trade for GBPUSD. Trailing Stop Instead? – a checkbox indicating that a trailing stop loss will be used rather than a static stop loss position, regardless of where the stop is placed as long as the absolute value is >.30%. Magic Number – a randomized read-only alphanumeric field for the unique number of each specific copy of the advisor for each chart and timeframe.