Advertisement
sygma1982

100% grid bot

May 25th, 2023
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. //@version=5
  2. strategy('(IK) Grid Script', overlay=true, pyramiding=14, close_entries_rule='ANY', default_qty_type=strategy.cash, initial_capital=100.0, currency='USD', commission_type=strategy.commission.percent, commission_value=0.1)
  3. i_autoBounds = input.bool(group='Grid Bounds', title='Use Auto Bounds?', defval=true) // calculate upper and lower bound of the grid automatically? This will theorhetically be less profitable, but will certainly require less attention
  4. i_boundSrc = input.string(group='Grid Bounds', title='(Auto) Bound Source', defval='Hi & Low', options=['Hi & Low', 'Average']) // should bounds of the auto grid be calculated from recent High & Low, or from a Simple Moving Average
  5. i_boundLookback = input.int(group='Grid Bounds', title='(Auto) Bound Lookback', defval=250, maxval=500, minval=0) // when calculating auto grid bounds, how far back should we look for a High & Low, or what should the length be of our sma
  6. i_boundDev = input.float(group='Grid Bounds', title='(Auto) Bound Deviation', defval=0.10, maxval=1, minval=-1) // if sourcing auto bounds from High & Low, this percentage will (positive) widen or (negative) narrow the bound limits. If sourcing from Average, this is the deviation (up and down) from the sma, and CANNOT be negative.
  7. i_upperBound = input.float(group='Grid Bounds', title='(Manual) Upper Boundry', defval=0.285) // for manual grid bounds only. The upperbound price of your grid
  8. i_lowerBound = input.float(group='Grid Bounds', title='(Manual) Lower Boundry', defval=0.225) // for manual grid bounds only. The lowerbound price of your grid.
  9. i_gridQty = input.int(group='Grid Lines', title='Grid Line Quantity', defval=8, maxval=15, minval=3) // how many grid lines are in your grid
  10.  
  11. f_getGridBounds(_bs, _bl, _bd, _up) =>
  12. if _bs == 'Hi & Low'
  13. _up ? ta.highest(close, _bl) * (1 + _bd) : ta.lowest(close, _bl) * (1 - _bd)
  14. else
  15. avg = ta.sma(close, _bl)
  16. _up ? avg * (1 + _bd) : avg * (1 - _bd)
  17.  
  18. f_buildGrid(_lb, _gw, _gq) =>
  19. gridArr = array.new_float(0)
  20. for i = 0 to _gq - 1 by 1
  21. array.push(gridArr, _lb + _gw * i)
  22. gridArr
  23.  
  24. f_getNearGridLines(_gridArr, _price) =>
  25. arr = array.new_int(3)
  26. for i = 0 to array.size(_gridArr) - 1 by 1
  27. if array.get(_gridArr, i) > _price
  28. array.set(arr, 0, i == array.size(_gridArr) - 1 ? i : i + 1)
  29. array.set(arr, 1, i == 0 ? i : i - 1)
  30. break
  31. arr
  32.  
  33. var upperBound = i_autoBounds ? f_getGridBounds(i_boundSrc, i_boundLookback, i_boundDev, true) : i_upperBound // upperbound of our grid
  34. var lowerBound = i_autoBounds ? f_getGridBounds(i_boundSrc, i_boundLookback, i_boundDev, false) : i_lowerBound // lowerbound of our grid
  35. var gridWidth = (upperBound - lowerBound) / (i_gridQty - 1) // space between lines in our grid
  36. var gridLineArr = f_buildGrid(lowerBound, gridWidth, i_gridQty) // an array of prices that correspond to our grid lines
  37. var orderArr = array.new_bool(i_gridQty, false) // a boolean array that indicates if there is an open order corresponding to each grid line
  38.  
  39. var closeLineArr = f_getNearGridLines(gridLineArr, close) // for plotting purposes - an array of 2 indices that correspond to grid lines near price
  40. var nearTopGridLine = array.get(closeLineArr, 0) // for plotting purposes - the index (in our grid line array) of the closest grid line above current price
  41. var nearBotGridLine = array.get(closeLineArr, 1) // for plotting purposes - the index (in our grid line array) of the closest grid line below current price
  42.  
  43. for i = 0 to array.size(gridLineArr) - 1 by 1
  44. if close < array.get(gridLineArr, i) and not array.get(orderArr, i) and i < array.size(gridLineArr) - 1
  45. buyId = i
  46. array.set(orderArr, buyId, true)
  47. strategy.entry(id=str.tostring(buyId), direction=strategy.long, qty=strategy.initial_capital / (i_gridQty - 1) / close, comment='#' + str.tostring(buyId))
  48. if close > array.get(gridLineArr, i) and i != 0
  49. if array.get(orderArr, i - 1)
  50. sellId = i - 1
  51. array.set(orderArr, sellId, false)
  52. strategy.close(id=str.tostring(sellId), comment='#' + str.tostring(sellId))
  53.  
  54. if i_autoBounds
  55. upperBound := f_getGridBounds(i_boundSrc, i_boundLookback, i_boundDev, true)
  56. lowerBound := f_getGridBounds(i_boundSrc, i_boundLookback, i_boundDev, false)
  57. gridWidth := (upperBound - lowerBound) / (i_gridQty - 1)
  58. gridLineArr := f_buildGrid(lowerBound, gridWidth, i_gridQty)
  59. gridLineArr
  60.  
  61. closeLineArr := f_getNearGridLines(gridLineArr, close)
  62. nearTopGridLine := array.get(closeLineArr, 0)
  63. nearBotGridLine := array.get(closeLineArr, 1)
  64.  
  65.  
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement