Eduardomm1

Retorno à média

Jun 28th, 2021 (edited)
933
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.85 KB | None | 0 0
  1. //@version=4
  2. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  3. // © Coded by Eduardo Mattje
  4.  
  5. strategy("Return to average", overlay=true, default_qty_type=strategy.cash, default_qty_value=1000, initial_capital=100000, process_orders_on_close=true)
  6.  
  7. // Settings
  8.  
  9. i_orderDirection = input("Long", "Order direction", options=["Both", "Long", "Short"], group="Order settings")
  10. i_exitType = input("Average", "Exit type", options=["Average", "Sell at close"], tooltip="The average type will place an exit order at the average.\n\nSell at close will sell (or buy if it is short) on the close of the bar if the high (or low if it is short) is above the average (or bellow if it is short).")
  11.  
  12. i_stopMult = input(2.0, "Stop loss amplitude multiplier", minval=0, step=0.1, group="Stop settings")
  13. i_useTR = input(true, "Use the true range for the amplitude calculations", tooltip="If you disable this, the amplitude will be calculated as the difference between the high and the low, instead of the true range of the bar.")
  14.  
  15. i_src = input(close, "Price source for MA calculation", group="MA settings")
  16. i_maPeriod = input(20, "MA period", minval=1, group="MA settings")
  17.  
  18. // MA
  19.  
  20. _ema = ema(i_src, i_maPeriod)
  21. plot(_ema, "EMA", color.red)
  22.  
  23. // Order variables
  24.  
  25. inMarket = strategy.opentrades != 0
  26. opened = strategy.opentrades[0] > strategy.opentrades[1]
  27. closed = strategy.closedtrades[0] > strategy.closedtrades[1]
  28.  
  29. longPosition = strategy.position_entry_name == "Long"
  30. shortPosition = strategy.position_entry_name == "Short"
  31.  
  32. maPerChange = (_ema - close) / _ema * 100
  33.  
  34. longCondition = open < _ema and maPerChange > 2 and i_orderDirection != "Short" and not inMarket
  35. shortCondition = open > _ema and maPerChange < - 2 and i_orderDirection != "Long" and not inMarket
  36.  
  37. // Get risk size
  38.  
  39. var startPrice = 0.0
  40. var riskSize = 0.0
  41.  
  42. if barstate.isfirst
  43.     strategy.entry("Get risk size", true)
  44.     startPrice := close
  45.  
  46. if barssince(barstate.isfirst) >= 1 and strategy.position_entry_name == "Get risk size"
  47.     riskSize := round((strategy.position_size * startPrice), 2)
  48.     strategy.close("Get risk size", comment="The risk size is " + tostring(riskSize) + " " + syminfo.currency)
  49.  
  50. // Stop loss calculations
  51.  
  52. var tempStopPriceLong = 0.0
  53. var tempStopPriceShort = 0.0
  54.  
  55. var stopPriceLong = 0.0
  56. var stopPriceShort = 0.0
  57.  
  58. var stopTicksLong = 0.0
  59. var stopTicksShort = 0.0
  60.  
  61. stopAmplitude = i_stopMult * (i_useTR ? tr(true) : abs(high - low))
  62.  
  63. tempStopPriceLong := low - stopAmplitude
  64. tempStopPriceShort := high + stopAmplitude
  65.    
  66. if not inMarket
  67.     stopPriceLong := tempStopPriceLong
  68.     stopPriceShort := tempStopPriceShort
  69.     stopTicksLong := abs(close - stopPriceLong)
  70.     stopTicksShort := abs(close - stopPriceShort)
  71.  
  72. orderSizeLong = riskSize / stopTicksLong
  73. orderSizeShort = riskSize / stopTicksShort
  74.  
  75. stopPrice = longPosition ? stopPriceLong : shortPosition ? stopPriceShort : na
  76.  
  77. plot(stopPrice ? stopPrice : stopPrice[1], "Stop price", color.red, 2, plot.style_linebr)
  78.  
  79. // Entry orders
  80.  
  81. enterLong() => strategy.entry("Long", true, orderSizeLong, comment="Entry long with a " + tostring(round(orderSizeLong * close)) + " " + syminfo.currency + " order")
  82. if longCondition
  83.     enterLong()
  84.  
  85. enterShort() => strategy.entry("Short", false, orderSizeShort, comment="Entry short with a " + tostring(round(orderSizeShort * close)) + " " + syminfo.currency + " order")
  86. if shortCondition
  87.     enterShort()
  88.  
  89. // Exit orders
  90.  
  91. exitLongC = high >= _ema
  92. exitShortC = low <= _ema
  93. exitAverage = i_exitType == "Average" ? _ema : na
  94.  
  95. strategy.exit("Long", "Long", limit=exitAverage, stop=stopPriceLong, comment="Exit long")
  96. strategy.exit("Short", "Short", limit=exitAverage, stop=stopPriceShort, comment="Exit short")
  97.  
  98. strategy.close("Long", exitLongC, "Exit long")
  99. strategy.close("Short", exitShortC, "Exit short")
  100.  
  101.  
Add Comment
Please, Sign In to add comment