Strategy for buying the winners of Python version

Author: Lydia, Created: 2022-12-22 22:04:41, Updated: 2023-09-20 09:22:41

img

Strategy for buying the winners of Python version

Trend strategy generally uses various indicators to judge the market direction, and uses the comparison results of various indicators as trading signals. In this way, it is unavoidable to use parameters and calculate indicators. Now that the parameters are used, there will be a fitting situation. In some markets, the strategy performs very well, but if you are unlucky and the market trend is very unfriendly to the current parameters, the strategy may perform very poorly. Therefore, I deem that the simpler the strategy design is, the better. This strategy will be more robust. Today we will share a trend strategy without indicators. The strategy code is very simple, only 40 lines.

Strategy code:

import time

basePrice = -1
ratio = 0.05
acc = _C(exchange.GetAccount)
lastCancelAll = 0
minStocks = 0.01

def CancelAll():
    while True : 
        orders = _C(exchange.GetOrders)
        for i in range(len(orders)) :
            exchange.CancelOrder(orders[i]["Id"], orders[i])
        if len(orders) == 0 :
            break
        Sleep(1000)

def main():
    global basePrice, acc, lastCancelAll
    exchange.SetPrecision(2, 3)
    while True:
        ticker = _C(exchange.GetTicker)
        if basePrice == -1 :
            basePrice = ticker.Last
        if ticker.Last - basePrice > 0 and (ticker.Last - basePrice) / basePrice > ratio :
            acc = _C(exchange.GetAccount)
            if acc.Balance * ratio / ticker.Last > minStocks :
                exchange.Buy(ticker.Last, acc.Balance * ratio / ticker.Last)
                basePrice = ticker.Last
        if ticker.Last - basePrice < 0 and (basePrice - ticker.Last) / basePrice > ratio : 
            acc = _C(exchange.GetAccount)
            if acc.Stocks * ratio > minStocks :
                exchange.Sell(ticker.Last, acc.Stocks * ratio)
                basePrice = ticker.Last
        ts = time.time()
        if ts - lastCancelAll > 60 * 5 :
            CancelAll()
            lastCancelAll = ts 
        LogStatus(_D(), "\n", "Ticker:", ticker, "\n", "Account information:", acc)
        Sleep(500)

Simple analysis of the strategy

The strategy principle is very simple. It does not use any indicators, it only uses the current price as the transaction trigger basis. There is only one major parameter ratio to control the opening position trigger.

Going long trigger:

if ticker.Last - basePrice > 0 and (ticker.Last - basePrice) / basePrice > ratio

Use the current price to compare with the base price. When the current price is greater than the base price and the price exceeds the ratio * 100%, trigger an order and pend long orders. After the order is placed, the base price is updated to the current price.

Short order trigger:

if ticker.Last - basePrice < 0 and (basePrice - ticker.Last) / basePrice > ratio

The going short direction principle is the same. The current price is used to compare the base price. When the current price is less than the base price and the price exceeds the ’ratio * 100%' ’, the pending order is triggered and the empty order is listed. After the order is placed, the base price is updated to the current price.

The order quantity of each order is ratio * 100% of the available fund value. Place an order unless the calculated order quantity is less than the minimum trading quantity minStocks set by the parameter.

In this way, the strategy follows the price changes to buy the winners.

Backtest

The time range of backtesting is about one year.

img

Running results:

img img img

Recently, some users said that there are few Python strategies. Later, I will share more strategies written in Python. The strategy code is also very simple, which is very suitable for quantitative beginners to learn. Strategy address: https://www.fmz.com/strategy/181185

The strategy is for reference, learning and backtesting only. If you are interested, you can optimize and upgrade it.


Related

More