Example 2: Regression model of opening volatility
Idea: Break through the physical upper end of the first K-line on the day of the minute period, and go long. The price falls below the lowest price of the first K-line on the day or the market has passed for 10 minutes, close the position and exit; If it falls below the lower end of the entity of the first K-line of the day of the minute period, go short, and the price rises higher than the highest price of the first K-line of the day or the market has passed 10 minutes, close the position and exit.
RKO:=VALUEWHEN(TIME=0900,O);// The opening price of the first K-line of the day in the minute period
RKC:=VALUEWHEN(TIME=0900,C);// The closing price of the first K-line of the day in the minute period
RKH:=VALUEWHEN(TIME=0900,H);// The highest price of the first K-line of the day in the minute period
RKL:=VALUEWHEN(TIME=0900,L);// The lowest price of the first K-line of the day in the minute period
CROSS(H,MAX(RKO,RKC))&&TIME<0910&&TIME>0900,BK;
CROSS(MIN(RKO,RKC),L)&&TIME<0910&&TIME>0900,SK;
C>RKH || TIME>=0910,BP;
C<RKL || TIME>=0910,SP;
AUTOFILTER;
// Applicable varieties, influenced by the external market and the opening volatility of the more violent varieties
Example of stop-loss model - time stop-loss:
Example 3: Price breakthrough channel model
Idea: Use ATR to calculate the upper and lower tracks of the price channel. After the record high and the current highest price exceeds the closing price of the previous K-line plus a certain multiple of ATR, the long position enter the market, if the price crossovers the lower track, close the position and exit. After the record low and the current lowest price exceeds the closing price of the previous K-line minus a certain multiple of ATR, the short position enters the market, the price crossovers the upper track, close the position and exits.
TR:=MAX(MAX((HIGH-LOW),ABS(REF(CLOSE,1)-HIGH)),ABS(REF(CLOSE,1)-LOW));
ATR:=MA(TR,26),COLORYELLOW; // Find the simple moving average of TR over 26 periods
C1:REF(C,1)+REF(ATR,1)*0.79; // Upper track
C2:REF(C,1)-REF(ATR,1)*0.79; // Lower track
HIGH>HHV(REF(HIGH,1),10)&&H>=REF(C,1)+REF(ATR,1)*0.79,BPK;
LOW<LLV(REF(L,1),10)&&L<=REF(C,1)-REF(ATR,1)*0.79,SPK;
CROSS(C2,C),SP; // The price breaks through the lower track, and the long position stop-loss is closed
CROSS(C,C1),BP; // The price breaks through the upper track, and the short position stop-loss is closed
AUTOFILTER;
Price breakthrough channel model:
Example 4: Morphological stop-loss model
Idea: The difference between current price and MA is defined as DRD, the sum of DRD in N days divided by the sum of absolute value of DRD. Set 5 as the market entry threshold. If RDV>5, the market entry will be long, and the K-line will have a downward gap, and the market exit will be closed. Set - 5 as the threshold for entering the market. If RDV <- 5, enter the market and go short, and the K-line has an upward gap, and close the position and exit market.
RMA:=MA(CLOSE,15);
DRD:=CLOSE-RMA; // Define the difference between the current price and MA as DRD
NDV:=SUM(DRD,15);
TDV:=SUM(ABS(DRD),15);
RDV:=VALUEWHEN(TDV>0,100*NDV/TDV); // The sum of 15 days DRD divided by the sum of the absolute value of DRD
RDV>5,BPK;
RDV<-5,SPK;
MAX(C,O)<REF(MIN(C,O),1),SP; // If there is a downward gap in the K-line, stop-loss of the long position
MIN(C,O)>REF(MAX(C,O),1),BP; // If there is an upward gap in the K-line, stop-loss of the short position
AUTOFILTER;
Morphological stop-loss model:
Example 5: K-line stop-loss model
Idea: When the two groups of moving averages are arranged in a long position and the current price is higher than the highest price of the previous K-line, enter the market to go long, and one negative line falls below the four moving averages to stop the long position loss. When the two groups of moving averages are in short position and the current price is lower than the lowest price of the last K-line, enter the market to go short, a positive line crossover four moving averages to stop the short position loss.
MA3:MA(CLOSE,3);
MA5:MA(CLOSE,5);
MA10:MA(CLOSE,10);
MA20:MA(CLOSE,20); // SMA combinations
MA5>MA20&&MA3>MA10&&HIGH>=REF(HIGH,1),BPK;
MA5<MA20&&MA3<MA10&&LOW<=REF(LOW,1),SPK;
ISDOWN&&O>MAX1(MA3,MA5,MA10,MA20)&&C<MIN1(MA3,MA5,MA10,MA20),SP;
// One negative line falls below the four moving averages to stop the long position loss
ISUP&&C>MAX1(MA3,MA5,MA10,MA20)&&O<MIN1(MA3,MA5,MA10,MA20),BP;
// A positive line crossover four moving averages to stop the short position loss
AUTOFILTER;
K-line stop-loss model:
Example 6: Index stop-loss model based on BOLL and SAR
Idea: When the highest price is greater than the upper Bollinger Bands, enter the market and go long, the parabolic steering value crosses 0, and stop the long position loss. When the lowest price is less than the lower Bollinger Bands, enter the market and go short, the parabolic steering value goes down 0, and stop the short position loss.
MID:=MA(CLOSE,26); // Find the average closing price of 26 periods, called the middle track of the Bollinger Bands
TMP2:=STD(CLOSE,26); // Find the standard deviation of the closing price over 26 periods
TOP:=MID+2*TMP2; // Bollinger Bands upper track
BOTTOM:=MID-2*TMP2; // Bollinger Bands lower track
STEP1:=2/100;
MVALUE1:=2/10;
SARLINE:SAR(4,STEP1,MVALUE1),CIRCLEDOT;
// Parabolic steering in 4 periods, step length of STEP1, and limit value of MVALUE1
HIGH>=TOP,BPK;
LOW<=BOTTOM,SPK;
CROSS(SARLINE,0),BP; // Parabolic steering value above 0, long position stop-loss
CROSS(0,SARLINE),SP; // Parabolic steering value below 0, short position stop-loss
AUTOFILTER;
The above is the general code framework of each stop-loss model. Readers can choose according to their own needs. The way to trade is to use various strategies and methods flexibly. The importance of stop-loss in a quantitative trading strategy is self-evident. When using the above models, readers should not copy them mechanically. They must check the applicability of their trading targets and models several times, and then conduct multiple back-testing of the simulation bot, make sure the model is correct, and then apply it to the real bot.
From: https://blog.mathquant.com/2023/01/30/principle-and-compilation-of-stop-loss-model.html