Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=4
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // © Coded by Eduardo Mattje
- strategy("Return to average", overlay=true, default_qty_type=strategy.cash, default_qty_value=1000, initial_capital=100000, process_orders_on_close=true)
- // Settings
- i_orderDirection = input("Long", "Order direction", options=["Both", "Long", "Short"], group="Order settings")
- 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).")
- i_stopMult = input(2.0, "Stop loss amplitude multiplier", minval=0, step=0.1, group="Stop settings")
- 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.")
- i_src = input(close, "Price source for MA calculation", group="MA settings")
- i_maPeriod = input(20, "MA period", minval=1, group="MA settings")
- // MA
- _ema = ema(i_src, i_maPeriod)
- plot(_ema, "EMA", color.red)
- // Order variables
- inMarket = strategy.opentrades != 0
- opened = strategy.opentrades[0] > strategy.opentrades[1]
- closed = strategy.closedtrades[0] > strategy.closedtrades[1]
- longPosition = strategy.position_entry_name == "Long"
- shortPosition = strategy.position_entry_name == "Short"
- maPerChange = (_ema - close) / _ema * 100
- longCondition = open < _ema and maPerChange > 2 and i_orderDirection != "Short" and not inMarket
- shortCondition = open > _ema and maPerChange < - 2 and i_orderDirection != "Long" and not inMarket
- // Get risk size
- var startPrice = 0.0
- var riskSize = 0.0
- if barstate.isfirst
- strategy.entry("Get risk size", true)
- startPrice := close
- if barssince(barstate.isfirst) >= 1 and strategy.position_entry_name == "Get risk size"
- riskSize := round((strategy.position_size * startPrice), 2)
- strategy.close("Get risk size", comment="The risk size is " + tostring(riskSize) + " " + syminfo.currency)
- // Stop loss calculations
- var tempStopPriceLong = 0.0
- var tempStopPriceShort = 0.0
- var stopPriceLong = 0.0
- var stopPriceShort = 0.0
- var stopTicksLong = 0.0
- var stopTicksShort = 0.0
- stopAmplitude = i_stopMult * (i_useTR ? tr(true) : abs(high - low))
- tempStopPriceLong := low - stopAmplitude
- tempStopPriceShort := high + stopAmplitude
- if not inMarket
- stopPriceLong := tempStopPriceLong
- stopPriceShort := tempStopPriceShort
- stopTicksLong := abs(close - stopPriceLong)
- stopTicksShort := abs(close - stopPriceShort)
- orderSizeLong = riskSize / stopTicksLong
- orderSizeShort = riskSize / stopTicksShort
- stopPrice = longPosition ? stopPriceLong : shortPosition ? stopPriceShort : na
- plot(stopPrice ? stopPrice : stopPrice[1], "Stop price", color.red, 2, plot.style_linebr)
- // Entry orders
- enterLong() => strategy.entry("Long", true, orderSizeLong, comment="Entry long with a " + tostring(round(orderSizeLong * close)) + " " + syminfo.currency + " order")
- if longCondition
- enterLong()
- enterShort() => strategy.entry("Short", false, orderSizeShort, comment="Entry short with a " + tostring(round(orderSizeShort * close)) + " " + syminfo.currency + " order")
- if shortCondition
- enterShort()
- // Exit orders
- exitLongC = high >= _ema
- exitShortC = low <= _ema
- exitAverage = i_exitType == "Average" ? _ema : na
- strategy.exit("Long", "Long", limit=exitAverage, stop=stopPriceLong, comment="Exit long")
- strategy.exit("Short", "Short", limit=exitAverage, stop=stopPriceShort, comment="Exit short")
- strategy.close("Long", exitLongC, "Exit long")
- strategy.close("Short", exitShortC, "Exit short")
Add Comment
Please, Sign In to add comment