Untitled

 avatar
unknown
plain_text
2 years ago
10 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/
// © robbieboi

//@version=5
strategy("[DEVELOPMENT]", overlay = true, pyramiding = 100, initial_capital = 20000, currency = currency.USD)

// The point of something with a name of "Development" is to test and try new algos that might have potential. The goal for this one is: if price is under EMA 200, long every time the RSI(14) reaches 30 and RSI(5) reaches 30 too. TP at EMA 800. When it's above EMA20, keep getting aggressive. 
// ABOUT THIS STRATEGY: This strategy has 4 entries and takes profit @ EMA800 or X% of profit. Mean reversion type of strategy with Martingale. 




// INPUTS 
// Indicator
rsi5 = ta.rsi(close, 5)
rsi14 = ta.rsi(close, 14)
ema20 = ta.ema(close, 20) 
ema200 = ta.ema(close, 200)
ema800 = ta.ema(close,800)
ema3 = ta.ema(close, 3)
ema5 = ta.ema(close, 5)
ema8 = ta.ema(close, 8)
ema10 = ta.ema(close, 10)

defaultQty = input.int(1000)
positionSizeMultiplier = input.float(1.2)

// CONSOLIDATION
// If the difference between EMA200 and EMA200[10] is less, then X percentage (let's say 0.6% for the sake of example)
consilidaitonVariable = ema200 - ema200[100]*100


// TP & SL
longTPInput = input.float(0.3, title='Long Take Profit %', step=0.1)/100
longSLInput = input.float(5, title='Long Stop Loss %', step=0.1)/100
shortTPInput = input.float(0.3, title='Short Take Profit %', step=0.1)/100
shortSLInput = input.float(5, title='Short Stop Loss %', step=0.1)/100

// TP & SL VARIABLES
var float longTakeProfit = 0.0
var float longStopLoss = 0.0
var float shortTakeProfit = 0.0
var float shortStopLoss = 0.0
var bool longStopTriggered = false
var bool longProfitTriggered = false
var bool shortStopTriggered = false
var bool shortProfitTriggered = false
var openLongTrades = 0
var openShortTrades = 0
var float positionSize = 1000
var float shortPositionSize = 1000
var bool consolidation = false

if strategy.position_size > 0
    longTakeProfit := strategy.position_avg_price * (1 + longTPInput)
    longStopLoss := strategy.position_avg_price * (1 - longSLInput)
    longStopTriggered := false
    longProfitTriggered := false    
if strategy.position_size < 0
    shortTakeProfit := strategy.position_avg_price * (1 - shortTPInput)
    shortStopLoss := strategy.position_avg_price * (1 + shortSLInput)
    shortStopTriggered := false
    shortProfitTriggered := false


// if math.abs(consilidaitonVariable) >= 6
//     consolidation := true

// if consolidation == true
//     positionSize := 20000
    
// if math.abs(consilidaitonVariable) < 6
//     consolidation := false

// LONG CONDITIONS
longEntry1 = ta.crossover(rsi5,30) and rsi14 < 40 and close < ema200 and close < ema800 
//plotshape(longEntry1, title = "LongEntry1", text = "LongEntry1", style = shape.triangleup, location = location.belowbar, color = color.green, size=size.tiny, editable = false, textcolor = color.green)

longEntry2 = ta.crossover(rsi5, 40) and close > ema20 and close < ema800
//plotshape(longEntry2, title = "LognEntry2", text = "LongEntry2", style = shape.triangleup, location = location.belowbar, color = color.green, size=size.tiny, editable = false, textcolor = color.green)

longEntry3 = ta.crossover(ema3,ema8) and rsi14 < 40 and close < ema200 and close < ema800
//plotshape(longEntry3, title = "LognEntry3", text = "LongEntry3", style = shape.triangleup, location = location.belowbar, color = color.green, size=size.tiny, editable = false, textcolor = color.green)

longEntry4 = ta.crossover(ema5,ema10) and rsi14 < 50 and close < ema200 and close < ema800
//plotshape(longEntry4, title = "LognEntry4", text = "LongEntry4", style = shape.triangleup, location = location.belowbar, color = color.green, size=size.tiny, editable = false, textcolor = color.green)

// LONG ENTRIES
if longEntry1
    strategy.entry("LongEntry1", strategy.long, comment = "Long1", qty = positionSize)
    openLongTrades := openLongTrades[1] + 1
    positionSize := defaultQty*(positionSizeMultiplier*openLongTrades)

if longEntry2
    strategy.entry("LongEntry2", strategy.long, comment = "Long2", qty = positionSize)
    openLongTrades := openLongTrades[1] + 1
    positionSize := defaultQty*(positionSizeMultiplier*openLongTrades)

if longEntry3
    strategy.entry("LongEntry3", strategy.long, comment = "Long2", qty = positionSize)
    openLongTrades := openLongTrades[1] + 1
    positionSize := defaultQty*(positionSizeMultiplier*openLongTrades)

if longEntry4
    strategy.entry("LongEntry4", strategy.long, comment = "Long2", qty = positionSize)
    openLongTrades := openLongTrades[1] + 1
    positionSize := defaultQty*(positionSizeMultiplier*openLongTrades)

// CLOSE LONGS
if close > ema800
    strategy.close("LongEntry1", "TP Hit EMA", immediately = true)
    strategy.close("LongEntry2", "TP Hit EMA", immediately = true)
    strategy.close("LongEntry3", "TP Hit EMA", immediately = true)
    strategy.close("LongEntry4", "TP Hit EMA", immediately = true)
    openLongTrades := 0
    consolidation := false

    
if  close > longTakeProfit and longStopTriggered == false
    strategy.close("LongEntry1", "Close - Price Target",immediately = true)
    strategy.close("LongEntry2", "Close - Price Target",immediately = true)
    strategy.close("LongEntry3", "Close - Price Target",immediately = true)
    strategy.close("LongEntry4", "Close - Price Target",immediately = true)
    longProfitTriggered := true
    openLongTrades := 0
    consolidation := false

if close < longStopLoss and longProfitTriggered == false and longStopTriggered == false
    strategy.close("LongEntry1", "Close - LongSL", immediately = true)
    strategy.close("LongEntry2", "Close - LongSL", immediately = true)
    strategy.close("LongEntry3", "Close - LongSL", immediately = true)
    strategy.close("LongEntry4", "Close - LongSL", immediately = true)
    longStopTriggered := true
    openLongTrades := 0
    consolidation := false

// // Short TP & SL
// if  close < shortTakeProfit and shortStopTriggered == false
//     strategy.close("Random_ID", "Close - LongSL",immediately = true)
//     shortProfitTriggered := true
//     openLongTrades := 0
//     consolidation := false

// if close > shortStopLoss and shortProfitTriggered == false and shortStopTriggered == false
//     strategy.close("Random_ID", "Close - shortSL", immediately = true)
//     shortStopTriggered := true
//     openLongTrades := 0
//     consolidation := false



// SHORT CONDITIONS
shortEntry1 = ta.crossunder(rsi5,70) and rsi14 < 60 and close > ema200 and close > ema800 and strategy.position_size <= 0
//plotshape(shortEntry1, title = "shortEntry1", text = "shortEntry1", style = shape.triangledown, location = location.abovebar, color = color.red, size=size.tiny, editable = false, textcolor = color.red)

shortEntry2 = ta.crossunder(rsi5, 60) and close < ema20 and close > ema800
// plotshape(shortEntry2, title = "shortEntry2", text = "shortEntry2", style = shape.triangledown, location = location.abovebar, color = color.red, size=size.tiny, editable = false, textcolor = color.red)

shortEntry3 = ta.crossunder(ema3,ema8) and rsi14 > 40 and close > ema200 and close > ema800
// plotshape(shortEntry3, title = "shortEntry3", text = "shortEntry3", style = shape.triangledown, location = location.abovebar, color = color.red, size=size.tiny, editable = false, textcolor = color.red)

shortEntry4 = ta.crossunder(ema5,ema10) and rsi14 > 50 and close > ema200 and close > ema800
// plotshape(shortEntry4, title = "shortEntry4", text = "shortEntry4", style = shape.triangledown, location = location.abovebar, color = color.red, size=size.tiny, editable = false, textcolor = color.red)


// SHORT ENTRIES
if shortEntry1
    strategy.entry("shortEntry1", strategy.short, comment = "Short1", qty = positionSize)
    openShortTrades := openShortTrades[1] + 1
    positionSize := defaultQty*(positionSizeMultiplier*openShortTrades)

if shortEntry2
    strategy.entry("shortEntry2", strategy.short, comment = "Short2", qty = positionSize)
    openShortTrades := openShortTrades[1] + 1
    positionSize := defaultQty*(positionSizeMultiplier*openShortTrades)

if shortEntry3
    strategy.entry("shortEntry3", strategy.short, comment = "Short3", qty = positionSize)
    openShortTrades := openShortTrades[1] + 1
    positionSize := defaultQty*(positionSizeMultiplier*openShortTrades)

if shortEntry4
    strategy.entry("shortEntry4", strategy.short, comment = "Short4", qty = positionSize)
    openShortTrades := openShortTrades[1] + 1
    positionSize := defaultQty*(positionSizeMultiplier*openShortTrades)


// CLOSE SHORTS
if close < ema800
    strategy.close("shortEntry1", "Short TP Hit EMA", immediately = true)
    strategy.close("shortEntry2", "Short TP Hit EMA", immediately = true)
    strategy.close("shortEntry3", "Short TP Hit EMA", immediately = true)
    strategy.close("shortEntry4", "Short TP Hit EMA", immediately = true)
    openShortTrades := 0
    consolidation := false

    
if  close < shortTakeProfit and shortStopTriggered == false
    strategy.close("shortEntry1", "Short Close - Price Target",immediately = true)
    strategy.close("shortEntry2", "Short Close - Price Target",immediately = true)
    strategy.close("shortEntry3", "Short Close - Price Target",immediately = true)
    strategy.close("shortEntry4", "Short Close - Price Target",immediately = true)
    shortProfitTriggered := true
    openShortTrades := 0
    consolidation := false

if close > shortStopLoss and shortProfitTriggered == false and shortStopTriggered == false
    strategy.close("shortEntry1", "Short Close - ShortSL", immediately = true)
    strategy.close("shortEntry2", "Short Close - ShortSL", immediately = true)
    strategy.close("shortEntry3", "Short Close - ShortSL", immediately = true)
    strategy.close("shortEntry4", "Short Close - ShortSL", immediately = true)
    shortStopTriggered := true
    openShortTrades := 0
    consolidation := false

Editor is loading...