Home > Algorithms for Trading

Algorithms for Trading

Fintechee 2020-01-18

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 tutorial for Forex trading. 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 trading field, 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 algorithms for trading. 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 trading 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 algorithms for trading only.

Algorithms for Trading | Deep Learning | Perceptron - Fintechee

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 algorithms for trading 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:

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:

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 algorithms for trading, 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. Thank you for reading.

We are going IEO
2020-01-10 Bounty News
Brokerless.CC
Sponsored by Fintech Angel Investors Network