Untitled

 avatar
unknown
plain_text
2 years ago
5.0 kB
10
Indexable
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ScryptoMB

//@version=5
strategy('ScryptoMB - DCA test', overlay=true, precision=2, pyramiding=51, calc_on_order_fills=false, 
 calc_on_every_tick=true, default_qty_type=strategy.fixed, currency=currency.USD, slippage=1, commission_type=strategy.commission.percent,
 commission_value=.025, process_orders_on_close=false, initial_capital=26000, margin_long=100, margin_short=100)

ema1 = ta.ema(close, 10)
ema2 = ta.ema(close, 20)

plot(ema1, color=color.yellow)
plot(ema2, color=color.orange)

longEntry = ta.crossover(ema1,ema2) and (strategy.opentrades == 0) and barstate.isconfirmed
// shortEntry = ta.crossunder(ema1,ema2) and (strategy.opentrades == 0) and barstate.isconfirmed

plotshape(longEntry, title='Long Entry', location=location.belowbar, style=shape.labelup, color=color.new(#2C9670, 0), text='Long', textcolor=color.new(color.white, 0))
// plotshape(shortEntry, title='Short Entry', location=location.abovebar, style=shape.labeldown, color=color.new(#B84343, 0), text='Short', textcolor=color.new(color.white, 0))

tpPerc = input.float(0.2, title="% Take Profit", minval=0.0, step=0.1, group="Exit Conditions") / 100
slPerc = input.float(3, title="% Stop Loss", minval=0.0, step=0.1, group="Exit Conditions") / 100

DCA1perc = input.float(0.15, title="DCA1%", step=0.1, group="DCA Bot Settings") / 100
DCA2perc = input.float(0.25, title="DCA2%", step=0.1, group="DCA Bot Settings") / 100
DCA3perc = input.float(0.6, title="DCA3%", step=0.1, group="DCA Bot Settings") / 100

quantity = input.float(100, title="Size Of Base Order", minval=.000000001, step=.000000001, group="DCA Bot Settings")
multiplier = input.float(1.5, title="Volume Multiplier", minval=1, step=.001, tooltip="Increasing this value above 1 will multiply each new orders volume by the amount you input. This makes your buys bigger every time you average down so you don't need as big of a bounce when it recovers to get out in profit.", group="DCA Bot Settings")


quantityConverted = quantity/close
quantMultiplied1 = quantityConverted * multiplier
quantMultiplied2 = quantMultiplied1 * multiplier
quantMultiplied3 = quantMultiplied2 * multiplier


// Pecentage based TP & SL 
TP = strategy.position_avg_price * (1 + tpPerc)
SL = strategy.opentrades.entry_price(0) * (1 - slPerc)

DCA1 = ta.valuewhen(longEntry and strategy.opentrades == 0,close,0) * (1-DCA1perc)
DCA2 = ta.valuewhen(longEntry and strategy.opentrades == 0,close,0) * (1-DCA2perc)
DCA3 = ta.valuewhen(longEntry and strategy.opentrades == 0,close,0) * (1-DCA3perc)



if longEntry and strategy.opentrades == 0
    strategy.entry("Base Order", strategy.long, qty=quantityConverted, comment="Base Order")


if longEntry and strategy.opentrades == 0
    strategy.entry("Safety Order 1", strategy.long, qty=quantMultiplied1, limit=DCA1, comment="Safety Order")

if longEntry and strategy.opentrades == 0
    strategy.entry("Safety Order 2", strategy.long, qty=quantMultiplied2, limit=DCA2, comment="Buy #3")

if longEntry and strategy.opentrades == 0
    strategy.entry("Safety Order 3", strategy.long, qty=quantMultiplied3, limit=DCA3, comment="Buy #4")


strategy.exit("Long Exit", "Base Order", limit=TP, stop=SL)
strategy.exit("Long Exit1", "Safety Order 1", limit=TP, stop=SL)
strategy.exit("Long Exit2", "Safety Order 2", limit=TP, stop=SL)
strategy.exit("Long Exit3", "Safety Order 3", limit=TP, stop=SL)


// Make sure that all open limit orders are canceled after exiting all the positions
longClose = strategy.position_size[1] > 0 and strategy.position_size == 0 ? 1 : 0

if longClose
    strategy.cancel_all()

entryLevel = plot(strategy.position_avg_price, title="Entry level", color=color.white, linewidth=1, style=plot.style_linebr, transp=50) 
LTP = plot((strategy.position_size > 0) ? TP : na, title="Take Profit 1", color=color.teal, style=plot.style_linebr, linewidth=1)
SLL = plot((strategy.position_size > 0) ? SL : na, title="Stop Loss", color=color.red, style=plot.style_linebr, linewidth=1)

LDCA1 = plot((strategy.position_size > 0) and (strategy.opentrades <= 1) ? DCA1 : na, title="DCA 1", color=color.fuchsia, style=plot.style_linebr, linewidth=1)
LDCA2 = plot((strategy.position_size > 0) and (strategy.opentrades <= 2) ? DCA2 : na, title="DCA 2", color=color.fuchsia, style=plot.style_linebr, linewidth=1)
LDCA3 = plot((strategy.position_size > 0) and (strategy.opentrades <= 3) ? DCA3 : na, title="DCA 3", color=color.fuchsia, style=plot.style_linebr, linewidth=1)


// Position sizing + leverage
// ge(value, precision) =>
//     math.round(value * math.pow(10, precision)) / math.pow(10, precision)

// port     = input.float(25,  group='Risk', title='Portfolio Percentage', step=0.1, minval=0.1, maxval=200)
// leverage = input.int(3,     group='Risk', title='Leverage',                       minval=1,   maxval=100)

// c = ge(strategy.equity * leverage / open * (port / 100), 4)
Editor is loading...