Package: pedlar
:chart_with_upwards_trend: pedlarPedlar is an algorithmic trading platform for Python designed for trading events, competitions and sessions such as Algothons. It includes a live web interface with multiple accounts with account sharing and live chat, an HTTP API with example Python trading agents and aZeroMQbased broker connection toMetaTrader5.Getting StartedIf there is already a ticker server and web server running, the client API underpedlarcan be used. If not, follow instructions on how to get them in theHostingsection. The client oragentAPI resolves around connecting to thetickerserver (where agents receive price updates) and thewebserver, in this case handles the broker connection. The ticker is separate to reduce overhead and latencies between making trades and just receiving price updates.InstallationThe client API is can be installed using:pip3install--no-cache-dir-UpedlarUsageThere are some helpful examples in thepedlarfolder. Here is a overview of the client API:frompedlar.agentimportAgentclassMyAgent(Agent):"""A trading agent."""defon_order(self,order):"""Called on placing a new order."""print("New order:",order)print("Orders:",self.orders)# Agent orders onlydefon_order_close(self,order,profit):"""Called on closing an order with some profit."""print("Order closed",order,profit)print("Current balance:",self.balance)# Agent balance onlydefon_tick(self,bid,ask,time=None):"""Called on every tick update."""print("Tick:",bid,ask,time)# self.buy()# self.sell()# self.close()defon_bar(self,bopen,bhigh,blow,bclose,time=None):"""Called on every bar update."""print("Bar:",bopen,bhigh,blow,bclose,time)if__name__=="__main__":importlogginglogging.basicConfig(level=logging.DEBUG)agent=MyAgent.from_args()agent.run()The extra parameters are parsed from the command line and can be run using:python3-umyagent.py-hKey things to keep in mind:Every agent takes control of its own orders and balance, it isnot syncedacross agents of a shared account. This setup is to keep agents isolated.Agents try to close orders when they are quit, if hard stopped or an error occurs an open orphan order might remain. In this case, one option would be manually invokeself.closewith the stale order id or simply reset the account.The ticker connection receives from ZeroMQ whereas the trade requests are made via HTTP. There might some ticks dropped if the trade request takes too long.Basic BacktestingThe agents can backtest against a CSV file of the following format, last column time is optional and time format is adjustable throughtime_formatof theAgentclass:tick,1.26361,1.26375,2019.01.03 23:44:42
tick,1.2636,1.26374,2019.01.03 23:44:59
tick,1.2636,1.26378,2019.01.03 23:45:00
bar,1.26386,1.26398,1.26355,1.2636,2019.01.03 23:45:00
tick,1.26359,1.26377,2019.01.03 23:45:02
tick,1.26357,1.26375,2019.01.03 23:45:05
tick,1.26356,1.26374,2019.01.03 23:45:07
tick,1.26358,1.26376,2019.01.03 23:45:10which can be used with an agentpython3 myagent.py -b ticks.csv. Essentially each line will invoke correspondingon_tickoron_barfunction.The actual profit and trade results are computed offline based on absolute price differences.This means the agent will run completely offline and the actual results are only useful to get an idea about the performance or train a neural network. Any extra broker commissions beyond bid-ask spread, price requotes etc are not factored.HostingPedlar involves 4 components that talk to each other to create a platform for agents to trade:ticker:is a ZeroMQPUBsocket that publishes tick and bar updates that are received by the agents which eventually triggeron_tickandon_barmethods. This component runs independently from others to provide a continuous stream of price updates.broker:is the component that actually executes live order requests on the market. Pedlar is broker agnostic and all it needs is methods to buy, sell and close an order with unique order ids.pedlarweb: is the main web server that handles user accounts, live leaderboard etc updates and agent trade requests.pedlar: is the client API that allows for Python based clients to receive updates fromtickerand make trade requests topedlarweb.Repository StructureThe client and server packages are separated to avoid any assumptions between their implementations. The main folders include:mt5:contains the MetaTrader 5 trading components that act as thetickerand thebrokerfor live trading.pedlar:contains the client API is the exposed API for building new agents.pedlarweb:is the web server that bridges agent trade requests and broker while providing a web user interface and live updates.PrerequisitesAll the extra packages required can be installed using:pip3install--no-cache-dir-U-rrequirements.txtRunning MT5To get the ticker and broker components up and running you need to:Install MetaTrader 5, or some existing installation would work.Install ZeroMQfor Windows since the scripts require thelibzmq.dllto run. You might need to install a compatibleVisual C++runtime to get ZeroMQ running.Find the installed ZeroMQbinfolder and move the DLL toLibrary\libzmq.dllas that is where thelibzmq.mqhheader expects it.Copy files MetaTrader 5 files to the expected folders of your installation. An easy way to find that is to open the MetaEditor (F4 from trader) and right click on a folder to selectOpen Folder. For development it is easier to write a script that syncs the repo folder with the expected folders.Once the ticker and broker is compiled, you can attach them to any chart that you want the tick updates to be sent and trade orders to be handled. You can run multiple at different ports as well. From "Options" the "Allow DLL Imports" needs to be enabled for obvious reasons.If you get DLL import error, it is most likely because the ZeroMQ DLL isn't happy or the C++ runtime is not compatible.Running Local BrokerAs an alternative to executing trades live, there islbroker.pythat implements same interface as the actual MT5 broker but instead executes the orders locally based on the latest ticks provided by MT5. It is a simple stand-alone script:python3lbroker.py-hRunning Web ServerThe web server is a standardFlaskapplication organised into thepedlarwebpackage. You need to create ainstance/config.pyto customise the default values. Once theconfig.pyoptions are as desired, a database can be initialised:mkdirinstance&&cpconfig.pyinstance/config.py# customise instance/config.py ex. database settingspython3-c"from pedlarweb import db; db.create_all()"If the in-memory default database is used, tables will be automatically created butdata is lost when server is stopped using an in-memory database.Then the server can be run using standard Flask options:python3runpedlarweb.py-h

usage:runpedlarweb.py[-h][-d][--hostHOST][--portPORT]Runpedlarweb.

optionalarguments:-h,--helpshowthishelpmessageandexit-d,--debugEnabledebugserver.--hostHOSTHostIPaddress.--portPORTHostport.Due toFlask-SocketIOtheeventletserver would be run.For convinience, a new user is created if none with the username exist from the login page.This choice is done to get people on-board as easy as possible without heavy registration and email confirmation schemes.FAQWhy ticker is separated from the web server?This design choice is done to reduce overhead and latency in receiving price updates for agents. As a result agents need to connect to both the ticker and the web server to function unless backtesting.Why not use ZeroMQ for trade requests instead of HTTP?HTTP APIs provide a more approachable and unified way of providing a service. HTTP does have more overhead but it makes it easier for other non-pedlar clients that talk topedlarwebto be built as well. Finally, it has an authentication mechanism that is enforced for every request.Why is the source code 2 space indented?The answer is a combination of personal style and to stop direct copy-paste from other resources. The code is linted usingPyLintalthough there are cases it is disabled on purpose.Limitations & To-DosCurrently the APIsupports trading only on a single instrumentmainly because the web server can talk to a single broker and a single broker can only be attached to single chart. The MT5 broker is fixed to the current chartSymbol()but there is space for passing the symbol, instrument from agent -> server -> broker. For the target audience of live sessions and competitions, a single instrument seems enough.Although polling is employed for ZeroMQ sockets, they are stillblockingin nature to avoid data loss. In that case, an agent can look like it has frozen against Ctrl+C. This case also occurs for MT5 ticker and broker scripts. The timeout on the polls can be adjust or a little patience helps.There is a mixture of line endings due MT5 running on Windows and development is somewhat split between Windows and Linux environments. Hopefully, we live in a day and age this is not a problem anymore.Built WithFlask-pedlarwebweb frameworkZeroMQ-tickerandbrokermessaging frameworkMetaTrader5- as the default trading platformSocketIO- server client messaging for dynamic updatesVue.JS- dynamic front-end renderingW3.CSS- front-end CSS library