How to write a trading strategy on FMZ Quant platform
After studying the previous sections, we finally ready to write a quantitative trading strategy. This will be the most important step in your entry into the quantitative trading from manual trading. In fact, it is not so mysterious. Writing a strategy is nothing more than realizing your ideas with code. This section will implement a quantitative trading strategy from scratch, after the study, everyone will familiar with how to write strategies on the FMZ Quant system.
First, open the official website of the FMZ Quant, log in your account. click on “Dashboard–strategy–Add Strategy”. please note that before starting to write the code, you need to select the types of programming language. in this section we will use JavaScript. select it from the drop-down menu. in addition, FMZ Quant platform also Support for Python, C++, and visual programming.
In the previous chapter, I introduced a moving average strategy. That is: if the price is higher than the average price of the last 10 days, open long position. if the price is lower than the average price of the last 10 days, short it. However, although the price can directly reflect the market status, there still will be many false breakthrough signals; therefore, we must upgrade and improve this strategy.
First, choose a larger period moving average to judge the trend direction. At least half of the false breakthrough signals have been filtered. The large cycle moving average is slow, but it will be more stable. Then, in order to increase the success rate of the opening position, we add another condition. This large cycle moving average is at least upward; finally, the relative positional relationship between price, short-term moving average and long-term moving average is used to form a complete trading strategy.
With the above strategy ideas, we can try to build this strategy logic. The logic here is not to let you calculate the law of celestial movement, it is not that complicated. It is nothing more than expressing the previous strategy ideas in words.
Open long position: If there is currently no position, and the closing price is greater than the short-term moving average, and the closing price is greater than the long-term moving average, and the short-term moving average is greater than the long-term moving average, and the long-term moving average is rising.
Open short position: If there is currently no position, and the closing price is less than the short-term moving average, and the closing price is less than the long-term moving average, and the short-term moving average is less than the long-term moving average, and the long-term moving average is falling.
Close Long position: If currently hold long position, and the closing price is less than the long-term moving average, or the short-term moving average is less than the long-term moving average, or the long-term moving average is falling.
Close Short position: If current hold short position, and the closing price is greater than the long-term moving average, or the short-term moving average is greater than the long-term moving average, or the long-term moving average is rising.
The above is the logic of the entire strategy, if we convert the text version of this strategy into code, it will include: the acquisition of the market quote, the calculation of indicators, placing order to open and close position, these three steps.
The first thing is to get the market quote. In this strategy, we only need to get the closing price. In the M language, the API to get the closing price is: CLOSE, which means you only need to write CLOSE in the coding area to obtain the latest K line closing price.
Next thing is to calculate indicator. In this strategy, we will use two indicators, namely: short-term moving average and long-term moving average. We assume that the short-term moving average is the 10-period moving average and the long-term moving average is the 50-period moving average. How to use code to represent these two? Please see below:
MA10:=MA(CLOSE,10); // Get the 10-cycle moving average of the latest K-line and save the result in variable MA10
MA50:=MA(CLOSE,50); // Get the 50-cycle moving average of the latest K-line and save the result in variable MA50
In manual trading, we can see at a glance whether the 50-period moving average is rising or falling, but how do we express it in code? Think carefully, judging whether the moving average is rising or not, is the current moving average of the K-line is larger than the moving average of the previous K-line? or is it hight than two previous K-line? If the answer is yes, then we can say that the moving average is rasing. We also can judge the falling by the same method.
MA10:=MA(CLOSE,10); //Get the 10-cycle moving average of the latest K line and save the result to variable MA10
MA50:=MA(CLOSE,50); //Get the 50-cycle moving average of the latest K line and save the result to variable MA10
MA10_1:=REF(MA10,1); //Get the 10-cycle moving average of the pervious K line and save the result to variable MA10_1
MA50_1:=REF(MA50,1); //Get the 50-cycle moving average of the pervious K line and save the result to variable MA50_1
MA10_2:=REF(MA10,2); //Get the 10-cycle moving average of the latest K line and save the result to variable MA10_2
MA50_2:=REF(MA50,2); //Get the 50-cycle moving average of the latest K line and save the result to variable MA50_2
MA50_ISUP:=MA50>MA50_1 AND MA50_1>MA50_2; //Determine whether the current 50-line moving average of the K line is rising
MA50_ISDOWN:=MA50<MA50_1 AND MA50_1<MA50_2; //Determine whether the current 50-line moving average of the K line is falling
Note that on lines 8 and 9 of the above code, the word “AND”, is a Logical Operators. which means when the both side of “and” condition are true, the whole sentence is true, otherwise it is false.(if only one side of condition is true, as a whole, it is still false). Translate it in to English: If the 50-period moving average of the current K-line is greater than the 50-period moving average of the previous K-line, and the 50-period moving average of the pervious K-line is greater than the K line before it 50-period moving average K-line, then Calculate the value as “yes”; otherwise, calculate the value as “no” and assign the result to “MA50_ISUP”.
The final step is to place orders, you only need to call the FMZ Quant’s order API to execute the buy and sell operation after the logic code. Please see below:
MA10:=MA(CLOSE,10); //Get the 10-cycle moving average of the latest K line and save the result to variable MA10
MA50:=MA(CLOSE,50); //Get the 50-cycle moving average of the latest K line and save the result to variable MA10
MA10_1:=REF(MA10,1); //Get the 10-cycle moving average of the pervious K line and save the result to variable MA10_1
MA50_1:=REF(MA50,1); //Get the 50-cycle moving average of the pervious K line and save the result to variable MA50_1
MA10_2:=REF(MA10,2); //Get the 10-cycle moving average of the latest K line and save the result to variable MA10_2
MA50_2:=REF(MA50,2); //Get the 50-cycle moving average of the latest K line and save the result to variable MA50_2
MA50_ISUP:=MA50>MA50_1 AND MA50_1>MA50_2; //Determine whether the current 50-line moving average of the K line is rising
MA50_ISDOWN:=MA50<MA50_1 AND MA50_1<MA50_2; //Determine whether the current 50-line moving average of the K line is falling
CLOSE>MA10 AND CLOSE>MA50 AND MA10>MA50 AND MA50_ISUP,BK; //open long position
CLOSE<MA10 AND CLOSE<MA50 AND MA10<MA50 AND MA50_ISUP,SK; //open short position
CLOSE<MA50 OR MA10<MA50,SP;//close long position
CLOSE>MA50 OR MA10>MA50,BP;//close short position
Note that line 13 and 14 of the above, the word “OR”, which is another logical operator, in the M language means “or”, translate it into English : if the current K line’s closing price is less than the current K line’s 50-period moving average, or the current K-line 10-period moving average is less than the current K-line 50-period moving average, the value is calculated as “Yes”. And place the order immediately; otherwise the calculation is “no” and do nothing.
Please note that “AND” and “OR” are all logical operators in the M language:
“AND” is when all conditions are “yes”, and the final condition is “yes”;
“OR” is when as long as any one of the conditions is “yes”, the final condition is “yes”.
The above is the entire process of writing a trading strategy on the FMZ Quant platform by using M programming language. There are three steps in total: from having a strategy idea, to strategy thinking and using text to describe the logic, and finally implementing a complete trading strategy with code. Although this is a simple strategy, the specific implementation process is similar to the complex strategy, except that the strategy and data structure of the strategy are different. Therefore, as long as you understand the quantitative strategy process in this section, you can conduct quantitative strategy research and practice on the FMZ Quant platform.
Try to implement the strategies in this section on your own.
On the basis of the strategy of this section, add the stop-loss and take-profit function.
In the development of quantitative trading strategies, programming languages are like weapons, a good programming language can help you get twice the result with half the effort. For example , there are more than a dozen of the most commonly used Python, C++, Java, C#, EasyLanguage, and M language in the quantitative trading world . Which weapon should you choose to battle on the battlefield? In the next section we will introduce these common programming languages, as well as the characteristics of each programming language itself.