Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=4
- //====================================================================
- // ADX DI AutoTrader
- // Version 1.4
- // Created by GreenDragon
- //====================================================================
- // 2019-08-29 - Beta V0.1 created
- // 2019-08-30 - remove green trigger when di crossing up on a pos adx
- // 2019-09-02 - modify exit to not fire when ADX < 25 but DI still pos
- // 2019-09-03 - added TP, TTP and TSL
- // 2019-09-09 - add slope filter
- // 2019-09-19 - add RSI filter - only allow buy when RSI last 2 are rising.
- // 2019-09-23 - remove slope filter and code as no use in filtering
- // 2019-10-02 - Fix bug in Trailing Stop Loss
- // 2019-10-15 - change both sell to be both down (removed)
- // 2019-10-30 - more work on TP, TTP & TSL
- // 2019-11-06 - V1.0 Official Release
- // 2019-11-10 - more work on TP, TTP, TSL
- // 2019-11-26 - total re-write of but sell control logic.
- // 2019-11-27 - limit vars with max_bars_back to stop compiler error
- // 2019-11-28 - more max_bars_back issues fixed in highest_ func
- // 2019-11-28 - fix volume filter, make volumecode global
- // 2019-12-06 - Add Dirty 30 marker and trade option.
- // 2019-12-16 - V1.4 - add fixed SL or Trailing
- //=====================================================================
- //
- // Description
- // -----------
- // This is a main window indicator that can be used to trade based on ADX DI or Both
- // it also has various filters that can be set to stop buy signals if the filter
- // conditions are not met. When filters are enabled the "Buy" will be DELAYED until
- // All set filters are true e.g. if adx fires below 200ma trade will not buy until over
- // 200ma provided that the adx di condition is still true.
- //
- // Filters
- // -------
- // 1. All filters and other variables can be set in the settings and then saved as
- // default or can be changed in the code.
- // 2. Background colours can be enabled so Green when trade is open and red when
- // trade closed.
- // 3. NQ's common simple MA lines can be enabled or disabled in settings, 8ma,
- // 21ma, 50ma, 200ma & 314ma
- // 4. Trading price labels can also be enabled in settings.
- // 5. Trade below 200ma is generally not a good idea and against the NQ methedology
- // but can be enabled in settings if needed.
- // 6. sell by ADX, DI or Both can be disabled to allow the TP, TTP, SL or TSL to continue
- // to run the trade
- // 7. Trade sell can be by TP or Trailing TP, choose in settings
- // 8. Stop Loss or Trailing Stop Loss can also be enabled.
- // 9. ADX Filter allows for the setting of DI Length (default 14) and ADX Threshold
- // (Default 25)
- // 10. RSI Filter can be enabled and you can chose 2 or 3 candle back, will stop buy
- // if RSI is declining over the set number of candles.
- // 11. Volume filter can be enabled and will stop buy if "Daily or Hourly" rolling
- // volume of the "Base Coin" is below the set threshold, e.g BATBTC trade BTC is
- // the Base Coin, ETHUSDT USDT is the Base Coin.
- // 12. Value Filter, this can be set to stop buy under a sertain coin price, this is
- // the coin being purchased NOT the base coin. Default coin threshold for trading
- // is 100 satoshis.
- //
- // Markers
- // -------
- // 1. Buy Marker is Green Up Triangle at bottom of chart
- // 2. Sell Marker is Red Down Triangle at bottom of chart
- // 3. TP Sell indicator is Blue Square at bottom of Chart and TP Value line is blue
- // line on chart from buy point to sell or TP point.
- // 4. TTP Sell indicator is Green Square at bottom of Chart and TTP Value line is Dark Green
- // line on chart from TP point to sell or TTP point.
- // 5. TSL Sell indicator is Orange Square at bottom of Chart and TSL Value line is Red
- // line on chart from buy point to sell or TSL point.
- // 6. Dirty 30 is purple triangle below candle.
- //
- // For backtesting you can uncomment the strategy entries and comment out study() and
- // alertcondition() entries.
- //=====================================================================
- //strategy(title="GD ADXDI AutoTrader VB 1.4", shorttitle="GD-ADXDI-Auto-V1.4", overlay = true, pyramiding = 0, calc_on_order_fills = true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, initial_capital = 10000, commission_type=strategy.commission.percent, commission_value=0.2)
- study("GD ADXDI AutoTrader V1.4", shorttitle="GD-ADXDI-Auto-V1.4", overlay=true, max_bars_back=4998)
- // create options for inputs
- TradeADX = "ADX", TradeDI = "DI Line", TradeBoth = "ADX + DI+", TradeDirty30 = "Dirty 30", TradeNone = "Disabled"
- tpon = "TP", ttpon = "TTP"
- RSIOpen = "Open", RSIClose="Close", RSIHigh="High", RSILow="Low", RSIhl2="hl2", RSIhlc3="hlc3", RSIohlc4="ohlc4"
- two = "two candles", three = "three candles"
- vH = "Hours", vD = "Days"
- // set main inputs
- b1 = input(false, title="╔══════════════════════════════════════════╗")
- c1 = input(false, title=" ADX DI Autotrader")
- c2 = input(false, title=" Version 1.4")
- c3 = input(false, title=" Created by GreenDragon")
- c4 = input(false, title=" --------------------------------------")
- c9 = input(false, title="╠══════════════════════════════════════════╣")
- enableBackground = input(true, title=" Enable Background colours")
- enableMAs = input(true, title=" Enable MA's lines")
- startOn = input(TradeBoth, title=" Trade based on ", options=[TradeADX, TradeDI, TradeBoth, TradeDirty30, TradeNone])
- b2 = input(false, title="╠══════════════════════════════════════════╣")
- b3 = input(false, title=" ******************************** Trade Settings ******************************** ")
- showTradeLbl = input(true, title=" Show Trade Price Labels")
- ebl200ma = input(false, title=" Enable Trade below 200ma")
- disableS = input( true, title=" Allow SELL by TP, TTP or SL ONLY. Not adx or di")
- enableTP = input( true, title=" Enable Take Profit or Trailing Take Profit")
- enableTPOn = input(ttpon, title=" Take Profit or Trailing Take Profit", options=[tpon,ttpon])
- tpLevel = input(2.2, title=" Take Profit Percentage",type=input.float, minval=0.001, maxval=100.0, step=0.001)
- ttpLevel = input(6, title=" Trailing Take Profit by Percentage",type=input.float, minval=0.001, maxval=100.0, step=0.001)
- enableTSL = input(false, title=" Enable Trailing Stop Loss, False = Fixed SL")
- tslLevel = input(-0.2, title=" Trailing Stop Loss Percentage",type=input.float, maxval=100.0, step=0.001)
- b4 = input(false, title="╠══════════════════════════════════════════╣")
- b5 = input(false, title=" ****************************** D30 Filter Settings ***************************** ")
- barcount = input(3, title=" Bar count since ma cross or adx")
- b6 = input(false, title="╠══════════════════════════════════════════╣")
- b7 = input(false, title=" ******************************** ADX Filter ************************************ ")
- len = input(14, title=" DI Length", type=input.integer)
- th = input(25, title=" Trend Threshold", type=input.integer)
- b8 = input(false, title="╠══════════════════════════════════════════╣")
- b9 = input(false, title=" ********************************* RSI Filter ********************************** ")
- enableRSI = input(false, title=" Enable RSI Filter")
- RSILength = input(14, title=" RSI Length", type=input.integer)
- RSIBasedOn = input(RSIohlc4, title=" RSI Source", options=[RSIOpen, RSIClose, RSIHigh, RSILow, RSIhl2, RSIhlc3, RSIohlc4])
- RSITwoThree = input(two, title=" RSI candles", options=[two, three])
- b10 = input(false, title="╠══════════════════════════════════════════╣")
- b11 = input(false, title=" ********************************* Volume Filter ******************************** ")
- eblVolume = input(true, title=" Enable Volume Filter")
- volThreshold = input( 30, title=" Volume Threshold in Base Coin ",type=input.integer, minval=1, step=1)
- measureOn = input( vD, title=" Measure Volume Range in ", options=[vH, vD])
- volTime = input( 1, title=" Volume Range in Days/Hours" ,type=input.integer, minval=1, maxval=100, step=1)
- b12 = input(false, title="╠══════════════════════════════════════════╣")
- b13 = input(false, title=" ********************************* Value Filter ******************************** ")
- eblValFilter = input( true, title=" Enable min Coin value to trade Filter (def 100 Satoshis)")
- valThreshold = input(0.00000100,title=" Min Value of Coin", type=input.float, minval=0.00000001, step=0.00000001)
- b16 = input(false, title="╚══════════════════════════════════════════╝")
- // choose src for indicator from
- // open, close, high, low, hl2, hlc3, ohlc4
- src = close
- // Trade Flags
- buyFlag = false
- sellFlag = false
- BuyCondition = false
- SellCondition = false
- tpAlert = false
- ttpAlert = false
- slAlert = false
- tslAlert = false
- // var to hold last valid buy or sell condition.
- var LastCondWasBuy = false
- ////////////////////////
- // NQ's Rules and Indies
- // Use Simple MA for NQ's 8, 200 & 314
- ma8 = sma(src, 8)
- ma21 = sma(src, 21)
- ema30 = ema(src, 30)
- ma50 = sma(src, 50)
- ma200 = sma(src,200)
- ma314 = sma(src,314)
- plot( ma8, color=color.red, style=plot.style_line, title="Ma 8", linewidth=1, transp= enableMAs ? 0 : 100)
- plot( ma21, color=color.purple, style=plot.style_line, title="Ma 21", linewidth=1, transp= enableMAs ? 0 : 100)
- plot( ema30, color=#880F4F, style=plot.style_line, title="ema 30", linewidth=2, transp= enableMAs ? 0 : 100)
- plot( ma50, color=color.orange, style=plot.style_line, title="Ma 50", linewidth=1, transp= enableMAs ? 0 : 100)
- plot( ma200, color=color.blue, style=plot.style_line, title="Ma 200",linewidth=2, transp= enableMAs ? 0 : 100)
- plot( ma314, color=color.green, style=plot.style_line, title="Ma 314",linewidth=2, transp= enableMAs ? 0 : 100)
- ////////////////////
- // Custom Functions
- ////////////////////
- // sumSeries() function
- // vol*close to get sum of Base Volume
- sumSeries(insrc, length) =>
- l = min(length, 4990)
- max_bars_back(insrc, 4996)
- total = 0.0
- if (l > 1)
- for i = 1 to l-1
- total := total + (insrc[i]*src[i])
- else
- total = (insrc[1]*src[1])
- total
- highest_(values, length) =>
- l = min(length, 4990)
- max_bars_back(values, 4997)
- max_bars_back(l, 1)
- h_val = values[0]
- if l >= 1
- for i = 0 to l-1
- if ( not na(values[i]) and values[i] > h_val )
- h_val := values[i]
- h_val
- // when and price
- whenandprice(event) =>
- when = nz(barssince(event))
- price = valuewhen(event,src,0)
- // left shift result by 1 candle
- [when + 1, price]
- // percentage() function
- percentage(source,per) =>
- percent = (source * (per/100))
- percent
- // ADX and DI function
- getADX() =>
- // hard code close not src for this function
- TRange = max(max(high-low, abs(high-nz(close[1]))), abs(low-nz(close[1])))
- DirMovePlus = high-nz(high[1]) > nz(low[1])-low ? max(high-nz(high[1]), 0): 0
- DirMoveMinus = nz(low[1])-low > high-nz(high[1]) ? max(nz(low[1])-low, 0): 0
- SmoothedTRange = 0.0
- SmoothedDirMovePlus = 0.0
- SmoothedDirMoveMinus = 0.0
- SmoothedTRange := nz(SmoothedTRange[1]) - (nz(SmoothedTRange[1])/len) + TRange
- SmoothedDirMovePlus := nz(SmoothedDirMovePlus[1]) - (nz(SmoothedDirMovePlus[1])/len) + DirMovePlus
- SmoothedDirMoveMinus := nz(SmoothedDirMoveMinus[1]) - (nz(SmoothedDirMoveMinus[1])/len) + DirMoveMinus
- DIPlus = SmoothedDirMovePlus / SmoothedTRange * 100
- DIMinus = SmoothedDirMoveMinus / SmoothedTRange * 100
- _di = th + (DIPlus-DIMinus)
- _DX = abs(DIPlus-DIMinus) / (DIPlus+DIMinus)*100
- _ADX = sum(_DX,14)/14
- _ADX := ((nz(_ADX[1])*13)+_DX)/14
- _ADX
- [_di, _ADX]
- // get ADX and di values
- [di, ADX] = getADX()
- cr = color.gray
- cg = color.gray
- cr := ADX <= 22 ? color.new(color.green, 80) :
- ADX <= 34 ? color.new(color.green, 60) :
- ADX <= 46 ? color.new(color.green, 35) :
- ADX <= 58 ? color.new(color.green, 5) :
- ADX <= 70 ? color.new(color.green, 0) :
- color.new(color.green, 0)
- cg := ADX <= 22 ? color.new(color.red, 80) :
- ADX <= 34 ? color.new(color.red, 60) :
- ADX <= 46 ? color.new(color.red, 35) :
- ADX <= 58 ? color.new(color.red, 5) :
- ADX <= 70 ? color.new(color.red, 0) :
- color.new(color.red, 0)
- ///////////////////////
- // inline volume code - moved out from function to stop
- // max_bars_back problem.
- // check on number of milliseconds needed
- milliseconds = 0
- if (measureOn == vD)
- milliseconds := 1000 * 60 * 60 * 24 * volTime
- if (measureOn == vH)
- milliseconds := 1000 * 60 * 60 * volTime
- NoOfCandles = 0
- volumeSum = 0.0
- if (timeframe.isseconds) // new second resolution
- NoOfCandles := ((milliseconds/1000) / timeframe.multiplier)
- volumeSum := sumSeries(volume, NoOfCandles)
- if (timeframe.isintraday)
- NoOfCandles := ((milliseconds/1000/60) / timeframe.multiplier)
- volumeSum := sumSeries(volume, NoOfCandles)
- if (timeframe.isdaily)
- if (measureOn == vD)
- volumeSum := volTime * (nz(volume[1])*nz(close[1]))
- if (measureOn == vH)
- volumeSum := 24 * volTime * (nz(volume[1])*nz(close[1]))
- if (timeframe.isweekly)
- if (measureOn == vD)
- volumeSum := 7 * volTime * (nz(volume[1])*nz(close[1]))
- if (measureOn == vH)
- volumeSum := 7 * 24 * volTime * (nz(volume[1])*nz(close[1]))
- if (timeframe.ismonthly)
- if (measureOn == vD)
- volumeSum := 30 * 7 * volTime * (nz(volume[1])*nz(close[1]))
- if (measureOn == vH)
- volumeSum := 30 * 7 * 24 * volTime * (nz(volume[1])*nz(close[1]))
- //////////
- // Filters
- //////////
- // RSI Filter
- // indi uses RSISource not src
- rsiOK() =>
- rsisrc = close
- ok = false
- if (RSIBasedOn == RSIOpen)
- rsisrc := open
- if (RSIBasedOn == RSIClose)
- rsisrc := close
- if (RSIBasedOn == RSIHigh)
- rsisrc := high
- if (RSIBasedOn == RSILow)
- rsisrc := low
- if (RSIBasedOn == RSIhl2)
- rsisrc := hl2
- if (RSIBasedOn == RSIhlc3)
- rsisrc := hlc3
- if (RSIBasedOn == RSIohlc4)
- rsisrc := ohlc4
- rsiValue = rsi(rsisrc, RSILength)
- if (RSITwoThree == two and (rsiValue > nz(rsiValue[1])))
- ok := true
- if (RSITwoThree == three and (rsiValue > nz(rsiValue[1])) and (nz(rsiValue[1]) > nz(rsiValue[2])))
- ok := true
- ok
- // 200ma Filter
- ma200OK() =>
- max_bars_back(src, 4899)
- max_bars_back(ma200, 4898)
- result = true
- closeBelow200 = (src < ma200)
- if ((ebl200ma == false) and closeBelow200)
- result := false
- result
- // Volume Filter
- volumeOK() =>
- ok = false
- if (volumeSum >= volThreshold)
- ok := true
- ok
- // Coin Value Filter
- valueOK() =>
- ok = false
- if (src >= valThreshold)
- ok := true
- ////////////////
- // Filter Checks
- ////////////////
- // main function to check all filters
- filtersOK() =>
- ok = true
- // // test RSIfilter
- if (enableRSI and not rsiOK())
- ok := false
- // test ms200 filter
- if (not ma200OK())
- ok := false
- // test Volume filter
- if (eblVolume and not volumeOK())
- ok := false
- // test Value filter
- if (eblValFilter and not valueOK())
- ok := false
- ok
- filterFired() =>
- ok = false
- if (filtersOK() and not filtersOK()[1])
- ok := true
- ok
- ///////////////////////
- // End of Filter Checks
- ///////////////////////
- /////////////////////
- // Main Trading Block
- /////////////////////
- // Common
- adxup = crossover(ADX, th)
- diup = crossover(di, th)
- adxdiup = adxup and di >= th
- diadxup = diup and ADX >= th and (ADX > ADX[1])
- bothup = ((ADX >= th) and (di >= th))
- // Preset d30 triggers
- D30True = false
- // Dirty 30 logic
- d30cross = crossover(ema30, ma200)
- firenow = (d30cross and bothup) // d30 cross after adx and di both up
- firenow2 = (adxdiup and barssince(d30cross) < barcount and barssince(diadxup) < barcount) // adx cross when d30 and di both up
- firenow3 = (diadxup and barssince(d30cross) < barcount and barssince(adxdiup) < barcount) // di cross when d30 and adx both up
- D30True := (firenow or firenow2 or firenow3)
- // for TP, TTP & TSL
- tadxFlag = ((adxdiup and filtersOK()) or (ADX > th and di >= th and filterFired())) and not LastCondWasBuy
- tadxSell = crossunder(ADX, th) and LastCondWasBuy
- diFlag = ((crossover(di, th) and filtersOK()) or (di > th and filterFired())) and not LastCondWasBuy
- disell = crossunder(di, th) and LastCondWasBuy
- bothFlag = (((adxdiup or diadxup) and filtersOK()) or (bothup and filterFired())) and not LastCondWasBuy
- bothsell = (crossunder(di, th)) and LastCondWasBuy
- ////////////////////
- // Trade on ADX ONLY
- if (startOn == TradeADX)
- buyFlag := tadxFlag
- if (disableS == false)
- sellFlag := tadxSell
- else
- // Trade on DI ONLY
- if (startOn == TradeDI)
- buyFlag := diFlag
- if (disableS == false)
- sellFlag := disell
- else
- // Trade Both
- if (startOn == TradeBoth)
- buyFlag := bothFlag
- if (disableS == false)
- sellFlag := bothsell
- else
- // Trade Dirty 30 - buy only allow TP logic to sell
- if (startOn == TradeDirty30)
- buyFlag := D30True
- // buy logic complete so get when and price
- [lastBuyCount, lastBuyPrice] = whenandprice(buyFlag)
- ////////////////////////
- // TP, TTP and TSL Block
- ////////////////////////
- // 1. Take Profit
- tpValue = (lastBuyPrice + percentage(lastBuyPrice, tpLevel))
- tpTrigger = (src > tpValue) and (nz(src[1]) <= tpValue)
- tpTriggeredCount = barssince(tpTrigger)
- allowTP = enableTP
- and (tpTrigger)
- and (nz(src[1]) <= tpValue)
- and (tpTriggeredCount < lastBuyCount)
- and LastCondWasBuy
- if (enableTPOn == tpon and allowTP)
- tpAlert := true
- // 2. Trailing Take Profit
- ttp = (src - percentage(src, ttpLevel))
- ttpValue = highest_(ttp, lastBuyCount[0])
- tpRunningCount = barssince(allowTP)
- ttpTrigger = (barssince(src < ttpValue) < tpRunningCount)
- allowTTP = enableTP
- and (tpRunningCount < lastBuyCount)
- and ttpTrigger
- and LastCondWasBuy
- if (enableTPOn == ttpon and allowTTP)
- ttpAlert := true
- // 3. Fixed Stop Loss
- slValue = (lastBuyPrice - percentage(lastBuyPrice, tslLevel))
- slTrigger = (src > slValue) and (nz(src[1]) <= slValue)
- slTriggeredCount = barssince(slTrigger)
- allowSL = (enableTSL == false)
- and (slTrigger)
- and (nz(src[1]) <= slValue)
- and (slTriggeredCount < lastBuyCount)
- and LastCondWasBuy
- if (enableTSL == false and allowSL)
- slAlert := true
- // 4. Trailing Stop Loss
- tslStart = src - percentage(src, tslLevel)
- tslValue = highest_(tslStart, lastBuyCount[0])
- tslTriggeredCount = barssince(src < tslValue)
- allowTSL = enableTSL
- and (src < tslValue)
- and (nz(src[1]) >= nz(tslValue[1]))
- and (tslTriggeredCount < lastBuyCount)
- and LastCondWasBuy
- if (allowTSL)
- tslAlert := true
- //update sellFlag for TP, TTP, SL or TSP
- sellFlag := (
- sellFlag or
- tpAlert or
- ttpAlert or
- slAlert or
- tslAlert )
- ///////////////////////////
- // End TP TTP and TSL Logic
- ///////////////////////////
- /////////////////////////////////////////////////////////////////////////////////////
- // PineCoders, this is where the issue happens.
- // If you comment out this if block the max_bars_back error goes away...
- // but then the code does not work.. Leaving this if block in will cause max_bars_back
- // error on short timeframes, FETBTC on Binance @45min causes the issue... 2H does not
- //////////////////////////////////////////////////////////////////////////////////////
- // New buy/sell allow logic
- if buyFlag and not LastCondWasBuy
- BuyCondition := true
- LastCondWasBuy := true
- ///////////////////////////////////
- // PineCoders, End of problem area.
- ///////////////////////////////////
- if sellFlag and LastCondWasBuy
- SellCondition := true
- LastCondWasBuy := false
- // New calculate last buy/sell as sell prices and position
- [lastBuy2Count, lastBuy2Price] = whenandprice(BuyCondition)
- [lastSell2Count, lastSell2Price] = whenandprice(SellCondition)
- // recalc tp values if changed ??? not sure
- tpValue := (lastBuy2Price + percentage(lastBuy2Price, tpLevel))
- slValue := (lastBuy2Price - percentage(lastBuy2Price, tslLevel))
- // GL - Next lines cause issue
- ttpValue := highest_(ttp, lastBuy2Count[0])
- tslValue := highest_(tslStart, lastBuy2Count[0])
- ///////////////////////////
- // Set Red Green Background
- // colour based on buy/sell
- setColour = color.red
- buyTrigger = (barssince(BuyCondition) <= barssince(SellCondition))
- if (buyTrigger)
- setColour := color.green
- bgcolor(setColour, transp = enableBackground ? 85 : 100)
- ///////////////////////////
- // plot TP, TTP & TSL Lines
- ///////////////////////////
- enablePlotTP = enableTP
- and ((enableTPOn == tpon) or (enableTPOn == ttpon))
- and (lastBuy2Count < lastSell2Count)
- and (lastBuy2Count > 1)
- enablePlotTTP = enableTP
- and (enableTPOn == ttpon)
- and (lastBuy2Count < lastSell2Count)
- and ((tpRunningCount) < lastBuy2Count)
- and (lastBuy2Count > 1)
- enablePlotTSL = (lastBuy2Count < lastSell2Count)
- and (lastBuy2Count > 1) //and enableTSL
- // extend the lines by 1 candle to line up with tp markers
- PlotTP = (enablePlotTP) and (enablePlotTTP == false)
- PlotTTP = (enablePlotTTP or enablePlotTTP[1] == true) and barssince(ttpTrigger) > 1
- PlotTSL = (enablePlotTSL or enablePlotTSL[1] == true)
- // now plot the lines
- plot(tpValue, color=PlotTP ? color.blue : na, style=plot.style_line, title="TP Line", linewidth=2, transp=0)
- plot(ttpValue, color=PlotTTP ? #1d6303 : na, style=plot.style_line, title="TTP Line", linewidth=2, transp=0)
- plot(enableTSL ? tslValue : slValue, color=PlotTSL ? color.red : na, style=plot.style_line, title="SL Line", linewidth=2, transp=0)
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////
- // Calc when to TP, TTP & TSL
- // done in 2 steps so all can be calculated
- // BEFORE final flag setting
- // 1. TP
- tpAlertFlag = tpAlert
- and (barssince(tpAlert) < lastBuy2Count)
- and (barssince(tpAlert[1]) > lastBuy2Count)
- // 2. TTP
- ttpAlertFlag = ttpAlert
- and (barssince(ttpAlert) < lastBuy2Count)
- and (barssince(ttpAlert[1]) > lastBuy2Count)
- // 3. TSL
- tslAlertFlag = tslAlert
- and (barssince(tslAlert) < lastBuy2Count)
- and (barssince(tslAlert[1]) > lastBuy2Count)
- // set the plot flags for TP, TTP & TSL
- plotTPFlag = tpAlertFlag
- and not (
- (lastBuy2Count >= barssince(ttpAlertFlag)) or
- (lastBuy2Count >= barssince(tslAlertFlag))
- )
- plotTTPFlag = ttpAlertFlag
- and not (
- (lastBuy2Count >= barssince(tpAlertFlag)) or
- (lastBuy2Count >= barssince(tslAlertFlag))
- )
- plotTSLFlag = tslAlertFlag
- and not (
- (lastBuy2Count >= barssince(tpAlertFlag)) or
- (lastBuy2Count >= barssince(ttpAlertFlag))
- )
- adxTrade = (startOn == TradeADX) and BuyCondition
- diTrade = (startOn == TradeDI) and BuyCondition
- bothTrade = (startOn == TradeBoth) and BuyCondition
- d30Trade = (startOn == TradeDirty30) and BuyCondition
- // Plot the TP, TTP & TSL Markers
- plotshape(plotTPFlag, color=color.blue, style=shape.square, location=location.bottom, title="TP Marker", size=size.tiny, editable=true, transp=10)
- plotshape(plotTTPFlag, color=#1d6303, style=shape.square, location=location.bottom, title="TTP Marker", size=size.tiny, editable=true, transp=10)
- plotshape(plotTSLFlag, color=color.orange, style=shape.square, location=location.bottom, title="TSL Marker", size=size.tiny, editable=true, transp=10)
- // Plot buy sell markers
- plotshape(BuyCondition, color=color.green, style=shape.triangleup, location=location.bottom, title="Buy", size=size.tiny, editable=true, transp=0)
- plotshape(SellCondition, color=color.red, style=shape.triangledown, location=location.bottom, title="Sell", size=size.tiny, editable=true, transp=0)
- // Plot Dirty 30 marker
- plotshape(D30True, color=#880F4F, style=shape.triangleup, location=location.belowbar, title="d30", size=size.tiny, editable=true, transp=0)
- // Show price labels if enabled
- if (showTradeLbl)
- if (plotTPFlag)
- ltp = label.new(bar_index, na, text = "TP " + tostring(lastSell2Price), color=color.blue, textcolor=color.white, style=label.style_labeldown, yloc=yloc.abovebar)
- if (plotTTPFlag)
- lttp = label.new(bar_index, na, text = "TTP " + tostring(lastSell2Price), color=#1d6303, textcolor=color.white, style=label.style_labeldown, yloc=yloc.abovebar)
- if (plotTSLFlag)
- ltsl = label.new(bar_index, na, text = "TSL " + tostring(lastSell2Price), color=color.red, textcolor=color.white, style=label.style_labeldown, yloc=yloc.abovebar)
- if (BuyCondition)
- lb = label.new(bar_index, na, text = "buy " + tostring(lastBuy2Price), color=color.green, textcolor=color.white, style=label.style_labelup, yloc=yloc.belowbar)
- if (adxTrade)
- lb = label.new(bar_index, na, text = "adx-buy", color=color.purple, textcolor=color.white, style=label.style_labeldown, yloc=yloc.abovebar)
- if (diTrade)
- lb = label.new(bar_index, na, text = "di-buy", color=color.purple, textcolor=color.white, style=label.style_labeldown, yloc=yloc.abovebar)
- if (bothTrade)
- lb = label.new(bar_index, na, text = "adxdi-buy", color=color.purple, textcolor=color.white, style=label.style_labeldown, yloc=yloc.abovebar)
- if (d30Trade)
- lb = label.new(bar_index, na, text = "d30-buy", color=color.purple, textcolor=color.white, style=label.style_labeldown, yloc=yloc.abovebar)
- // Debug Lable
- // lbv = label.new(bar_index, na, text = "Vol " + tostring(volumeSum), color=color.blue, textcolor=color.white, style=label.style_labeldown, yloc=yloc.abovebar)
- if (SellCondition and not (plotTPFlag or plotTTPFlag or plotTSLFlag))
- ls = label.new(bar_index, na, text = "sell " + tostring(lastSell2Price), color=color.red, textcolor=color.white, style=label.style_labelup, yloc=yloc.belowbar)
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // setup the alerts for autoview to trade (multiples of 99% as 100% does not work for market buy/sell on binance)
- alertcondition(BuyCondition, title='_BUY', message='AUTO - Buy - {{ticker}}\ne={{exchange}} s={{ticker}} b=buy q=20% t=market')
- alertcondition(SellCondition, title='_SELL', message='AUTO - Sell - {{ticker}}\ne={{exchange}} s={{ticker}} b=sell q=99% t=market | delay=2 b=sell q=99% t=market | delay=2 b=sell q=99% t=market')
- ////////////////////////////////////////////////////////////////////////////////////
- // Enter and close long orders for the Strategy Tester, no shorting in this version
- //strategy.entry(id="Enter Long", long=true, when = (BuyCondition == true ))
- //strategy.close(id="Enter Long", when = (SellCondition == true))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement