0bps

Strategy vault / Other

TD Sequential

Python strategy by btccccrazy.

PythonOther
Author
btccccrazy
Complexity
Starter · 77 lines
Parameters
0

The original spec has no written description. Read the source below.

Source Python

'''backtest
start: 2021-01-01 00:00:00
end: 2021-03-12 00:00:00
period: 15m
exchanges: [{"eid":"Futures_OKCoin","currency":"BTC_USD"}]
'''

import time
import pandas as pd
import numpy as np

def main():
    exchange.SetContractType("quarter")
    while True:
        #start_time = time.time()
        records = exchange.GetRecords(PERIOD_H1)
        if len(records) < 100:
            Sleep(1000)
            return
        kline = pd.DataFrame(records)
        kline.columns = ['time','open','high','low','close','volume','OpenInterest']
        i = 0
        j = 0
        for x in range(len(kline)-40,len(kline)):
            if kline['close'].values[x] > kline['close'].values[x-4]:
                i += 1
            else:
                i = 0
            if kline['close'].values[x] < kline['close'].values[x-4]:
                j -= 1
            else:
                j = 0
        TDindex = i if i>0 else j
        #if 13 > TDindex >= 8 :
            #Log('Friendly Reminder: Entering Overbought Area, Consider Selling to Take Profit:')
        #elif 21 > TDindex >= 13 :
            #Log('General warning, if you enter the super overbought area, you may consider selling to take profit.:')
        #elif TDindex >= 21 :
            #Log('Serious warning: if you enter a super overbought zone, consider taking profits by clearing your position:')
        #elif -13 < TDindex <= -8 :
            #Log('Warm warning, if you enter the oversold area, you may consider buying for a rebound.:')
        #elif -21 < TDindex <= -13 :
            #Log('General warning, if you enter the super oversold area, you may consider adding a position to buy.:')
        #elif TDindex <= -21 :
            #Log('Serious warning: entering a super oversold zone, consider going all in:')
        position = exchange.GetPosition()
        #if len(position) > 0:
            #Log(position[0]["Type"])
        if TDindex == 9 or TDindex == 13 or TDindex == 22:  #and len(position) == 0 :
            Log('Upper rootKLine'sTDSequence is:',TDindex)
            exchange.SetDirection("sell")
            id2 = exchange.Sell(-1, 1)
        
        if TDindex == -9 or TDindex == -13 or TDindex == -22:  #and len(position) == 0 :
            Log('Upper rootKLine'sTDSequence is:',TDindex)
            exchange.SetDirection("buy")
            id2 = exchange.Buy(-1, 1)
        if len(position) > 0:
            if position[0]["Type"] ==0 and TDindex >= 2 :
                #Log(position[0]["Type"])
                Log('Upper rootKLine'sTDSequence is:',TDindex)
                exchange.SetDirection("closebuy")
                id2 = exchange.Sell(-1, 1)
            
            if position[0]["Type"] ==1 and TDindex <= -2 :
                #Log(position[0]["Type"])
                Log('Upper rootKLine'sTDSequence is:',TDindex)
                exchange.SetDirection("closesell")
                id2 = exchange.Buy(-1, 1)
        # end_time = time.time()
        #spend_time = end_time - start_time
        Sleep(1000*900)
                
                
                
    

Published by btccccrazy on the FMZ strategy square · last modified 2021-03-16. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.

Build this with AgenKit

A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/TD-Sequential.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. Get tests, backtest, risk controls and a paper-trading switch

Understand it first

Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.

Related specs

esc

Try , , or .

↑↓ move↵ open