The resource loading... loading...

The coin-throwing strategy (●''●)

Author: Peanut butter and jelly, Date: 2020-04-21 17:39:52
Tags:

The full T-Shine public domain, translated by T-Shine Special Policy. Below is a transcript of the video. If you want to know more about the "Quantified World of Thousands" get more strategic source code! I'm also going to advertise for myself. Publications "Quantitative logs of soybeans" I'm not going to go into detail about how I'm going to do this. I'm sure you'll get more benefits.


With a good volatility rate, it's as simple as that to run and win the BTC jackpot! The original, The Ocean Ocean, The Quantified World of Thousands 3 days ago Quantitative strategy research and development is actually a two-way street, very difficult for those just starting out, difficult not only to code at the level of the magic wand, but also difficult at the level of strategic logical thinking. Both are important and should not be biased.

Hello to all the thousands of quantities!

This is the second edition of this special edition, and we are honored to be invited to the O'Reilly O'Reilly Museum to present: How to use the volatility factor to easily win the BTC market, and achieve a decreasing dimensional attack on Bitcoin!

Liu Shen comes from a traditional quantity investment institution, has also been deeply involved in the coin exchange business, has a rich experience and unique insights in the field of quantification. The content of this issue covers the revelation of ideas, coding realization and personal realization, etc.

The following applause: Please tell us about your volatility strategy.

01

The Preface

Hello, today I have the privilege of pushing the article in the thousands of quantum publications, and also thank you for the invitation of the owner of T (one of the thousands of foreign numbers).

The owner of T said to write a quantified, and not give any scope, really do not know where to write from. Then start from your favorite topic to discuss with others. Quantification indicators and strategies (that can be assisted and automated), of course, finally we also add an old saying: investing in the market is risky, to enter the market need to be careful, the strategy is only to provide ideas and lessons, profit and loss.

After the disclaimer, we begin the actual topic below.

02

A simple volatility strategy

People who know me know that I personally don't like alpha games, I'm more of a beta player, and I'm more interested in beta research. So why, e.........mmmmm, I don't know.

The development of quantitative strategies is actually two-sided, it is very difficult for people who are just starting out, it is difficult not only to code at the level of the magic bullet, but also difficult to think strategically at the level of the algorithm. Both are important and should not be biased. Today's strategy is actually inspired by a research paper from HuaThai many years ago.

The strategy algorithm uses the principle of rolling yield fluctuations in the fall of the price of the logarithmic periodic slope, according to which the range is calculated to find the highest and lowest values of the rolling periodic slope, the highest value as the up pipeline, the lowest value as the down pipeline, break the up pipeline, open a position.

The specific graphical visualization interface can be found in the following PPT. The graphic was drawn by myself using Pyecharts.

img

In fact, this strategy is the same as the one I used before doing broad-based ETFs, and of course also used to buy and sell stocks when choosing an index, and then moved directly to the coin circle, and was surprised to find that it really reduces the impact, the parameters do not need to change.

img

The following graph shows the performance of the review for the year, with a screenshot of the code logic for specific parts:

img

The above is actually the reading of the data and the calculation of the indicator data through the pandas.

img

Once the calculation is complete, the data can be output via the pd.to_csv function and the pyecharts used in the screenshot above can be visualized. Note: I used the old version of pyecharts.

All the specific strategies, visualizations, and performance indicator codes are chatter T-shirts.

03

Quantifying

First, good tactics are not afraid of publicity, and this is not a war-level weapon development that will decide life and death, so I and some other agencies or individuals, are not afraid of so-called secret tactics, because in my opinion, CTA has no secrets. This version is the oldest version, and several versions have been upgraded on this basis, for example, adding other conditions of judgment and deterrence, etc., and of course including other varieties of cycle parameters adjustments, etc.

Secondly, many people, whether new or new or even old players, need inspiration, including factor mining of stocks, ideas for timing strategies, etc. These often come from subjective experience, research papers, communication exchanges within the circle, etc.

Finally, to sum up, quantification was originally a byproduct, and programmatic transactions belonged to the subset of quantification, as far back as the university (circa 2009), when TB, pyramid, etc. were being hunted, and if it continues today, it can be said that this part of the earliest prophetic forecasters is 10 years old, not including those who brought back the high-frequency strategies and systems from Wall Street. Therefore, the quantification strategy or programmatic strategy in China has been going on for some time, but in the current market share and players, and policy support, quantification is still a very small part of existence, despite the fact that many analytical and strategic models are built.

Lastly, thank you to the Quantified Quadrant community for their trust in my profession and for the article invitation. If you have any specific code and strategy questions, please email me personally or T Quadrant, I'm also in the T Quadrant group.

Finally, thank you again for the wonderful explanation!

If you haven't joined the Qt discussion group yet, please join the group and get the information!

It's a beautiful building!

img

WeChat wiped out The issue of public attention


/*backtest
start: 2020-01-20 00:00:00
end: 2021-01-19 23:59:00
period: 15m
basePeriod: 5m
exchanges: [{"eid":"Futures_BitMEX","currency":"XBT_USD","fee":[0.008,0.1]}]
args: [["st",0.1]]
*/

// 初始化
exchange.SetContractType('XBTUSD')
_CDelay(100)
// 止盈止损
var TP_status = false // 是否触发追踪止盈 
var TP_HH = 0
var TP_LL = 0
var B = 1

// 获取交易所信息
function UpdateInfo() {
    account = exchange.GetAccount()
    pos = exchange.GetPosition()
    records = exchange.GetRecords()
    ticker = exchange.GetTicker()
}

// 定制本次盈亏
function Onept() {
    // 更新用户信息
    UpdateInfo()
    // 如果现在余额 大于 之前的余额, 那么 盈利次数+1, 且pt_1设为现在余额
    if (account.Stocks - pt_1 > 0) {
        pt_times = pt_times + 1
        Log('这回赚钱啦~~~~ (^U^)ノ~YO', account.Stocks - pt_1)
        B = 1
        pt_1 = account.Stocks
    }
    // 如果现在余额 小于 之前的余额, 那么 亏损次数+1, 且pt_1设为现在余额
    if (account.Stocks - pt_1 < 0) {
        st_times = st_times + 1
        Log('这回亏掉了.... /(ㄒoㄒ)/~~', account.Stocks - pt_1)
        B = B * 1.618
        pt_1 = account.Stocks
    }
}

// 画线
function PlotMA_Kline(records) {
    $.PlotRecords(records, "K")
}

// 追踪止盈 初始%, 追踪U
function TP() {
    var TP_first_long = pos[0].Price + tp_first * ticker.Last
    var TP_trailing_long = TP_HH - trailing_tp * ticker.Last
    var TP_first_short = pos[0].Price - tp_first * ticker.Last
    var TP_trailing_short = TP_LL + trailing_tp * ticker.Last
    // 当多仓时, 现价大于开仓+初始止赢价 -> 触发追踪止盈 
    if ((pos[0].Type == 0) && (ticker.Last > TP_first_long)) {
        // Log('当多仓时, 现价大于开仓+初始止赢价 -> 触发追踪止盈', TP_HH)
        TP_status = true
        // 触发追踪止盈, 未初始化开仓最大价格 -> 开仓后最大价格更新为现价
        if (TP_status === true && TP_HH == 0) {
            Log('触发追踪止盈, 未初始化开仓最大价格 -> 开仓后最大价格更新为现价', TP_HH)
            TP_HH = ticker.Last
        }
        // 触发追踪止盈, 已有开仓后最大价格, 现价大于开仓后最大价格 -> 开仓后最大价格更新为现价
        else if (TP_status === true && TP_HH != 0 && ticker.Last > TP_HH) {
            Log('触发追踪止盈, 已有开仓后最大价格, 现价大于开仓后最大价格 -> 开仓后最大价格更新为现价', TP_HH)
            TP_HH = ticker.Last
        }
        // 触发追踪止盈, 已有开仓后最大价格, 现价小于 (开仓后最大价格减 - 回撤USD) -> 开空平仓止盈
        else if (TP_status === true && TP_HH != 0 && ticker.Last < TP_trailing_long) {
            Log('触发追踪止盈, 已有开仓后最大价格, 现价小于 (开仓后最大价格减 - 回撤USD) -> 开空平仓止盈', TP_HH)
            exchange.SetDirection("closebuy")
            exchange.Sell(ticker.Buy, pos[0].Amount, "在" + ticker.Last + "止赢平多仓!! 开仓价格: " + pos[0].Price + "数量: " + pos[0].Amount)
            $.PlotFlag(new Date().getTime(), 'Sell', 'PT_BK' + ticker.Sell)
            Onept()
            TP_status = false
            TP_HH = 0
        }
    }
    // 当空仓时, 现价小于开仓-初始止赢价 -> 触发追踪止盈
    else if ((pos[0].Type == 1) && (ticker.Last < TP_first_short)) {
        // Log('当空仓时, 现价小于开仓-初始止赢价 -> 触发追踪止盈', TP_LL)
        TP_status = true
        // 触发追踪止盈, 未初始化开仓最大价格 -> 开仓后最小价格更新为现价
        if (TP_status === true && TP_LL == 0) {
            Log('触发追踪止盈, 未初始化开仓最大价格 -> 开仓后最小价格更新为现价', TP_LL)
            TP_LL = ticker.Last
        }
        // 触发追踪止盈, 已有开仓后最小价格, 现价小于开仓后最小价格 -> 开仓后最小价格更新为现价
        else if (TP_status === true && TP_LL != 0 && ticker.Last < TP_LL) {
            Log('触发追踪止盈, 已有开仓后最小价格, 现价小于开仓后最小价格 -> 开仓后最小价格更新为现价', TP_LL)
            TP_LL = ticker.Last
        }
        // 触发追踪止盈, 已有开仓后最小价格, 现价大于 (开仓后最小价格减 + 回撤USD) -> 开多平仓止盈
        else if (TP_status === true && TP_LL != 0 && ticker.Last > TP_trailing_short) {
            Log('触发追踪止盈, 已有开仓后最小价格, 现价大于 (开仓后最小价格减 + 回撤USD) -> 开多平仓止盈', TP_LL)
            exchange.SetDirection("closesell")
            exchange.Buy(ticker.Sell, pos[0].Amount, "在" + ticker.Last + "止赢平空仓!! 开仓价格: " + pos[0].Price + "数量: " + pos[0].Amount)
            $.PlotFlag(new Date().getTime(), 'Buy', 'PT_SK' + ticker.Sell)
            Onept()
            TP_status = false
            TP_LL = 0
        }
    }
}

// 止损 %
function Stoploss() {
    // 当多仓时, 现价小于开仓-止损价, 做空平多
    if ((pos[0].Type == 0) && (ticker.Last < pos[0].Price - st * ticker.Last)) {
        Log('当多仓时, 现价小于开仓-止损价, 做空平多')
        exchange.SetDirection("closebuy")
        exchange.Sell(ticker.Buy, pos[0].Amount, "在" + ticker.Last + "止损平多仓!! 开仓价格: " + pos[0].Price + "数量: " + pos[0].Amount)
        $.PlotFlag(new Date().getTime(), 'Sell', 'ST_BK' + ticker.Buy)
        Onept()
    }
    // 当空仓时, 现价大于开仓+止损价, 做多平空
    else if ((pos[0].Type == 1) && (ticker.Last > pos[0].Price + st * ticker.Last)) {
        Log('当空仓时, 现价大于开仓+止损价, 做多平空')
        exchange.SetDirection("closesell")
        exchange.Buy(ticker.Sell, pos[0].Amount, "在" + ticker.Last + "止损平空仓!! 开仓价格: " + pos[0].Price + "数量: " + pos[0].Amount)
        $.PlotFlag(new Date().getTime(), 'Buy', 'ST_SK' + ticker.Sell)
        Onept()
    }
}

// 计算凯利公式 仓位
function PriceAmount() {
    // 赢可以赢多少 
    y = tp_first
    // 输会输多少 
    s = st
    //赔率
    b = y / s
    // 赢的概率
    if (total_times < 10) {
        p = 0.382
    } else {
        p = pt_times / total_times
    }
    // 输的概率
    q = 1 - p
    // 凯莉公式
    f = (b * p - q) / b
    // 限制B最大值
    if (B > 16.18) {
        B = 16.18
    }
    //Amount = _N(Math.abs(f) * account.Stocks * ticker.Last * B, 0)
    Amount = _N(0.618 * account.Stocks * ticker.Last, 0)
    //Log(Amount)
}

// 交易逻辑
function onTick() {
    // 获取均匀分布 0-9 随机数
    ToTheMoon = Math.floor(Math.random() * 10)
    // 无仓位时
    if (pos.length == 0) {
        // Long 
        if (ToTheMoon > 5) {
            exchange.SetDirection("buy")
            exchange.Buy(ticker.Sell, Amount)
            $.PlotFlag(new Date().getTime(), 'Buy', 'BK' + ticker.Sell)
            total_times = total_times + 1
        }
        // Short 
        if (ToTheMoon < 4) {
            exchange.SetDirection("sell")
            exchange.Sell(ticker.Buy, Amount)
            $.PlotFlag(new Date().getTime(), 'Sell', 'SK' + ticker.Buy)
            total_times = total_times + 1
        }
    }
        // 多仓时
    if (pos.length > 0 && pos[0].Type == 0) {
        // 平多 
        if (ToTheMoon < 1) {
            exchange.SetDirection("closebuy")
            exchange.Sell(ticker.Buy, pos[0].Amount)
            $.PlotFlag(new Date().getTime(), 'Sell', 'PBK')
            Onept()
        }
    }
    // 空仓时
    if (pos.length > 0 && pos[0].Type == 1) {
        // 平空 
        if (ToTheMoon > 8) {
            exchange.SetDirection("closesell")
            exchange.Buy(ticker.Sell, pos[0].Amount)
            $.PlotFlag(new Date().getTime(), 'Buy', 'PSK')
            Onept()
        }
    }
}


function main() {
    UpdateInfo()
    // 统计
    pt_1 = account.Stocks
    total_times = 0
    pt_times = 0
    st_times = 0
    while (1) {
        UpdateInfo()
        PriceAmount()
        onTick()
        PlotMA_Kline(records)
        if (pos.length > 0) {
            TP()
        }
        if (pos.length > 0) {
            Stoploss()
        }
        LogStatus("总余额: " + _N(ticker.Last * account.Stocks, 2), " 下单量: " + Amount, " 下单倍数: " + B, " ToTheMoon: " + ToTheMoon, " 下单量比: " + _N(Amount * 100 / _N(ticker.Last * account.Stocks, 2), 2), "% 胜率: " + _N(p * 100, 2), "%", total_times, pos)
    }
}

More

AllyCan it be converted to a beta version?