Tradingview策略分享2-LinRegCandles UT MIX

视频描述

视频内容:

哈罗小伙伴们,这期分享一个策略 收益 800%?  LinRegCandles UT MIX

打开tradingview  指标栏 加入第一个指标 Linear Regression Candles     ugurvu

加入第二个指标 UT Bot Alerts                      QuantNomad

K线设置, ma线颜色设置

KT量化整理成交易策略最终形态   参数设置 2h

指标总体思路:BUY信号,价格橙色线以上做多 ,   SELL信号,价格橙色线以下做空

我们将指标优化,改写成策略

改写后的策略回测效果 策略回测效果(时间周期:2H):

检验策略是否使用未来函数重绘

更多的参数设置可能带来不一样的效果,有需要的小伙伴可以自行测试

策略下载地址 www.abcserver.cloud

这是一个分享学习量化策略知识的频道, 欢迎订阅关注

策略源代码:

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

// © ktrader2100

// use ETC 76 26 2 3  2H

//@version=5

strategy(title="LinRegCandles UT MIX", overlay=true, currency="USD", pyramiding=0,

default_qty_type=strategy.cash, calc_on_every_tick=false, initial_capital=1000)

// study(title="Humble LinReg Candles", shorttitle="LinReg Candles", format=format.price, precision=4, overlay=true)

signal_length = input.int(title="Signal Smoothing", minval = 1, maxval = 200, defval = 11)

sma_signal = input.bool(title="Simple MA (Signal Line)", defval=true)

lin_reg = input.bool(title="Lin Reg", defval=true)

linreg_length = input.int(title="Linear Regression Length", minval = 1, maxval = 200, defval = 11)

bopen = lin_reg ? ta.linreg(open, linreg_length, 0) : open

bhigh = lin_reg ? ta.linreg(high, linreg_length, 0) : high

blow = lin_reg ? ta.linreg(low, linreg_length, 0) : low

bclose = lin_reg ? ta.linreg(close, linreg_length, 0) : close

r = bopen < bclose

signal = sma_signal ? ta.sma(bclose, signal_length) : ta.ema(bclose, signal_length)

plotcandle(r ? bopen : na, r ? bhigh : na, r ? blow: na, r ? bclose : na, title="LinReg Candles", color= color.green, wickcolor=color.green, bordercolor=color.green, editable= true)

plotcandle(r ? na : bopen, r ? na : bhigh, r ? na : blow, r ? na : bclose, title="LinReg Candles", color=color.red, wickcolor=color.red, bordercolor=color.red, editable= true)

plot(signal, color=color.rgb(160, 12, 12))

// UT Bot Alerts

// Inputs

a = input(1,     title = "Key Vaule. 'This changes the sensitivity'")

c = input(10,    title = "ATR Period")

h = input(false, title = "Signals from Heikin Ashi Candles")

xATR  = ta.atr(c)

nLoss = a * xATR

src = h ? request.security(syminfo.tickerid, timeframe.period, close, lookahead = barmerge.lookahead_off) : close

xATRTrailingStop = 0.0

xATRTrailingStop := src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0) ? math.max(nz(xATRTrailingStop[1]), src - nLoss) :

  src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0) ? math.min(nz(xATRTrailingStop[1]), src + nLoss) :

  src > nz(xATRTrailingStop[1], 0) ? src - nLoss : src + nLoss

pos = 0  

pos :=  src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0) ? 1 :

  src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0) ? -1 : nz(pos[1], 0)

 

xcolor = pos == -1 ? color.red: pos == 1 ? color.green : color.blue

ema   = ta.ema(src,1)

above = ta.crossover(ema, xATRTrailingStop)

below = ta.crossover(xATRTrailingStop, ema)

buy  = src > xATRTrailingStop and above

sell = src < xATRTrailingStop and below

barbuy  = src > xATRTrailingStop

barsell = src < xATRTrailingStop

plotshape(buy,  title = "Buy",  text = 'Buy',  style = shape.labelup,   location = location.belowbar, color= color.green, textcolor = color.white, size = size.tiny)

plotshape(sell, title = "Sell", text = 'Sell', style = shape.labeldown, location = location.abovebar, color= color.red,   textcolor = color.white, size = size.tiny)

barcolor(barbuy  ? color.green : na)

barcolor(barsell ? color.red   : na)

alertcondition(buy,  "UT Long",  "UT Long")

alertcondition(sell, "UT Short", "UT Short")

// KT STGS

symbol_name = syminfo.tickerid

// kt stgs start ...

var LQ_RATIO = 0.6

INIT_USDT_UNIT = strategy.initial_capital

PLR = input.float(0, 'PLR', minval=0, step=0.1, group='KT Trade Opts')

showprofit = input.bool(false, "showprofit", group='KT Trade Opts')

check100xlist = array.new_string(0, '')

if barstate.isfirst

   check100xlist.push('BTCUSDT')

   check100xlist.push('ETHUSDT')

   check100xlist.push('SOLUSDT')

   for i = 0 to array.size(check100xlist) - 1

       strValue = array.get(check100xlist, i)

       if str.contains(symbol_name, strValue)

           LQ_RATIO := 0.6

           break

       LQ_RATIO := 0.8

// plot(LQ_RATIO, title = 'LQ_RATIO')

//-----------------------------------------------------------------------------}

//UDT's

//-----------------------------------------------------------------------------{

type ktraderes

   float profit

   float profit_p

   string opt

   string ptime

   float amount

   float usdt_amount

   float open_price

   float pprice

   string debuginfo

   float stoplose

   float takeprofit

type ktorder

   string type

   int time

   string timedate

   string ptime

   float pt

   float currency

   float price

   float pprice

   float profit

   string msg

   float stoplose

   float takeprofit

//-----------------------------------------------------------------------------}

//Variables

//-----------------------------------------------------------------------------{

varip orders = array.new<ktorder>()

//Methods - functions

//-

method n(float piv) => bool out = not na(piv)

pushmsg(string tradecon, float tradeconprice, string side, float price, float currency, float stoplose = 0 , float takeprofit = 0) =>

   // strtpl = '{"tradecon": ">", "tradeconprice": "92946", "side": "sell", "price": "92946", "currency": "5000", "stoplose": "0", "takeprofit": "0"}'

   // strtpl = "{"tradecon": {0}, "tradeconprice": "{1}", "side": "{2}", "price": "{3}", "currency": "{4}", "stoplose": "{5}", "takeprofit": "{6}"}'

   msgstring = '{"tradecon": "'+tradecon+'", "tradeconprice": "'+str.tostring(tradeconprice)+'", "side": "'+side+'", "price": "'+str.tostring(price)+'", "currency": "'+str.tostring(currency)+'", "stoplose": "'+str.tostring(stoplose)+'", "takeprofit": "'+str.tostring(takeprofit)+'" , "symbol_name": "'+symbol_name+'"}'

   // msgstring = str.format(strtpl, tradecon)

   alert(message = msgstring, freq = alert.freq_once_per_bar)

   ''

getTradeResult(string ptime, float price) =>

   orderlen = orders.size()

   float Ebamount = 0

   float Esamount = 0

   float Ebcurrency = 0

   float Escurrency = 0

   Eshow = ''

   tradeopt = 'none'

   float amount = 0

   float open_price = 0

   float profit = 0

   float profit_p = 0

   if orderlen > 0

       for i = 0 to orderlen -1

           dfrow = orders.get(i)

           if dfrow.type == 'buy'

               Ebamount := Ebamount + float(dfrow.currency) / float(dfrow.price)

               Ebcurrency := Ebcurrency + float(dfrow.currency)

           else

               Esamount := Esamount + float(dfrow.currency) / float(dfrow.price)

               Escurrency := Escurrency + float(dfrow.currency)

       if Ebamount > 0 and Esamount > 0

           if Ebamount > Esamount

               tradeopt := 'buy'

               amount := Ebamount - Esamount

           else

               tradeopt := 'sell'

               amount := Esamount - Ebamount

       else

           if Ebamount > 0

               tradeopt := 'buy'

               amount := Ebamount

           else if Esamount > 0

               tradeopt := 'sell'

               amount := Esamount

       if tradeopt != 'none'

           if tradeopt == 'buy'

               profit := amount * price - (Ebcurrency - Escurrency)

           else

               profit := (Escurrency - Ebcurrency) - (amount * price)

   // if profit != 0

   if tradeopt == 'buy'

       profit_p := profit / (Ebcurrency - Escurrency) * 100

   else if tradeopt == 'sell'

       profit_p := profit / (Escurrency - Ebcurrency) * 100

   debuginfo = "Escurrency: " + str.tostring(Escurrency) + "Ebcurrency: " + str.tostring(Ebcurrency)

   float stoplose = 0

   float takeprofit = 0

   if orderlen == 1

       stoplose := orders.get(0).stoplose

       takeprofit := orders.get(0).takeprofit

   res = ktraderes.new(debuginfo = debuginfo, profit = profit, profit_p = profit_p, pprice = price,

    amount = amount, opt = tradeopt, open_price = open_price, ptime = ptime, stoplose = stoplose, takeprofit = takeprofit)

   res

transTimeToTimedate(int transtime, int ghour)=>

   needtranstime = transtime

   if transtime == 0

       needtranstime := time

   gmt = ghour > 0 ? 'GMT+'+str.tostring(ghour) : 'GMT'

   timedate = str.format_time(int(needtranstime), "yyyy-MM-dd HH:mm:ss", gmt)

   timedate

nowstr = transTimeToTimedate(0, 8)

addOrder(optype, currency, price, time, timedate, msg="减仓", stoplose=0, takeprofit=0)=>

   tdres = getTradeResult(nowstr, close)

   canopt = true

   if tdres.opt != 'none'

       if tdres.opt == optype

           canopt := false

   if canopt == true

       neworder = ktorder.new(type = optype, currency = currency, price = price, time = time, timedate = timedate, msg = msg, stoplose=stoplose, takeprofit=takeprofit)

       if optype == 'buy'

           strategy.order("buy", strategy.long, qty = currency / price, comment = msg)

       else if optype == 'sell'

           strategy.order("sell", strategy.short,  qty = currency / price, comment = msg)

       orders.push(neworder)

print_profit(atprice) =>

   if showprofit == true

       orderl = orders.size()

       int i = 0

       showtext = ""

       showcolumns = array.from("type", "price", "currency", "stop", "take","time", "msg")

       showtext := array.join(showcolumns, ",") + "n"

       ktres = getTradeResult(transTimeToTimedate(0, 8), close)

       while i < orders.size()

           corder = orders.get(i)

           jarr = array.from(corder.type, str.tostring(corder.price), str.tostring(corder.currency), str.tostring(corder.stoplose), str.tostring(corder.takeprofit), corder.timedate, corder.msg)

           rowstr = jarr.join(',')

           if str.length(showtext) > 1000

               showtext += "n..."

               // last line dump

               lastlinenum = orders.size() - 1

               corder := orders.get(orders.size() - 1)

               jarr := array.from(corder.type, str.tostring(corder.price), str.tostring(corder.currency), str.tostring(corder.stoplose), str.tostring(corder.takeprofit), corder.timedate, corder.msg)

               rowstr := jarr.join(',')

               showtext += ("n" + str.tostring(lastlinenum) + '#' + rowstr)

               break

           showtext += ("n" + str.tostring(i) + '#' + rowstr)

           i += 1

           ''

       resshowstr = "n res:---" + ktres.opt + "n profit:" + str.tostring(ktres.profit) + "n amount:" +

    str.tostring(ktres.amount) + "n pprice:" + str.tostring(ktres.pprice)+

    "n profit_p: " + str.tostring(ktres.profit_p)

       label.new(bar_index, atprice, showtext + resshowstr, size = size.small, color = color.white)

   ''

// todo starts ....

buycon = false

sellcon = false

tdres = getTradeResult(nowstr, close)

pmsg = false

tradeopt = 'none'

float stoplose = 0

float takeprofit = 0

// todo cac...

// bclose = lin_reg ? ta.linreg(close, linreg_length, 0) : close

// r = bopen < bclose

// signal = sma_signal ? ta.sma(bclose, signal_length) : ta.ema(bclose, signal_length)

// plotcandle(r ? bopen : na, r ? bhigh : na, r ? blow: na, r ? bclose : na, title="LinReg Candles", color= color.green, wickcolor=color.green, bordercolor=color.green, editable= true)

// plotcandle(r ? na : bopen, r ? na : bhigh, r ? na : blow, r ? na : bclose, title="LinReg Candles", color=color.red, wickcolor=color.red, bordercolor=color.red, editable= true)

// plot(signal, color=color.rgb(160, 12, 12))

// barcolor(barbuy  ? color.green : na)

// barcolor(barsell ? color.red   : na)

if barbuy and bopen < bclose and bclose > signal

   buycon := true

if barsell and bopen > bclose and bclose < signal

   sellcon := true

// todo cac end...

if buycon

   tradeopt := 'buy'

   if tdres.opt == 'sell'

       strategy.close_all()

       orders := array.new<ktorder>(0)

       tdres := getTradeResult(nowstr, close)

   if tdres.opt == 'none'

       if PLR > 0

           mv = close * LQ_RATIO / 100

           stoplose := close - mv

           takeprofit := close + mv * PLR

       else

           stoplose := 0

           takeprofit := 0

       currency = INIT_USDT_UNIT

       mcurrency = 1

       msg='开多'

       addOrder(optype = tradeopt, currency = currency, price = close, time=time,

            timedate = nowstr, msg=msg,

            stoplose = stoplose, takeprofit = takeprofit)

       pushmsg(tradecon='<',

                tradeconprice=close,

                side='buy',

                price=close,

                currency=mcurrency,

                stoplose = 0,

                takeprofit = 0)

if sellcon

   tradeopt := 'sell'

   if tdres.opt == 'buy'

       strategy.close_all()

       orders := array.new<ktorder>(0)

       tdres := getTradeResult(nowstr, close)

   if tdres.opt == 'none'

       if PLR > 0

           mv = close * LQ_RATIO / 100

           stoplose := close + mv

           takeprofit := close - mv * PLR

       else

           stoplose := 0

           takeprofit := 0

       currency = INIT_USDT_UNIT

       mcurrency = 1

       msg='开空'

       addOrder(optype = tradeopt, currency = currency, price = close,

                time=time, timedate = nowstr, msg=msg,

                stoplose = stoplose, takeprofit = takeprofit)

       pushmsg(tradecon='>',

                tradeconprice=close,

                side='sell',

                price=close,

                currency=mcurrency,

                stoplose = 0,

                takeprofit = 0)

if barstate.islastconfirmedhistory

   print_profit(0)


// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ktrader2100

// use ETC 76 26 2 3  2H
//@version=5
strategy(title="LinRegCandles UT MIX", overlay=true, currency="USD", pyramiding=0,
 default_qty_type=strategy.cash, calc_on_every_tick=false, initial_capital=1000)

// study(title="Humble LinReg Candles", shorttitle="LinReg Candles", format=format.price, precision=4, overlay=true)
signal_length = input.int(title="Signal Smoothing", minval = 1, maxval = 200, defval = 11)
sma_signal = input.bool(title="Simple MA (Signal Line)", defval=true)

lin_reg = input.bool(title="Lin Reg", defval=true)
linreg_length = input.int(title="Linear Regression Length", minval = 1, maxval = 200, defval = 11)

bopen = lin_reg ? ta.linreg(open, linreg_length, 0) : open
bhigh = lin_reg ? ta.linreg(high, linreg_length, 0) : high
blow = lin_reg ? ta.linreg(low, linreg_length, 0) : low
bclose = lin_reg ? ta.linreg(close, linreg_length, 0) : close

r = bopen < bclose

signal = sma_signal ? ta.sma(bclose, signal_length) : ta.ema(bclose, signal_length)

plotcandle(r ? bopen : na, r ? bhigh : na, r ? blow: na, r ? bclose : na, title="LinReg Candles", color= color.green, wickcolor=color.green, bordercolor=color.green, editable= true)
plotcandle(r ? na : bopen, r ? na : bhigh, r ? na : blow, r ? na : bclose, title="LinReg Candles", color=color.red, wickcolor=color.red, bordercolor=color.red, editable= true)

plot(signal, color=color.rgb(160, 12, 12))


// UT Bot Alerts
// Inputs
a = input(1,     title = "Key Vaule. 'This changes the sensitivity'")
c = input(10,    title = "ATR Period")
h = input(false, title = "Signals from Heikin Ashi Candles")

xATR  = ta.atr(c)
nLoss = a * xATR

src = h ? request.security(syminfo.tickerid, timeframe.period, close, lookahead = barmerge.lookahead_off) : close

xATRTrailingStop = 0.0
xATRTrailingStop := src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0) ? math.max(nz(xATRTrailingStop[1]), src - nLoss) : 
   src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0) ? math.min(nz(xATRTrailingStop[1]), src + nLoss) : 
   src > nz(xATRTrailingStop[1], 0) ? src - nLoss : src + nLoss
 
pos = 0   
pos :=	src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0) ? 1 : 
   src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0) ? -1 : nz(pos[1], 0)
   
xcolor = pos == -1 ? color.red: pos == 1 ? color.green : color.blue 

ema   = ta.ema(src,1)
above = ta.crossover(ema, xATRTrailingStop)
below = ta.crossover(xATRTrailingStop, ema)

buy  = src > xATRTrailingStop and above 
sell = src < xATRTrailingStop and below

barbuy  = src > xATRTrailingStop 
barsell = src < xATRTrailingStop 

plotshape(buy,  title = "Buy",  text = 'Buy',  style = shape.labelup,   location = location.belowbar, color= color.green, textcolor = color.white, size = size.tiny)
plotshape(sell, title = "Sell", text = 'Sell', style = shape.labeldown, location = location.abovebar, color= color.red,   textcolor = color.white, size = size.tiny)

barcolor(barbuy  ? color.green : na)
barcolor(barsell ? color.red   : na)

alertcondition(buy,  "UT Long",  "UT Long")
alertcondition(sell, "UT Short", "UT Short")

// KT STGS
symbol_name = syminfo.tickerid
// kt stgs start ...
var LQ_RATIO = 0.6
INIT_USDT_UNIT = strategy.initial_capital
PLR = input.float(0, 'PLR', minval=0, step=0.1, group='KT Trade Opts')
showprofit = input.bool(false, "showprofit", group='KT Trade Opts')
check100xlist = array.new_string(0, '')
if barstate.isfirst
    check100xlist.push('BTCUSDT')
    check100xlist.push('ETHUSDT')
    check100xlist.push('SOLUSDT')

    for i = 0 to array.size(check100xlist) - 1
        strValue = array.get(check100xlist, i)
        if str.contains(symbol_name, strValue)
            LQ_RATIO := 0.6
            break
        LQ_RATIO := 0.8

// plot(LQ_RATIO, title = 'LQ_RATIO')

//-----------------------------------------------------------------------------}
//UDT's
//-----------------------------------------------------------------------------{
type ktraderes
    float profit
    float profit_p
    string opt
    string ptime
    float amount
    float usdt_amount
    float open_price
    float pprice
    string debuginfo
    float stoplose
    float takeprofit

type ktorder
    string type
    int time
    string timedate
    string ptime
    float pt
    float currency
    float price
    float pprice
    float profit
    string msg
    float stoplose
    float takeprofit

//-----------------------------------------------------------------------------}
//Variables
//-----------------------------------------------------------------------------{
varip orders = array.new()

//Methods - functions
//-
method n(float piv) => bool out = not na(piv)

pushmsg(string tradecon, float tradeconprice, string side, float price, float currency, float stoplose = 0 , float takeprofit = 0) =>
    // strtpl = '{"tradecon": ">", "tradeconprice": "92946", "side": "sell", "price": "92946", "currency": "5000", "stoplose": "0", "takeprofit": "0"}'
    // strtpl = "{"tradecon": {0}, "tradeconprice": "{1}", "side": "{2}", "price": "{3}", "currency": "{4}", "stoplose": "{5}", "takeprofit": "{6}"}'
    msgstring = '{"tradecon": "'+tradecon+'", "tradeconprice": "'+str.tostring(tradeconprice)+'", "side": "'+side+'", "price": "'+str.tostring(price)+'", "currency": "'+str.tostring(currency)+'", "stoplose": "'+str.tostring(stoplose)+'", "takeprofit": "'+str.tostring(takeprofit)+'" , "symbol_name": "'+symbol_name+'"}'
    // msgstring = str.format(strtpl, tradecon)

    alert(message = msgstring, freq = alert.freq_once_per_bar)
    ''

getTradeResult(string ptime, float price) =>
    orderlen = orders.size()
    float Ebamount = 0
    float Esamount = 0
    float Ebcurrency = 0
    float Escurrency = 0
    Eshow = ''
    tradeopt = 'none'
    float amount = 0
    float open_price = 0
    float profit = 0
    float profit_p = 0

    if orderlen > 0
        for i = 0 to orderlen -1
            dfrow = orders.get(i)
            if dfrow.type == 'buy'
                Ebamount := Ebamount + float(dfrow.currency) / float(dfrow.price)
                Ebcurrency := Ebcurrency + float(dfrow.currency)
            else
                Esamount := Esamount + float(dfrow.currency) / float(dfrow.price)
                Escurrency := Escurrency + float(dfrow.currency)

        if Ebamount > 0 and Esamount > 0
            if Ebamount > Esamount
                tradeopt := 'buy'
                amount := Ebamount - Esamount
            else
                tradeopt := 'sell'
                amount := Esamount - Ebamount
        else
            if Ebamount > 0
                tradeopt := 'buy'
                amount := Ebamount
            else if Esamount > 0
                tradeopt := 'sell'
                amount := Esamount

        if tradeopt != 'none'
            if tradeopt == 'buy'
                profit := amount * price - (Ebcurrency - Escurrency)
            else
                profit := (Escurrency - Ebcurrency) - (amount * price)

    // if profit != 0
    if tradeopt == 'buy'
        profit_p := profit / (Ebcurrency - Escurrency) * 100
    else if tradeopt == 'sell'
        profit_p := profit / (Escurrency - Ebcurrency) * 100

    debuginfo = "Escurrency: " + str.tostring(Escurrency) + "Ebcurrency: " + str.tostring(Ebcurrency)

    float stoplose = 0
    float takeprofit = 0
    if orderlen == 1
        stoplose := orders.get(0).stoplose
        takeprofit := orders.get(0).takeprofit

    res = ktraderes.new(debuginfo = debuginfo, profit = profit, profit_p = profit_p, pprice = price,
     amount = amount, opt = tradeopt, open_price = open_price, ptime = ptime, stoplose = stoplose, takeprofit = takeprofit)
    res


transTimeToTimedate(int transtime, int ghour)=>
    needtranstime = transtime
    if transtime == 0
        needtranstime := time

    gmt = ghour > 0 ? 'GMT+'+str.tostring(ghour) : 'GMT'
    timedate = str.format_time(int(needtranstime), "yyyy-MM-dd HH:mm:ss", gmt)
    timedate
nowstr = transTimeToTimedate(0, 8)

addOrder(optype, currency, price, time, timedate, msg="减仓", stoplose=0, takeprofit=0)=>
    tdres = getTradeResult(nowstr, close)
    canopt = true
    if tdres.opt != 'none'
        if tdres.opt == optype
            canopt := false
    if canopt == true
        neworder = ktorder.new(type = optype, currency = currency, price = price, time = time, timedate = timedate, msg = msg, stoplose=stoplose, takeprofit=takeprofit)
        if optype == 'buy'
            strategy.order("buy", strategy.long, qty = currency / price, comment = msg)
        else if optype == 'sell'
            strategy.order("sell", strategy.short,  qty = currency / price, comment = msg)
        orders.push(neworder)

print_profit(atprice) =>
    if showprofit == true
        orderl = orders.size()
        int i = 0
        showtext = ""
        showcolumns = array.from("type", "price", "currency", "stop", "take","time", "msg")
        showtext := array.join(showcolumns, ",") + "n"
        ktres = getTradeResult(transTimeToTimedate(0, 8), close)
        while i < orders.size()
            corder = orders.get(i)

            jarr = array.from(corder.type, str.tostring(corder.price), str.tostring(corder.currency), str.tostring(corder.stoplose), str.tostring(corder.takeprofit), corder.timedate, corder.msg)
            rowstr = jarr.join(',')

            if str.length(showtext) > 1000
                showtext += "n..."
                // last line dump
                lastlinenum = orders.size() - 1
                corder := orders.get(orders.size() - 1)
                jarr := array.from(corder.type, str.tostring(corder.price), str.tostring(corder.currency), str.tostring(corder.stoplose), str.tostring(corder.takeprofit), corder.timedate, corder.msg)
                rowstr := jarr.join(',')
                showtext += ("n" + str.tostring(lastlinenum) + '#' + rowstr)
                break
            showtext += ("n" + str.tostring(i) + '#' + rowstr)
            i += 1
            ''
        resshowstr = "n res:---" + ktres.opt + "n profit:" + str.tostring(ktres.profit) + "n amount:" +
     str.tostring(ktres.amount) + "n pprice:" + str.tostring(ktres.pprice)+
     "n profit_p: " + str.tostring(ktres.profit_p)
        label.new(bar_index, atprice, showtext + resshowstr, size = size.small, color = color.white)
    ''

// todo starts ....
buycon = false
sellcon = false
tdres = getTradeResult(nowstr, close)
pmsg = false
tradeopt = 'none'
float stoplose = 0
float takeprofit = 0

// todo cac...

// bclose = lin_reg ? ta.linreg(close, linreg_length, 0) : close

// r = bopen < bclose

// signal = sma_signal ? ta.sma(bclose, signal_length) : ta.ema(bclose, signal_length)

// plotcandle(r ? bopen : na, r ? bhigh : na, r ? blow: na, r ? bclose : na, title="LinReg Candles", color= color.green, wickcolor=color.green, bordercolor=color.green, editable= true)
// plotcandle(r ? na : bopen, r ? na : bhigh, r ? na : blow, r ? na : bclose, title="LinReg Candles", color=color.red, wickcolor=color.red, bordercolor=color.red, editable= true)

// plot(signal, color=color.rgb(160, 12, 12))

// barcolor(barbuy  ? color.green : na)
// barcolor(barsell ? color.red   : na)

if barbuy and bopen < bclose and bclose > signal
    buycon := true

if barsell and bopen > bclose and bclose < signal
    sellcon := true

// todo cac end...
if buycon
    tradeopt := 'buy'
    if tdres.opt == 'sell'
        strategy.close_all()
        orders := array.new(0)
        tdres := getTradeResult(nowstr, close)

    if tdres.opt == 'none'
        if PLR > 0
            mv = close * LQ_RATIO / 100
            stoplose := close - mv
            takeprofit := close + mv * PLR
        else
            stoplose := 0
            takeprofit := 0

        currency = INIT_USDT_UNIT
        mcurrency = 1
        msg='开多'

        addOrder(optype = tradeopt, currency = currency, price = close, time=time,
             timedate = nowstr, msg=msg,
             stoplose = stoplose, takeprofit = takeprofit)

        pushmsg(tradecon='<',
                 tradeconprice=close,
                 side='buy',
                 price=close,
                 currency=mcurrency,
                 stoplose = 0,
                 takeprofit = 0)

if sellcon
    tradeopt := 'sell'
    if tdres.opt == 'buy'
        strategy.close_all()
        orders := array.new(0)
        tdres := getTradeResult(nowstr, close)

    if tdres.opt == 'none'
        if PLR > 0
            mv = close * LQ_RATIO / 100
            stoplose := close + mv
            takeprofit := close - mv * PLR
        else
            stoplose := 0
            takeprofit := 0


        currency = INIT_USDT_UNIT
        mcurrency = 1
        msg='开空'

        addOrder(optype = tradeopt, currency = currency, price = close,
                 time=time, timedate = nowstr, msg=msg,
                 stoplose = stoplose, takeprofit = takeprofit)

        pushmsg(tradecon='>',
                 tradeconprice=close,
                 side='sell',
                 price=close,
                 currency=mcurrency,
                 stoplose = 0,
                 takeprofit = 0)

if barstate.islastconfirmedhistory
    print_profit(0)