Algorithms for Trading ~

WEB Trader Get it on Google Play
  • Post by Fintechee
  • Jan 18, 2020
Algorithmic Trading System Architecture supports Algorithms for Trading. Fintechee offers Artificial Intelligence

Algorithmic Trading System Architecture supports Algorithms for Trading. Fintechee offers Artificial Intelligence to improve Expert Advisor signals’ precision.

Algorithms for Trading are another name of the Automated Trading concept. The instrument which implements the algorithms for trading is called Expert Advisor(EA). Automated trading includes EA based on news events, EA based on fundamental analysis, EA based on technical indicators and EA based on Artificial Intelligence.

We have discussed EA based on technical indicators in our Expert Advisor Tutorial. We won’t expand repeatedly. Please refer to the corresponding materials.

In this article to mainly discuss algorithms for trading, we will introduce EA based on artificial intelligence. As a new popular technology skill, artificial intelligence has been applied in many fields. In the algorithmic trading system architecture, artificial intelligence can improve the performance and the precision of the Trading Signals generated by EA, so artificial intelligence has a very promising future.


Artificial Intelligence and Deep Learning for Trading

First of all, Please open our home page and then click the “WEB TRADER” button to access our demo and you will be led to our WEB Trader. This is the basis of our algorithmic trading system architecture. Besides, if you have no experience to use our WEB Trader, please read our tutorial for Forex trading before you practice.

We apply artificial intelligence for algorithmic trading system architecture to improve the performance and the precision of the trading signals generated by EA.

Deep learning is a kind of “Artificial Intelligence”. Fintechee integrates with Synaptic JS which is known as an amazing Javascript library for deep learning. The sample used by this tutorial is implemented by SynapticJS as well. This tutorial will step by step let you know how to convert a webpage to a trained neural network that helps you analyze the market. Please refer to other materials if you want to learn the theory of deep leaning. In this article, we focus on the algorithmic trading system architecture only.


Machine Learning for Trading Tutorial Video


Perceptron

The sample in the tutorial video is implemented by Perceptron, a branch of neural networks. Maybe you will ask why not use convolutional NN, which is well-known recently. I think the basic concepts are the same. So, no matter what neural network we are using, our purpose is to make an algorithmic trading system architecture to help us analyze the market. And after a long time of practicing, we concluded that analyzing the market is not so sensitive to technical skills. Perceptron is satisfied to help us do that. That’s the reason why we don’t use complicated neural network structures. Besides, for a beginner, the concept of the perceptron is easy to understand. We just need to prepare these stuff below for constructing a neural network:

  • The number of layers and the number of neurons for every layer.
  • What to input(raw data such as candlestick or indicator).
  • The signal to end up training.

After preparing for the structure of the neural network, it’s time to code the EA. The process of developing an EA with AI is simple as well. Please do these things:

  • Code two programs. One is for gathering historical data as input parameters and for training. The other one is for calculating the new input parameter by the trained NN model to get signals(“go long” or “go short”).
  • Run the first EA to train the NN model.
  • Run the second EA to get signals.

Please check the source codes below as reference.


Training

This EA is for gathering historical data as input parameters and for training.

registerEA(
"sample_training_neuron_model",
"A test EA to train neuron model",
[{
	name: "period",
	value: 20,
	required: true,
	type: "Number",
	range: [1, 100]
}, {
	name: "inputNum",
	value: 20,
	required: true,
	type: "Number",
	range: [1, 100]
}, {
	name: "hiddenNum",
	value: 50,
	required: true,
	type: "Number",
	range: [1, 100]
}, {
	name: "diffPrice",
	value: 0.0001,
	required: true,
	type: "Number",
	range: [0, 10]
}],
function (context) { // Init()
			var account = getAccount(context, 0)
			var brokerName = getBrokerNameOfAccount(account)
			var accountId = getAccountIdOfAccount(account)
			var symbolName = "EUR/USD"

			window.chartHandle = getChartHandle(context, brokerName, accountId, symbolName, TIME_FRAME.M1)
			var period = getEAParameter(context, "period")
			window.indiHandle = getIndicatorHandle(context, brokerName, accountId, symbolName, TIME_FRAME.M1, "rsi", [{
				name: "period",
				value: period
			}])
		},
function (context) { // Deinit()
			var period = getEAParameter(context, "period")
			var inputNum = getEAParameter(context, "inputNum")
			var hiddenNum = getEAParameter(context, "hiddenNum")
			var arrOpen = getData(context, window.chartHandle, DATA_NAME.OPEN)
			var arrClose = getData(context, window.chartHandle, DATA_NAME.CLOSE)
			var arrRsi = getData(context, window.indiHandle, "rsi")

			if (arrRsi.length <= period + 1) return
			if (inputNum + period - 1 > arrRsi.length) throw new Error("No enough data.")

			// extend the prototype chain
			Perceptron.prototype = new synaptic.Network()
			Perceptron.prototype.constructor = Perceptron

			var myPerceptron = new Perceptron(inputNum, hiddenNum, 1)
			var myTrainer = new synaptic.Trainer(myPerceptron)

			var diffPrice = getEAParameter(context, "diffPrice")
			var trainingSet = []
			var longCount = 0
			var shortCount = 0

			for (var i = period - 1; i < arrRsi.length - inputNum; i++) {
				if (arrClose[i * inputNum + inputNum] - arrOpen[i * inputNum + inputNum] > diffPrice) {
					var input = []

					for (var j = 0; j < inputNum; j++) {
						input.push(arrRsi[i * inputNum + j] / 100)
					}

					trainingSet.push({
						input: input,
						output: [0]
					})

					longCount++
				} else if (arrOpen[i * inputNum + inputNum] - arrClose[i * inputNum + inputNum] > diffPrice) {
					var input = []

					for (var j = 0; j < inputNum; j++) {
						input.push(arrRsi[i * inputNum + j] / 100)
					}

					trainingSet.push({
						input: input,
						output: [1]
					})

					shortCount++
				}
			}

			myTrainer.train(trainingSet)
			localStorage.sample_training_neuron_model = JSON.stringify(myPerceptron.toJSON())
			printMessage(longCount + ", " + shortCount)
			printMessage(JSON.stringify(trainingSet))
			printMessage(JSON.stringify(myPerceptron.toJSON()))
		},
function (context) { // OnTick()
})

Running

This EA is for calculating the new input parameter by the trained NN model to get signals(“go long” or “go short”).

registerEA(
"sample_run_neuron_model",
"A test EA to run neuron model",
[{
	name: "period",
	value: 20,
	required: true,
	type: "Number",
	range: [1, 100]
}, {
	name: "inputNum",
	value: 20,
	required: true,
	type: "Number",
	range: [1, 100]
}, {
	name: "threshold",
	value: 0.3,
	required: true,
	type: "Number",
	range: [0, 1]
}, {
	name: "takeProfit",
	value: 0.0001,
	required: true,
	type: "Number",
	range: [0, 100]
}],
function (context) { // Init()
			if (typeof localStorage.sample_training_neuron_model == "undefined") return

			window.myPerceptron = synaptic.Network.fromJSON(JSON.parse(localStorage.sample_training_neuron_model))

			var account = getAccount(context, 0)
			var brokerName = getBrokerNameOfAccount(account)
			var accountId = getAccountIdOfAccount(account)
			var symbolName = "EUR/USD"

			getQuotes (context, brokerName, accountId, symbolName)
			window.chartHandle = getChartHandle(context, brokerName, accountId, symbolName, TIME_FRAME.M1)
			var period = getEAParameter(context, "period")
			window.indiHandle = getIndicatorHandle(context, brokerName, accountId, symbolName, TIME_FRAME.M1, "rsi", [{
				name: "period",
				value: period
			}])
		},
function (context) { // Deinit()
			delete window.currTime
		},
function (context) { // OnTick()
			var arrTime = getData(context, window.chartHandle, DATA_NAME.TIME)
			if (typeof window.currTime == "undefined") {
				window.currTime = arrTime[arrTime.length - 1]
			} else if (window.currTime != arrTime[arrTime.length - 1]) {
				window.currTime = arrTime[arrTime.length - 1]
			} else {
				return
			}

			var account = getAccount(context, 0)
			var brokerName = getBrokerNameOfAccount(account)
			var accountId = getAccountIdOfAccount(account)
			var symbolName = "EUR/USD"

			var period = getEAParameter(context, "period")
			var inputNum = getEAParameter(context, "inputNum")
			var threshold = getEAParameter(context, "threshold")
			var takeProfit = getEAParameter(context, "takeProfit")
			var arrRsi = getData(context, window.indiHandle, "rsi")

			if (inputNum + period - 1 > arrRsi.length) throw new Error("No enough data.")

			var input = []

			for (var i = arrRsi.length - inputNum - 1; i < arrRsi.length - 1; i++) {
				input.push(arrRsi[i] / 100)
			}

			var result = window.myPerceptron.activate(input)[0]
			printMessage(result)

			var ask = getAsk(context, brokerName, accountId, symbolName)
			var bid = getBid(context, brokerName, accountId, symbolName)
			var volume = 0.01

			if (result < 0.5 - threshold) {
				sendOrder(brokerName, accountId, symbolName, ORDER_TYPE.OP_BUY, 0, 0, volume, ask+takeProfit, bid-3*takeProfit, "")
			} else if (result > 0.5 + threshold) {
				sendOrder(brokerName, accountId, symbolName, ORDER_TYPE.OP_SELL, 0, 0, volume, bid-takeProfit, ask+3*takeProfit, "")
			}
})

For more features that our WEB Trader provides regarding algorithmic trading system architecture, please read this article: Automated Forex Trading.

We published the source codes of the samples on our GitHub repository and the page - SDK Trading as well. You can check the source codes of the samples there. If you have any questions, please contact us.

If you want to get notified about our updates, please subscribe to our free newsletter. Thank you for reading.