Writing group instructions in a quantitative trading strategy

Author: Goodness, Created: 2019-07-10 09:55:13, Updated: 2019-07-16 15:37:32

Why group commands?

The needs of strategy developers

Different markets require different benchmarks. Can I set different stop loss spreads based on different opening conditions?

For example, the traditional model of writing the settlement conditions does not distinguish between different opening conditions.

The following code is a simple strategy for traditional indistinguishable storage conditions:

MA5^^MA(C,5);
MA10^^MA(C,10);
RSV:=(CLOSE-LLV(LOW,9))/(HHV(HIGH,9)-LLV(LOW,9))*100;
K..SMA(RSV,3,1);
D..SMA(K,3,1);
CROSS(MA5,MA10)||CROSS(K,D),BK;
C>HV(H,10)||C<BKPRICE-5*MINPRICE,SP;
AUTOFILTER;

And it's not the same with the grouping command.

The subgrouping instruction can be used to divide the open condition into n groups, where the open position of one group is only open for the corresponding open condition of the other group, and the open position of the other group is not signalled or assigned.

For example:

The first group was multi-conditional.

MA5^^MA(C,5);
MA10^^MA(C,10);
CROSS(MA5,MA10),BK;
CROSS(MA10,MA5),SP;

The second group was multi-conditional.

RSV:=(CLOSE-LLV(LOW,9))/(HHV(HIGH,9)-LLV(LOW,9))*100;
K..SMA(RSV,3,1);
D..SMA(K,3,1); 
CROSS(K,D),BK; 
C>HV(H,10)||C<BKPRICE-5*MINPRICE,SP;

How do we distinguish between different groups of conditions in the same model? Let's implement them.

How to write a group command

First, the models are divided into filtered and non-filtered models:

  • Filtering model: Different open conditions want to be leveled with different breakeven conditions, which can be implemented using instruction clustering.

  • Non-filtering model: The first entry strategy is different from the leverage strategy, which can be implemented using instruction clustering.

Filtering model

//A组指令
A组的开多条件,BK('A');
A组的开空条件,SK('A');
A组的平多条件,SP('A');
A组的平空条件,BP('A');
//B组指令
B组的开多条件,BPK('B');
B组的开空条件,SPK('B');
B组的平多条件,SP('B');
B组的平空条件,BP('B');
AUTOFILTER;//过滤函数

Note: A filtering model's grouping requires that the grouping be added after the transaction instruction and enclosed with a single quotation mark. For example, BK (A)

Unfiltered model

//A组指令
A组的开多条件1,BK('A',2);
A组的开空条件1,SK('A',2);
A组的加多条件2,BK('A',1);
A组的加空条件2,SK('A',1);
A组的平多条件,SP('A',GROUPBKVOL('A'));
A组的平空条件,BP('A',GROUPSKVOL('A'));
//B组指令
B组的加多条件,BK('B',1);
B组的加空条件,SK('B',1);
B组的平多条件1,SP('B',GROUPBKVOL('B'));
B组的平空条件1,BP('B',GROUPSKVOL('B'));

Note: The grouping of non-filtered models requires the addition of groups and hand numbers after the transaction instructions, and groups need to be enclosed by single quotes. For example, BK ((A, 2)

Mechanism of operation of the subgroup command

Filtering model: first group filter, then signal filter

  • Group filtering means: if the previous K-line signal is an opening signal from group A (BK SK BPK SPK), the current K-line can only be a placement signal from group A. If the previous K-line signal is a placement signal from group A (BP SP), the current K-line can be an opening signal from any group.

  • Uncoupled settlement conditions can only open uncoupled settlement conditions.

Signal filtering refers to:

The priority order is:

  • The upper K-line is BK, the current K-line must be SPK or SP (SPK takes precedence over SP, following symmetry)
  • The upper K line is SK, the current K line must be BPK or BP.
  • The top K line is BP, and the current K line must be BK or SK.
  • The top K line is SP. The current K line must be BK or SK.
  • The top K line is BPK, the current K line must be SPK or SP.
  • The upper K line is SPK, the current K line must be either BPK or BP.

This is a non-filter model:

  • If the previous signal is an opening signal for group A, the next signal must be an uptrend signal or a breakdown signal for group A.
  • If the previous signal is the placement signal of group A and group A holds 0, the next signal can be the opening signal of any group.
  • If Group A holds a position greater than 0, then it must be an open position signal or a close position signal for Group A. Note: Uncoupled closing conditions only open uncoupled closing conditions

Analysis of the case of grouping instructions

In the following, we'll use a few strategies to show how these instructions are grouped when writing code.

Filtering model

Trading ideas: Using 20-cycle and 60-cycle straightforward gold forks as trend judging criteria.

  • Do more above the 20-period mean line than above the 60-period mean line.
  • In a multi-trend, if the highest price is 10 k new high and for the sunline, the trend is more. When flat, with a larger stop-loss point stop-loss flat or there is a uniform dead fork flat. Do the opposite.
  • In a bullish trend, if the KDJ indicator is a gold fork and for the sun line, the wave band is more. When the equilibrium is established, the stop loss equilibrium is established at a smaller stop loss point or the stop loss equilibrium is established at a lower stop loss point of the k-line.

The code is:

MA20^^MA(C,20);
MA60^^MA(C,60);
RSV:=(CLOSE-LLV(LOW,9))/(HHV(HIGH,9)-LLV(LOW,9))*100;
K:=SMA(RSV,3,1);
D:=SMA(K,3,1);
J:=3*K-2*D;
HH:=HV(H,10);
LL:=LV(L,10);
MA20>MA60&&H>HH&&C>O,BK('A');
MA20<MA60&&L<LL&&C<O,SK('A');
L<LV(L,5)||CROSSDOWN(MA20,MA60)||C<BKPRICE-5*MINPRICE,SP('A');
H>HV(H,5)||CROSSUP(MA20,MA60)||C>SKPRICE+5*MINPRICE,BP('A');//只平A组开仓
MA20>MA60&&CROSSUP(K,D)&&C>O,BK('B');
MA20<MA60&&CROSSDOWN(K,D)&&C<O,SK('B');
C>BKPRICE+5*MINPRICE||C<BKPRICE-2*MINPRICE||C<REF(L,BARSBK),SP('B');
C<SKPRICE-5*MINPRICE||C>SKPRICE+2*MINPRICE||C>REF(H,BARSSK),BP('B');//只平B组开仓
//不同的开仓条件开仓,用不同的平仓条件,有针对性的平仓。达到不同行情试用不同策略的目的。
AUTOFILTER;

Unfiltered model

Trading idea: Opening a position for the first time with a 5-cycle and 10-cycle straightforward gold fork as a condition.

  • 5 cycles and 60 cycles of even-line gold forks before the first open position and no break even; 5 cycles and 60 cycles of even-line dead forks before the first open position and no break even;
  • In a five-cycle trend larger than 60 cycles, the highest price creates a new high of 10 basis k, and then a second higher position. In a five-cycle trend less than 60 cycles, the lowest price creates a new low of 10 basis k, and then a second higher position.
  • For the first time after adding more positions, create a new low or smaller stop loss position to create 5 k lines.
  • For the second additional position, the linear dead fork equilibration is carried out in 5 cycles and 60 cycles.

The code is:

MA5^^MA(C,5);
MA10^^MA(C,10);
MA20:=MA(C,20);
MA60^^MA(C,60);
RSV:=(CLOSE-LLV(LOW,9))/(HHV(HIGH,9)-LLV(LOW,9))*100;
K:=SMA(RSV,3,1);
D:=SMA(K,3,1);
J:=3*K-2*D;
HH:=HV(H,10);
LL:=LV(L,10);
CROSSUP(MA5,MA10)&&BKVOL=0&&C>=O,BK('A',2);
CROSSDOWN(MA5,MA10)&&SKVOL=0&&C<=O,SK('A',2);
CROSSUP(MA5,MA60)&&ISLASTBK&&BKVOL=2,BK('A',1);
CROSSDOWN(MA5,MA60)&&ISLASTSK&&SKVOL=2,SK('A',1);
MA5>MA60&&H>HH&&ISLASTSP&&REF(GROUPBKVOL('A'),BARSSP+1)>0,BK('B',1);
MA5<MA60&&L<LL&&ISLASTBP&&REF(GROUPSKVOL('A'),BARSBP+1)>0,SK('B',1);
L<LV(L,5)||C<REF(L,BARSBK)&&(C<BKPRICE-2*MINPRICE),SP('A',GROUPBKVOL('A'));
H>HV(H,5)||C>REF(H,BARSSK)&&(C>SKPRICE+2*MINPRICE),BP('A',GROUPSKVOL('A'));
C>BKPRICE+10*MINPRICE||CROSSDOWN(MA5,MA60),SP('B',BKVOL);
C<SKPRICE-10*MINPRICE||CROSS(MA5,MA60),BP('B',SKVOL);

The above is a specific case analysis of these two models, from which the reader can see how My language handles grouping instructions, and how each person can develop different grouping requirements according to their own strategy logic, so that they can try to express the strategy logic they want to express in the code in the clearest and least error-prone way possible.


More