A, in this paper,

“A buy on the fall, a sell on the rise” may be a lot of transactions in the minds of beginners confused, is there really banker can see his account through the background? The answer is negative, accurate to say is not banker target you, but target “you this group of retail investors”. Encounter obstacles while driving, for example, most people will escape, trade the same is true, most retail investors see good will also have the same reflection, will open more warehouse charge in, when the number of retail investors, after buying in more and more retail bags for sale, because there is no buy force support, prices will come down, this is herd behaviour.

What is engulfed form

How can this be avoided? Can only increase the professional knowledge reserve, armed with quantitative trading tools, this paper to The Japanese candle chart technology in the engulfing pattern as an example to develop a trading strategy. This strategy is developed in Python using inventor’s (FMZ.COM) quantitative trading platform.

What is engulfing form? Engulf form is also called holding line form, it is composed of 2 K line composite form (as shown in the figure above), many domestic traders will be “Yang bao Yin” or “Yin Bao Yang” to express the engulf form. Among them, “Yang bao Yin” is rising engulfing form, “Yin Bao Yang” is falling engulfing form. From the diagram, it can be seen that the rising engulfing pattern is a big Yin line covering the front Yin line, while the falling engulfing pattern is a big Yin line covering the front Yang line.

3. The meaning of form

Engulfing form is the appearance of market state, but it actually contains the psychological and capital game between traders. It indicates that the market price trend is about to reverse, in practical use, the engulfing pattern will play a very good effect on analysis and trading. In particular, in and out of the field, or stop surplus stop, the engulfing form can be a good reference. As shown below:

In the left arrow of the figure above, the market was originally in a downward trend, but then a green sun line appeared, which swallowed up the red Yin line before it, forming a stage bottom. This situation shows that the market strength of bulls began to exceed that of bears. In the right arrow, the market was in a rising trend, but then there was a red big Yin line, which swallowed up the green positive line before it, forming a stage top, this situation shows that the market bearish power began to exceed the strength of the bulls.

4. Strategy implementation

Step 1: Set up the backtest configuration

'''backtest
start: 2015-02-22 00:00:00
end: 2021-05-25 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
'''
Copy the code

Step 2: Import the library

import talib
import numpy as np
Copy the code

Step 3: Write the policy framework

def on_tick():
    pass


def main():
    while True:
        on_tick()
        Sleep(1000)
Copy the code

In the above strategy framework, the program first executes main(), then goes into infinite loop mode and executes on_tick() repeatedly, so in this framework, you just need to write the strategy logic in on_tick().

Step 4: Capture and process data

def get_ohlc(records):
    dic = {}
    open_price = []
    high_price = []
    low_price = []
    close_price = []
    for i in records:
        open_price.append(i['Open'])
        high_price.append(i['High'])
        low_price.append(i['Low'])
        close_price.append(i['Close'])
    return {
        'open': np.array(open_price),
        'high': np.array(high_price),
        'low': np.array(low_price),
        'close': np.array(close_price),
    }

def on_tick():
    records = exchange.GetRecords()
    ohlc = get_ohlc(records)
    engulfed = talib.CDLENGULFING(ohlc['open'], ohlc['high'], ohlc['low'], ohlc['close']).tolist()
    depth = exchange.GetDepth()
    ask = depth.Asks[0].Price
    bid = depth.Bids[0].Price
Copy the code

The advantage of using Python writing strategy is that many third-party libraries can be used. For example, many technical indicators can be obtained from talib library. In this strategy, the engulfing CDLENGULFING() function in Talib library is used. So we first create a get_OHlc function that converts k-line data into numpy array data, as shown in the code above.

Step 5: Place a trade

    global mp
    if mp == 1 and engulfed[-1] == 100:
        exchange.SetDirection("closebuy")
        exchange.Sell(bid, 1)
        mp = 0
    if mp == -1 and engulfed[-1] == -100:
        exchange.SetDirection("closesell")
        exchange.Buy(ask, 1)
        mp = 0
    if mp == 0 and engulfed[-1] == -100:
        exchange.SetDirection("buy")
        exchange.Buy(ask, 1)
        mp = 1
    if mp == 0 and engulfed[-1] == 100:
        exchange.SetDirection("sell")
        exchange.Sell(bid, 1)
        mp = -1
Copy the code

Before placing an order, a global variable MP should be created in the external global environment first. The function of this variable is to simulate the current position status. The initial value of MP is 0, and mp is reset to 1 after opening long orders, -1 after opening short orders and 0 after closing positions. In this order trade condition, we only use the simplest strategic logic, namely, when there is a uptrend engulf pattern and ATR greater than 30, flat long open, when there is a downtrend engulf pattern and ATR greater than 30. Now let’s see how this strategy logic works.

5. Strategy backtest

  • Start date: 2015-02-22
  • Test end date: 2021-04-18
  • Data type: Rapeseed meal index
  • Data cycle: daily line
  • Slip point: 2 jumps for each open position

Back to the test configuration

To measure performance

Money curve

Sixth, the complete strategy

'''backtest
start: 2015-02-22 00:00:00
end: 2021-05-25 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
'''

import talib
import numpy as np

mp = 0

def get_ohlc(records):
    dic = {}
    open_price = []
    high_price = []
    low_price = []
    close_price = []
    for i in records:
        open_price.append(i['Open'])
        high_price.append(i['High'])
        low_price.append(i['Low'])
        close_price.append(i['Close'])
    return {
        'open': np.array(open_price),
        'high': np.array(high_price),
        'low': np.array(low_price),
        'close': np.array(close_price),
    }

def on_tick():
    records = exchange.GetRecords()
    ohlc = get_ohlc(records)
    engulfed = talib.CDLENGULFING(ohlc['open'], ohlc['high'], ohlc['low'], ohlc['close']).tolist()
    atr = TA.ATR(records, 14)[-1]
    depth = exchange.GetDepth()
    ask = depth.Asks[0].Price
    bid = depth.Bids[0].Price
    global mp
    if mp == 1 and engulfed[-1] == 100:
        exchange.SetDirection("closebuy")
        exchange.Sell(bid, 1)
        mp = 0
    if mp == -1 and engulfed[-1] == -100:
        exchange.SetDirection("closesell")
        exchange.Buy(ask, 1)
        mp = 0
    if mp == 0 and engulfed[-1] == -100 and atr > 30:
        exchange.SetDirection("buy")
        exchange.Buy(ask, 1)
        mp = 1
    if mp == 0 and engulfed[-1] == 100 and atr > 30:
        exchange.SetDirection("sell")
        exchange.Sell(bid, 1)
        mp = -1
   
def main():
    exchange.SetContractType('RM000')
    while True:
        on_tick()
        Sleep(1000)

Copy the code

www.fmz.com/strategy/28…