Hans 123 breakthrough strategy in days

Posted by Stille on Wed, 19 Feb 2020 11:06:08 +0100

Preface

HANS123 strategy was first used in the foreign exchange market. Its trading mode is relatively simple and belongs to the trend breakthrough system. This trading mode can enter the market at the first time when the trend is formed, so it is favored by many traders. So far, HANS123 has expanded many versions. Today, we will understand HANS123 strategy together.

Strategy principle

Some people think that the morning opening is the time when market divergence is the biggest, and it is easy to see the situation of unclear price trend direction and wide volatility. After 30 minutes or so, the market fully digests all kinds of overnight information, and the price trend will tend to be rational and return to normal. That is to say: the market trend in the first 30 minutes or so basically constitutes today's overall trading pattern.

  • On track: maximum price within 30 minutes of opening
  • Down track: lowest price within 30 minutes of opening

The relative high and low points generated at this time form the effective high and low points in Dow Theory, and HANS123 strategy is the transaction logic established by this. In the domestic futures market, it will open at 09:00 a.m. and 09:30 a.m. to judge whether it is a long or a short today. When the price breaks through the high point, it is easy for the price to continue to rise; when the price breaks through the low point, it is easy for the price to continue to fall.

  • Long position opening: there is no position at present, and the price breaks through the track up
  • Short position opening: there is no position at present, and the price breaks down

Although the breakthrough strategy can enter in the first time when the trend is formed. But this advantage is also a double-edged sword. The result of sensitive admission is that the price breakthrough fails. So it is necessary to set stop loss. At the same time, in order to achieve the strategic logic of winning, offsetting, losing and shrinking, it is also necessary to set up a profit stop.

  • Long stop loss: hold multiple orders at present and reach the loss amount
  • Short stop loss: the current short holding order reaches the loss amount
  • Long stop profit, hold multiple documents at present, and reach the profit amount
  • Short stop profit, currently hold short order, reach profit amount

Strategy compilation

Open: fmz.com website > login > control center > policy Library > new policy > click the drop-down menu in the upper right corner to select Python language and start to write the policy. Pay attention to the comments in the following code.

Step 1: write a strategic framework

# Policy main function
def onTick():
    pass


# Program entrance
def main():
    while True:  # Enter infinite loop mode
        onTick()  # Execute policy main function
        Sleep(1000)  # Dormancy for one second

Write the strategy framework, which has been learned in the previous chapters. One is the onTick function, and the other is the main function, in which the onTick function is executed in infinite loops in the main function.

Step 2: define global variables

up_line = 0  # Upper rail
down_line = 0  # Lower rail
trade_count = 0  # Transaction times of the day

Because the up and down tracks are only counted at 09:30, and the rest of the time is not counted, we need to write these two variables outside the loop. In addition, in order to limit the number of transactions in a day, the trade ﹣ count variable is also written outside the cycle. Before using these two global variables in the onTick policy main function, you need to use the global keyword reference.

Step 3: get data

exchange.SetContractType("rb888")  # Subscription futures
bar_arr = _C(exchange.GetRecords, PERIOD_M1)  # Get 1 minute K-line array
current_close = bar_arr[-1]['Close']  # Get the latest price
if len(bar_arr) < 50:  # If less than 50 K lines
    return  # Return to continue waiting for data

To obtain data, first use the SetContractType function in the inventor's quantification API to subscribe to futures varieties, and then use the GetRecords function to obtain the K-line array. You can also pass in the K-line array of the specified PERIOD_M11 minute when using the GetRecords function.

The next step is to obtain the latest price, which is used to determine the position relationship between the current price and the up and down tracks. At the same time, when the Buy or Sell function is used for an order transaction, the specified price needs to be passed in. In addition, don't forget to filter the number of K-lines, because if there are too few K-lines, there will be an error that can't be calculated.

Step 4: process the time function

def current_time():
    current_time = bar_arr[-1]['Time']  # Get the current K-line timestamp
    time_local = time.localtime(current_time / 1000)  # Processing timestamp
    hour = time.strftime("%H", time_local)  # Format the timestamp and get the hours
    minute = time.strftime("%M", time_local)  # Format the timestamp and get the minutes
    if len(minute) == 1:
        minute = "0" + minute
    return int(hour + minute)

When calculating the up and down track and placing an order transaction, it is necessary to judge whether the current time conforms to the transaction time specified by us. Therefore, for the convenience of judgment, we need to deal with the specific hours and minutes of the current K-line.

Step 5: calculate the up and down rails

global up_line, down_line, trade_count  # Introducing global variables
current_time = current_time()  # processing time
if current_time == 930:  # If the latest K line time is 09:30
    up_line = TA.Highest(bar_arr, 30, 'High') + count  # Top 30 K lines
    down_line = TA.Lowest(bar_arr, 30, 'Low') - count  # The lowest price of the first 30 K lines
    trade_count = 0  # Reset transactions to 0

To calculate the up and down tracks, we first need to introduce the global variables we defined before, and use the global keyword. Imagine that we need to calculate the highest price and the lowest price between 09:00 and 09:30, because we use one minute K-line data, that is, when the time is 09:30, there are just 30 k-lines, so we can directly calculate the highest price and the lowest price of these 30 k-lines.

Step 6: acquire position

position_arr = _C(exchange.GetPosition)  # Get position array
if len(position_arr) > 0:  # If the position array length is greater than 0
    position_arr = position_arr[0]  # Get position dictionary data
    if position_arr['ContractType'] == 'rb888':  # If the position is equal to the subscription
        if position_arr['Type'] % 2 == 0:  # If it's more than one
            position = position_arr['Amount']  # Assigned position quantity is positive
        else:
            position = -position_arr['Amount']  # Assigned position quantity is negative
        profit = position_arr['Profit']  # Gain and loss on position
else:
    position = 0  # Assigned position quantity is 0
    profit = 0  # Assigned position profit and loss is 0

Position status involves strategic logic. In the first ten courses, we always use virtual position, but in the real trading environment, it is better to use GetPosition function to obtain the real position information, including position direction, position profit and loss, position quantity, etc.

Step 7: place an order

# If the closing is near or the stop is reached
if current_time > 1450 or profit > stop * 3 or profit < -stop:
    if position > 0:  # If you hold more than one
        exchange.SetDirection("closebuy")  # Set transaction direction and type
        exchange.Sell(current_close - 1, 1)  # Ping duo Shan
    elif position < 0:  # If blank
        exchange.SetDirection("closesell")  # Set transaction direction and type
        exchange.Buy(current_close + 1, 1)  # Flat bill
# If there is no current position, and it is less than the specified number of transactions, and within the specified transaction time
if position == 0 and trade_count < 2 and 930 < current_time < 1450:
    if current_close > up_line:  # If the price is higher than the upper rail
        exchange.SetDirection("buy")  # Set transaction direction and type
        exchange.Buy(current_close + 1, 1)  # Open multiple
        trade_count = trade_count + 1  # Number of transactions plus one
    elif current_close < down_line:  # If the price is less than the lower track
        exchange.SetDirection("sell")  # Set transaction direction and type
        exchange.Sell(current_close - 1, 1)  # Empty list
        trade_count = trade_count + 1  # Number of transactions plus one

In order to avoid logical errors in the strategy, it is better to write the closing logic in front of the opening logic. In this strategy, when opening a position, it is necessary to judge the current position status, whether it is within the specified trading time, and then judge the relationship between the current price and the up and down track. To close a position is to first judge whether it is close to the end of the market, or whether it meets the stop loss and gain conditions.

HANS123 is a very typical and effective automatic trading strategy. Its basic principle is to break through the highest price or the lowest price of the previous market in a certain period of time to do long or short. After the optimization of stop loss and stop profit and other parameters, this system can be applied to almost all foreign exchange varieties with stable profits. This is also an early entry mode of trading, with appropriate filtering technology, or to improve its odds.

Complete strategy

Click to copy the complete policy source code https://www.fmz.com/strategy/179805 No need to configure direct back testing

Ending

The above is the principle and code analysis of HANS123 strategy. In fact, HANS123 strategy provides a better entry time. You can also improve the entry time according to your own understanding of the market and the understanding of the transaction, or you can optimize the profit and loss stop parameters according to the variety volatility to achieve better results.

Topics: Blockchain less Python REST