The resource loading... loading...

The 30 lines of code take you into the world of quantitative investing.

Author: Inventors quantify - small dreams, Created: 2016-04-06 10:17:26, Updated: 2020-01-13 15:17:11

The ultra-simplified median strategy of 30 lines to create a positive return system

That's right! That's right, it's 30 lines of code! Just 30 lines of code, I'm used to looking at the code first, so you can get a macro view!

The policy parameters are:

Parameters Describe Types The default
FastPeriod Fast-track cycle Numbers (number) 3
SlowPeriod Slow line cycle Numbers (number) 7
EnterPeriod Observation period of entry into the market Numbers (number) 3
ExitFastPeriod Off-market fast-line cycle Numbers (number) 3
ExitSlowPeriod Off-market slow-line cycle Numbers (number) 7
ExitPeriod Observation period after discontinuation Numbers (number) 1
PositionRatio Proportion of positions Numbers (number) 0.8
Interval Round-up period ((seconds)) Numbers (number) 10

img

  • Imported交易类库It's easy to write a strategy, without having to worry about whether to buy or sell.
  • When reading the code and finding undeclared variables confused, answer to the group.
function main() {
    var STATE_IDLE  = -1;
    var state = STATE_IDLE;
    var opAmount = 0;
    var initAccount = $.GetAccount();
    Log(initAccount);
    while (true) {
        if (state === STATE_IDLE) {
            var n = $.Cross(FastPeriod, SlowPeriod);
            if (Math.abs(n) >= EnterPeriod) {
                opAmount = parseFloat((initAccount.Stocks * PositionRatio).toFixed(3));
                var obj = n > 0 ? $.Buy(opAmount) : $.Sell(opAmount);
                if (obj) {
                    opAmount = obj.amount;
                    state = n > 0 ? PD_LONG : PD_SHORT;
                    Log("开仓详情", obj, "交叉周期", n);
                }
            }
        } else {
            var n = $.Cross(ExitFastPeriod, ExitSlowPeriod);
            if (Math.abs(n) >= ExitPeriod && ((state === PD_LONG && n < 0) || (state === PD_SHORT && n > 0))) {
                var obj = state === PD_LONG ? $.Sell(opAmount) : $.Buy(opAmount);
                state = STATE_IDLE;
                var nowAccount = $.GetAccount();
                LogProfit(nowAccount.Balance - initAccount.Balance, '钱:', nowAccount.Balance, '币:', nowAccount.Stocks, '平仓详情:', obj, "交叉周期", n);
            }
        }
        Sleep(Interval*1000);
    }
}
  • This strategy has only one main function function main (), with no other function modules.
  • I've shared a code description of this strategy on the QQ group, so beginners can see it for easy learning.
  • If you are not a member of the official QQ group, please join: 309368835 Inventors of Quantitative EA Exchange (BotVS)
  • To take care of students who do not have a basic knowledge of JavaScript language, we have explained the grammar here in a simple way so that the code is not misunderstood by other students.

Variable declaration

Use the keywordvar+ variable name, for examplevar name = “小明”

Circulation

while(循环条件){
    // 将会重复执行的代码
}

The loop condition is true if the code inside {} is executed repeatedly. The loop condition is false if the loop is skipped.

Conditional Branch

if (判断条件){
    // 执行代码
} else {
    // 执行代码
}

It's very simple, translated as If (true) {executes this code} its its case {executes this code}

Assignment

One=The number is the assignment, for example.

name = “张三”;

It's easy to confuse attribution with comparison. For example,==The two equals. Examples“张三”==“李四”It's clear that Zhang is not Li.“张三”==“李四”I'm going to fake it.

Related APIs

Before we demonstrate the policy, we'll take a look at the API and template export functions used in the policy, with detailed API documentation and template source code instructions on Inventor Quantification.

  • $.GetAccount function: Template export function used to obtain account information from an exchange
  • Log functions: API used to export information to logs, display numbers etc.
  • $.Cross Function: Template export function used to detect crossover of even-line indicators
  • $.Buy function: Template export function used to buy operations
  • $.Sell function: Template export function used to sell operations
  • LogProfit Function: API, for output of the gain, the function can add other information from the second parameter
  • Sleep function: API, which pauses the program for a moment, with a parameter unit of milliseconds.

The policy code also uses Javascript library functions, object methods, etc.

  • Math.abs ((): returns the absolute value of the parameter
  • The toFixed (() function of the Number object: keeps the small digit four-five by the parameter. Note that the function returns a string.
  • parseFloat global function: returns the floating point number of the string parameter.

You can find more information about JavaScript above at: JavaScript Number Objecthttp://www.w3school.com.cn/jsref/jsref_obj_number.aspStudying on the job

The program's flowchart is hand-drawn, without tools, amateurish.

img

Step by step, build our strategy

All the strategic procedures are at the entrance.main()A function, i.e. the policy starts from the main function.

img

img

img

img

img

The real thing

The simple 30-line strategy is done! Deploy the code to the host robot, and you'll be able to use it to create your own website. The Running! strategy is running, buy the operation.

img

Thanks to Inventor Quantify for providing such a streamlined code, there are a lot of open source strategies on Inventor Quantification to learn and improve alongside a lot of great ones.

官方QQ群定期更新学习资源,为量化学习者铺平道路,登堂入室。

Supporting video teaching

http://v.youku.com/v_show/id_XMTUyNDY1NjQ2NA==.html

References


Related

More

FMZ_JHHas the $.GetAccount function in the API been updated to exchange.GetAccount (())?

wojiushizhemedeshuaiqidemeinanziI understand the code, but I don't know exactly what this strategy does.

:)Thank you little dream, your article is very well written.

lrj2uThe owner's tutorial is very well written, thank you!

bincoinThanks to the landlord, I'm going to study hard.

The short-line king won selling the high-price strategyI'm not going to go into detail.

Inventors quantify - small dreams$.GetAccount is the export function of the FMZ template (the template is reusable code, see the description of the template in the API documentation). $.GetAccount is the export function of this template: https://www.fmz.com/strategy/10989. The above strategies are only a part of the code, with no policy parameter set, and the complete strategy can be searched in the Strategy Square in 30 lines.

wojiushizhemedeshuaiqidemeinanziThat's what happened. Thank you very much.

Inventors quantify - small dreamsThere are two straight lines, the slow line on the fast line is usually a buy signal, with a higher probability of upward movement (automatic buy operation); the slow line on the fast line is generally a sell signal, with a higher probability of downward movement (automatic sell operation); by doing this, you can earn a profit in the upward trend of the market.

Inventors quantify - small dreamsThank you for your support, I will be doing more learning material later!

Inventors quantify - small dreamsWe will continue to offer our products later! Thank you for your support.