Type/to search
0
Follow
80
Followers
Crocodile line trading system Python version
Discussions
Created 2020-05-07 14:33:19  Updated 2025-01-14 20:38:03
 0
 1557

img

Summary

People who have done financial trading will probably have an experience. Sometimes the price fluctuations are regular, but more often it shows an unstable state of random walk. It is this instability that is where market risks and opportunities lie. Instability also means unpredictable, so how to make returns more stable in an unpredictable market environment is also a problem for every trader. This article will introduce the crocodile trading rules strategy, hoping to inspire everyone.

What is a crocodile line

img

The crocodile line is actually three special moving averages, which correspond to the chin of the blue line, the teeth of the red line, and the upper lip of the green line. The chin is a 13-period moving average and moves 8 bars in the future. The tooth is an 8-period moving average and moves 5 bars in the future. The upper lip is a 5-period moving average and moves 3 bars in the future.

Principle of crocodile line

The crocodile line is a set of technical analysis methods summarized based on geometry and nonlinear dynamics. When the crocodile's chin, teeth and upper lip are closed or entangled, it means that the crocodile is asleep. At this time, we usually stay outside the market until the fragment appears, and only participate in the obvious trend market.

The longer the crocodile sleeps, the more hungry it will be when it wakes up, so once it wakes up, it will open its mouth wide. If the upper lip is above the teeth and the teeth are above the chin, it indicates that the market has entered a bull market and the crocodiles are going to eat beef. If the upper lip is below the teeth and the teeth are below the chin, it indicates that the market has entered a bear market and the crocodiles are going to eat bear meat. Until it is full, it will then close its mouth again (hold and make a profit).

Crocodile line calculation formula

Upper lip = REF(SMA(VAR1,5,1),3)
Teeth = REF(SMA(VAR1,8,1),5)
Chin = REF(SMA(VAR1,13,1)

Crocodile strategy composition

Step 1: Write a strategy framework

python
# Strategy main function def onTick():      pass # Program entry def main ():      while True: # Enter infinite loop mode          onTick() # execute strategy main function          Sleep(1000) # sleep for 1 second

FMZ using the polling mode, one is the onTick function, and the other is the main function, in which the onTick function is executed in an infinite loop in the main function.

Step 2: Import Python library

javascript
import talib import numpy as np

The SMA function is used in our strategy. SMA is the arithmetic mean. There are already ready-made SMA functions in the talib library, so directly import the talib Python library and then call it directly. Because when calling this function, you need to pass in numpy format parameters, so we need to use import to import these two Python libraries at the beginning of the strategy.

Step 3: Convert K-line array data

python
# Convert the K-line array into an array of highest price, lowest price, and closing price, for conversion to numpy.array def get_data(bars): arr = [] for i in bars: arr.append(i['Close']) return arr

Here we created a get_data function, the purpose of this function is to process the ordinary K-line array into numpy format data. The input parameter is a K-line array, and the output result is processed data in numpy format.

Step 4: Obtain position data

python
# Get the number of positions def get_position ():      # Get position      position = 0 # The number of assigned positions is 0      position_arr = _C (exchange.GetPosition) # Get array of positions      if len (position_arr)> 0: # If the position array length is greater than 0          for i in position_arr:              if i ['ContractType'] == 'rb000': # If the position symbol is equal to the subscription symbol                  if i ['Type']% 2 == 0: # If it is long position                      position = i ['Amount'] # Assigning a positive number of positions                  else:                      position = -i ['Amount'] # Assigning a negative number of positions      return position

Position status involves strategy logic. Our first ten lessons have always used virtual positions, but in a real trading environment it is best to use the GetPosition function to obtain real position information, including: position direction, position profit and loss, number of positions, etc.

Step 5: Get the data

python
exchange.SetContractType('rb000') # Subscribe the futures varieties      bars_arr = exchange.GetRecords() # Get K line array      if len(bars_arr) < 22: # If the number of K lines is less than 22          return

Before acquiring data, you must first use the SetContractType function to subscribe to relevant futures varieties. FMZ supports all Chinese commodity futures varieties. After subscribing to the futures symbol, you can use GetRecords function to obtain K-line data, which returns an array.

Step 6: Calculate the data

python
np_arr = np.array (get_data (bars_arr)) # Convert closing price array sma13 = talib.SMA (np_arr, 130) [-9] # chin sma8 = talib.SMA (np_arr, 80) [-6] # teeth sma5 = talib.SMA (np_arr, 50) [-4] # upper lip current_price = bars_arr [-1] ['Close'] # latest price

Before calculating the SMA using the talib library, you need to use the numpy library to process the ordinary K-line array into numpy data. Then get the chin, teeth and upper lip of the crocodile line separately. In addition, the price parameter needs to be passed in when placing an order, so we can use the closing price in the K-line array.

Step 7: Place an order

python
position = get_position () if position == 0: # If there is no position      if current_price> sma5: # If the current price is greater than the upper lip          exchange.SetDirection ("buy") # Set the trading direction and type          exchange.Buy (current_price + 1, 1) # open long position order      if current_price <sma13: # If the current price is less than the chin          exchange.SetDirection ("sell") # Set the trading direction and type          exchange.Sell (current_price-1, 1) # open short position order      if position> 0: # If you have long positions      if current_price <sma8: # If the current price is less than teeth          exchange.SetDirection ("closebuy") # Set the trading direction and type          exchange.Sell (current_price-1, 1) # close long position if position <0: # If you have short position      if current_price> sma8: # If the current price is greater than the tooth          exchange.SetDirection ("closesell") # Set the trading direction and type          exchange.Buy (current_price + 1, 1) # close short position

Before placing an order, you need to get the actual position. The get_position function we defined earlier will return the actual number of positions. If the current position is long, it will return a positive number. If the current position is short, it will return a negative number. If there is no position, returns 0. Finally, the buy and sell functions are used to place orders according to the above trading logic, but before this, the trading direction and type also need to be set.

Complete strategy

'' 'backtest start: 2019-01-01 00:00:00 end: 2020-01-01 00:00:00 period: 1h exchanges: [{"eid": "Futures_CTP", "currency": "FUTURES"}] '' ' import talib import numpy as np # Convert the K-line array into an array of highest price, lowest price, and closing price, used to convert to numpy.array type data def get_data (bars):     arr = []     for i in bars:         arr.append (i ['Close'])     return arr # Get the number of positions def get_position ():     # Get position     position = 0 # The number of assigned positions is 0     position_arr = _C (exchange.GetPosition) # Get array of positions     if len (position_arr)> 0: # If the position array length is greater than 0         for i in position_arr:             if i ['ContractType'] == 'rb000': # If the position symbol is equal to the subscription symbol                 if i ['Type']% 2 == 0: # If it is long                     position = i ['Amount'] # Assign a positive number of positions                 else:                     position = -i ['Amount'] # Assign a negative number of positions     return position           # Strategy main function def onTick ():     # retrieve data     exchange.SetContractType ('rb000') # Subscribe to futures varieties     bars_arr = exchange.GetRecords () # Get K line array     if len (bars_arr) <22: # If the number of K lines is less than 22         return          # Calculation     np_arr = np.array (get_data (bars_arr)) # Convert closing price array     sma13 = talib.SMA (np_arr, 130) [-9] # chin     sma8 = talib.SMA (np_arr, 80) [-6] # teeth     sma5 = talib.SMA (np_arr, 50) [-4] # upper lip     current_price = bars_arr [-1] ['Close'] # latest price     position = get_position ()     if position == 0: # If there is no position         if current_price> sma5: # If the current price is greater than the upper lip             exchange.SetDirection ("buy") # Set the trading direction and type             exchange.Buy (current_price + 1, 1) # open long position order         if current_price <sma13: # If the current price is less than the chin             exchange.SetDirection ("sell") # Set the trading direction and type             exchange.Sell (current_price-1, 1) # open short position order              if position> 0: # If you have long positions         if current_price <sma8: # If the current price is less than teeth             exchange.SetDirection ("closebuy") # Set the trading direction and type             exchange.Sell (current_price-1, 1) # close long position     if position <0: # If you have short positions         if current_price> sma8: # If the current price is greater than the tooth             exchange.SetDirection ("closesell") # Set the trading direction and type             exchange.Buy (current_price + 1, 1) # close short position              # Program main function def main ():     while True: # loop         onTick () # execution strategy main function         Sleep (1000) # sleep for 1 second

Directly click the link below to copy the complete strategy without configuration:
https://www.fmz.com/strategy/199025

End

The biggest role of the crocodile trading rule is to help us maintain the same direction as the market when trading, regardless of how the current market price changes, and continue to profit until the consolidation market appears. The crocodile line can be used well with other MACD and KDJ indicators.

Comment
All comments (0)
No data
No data
  • 1
iPhone Download
Forums
PINE Language
© 2015 - ∞ INVENTOR PTE LTD (SG)