Introduction to Python Simple small scripts - statistical one-sided continuous fields

Author: bb, Created: 2018-07-23 15:53:45, Updated: 2018-07-24 12:27:27

Recently I've been studying Quantitative Math, and I'm not sure what the basics are, so knock something slowly.

Today, a small script was tapped to count the number of one-sided trends (continuous ups or downs) over the last 50 days (it seems that the platform can only test for 50 days with OK analogue). I was going to write a ↓. It's really easy to identify shocks and trends!https://www.fmz.cn/bbs-topic/1638The idea of using the new lows of the next cycle to contrast the new highs of this cycle to judge the trend; both up and down trends can be used. Thank you for this article, but when I was testing, I set the K-line cycle to only 50 days, and the set time interval didn't work, so I lacked data support.

The effect is as follows:

img

The output order is: id pump> Time pump> Stage maximum pump> Stage minimum pump> Falling state pump> Continuous number of times

  • Filtering single drop and hold trades such as: drop> drop> drop> drop> drop> drop> drop> drop> drop> this state does not print without the use of continuity, which is not helpful for analysis.
  • The highest price of the next cycle can be contrasted with the highest price of the current cycle. Low can be contrasted with the current cycle. High can be contrasted with the current cycle.
  • It's really embarrassing to retest the time of the transaction, for example, the 3-minute line is 00:57>01:00>01:57>02:00>02:57... why???
  • I've been asked not to be too arrogant and not to write Python.

Do you have any soup?

  • I think it's useful, at least better than staring, but the time cycle is too short and I can't see the law of the jungle.
  • However, if the current market situation, according to the calendar, has seen a unilateral rise in the market for 12 consecutive days, what are the chances of a rise on the 13th day?
  • I'm the one who came up with the idea to do this test, not to please anyone, but to change my mind and try it.
  • Welcome to the exchange, I'm a normal lobster.

Below is some streamlined code.


def main():
    k_line = exchange.GetRecords(PERIOD_M1) #设置1分钟K线数据
    i1 = 0 #i1 i2分别记录涨、跌次数
    i2 = 0
    j_c = 0 #用作记录所有持续行情次数总和的变量
    for i,k_line_single in enumerate(k_line): #i为记录循环次数 从0开始 k_line_single为单行记录文本
        otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", time.localtime(k_line_single.Time / 1000))#时间戳转换代码
        state_text = "" #重置state_text 避免状态连续继承  状态文本
        Duration = "" #重置持续行情次数变量
        if k_line_single.High > k_line[i-1].High: #判断最高价高于前一周期最高价的情况         
            i2 = 0 #重置连续下跌计数变量
            if k_line[i-1].High > k_line[i-2].High or k_line[i+1].High > k_line_single.High: #当高于前一周期或后一周期高于此周期价
                state_text = "连续上涨UP UP UP!"
                i1 += 1 #记录连续上涨次数
        elif k_line_single.High < k_line[i-1].High: #判断不为持平状态  
            i1 = 0
            if k_line[i-1].High < k_line[i-2].High or k_line[i+1].High < k_line_single.High:#当前一周期价格低于前前一周期 或 后一周期价格低于当前周期,表示一定是连续下跌
                state_text = "连续下跌LOW LOW !" 
                i2 += 1 #记录连续下跌次数
        else:
            i1 = 0 #重置连续行情次数
            i2 = 0
        if state_text == "连续上涨GO GO GO!" or state_text == "连续下跌LOW LOW !": #只有当连续上涨或下跌时 才会打印结果 
            j_c += 1
            Duration = "Duration:",i1 + i2 #连续单边行情持续次数
            Log(i+1,"-- Time:",otherStyleTime,"-- High:",k_line_single.High,"-- Low:",k_line_single.Low,"--》",state_text,Duration)
    Log("运行完毕。。符合条件的总数为:",j_c)

And then there's a code that doesn't have the same effect as the streamlined code, which is 21 more lines of code than the above.

def main():
    k_line = exchange.GetRecords(PERIOD_M1)
    state_text = "" #状态文本
    i1 = 0
    i2 = 0
    i3 =0
    state_3 = 0 #行情状态 涨=1 跌=2 平=3
    j_c = 0 #用作记录所有持续行情次数总和的变量
    for i,k_line_single in enumerate(k_line): #i为记录循环次数 从0开始 k_line_single为单行记录文本
        timeStamp = k_line_single.Time / 1000 #时间戳转换代码开始
        timeArray = time.localtime(timeStamp)
        otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", timeArray)#时间戳转换代码结束
        
        if k_line_single.High > k_line[i-1].High: #判断最高价高于前一周期最高价的情况
            state_text = "价格出现上涨"
            i2 = 0
            i3 = 0
            state_3 = 1
            if k_line[i-1].High > k_line[i-2].High or k_line[i+1].High > k_line_single.High: #当高于前一周期或后一周期高于此周期价
                state_text = "连续上涨GO GO GO!"
                i1 += 1
        else:
            if k_line_single.High != k_line[i-1].High:
                state_text = "价格出现下跌"
                i1 = 0
                i3 = 0
                state_3 = 2
                if k_line[i-1].High < k_line[i-2].High or k_line[i+1].High < k_line_single.High:
                    state_text = "连续下跌LOW LOW !"
                    i2 += 1
            else:
                state_text = "价格相比持平"
                i1 = 0
                i2 = 0
                state_3 = 3
                if k_line[i-1].High == k_line[i-2].High:# or k_line[i+1].High == k_line_single.High:
                    state_text = "连续持平PING PING !"
                    i3 = 0 #不调试持平持续数量了
        if state_3 != 3: #不打印持平的结果
            Duration = i1 + i2 + i3
            if Duration == 0:
                Duration = ""
            else:
                Duration = "Duration:",i1 + i2 + i3
            if i1 != 0 or i2 != 0: #只有当连续上涨或下跌时 才会打印结果
                j_c += 1
                Log(i+1,"-- Time:",otherStyleTime,"-- High:",k_line_single.High,"-- Low:",k_line_single.Low,"--》",state_text,Duration)
    Log("运行完毕。。符合条件的总数为:",j_c)

More

ZeroThank you for sharing, there's a new version, the forum is in markdown format.

bbI'm sorry, but I've only been able to solve half of the code I sent.

qq89520Thank you for sharing.

bbThank you very much.