设为首页 收藏本站 切换语言 切换语言

tradingview指标策略转码有人会吗

| 发表于 2025-8-3 16:17:57 | 显示全部楼层 |复制链接
我有一个基于tradingview指标的策略想写成量化,但是ai写不出效果,手动回测下来效果是不错的,有大佬会写吗?指标源码是这个,免费的
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © HeWhoMustNotBeNamed
  3. //   __    __            __       __  __                  __       __                        __      __    __              __      _______             __    __                                          __
  4. //  /  |  /  |          /  |  _  /  |/  |                /  \     /  |                      /  |    /  \  /  |            /  |    /       \           /  \  /  |                                        /  |
  5. //  $ |  $ |  ______  $ | / \ $ |$ |____    ______  [        DISCUZ_CODE_17        ]nbsp; \   /$ | __    __   _______  _$ |_   [        DISCUZ_CODE_17        ]nbsp; \ $ |  ______   _$ |_   $  |  ______  [        DISCUZ_CODE_17        ]nbsp; \ $ |  ______   _____  ____    ______    ____$ |
  6. //  $ |__$ | /      \ $ |/[        DISCUZ_CODE_17        ]nbsp; \$ |[        DISCUZ_CODE_17        ]nbsp;     \  /      \ $  \ /$ |/  |  /  | /       |/ [        DISCUZ_CODE_17        ]nbsp;  |  $  \$ | /      \ / [        DISCUZ_CODE_17        ]nbsp;  |  $ |__$ | /      \ $  \$ | /      \ /     \/    \  /      \  /    $ |
  7. //  [        DISCUZ_CODE_17        ]nbsp;   $ |/$[        DISCUZ_CODE_17        ]nbsp; |$ /$  $ |$  |/$[        DISCUZ_CODE_17        ]nbsp; |$  /$ |$ |  $ |/$/ $/   $  $ |/$[        DISCUZ_CODE_17        ]nbsp; |$/   [        DISCUZ_CODE_17        ]nbsp;   [        DISCUZ_CODE_17        ]lt; /$[        DISCUZ_CODE_17        ]nbsp; |$  $ | $[        DISCUZ_CODE_17        ]nbsp; |$ $  |/$[        DISCUZ_CODE_17        ]nbsp; |/$ |
  8. //  $ |[        DISCUZ_CODE_17        ]nbsp;   $ |$ $/$ $ |$ |  $ |$ |  $ |$ $ $/$ |$ |  $ |[        DISCUZ_CODE_17        ]nbsp;     \   $ | __ $ $ $ |$ |  $ |  $ | __ $  |[        DISCUZ_CODE_17        ]nbsp;   $ |$ $ $ | /    $ |$ | $ | $ |[        DISCUZ_CODE_17        ]nbsp;   $ |$ |  $ |
  9. //  $ |  $ |$/ $/  $ |$ |  $ |$ \__$ |$ |$/ $ |$ \__$ | $[        DISCUZ_CODE_17        ]nbsp; |  $ |/  |$ |$ |$ \__$ |  $ |/  |$ |__$ |$/ $ |$ |/$ |$ | $ | $ |$/ $ \__$ |
  10. //  $ |  $ |[        DISCUZ_CODE_17        ]nbsp;      |$/    $ |$ |  $ |[        DISCUZ_CODE_17        ]nbsp;   $/ $ | $/  $ |[        DISCUZ_CODE_17        ]nbsp;   $/ /     $/   [        DISCUZ_CODE_17        ]nbsp; $/ $ | $ |[        DISCUZ_CODE_17        ]nbsp;   $/   [        DISCUZ_CODE_17        ]nbsp; $/ [        DISCUZ_CODE_17        ]nbsp;   $/ [        DISCUZ_CODE_17        ]nbsp;      |$ | $ |[        DISCUZ_CODE_17        ]nbsp;   $ |$ | $ | $ |[        DISCUZ_CODE_17        ]nbsp;      |[        DISCUZ_CODE_17        ]nbsp;   $ |
  11. //  $/   $/  $/ $/      $/ $/   $/  $/  $/      $/  $/  $/     $/  $/   $/  $/     $/  $/   $/ $/   $/  $/ $/  $/  $/  $/  $/
  12. //                                                                                                                                                                                                         
  13. //                                                                                                                                                                                                         
  14. //
  15. //@version=4
  16. study("Double Top/Bottom - Ultimate (OS)", shorttitle="W/M - Ultimate(OS)", overlay=true, max_lines_count=500, max_labels_count=500, max_bars_back=1000)
  17. length = input(10, step=5, minval=5)
  18. showZigzag = input(false)
  19. showPivots = input(true)
  20. showStats = input(true)
  21. bullishColor = input(color.green)
  22. bullTrapColor = input(color.orange)
  23. bearishColor = input(color.red)
  24. bearTrapColor = input(color.lime)
  25. textColor = input(color.black)
  26. MaxRiskPerReward = input(30, step=5, minval=5, maxval=100)
  27. DisplayRiskPerReward = input(true)
  28. var zigzagvalues = array.new_float(0)
  29. var zigzagindexes = array.new_int(0)
  30. var zigzagdir = array.new_int(0)
  31. var doubleTopBottomValues = array.new_float(3)
  32. var doubleTopBottomIndexes = array.new_int(3)
  33. var doubleTopBottomDir = array.new_int(3)
  34. int max_array_size = 10
  35. max_bars_back(high, 1000)
  36. max_bars_back(low, 1000)
  37. var lineArray = array.new_line(0)
  38. var labelArray = array.new_label(0)
  39. pivots(length)=>
  40.     float ph = highestbars(high, length) == 0 ? high : na
  41.     float pl = lowestbars(low, length) == 0 ? low : na
  42.     dir = 0
  43.     dir := iff(ph and na(pl), 1, iff(pl and na(ph), -1, dir[1]))
  44.     [dir, ph, pl]
  45. add_to_array(value, index, dir)=>
  46.     mult = array.size(zigzagvalues) < 2? 1 :
  47.                  (dir*value > dir*array.get(zigzagvalues,1))? 2 : 1
  48.     array.unshift(zigzagindexes, index)
  49.     array.unshift(zigzagvalues, value)
  50.     array.unshift(zigzagdir, dir*mult)
  51.     if array.size(zigzagindexes) > max_array_size
  52.         array.pop(zigzagindexes)
  53.         array.pop(zigzagvalues)
  54.         array.pop(zigzagdir)
  55. add_to_zigzag(dir, dirchanged, ph, pl, index)=>
  56.     value = (dir == 1? ph : pl)
  57.     if array.size(zigzagvalues) == 0 or dirchanged
  58.         add_to_array(value, index, dir)
  59.     else if(dir == 1 and value > array.get(zigzagvalues, 0)) or (dir == -1 and value < array.get(zigzagvalues, 0))
  60.         array.shift(zigzagvalues)
  61.         array.shift(zigzagindexes)
  62.         array.shift(zigzagdir)
  63.         add_to_array(value, index, dir)
  64. zigzag(length)=>
  65.     [dir, ph, pl] = pivots(length)
  66.     dirchanged = change(dir)
  67.     if(ph or pl)
  68.         add_to_zigzag(dir, dirchanged, ph, pl, bar_index)
  69. calculate_double_pattern()=>
  70.     doubleTop = false
  71.     doubleTopConfirmation = 0
  72.     doubleBottom = false
  73.     doubleBottomConfirmation = 0
  74.     if(array.size(zigzagvalues) >= 4)
  75.         index = array.get(zigzagindexes, 1)
  76.         value = array.get(zigzagvalues, 1)
  77.         highLow = array.get(zigzagdir, 1)
  78.         
  79.         lindex = array.get(zigzagindexes, 2)
  80.         lvalue = array.get(zigzagvalues, 2)
  81.         lhighLow = array.get(zigzagdir, 2)
  82.         
  83.         llindex = array.get(zigzagindexes, 3)
  84.         llvalue = array.get(zigzagvalues, 3)
  85.         llhighLow = array.get(zigzagdir, 3)
  86.         risk = abs(value-llvalue)
  87.         reward = abs(value-lvalue)
  88.         riskPerReward = risk*100/(risk+reward)
  89.         
  90.         if(highLow == 1 and llhighLow == 2 and lhighLow < 0  and riskPerReward < MaxRiskPerReward)
  91.             doubleTop := true
  92.         if(highLow == -1 and llhighLow == -2 and lhighLow > 0 and riskPerReward < MaxRiskPerReward)
  93.             doubleBottom := true
  94.         if(doubleTop or doubleBottom)
  95.             array.set(doubleTopBottomValues, 0, value)
  96.             array.set(doubleTopBottomValues, 1, lvalue)
  97.             array.set(doubleTopBottomValues, 2, llvalue)
  98.             
  99.             array.set(doubleTopBottomIndexes, 0, index)
  100.             array.set(doubleTopBottomIndexes, 1, lindex)
  101.             array.set(doubleTopBottomIndexes, 2, llindex)
  102.             
  103.             array.set(doubleTopBottomDir, 0, highLow)
  104.             array.set(doubleTopBottomDir, 1, lhighLow)
  105.             array.set(doubleTopBottomDir, 2, llhighLow)
  106.    
  107.     [doubleTop, doubleBottom]
  108. get_crossover_info(doubleTop, doubleBottom)=>
  109.     index = array.get(doubleTopBottomIndexes, 0)
  110.     value = array.get(doubleTopBottomValues, 0)
  111.     highLow = array.get(doubleTopBottomDir, 0)
  112.    
  113.     lindex = array.get(doubleTopBottomIndexes, 1)
  114.     lvalue = array.get(doubleTopBottomValues, 1)
  115.     lhighLow = array.get(doubleTopBottomDir, 1)
  116.    
  117.     llindex = array.get(doubleTopBottomIndexes, 2)
  118.     llvalue = array.get(doubleTopBottomValues, 2)
  119.     llhighLow = array.get(doubleTopBottomDir, 2)
  120.    
  121.     latestDoubleTop = false
  122.     latestDoubleBottom = false
  123.     latestDoubleTop := doubleTop ? true : doubleBottom? false : latestDoubleTop[1]
  124.     latestDoubleBottom := doubleBottom? true : doubleTop? false : latestDoubleBottom[1]
  125.    
  126.     doubleTopConfirmation = 0
  127.     doubleBottomConfirmation = 0
  128.     doubleTopConfirmation := latestDoubleTop? (crossunder(low, lvalue) ? 1 : crossover(high, llvalue) ? -1 : 0) : 0
  129.     doubleBottomConfirmation := latestDoubleBottom? (crossover(high, lvalue) ? 1 : crossunder(low, llvalue) ? -1 : 0) : 0
  130.     [doubleTopConfirmation, doubleBottomConfirmation]
  131. draw_double_pattern(doubleTop,doubleBottom,doubleTopConfirmation,doubleBottomConfirmation)=>
  132.     index = array.get(doubleTopBottomIndexes, 0)
  133.     value = array.get(doubleTopBottomValues, 0)
  134.     highLow = array.get(doubleTopBottomDir, 0)
  135.    
  136.     lindex = array.get(doubleTopBottomIndexes, 1)
  137.     lvalue = array.get(doubleTopBottomValues, 1)
  138.     lhighLow = array.get(doubleTopBottomDir, 1)
  139.    
  140.     llindex = array.get(doubleTopBottomIndexes, 2)
  141.     llvalue = array.get(doubleTopBottomValues, 2)
  142.     llhighLow = array.get(doubleTopBottomDir, 2)
  143.    
  144.     isBullish = true
  145.     isBullish := (doubleTop or doubleBottom)? doubleTop : isBullish[1]
  146.    
  147.     risk = abs(value-llvalue)
  148.     reward = abs(value-lvalue)
  149.     riskPerReward = risk*100/(risk+reward)
  150.    
  151.     base = line.new(x1=index, y1=value,
  152.                  x2 = llindex, y2=llvalue,
  153.                  color=doubleTop? bearishColor : bullishColor, width=2, style=line.style_solid)
  154.     l1 = line.new(x1=index, y1=value,
  155.                  x2 = lindex, y2=lvalue,
  156.                  color=doubleTop? bearishColor : bullishColor, width=2, style=line.style_dotted)
  157.     l2 = line.new(x1=lindex, y1=lvalue,
  158.                  x2 = llindex, y2=llvalue,
  159.                  color=doubleTop? bearishColor : bullishColor, width=2, style=line.style_dotted)
  160.     labelText = (doubleTop? "Double Top" : "Double Bottom") + (DisplayRiskPerReward ? " RR - "+tostring(riskPerReward) : "")
  161.    
  162.     baseLabel = label.new(x=index, y=value, text=labelText, yloc=doubleTop?yloc.abovebar:yloc.belowbar,
  163.                              color=doubleTop?bearishColor:bullishColor,
  164.                              style=doubleTop?label.style_label_down:label.style_label_up,
  165.                              textcolor=textColor, size=size.normal)
  166.    
  167.     if not (doubleTop or doubleBottom)
  168.         line.delete(base)
  169.         line.delete(l1)
  170.         line.delete(l2)
  171.         label.delete(baseLabel)
  172.         
  173.     var doubleTopCount = 0
  174.     var doubleBottomCount = 0
  175.     doubleTopCount := doubleTop? nz(doubleTopCount[1],0) + 1 : nz(doubleTopCount[1],0)
  176.     doubleBottomCount := doubleBottom? nz(doubleBottomCount[1],0) + 1 : nz(doubleBottomCount[1],0)
  177.         
  178.     if(line.get_x2(base) == line.get_x2(base[1]))
  179.         line.delete(base[1])
  180.         line.delete(l1[1])
  181.         line.delete(l2[1])
  182.         label.delete(baseLabel[1])
  183.         doubleTopCount := doubleTop? doubleTopCount -1 : doubleTopCount
  184.         doubleBottomCount := doubleBottom? doubleBottomCount -1 : doubleBottomCount
  185.    
  186.    
  187.     if(barstate.islast)
  188.         lres = line.new(x1=bar_index, y1=lvalue,
  189.                      x2 = lindex, y2=lvalue,
  190.                      color=isBullish? bearishColor : bullishColor, width=2, style=line.style_dashed, extend=extend.left)
  191.         lsup = line.new(x1=bar_index, y1=llvalue,
  192.                      x2 = llindex, y2=llvalue,
  193.                      color=isBullish? bullishColor : bearishColor , width=2, style=line.style_dashed, extend=extend.left)
  194.    
  195.     doubleTopConfirmationCount = doubleTopConfirmation>0? 1 : 0
  196.     doubleBottomConfirmationCount = doubleBottomConfirmation>0? 1:0
  197.     doubleTopInvalidationCount = doubleTopConfirmation<0? 1 : 0
  198.     doubleBottomInvalidationCount = doubleBottomConfirmation<0? 1:0
  199.    
  200.     if(doubleTopConfirmation != 0 or doubleBottomConfirmation !=0)
  201.         if(doubleTopConfirmation>0 or doubleBottomConfirmation > 0)
  202.             lresbreak = line.new(x1=lindex, y1=lvalue,
  203.                      x2 = bar_index, y2=lvalue,
  204.                      color=isBullish? bearishColor : bullishColor, width=2, style=line.style_dashed)
  205.             if(line.get_x1(lresbreak[1]) == line.get_x1(lresbreak))
  206.                 doubleTopConfirmationCount := 0
  207.                 doubleBottomConfirmationCount := 0
  208.                 doubleTopInvalidationCount := 0
  209.                 doubleBottomInvalidationCount := 0
  210.                 line.delete(lresbreak)
  211.                 lresbreak := lresbreak[1]
  212.         else if(doubleTopConfirmation<0 or doubleBottomConfirmation < 0)
  213.             lsupbreak = line.new(x1=llindex, y1=llvalue,
  214.                      x2 = bar_index, y2=llvalue,
  215.                      color=isBullish? bullishColor : bearishColor , width=2, style=line.style_dashed)
  216.             if(line.get_x1(lsupbreak[1]) == line.get_x1(lsupbreak))
  217.                 doubleTopInvalidationCount := 0
  218.                 doubleBottomInvalidationCount := 0
  219.                 doubleTopConfirmationCount := 0
  220.                 doubleBottomConfirmationCount := 0
  221.                 line.delete(lsupbreak)
  222.                 lsupbreak := lsupbreak[1]
  223.     doubleTopConfirmationCount := nz(doubleTopConfirmationCount[1],0)+doubleTopConfirmationCount
  224.     doubleBottomConfirmationCount := nz(doubleBottomConfirmationCount[1],0)+doubleBottomConfirmationCount
  225.     doubleTopInvalidationCount := nz(doubleTopInvalidationCount[1],0)+doubleTopInvalidationCount
  226.     doubleBottomInvalidationCount := nz(doubleBottomInvalidationCount[1],0)+doubleBottomInvalidationCount
  227.     [doubleTopCount, doubleBottomCount, doubleTopConfirmationCount, doubleBottomConfirmationCount, doubleTopInvalidationCount, doubleBottomInvalidationCount]
  228. zigzag(length)
  229. [doubleTop, doubleBottom] = calculate_double_pattern()
  230. [doubleTopConfirmation, doubleBottomConfirmation] = get_crossover_info(doubleTop, doubleBottom)
  231. [doubleTopCount, doubleBottomCount,
  232.                  doubleTopConfirmationCount, doubleBottomConfirmationCount,
  233.                  doubleTopInvalidationCount, doubleBottomInvalidationCount] = draw_double_pattern(doubleTop,doubleBottom,doubleTopConfirmation,doubleBottomConfirmation)
  234. var stats = table.new(position = position.top_right, columns = 5, rows = 5, border_width = 1)
  235. if(barstate.islast and showStats)
  236.     colorWorst = color.rgb(255, 153, 51)
  237.     colorBest = color.rgb(51, 204, 51)
  238.     colorBad = color.rgb(255, 204, 153)
  239.     colorGood = color.rgb(204, 255, 204)
  240.     colorNeutral = color.rgb(255, 255, 204)
  241.    
  242.     dtConfirmationPercent = doubleTopConfirmationCount+doubleTopInvalidationCount == 0? 0.5 : doubleTopConfirmationCount/(doubleTopConfirmationCount+doubleTopInvalidationCount)
  243.     dbConfirmationPercent = (doubleBottomConfirmationCount+doubleBottomInvalidationCount) == 0? 0.5 : doubleBottomConfirmationCount/(doubleBottomConfirmationCount+doubleBottomInvalidationCount)
  244.    
  245.     dtColor = dtConfirmationPercent >= 0.8? colorBest :
  246.                  dtConfirmationPercent >= 0.6? colorGood :
  247.                  dtConfirmationPercent >= 0.4? colorNeutral :
  248.                  dtConfirmationPercent >= 0.2? colorBad : colorWorst
  249.     dbColor = dbConfirmationPercent >= 0.8? colorBest :
  250.                  dbConfirmationPercent >= 0.6? colorGood :
  251.                  dbConfirmationPercent >= 0.4? colorNeutral :
  252.                  dbConfirmationPercent >= 0.2? colorBad : colorWorst
  253.     table.cell(table_id = stats, column = 0, row = 0 , text = "", bgcolor=color.black, text_color=color.white)
  254.     table.cell(table_id = stats, column = 0, row = 1 , text = "Double Top", bgcolor=color.black, text_color=color.white)
  255.     table.cell(table_id = stats, column = 0, row = 2 , text = "Double Bottom", bgcolor=color.black, text_color=color.white)
  256.    
  257.     table.cell(table_id = stats, column = 1, row = 0 , text = "Count", bgcolor=color.black, text_color=color.white)
  258.     table.cell(table_id = stats, column = 2, row = 0 , text = "Confirmation", bgcolor=color.black, text_color=color.white)
  259.     table.cell(table_id = stats, column = 3, row = 0 , text = "Invalidation", bgcolor=color.black, text_color=color.white)
  260.    
  261.     table.cell(table_id = stats, column = 1, row = 1, text = tostring(doubleTopCount), bgcolor=dtColor)
  262.     table.cell(table_id = stats, column = 1, row = 2, text = tostring(doubleBottomCount), bgcolor=dbColor)
  263.    
  264.     table.cell(table_id = stats, column = 2, row = 1, text = tostring(doubleTopConfirmationCount), bgcolor=dtColor)
  265.     table.cell(table_id = stats, column = 3, row = 1, text = tostring(doubleTopInvalidationCount), bgcolor=dtColor)
  266.    
  267.     table.cell(table_id = stats, column = 2, row = 2, text = tostring(doubleBottomConfirmationCount), bgcolor=dbColor)
  268.     table.cell(table_id = stats, column = 3, row = 2, text = tostring(doubleBottomInvalidationCount), bgcolor=dbColor)
  269. if(barstate.islast and array.size(zigzagindexes) > 1)
  270.     lastHigh = 0.0
  271.     lastLow = 0.0
  272.     for x = 0 to array.size(zigzagindexes)-1
  273.         i = array.size(zigzagindexes)-1-x
  274.         index = array.get(zigzagindexes, i)
  275.         value = array.get(zigzagvalues, i)
  276.         highLow = array.get(zigzagdir, i)
  277.         index_offset = bar_index-index
  278.         
  279.         labelText = highLow ==2 ? "HH" : highLow == 1? "LH" : highLow == -1? "HL" : "LL"
  280.         labelColor = highLow ==2 ? bullishColor : highLow == 1? bullTrapColor : highLow == -1? bearTrapColor : bearishColor
  281.         labelStyle = highLow > 0? label.style_label_down : label.style_label_up
  282.         // labelLocation = highLow > 0? yloc.abovebar : yloc.belowbar
  283.         labelLocation = yloc.price
  284.         if(showPivots)
  285.             l = label.new(x=index, y = value, text=labelText, xloc=xloc.bar_index, yloc=labelLocation, style=labelStyle, size=size.tiny,
  286.                                  color=labelColor, textcolor=textColor)
  287.             array.unshift(labelArray, l)
  288.             if(array.size(labelArray) > 100)
  289.                 label.delete(array.pop(labelArray))
  290.         if(i < array.size(zigzagindexes)-1 and showZigzag)
  291.             indexLast = array.get(zigzagindexes, i+1)
  292.             valueLast = array.get(zigzagvalues, i+1)
  293.             l = line.new(x1=index, y1=value,
  294.                          x2 = indexLast, y2=valueLast,
  295.                          color=labelColor, width=2, style=line.style_solid)
  296.                         
  297.             array.unshift(lineArray, l)
  298.             if(array.size(lineArray) > 100)
  299.                 line.delete(array.pop(lineArray))
  300. alertcondition(doubleBottom, "Double Bottom", "Probable double bottom observed for {{ticker}} on {{interval}} timeframe")
  301. alertcondition(doubleBottomConfirmation > 0, "Double Bottom Confirmation", "Double bottom confirmation observed for {{ticker}} on {{interval}} timeframe")
  302. alertcondition(doubleBottomConfirmation < 0, "Double Bottom Invalidation", "Double bottom invalidation observed for {{ticker}} on {{interval}} timeframe")
  303. alertcondition(doubleTop, "Double Top", "Probable double top observed for {{ticker}} on {{interval}} timeframe")
  304. alertcondition(doubleTopConfirmation > 0, "Double Top Confirmation", "Double top confirmation observed for {{ticker}} on {{interval}} timeframe")
  305. alertcondition(doubleTopConfirmation < 0, "Double Top Invalidation", "Double top invalidation observed for {{ticker}} on {{interval}} timeframe")
复制代码
举报

评论 使用道具

精彩评论1

bigwin
DDD
| 发表于 2025-8-7 10:32:10 | 显示全部楼层
这个有重绘
举报

点赞 评论 使用道具

发新帖
EA交易
您需要登录后才可以评论 登录 | 立即注册

 简体中文国旗 简体中文
 繁體中文国旗 繁體中文
 English国旗 English(英语)
 日本語国旗 日本語(日语)
 Deutsch国旗 Deutsch(德语)
 Русский язык国旗 Русский язык(俄语)
 بالعربية国旗 بالعربية(阿拉伯语)
 Türkçe国旗 Türkçe(土耳其语)
 Português国旗 Português(葡萄牙语)
 ภาษาไทย国旗 ภาษาไทย(泰国语)
 한어国旗 한어(朝鲜语/韩语)
 Français国旗 Français(法语)
翻译