Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=4
- //VonKrieger
- // To change this from a Strategy to a Indicator:-
- // 1) uncomment the next line and comment out the strategy line.
- // 2) at the end of the file comment out the last 2 lines
- // study(title="VonKrieger_Turtle_Indicator", overlay=true)
- strategy(title="VonKrieger_Turtle_Strategy", overlay=true, initial_capital=100000, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type=strategy.commission.percent, commission_value=0.1, pyramiding=5)
- stopInput = input(2.0, "Stop N", step=.5)
- pyramidInput = input(1, "Pyramid N", step=.5)
- l1LongInput = input(20, "L1 Long", minval=5)
- l2LongInput = input(55, "L2 Long", minval=5)
- l1LongExitInput = input (10, "L1 Long Exit", minval=5)
- l2LongExitInput = input (20, "L2 Long Exit", minval=5)
- FromYear = input(2000, "From Year", minval=1900), FromMonth = input(1, "From Month", minval=1, maxval=12), FromDay = input(1, "From Day", minval=1, maxval=31)
- ToYear = input(9999, "To Year", minval=1900), ToMonth = input(1, "To Month", minval=1, maxval=12), ToDay = input(1, "To Day", minval=1, maxval=31)
- FromDate = timestamp(FromYear, FromMonth, FromDay, 00, 00), ToDate = timestamp(ToYear, ToMonth, ToDay, 23, 59)
- TradeDateIsAllowed() => time >= FromDate and time <= ToDate
- l1Long = highest(l1LongInput)
- l1LongExit = lowest(l1LongExitInput)
- l2Long = highest(l2LongInput)
- l2LongExit = lowest(l2LongExitInput)
- bool win = false // tracks if last trade was winning trade of losing trade.
- float totalPrice = 0.0 // tracks total price, used for calculating avgPrice
- float buyPrice = 0.0 // tracks the buy price of the last long position.
- float avgPrice = 0.0 // tracks the avg price of all currently held long positions.
- float nextBuyPrice = 0.0 // tracks the next buy price
- float stopPrice = na // tracks the stop price
- int totalBuys = 0 // tracks the total # of pyramid buys
- bool inBuy = false // tracks if we are in a long position or not.
- float l1LongPlot = highest(l1LongInput) // tracks the L1 price to display
- float l2LongPlot = highest(l2LongInput) // tracks the L2 price to display
- float n = atr(14) // tracks the n used to calculate stops and pyramid buys
- string mode = 'L1' // tracks whether we are in a L1 position or L2 position.
- bool fake = na // tracks if this is a fake trade, see comments below.
- string longLevel = na // tracks where long positions, stops, pyramid buys occur.
- // by default use the last value from the previous bar.
- buyPrice := buyPrice[1]
- totalBuys := totalBuys[1]
- nextBuyPrice := nextBuyPrice[1]
- stopPrice := stopPrice[1]
- avgPrice := avgPrice[1]
- totalPrice := totalPrice[1]
- win := win[1]
- // State to track if we are in a long positon or not.
- inBuy := not inBuy[1] and (close > l1Long[1] or close > l2Long[1]) ? true : inBuy[1]
- inBuy := inBuy[1] and close < stopPrice ? false : inBuy
- inBuy := inBuy[1] and mode[1] == 'L1' and close < l1LongExit[1] ? false : inBuy
- inBuy := inBuy[1] and mode[1] == 'L2' and close < l2LongExit[1] ? false : inBuy
- // State to track if we are ia a fake trade. If the last trade was a winning, we need to skip the next trade.
- // We still track it though as a fake trade (not counted against us). as the outcome determines if we can
- // can take the next trade.
- fake := not inBuy[1] and close > l1Long[1] and win[1] ? true : fake[1]
- fake := not inBuy[1] and close > l1Long[1] and not win[1] ? false : fake
- fake := close > l2Long[1] ? false : fake
- // Series representing the l1 and l2 levels. If we break out above the l1 or l2 level, we want the
- // line to stay at the breakout level, not follow it up.
- l1LongPlot := iff(not inBuy or (inBuy and mode == 'L1' and fake),l1Long[1],l1LongPlot[1])
- l2LongPlot := iff(not inBuy or (inBuy and mode == 'L1' and fake),l2Long[1],l2LongPlot[1])
- // Variable in the series is only set when it happens. Possible values is L1, L2, SR
- // (stopped out with a loss), SG (exited with a gain), and 'P' for pyramid buy.
- longLevel := not inBuy[1] and close > l1Long[1] ? 'L1' : na
- longLevel := not inBuy[1] and close > l2Long[1] ? 'L2' : longLevel
- longLevel := not inBuy[1] and close > l1Long[1] and close > l2Long[1] and fake ? 'L2' : longLevel
- // longLevel := longLevel == na and close > l2Long[1] and mode[1] != 'L2' ? 'L2' : longLevel
- longLevel := longLevel == na and close > l2Long[1] and mode[1] == 'L1' ? na : longLevel // don't switch to L2 if we are already in a L1
- longLevel := longLevel == na and close > l2Long[1] and mode[1] == 'L1' and fake[1] ? 'L2' : longLevel
- // Either 'L1' or 'L2' depending on what breakout level we are in.
- mode := longLevel == na ? mode[1] : longLevel
- // Variables to track calculating avgPrice and nextBuyPrice for pyramiding.
- if longLevel == 'L1' or longLevel == 'L2'
- buyPrice = close
- totalBuys := 1
- totalPrice := close
- avgPrice := buyPrice
- stopPrice := close - (stopInput*n)
- nextBuyPrice := high + (pyramidInput*n)
- // Marks if we hit our next buy price, if so mark it with a 'P'
- longLevel := inBuy[1] and close > nextBuyPrice and TradeDateIsAllowed() and totalBuys < 5 ? 'P' : longLevel
- if longLevel == 'P'
- buyPrice = close
- totalBuys := totalBuys[1] + 1
- totalPrice := totalPrice[1] + buyPrice
- avgPrice := totalPrice / totalBuys
- stopPrice := close - (stopInput*n)
- nextBuyPrice := high + (pyramidInput*n)
- // Tracks stops and exits, marking them with SG or SR
- longLevel := longLevel == na and inBuy[1] and close < stopPrice and close >= avgPrice ? 'SG' : longLevel
- longLevel := longLevel == na and inBuy[1] and close < stopPrice and close < avgPrice ? 'SR' : longLevel
- longLevel := longLevel == na and mode[1] == 'L1' and inBuy[1] and (close < l1LongExit[1]) and close >= avgPrice ? 'SG' : longLevel
- longLevel := longLevel == na and mode[1] == 'L2' and inBuy[1] and (close < l2LongExit[1]) and close >= avgPrice ? 'SG' : longLevel
- longLevel := longLevel == na and mode[1] == 'L1' and inBuy[1] and (close < l1LongExit[1]) and close < avgPrice ? 'SR' : longLevel
- longLevel := longLevel == na and mode[1] == 'L2' and inBuy[1] and (close < l2LongExit[1]) and close < avgPrice ? 'SR' : longLevel
- // Tracks if the trade was a win or loss.
- win := longLevel == 'SG' ? true : win
- win := longLevel == 'SR' ? false : win
- // Variables used to tell strategy when to enter/exit trade.
- enterLong = (longLevel == 'L1' or longLevel == 'L2' or longLevel == 'P') and not fake and TradeDateIsAllowed()
- exitLong = (longLevel == 'SG' or longLevel == 'SR') and not fake and TradeDateIsAllowed()
- p1 = plot(l1LongPlot, title="l1 long", linewidth=3, style=plot.style_stepline, color=color.green)
- p2 = plot(l1LongExit[1], title="l1 exit", linewidth=3, style=plot.style_stepline, color=color.red)
- p3 = plot(l2LongPlot, title="l2 long", linewidth=2, style=plot.style_stepline, color=color.green)
- p4 = plot(l2LongExit[1], title="l2 exit", linewidth=2, style=plot.style_stepline, color=color.red)
- color1 = color.new(color.black, 0)
- color2 = color.new(color.black, 100)
- col= (inBuy) ? color1 : color2
- p5 = plot(stopPrice, title="stop", linewidth=2, style=plot.style_circles, join=true, color=col)
- p6 = plot(nextBuyPrice, title="next buy", linewidth=2, style=plot.style_circles, join=true, color=col)
- fill(p1, p3, color=color.green)
- fill(p2, p4, color=color.red)
- plotshape(longLevel == 'L1' and not fake ? true : false, color=color.green, transp=40, style=shape.triangleup, text="L1") // up arrow for entering L1 trade
- plotshape(fake and longLevel == 'L1' ? true : false, color=color.gray, transp=40, style=shape.triangleup, text="L1") // up arrow for entering L1 trade
- plotshape((mode == 'L2' or (mode == 'L1' and not fake)) and longLevel == 'P' ? true : false, color=color.green, transp=40, style=shape.triangleup, text='P') // up arrow for entering L1 trade
- plotshape(mode == 'L1' and fake and longLevel == 'P' ? true : false, color=color.gray, transp=40, style=shape.triangleup, text='P') // up arrow for entering L1 trade
- plotshape(longLevel == 'L2' ? true : false, color=color.green, transp=40, style=shape.triangleup, text="L2") // up arrow for entering L2 trade
- plotarrow(longLevel == 'L1' ? 1 : 0, colordown=color.black, colorup=color.green, transp=40) // up arrow for entering L1 trade
- plotarrow(longLevel == 'L2' ? 1 : 0, colordown=color.black, colorup=color.green, transp=40) // up arrow for entering L2 trade
- plotarrow(longLevel == 'SR' ? -1 : 0, colordown=color.red, colorup=color.purple, transp=40) // down arrow for losing trade
- plotarrow(longLevel == 'SG' ? -1 : 0, colordown=color.green, colorup=color.purple, transp=40) // down arrow for winning trade
- // plotarrow(win ? -1 : 0, colordown=color.yellow, colorup=color.purple, transp=40) // down arrow for winning trade
- // plotshape(inBuy[1], color=color.blue, transp=40, text='X') // down arrow for winning trade
- // label.new(bar_index, high, style=label.style_none, text=longLevel)
- alertcondition(low < stopPrice, title="crosses under stop price", message="price crossed under stop price")
- alertcondition(high > l1Long, title="crosses over L1 price", message="price crossed over L1 price")
- alertcondition(high > l2Long, title="crosses over L2 price", message="price crossed over L2 price")
- alertcondition(low < l1LongExit, title="crosses under L1 exit price", message="price crossed under L1 exit price")
- alertcondition(low < l2LongExit, title="crosses under L2 exit price", message="price crossed under L2 exit price")
- strategy.entry("long", strategy.long, comment='long', when=enterLong)
- strategy.close("long", when=exitLong)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement