Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- strategy(title="Grospector DCA V.2", shorttitle="GDCA V.2",precision=7, overlay=true)
- //------------------------------ MA ------------------------------------------------------------------
- len = input.int(title="MA Period", defval=12*4)
- strongLen = input.int(title="Strong Period from MA Period", defval=12)
- sellMulti = input.float(title="Strong Sell Multi", defval=2)
- strongSellMulti = input.float(title="Sell Multi", defval=5)
- labelSwitch = input.bool(title='Turn on assistive text', defval=true)
- zoneSwitch = input.int(title='Turn on zone',tooltip="1 = all, 2 = chart only, 3 = data only,4 = none", defval=2)
- DCA = input.int(title="DCA Plan (USD)",defval=5)
- WaitingDCA = input.float(title="Waiting DCA (USD)",defval=0)
- Invested = input.int(title="Invested (USD)",defval=0)
- strategySwitch = input.bool(title='Turn on strategy', defval=true)
- seperateDayOfMonth = input.int(title='Seperate day of month',tooltip="0 = none, 1-31 day", defval=28,minval=0,maxval=31)
- useContribution = input.bool(title='Turn on use contribution', defval=true)
- StartSellZone = input.int(title="Start Sell Zone %",defval=100,minval=0,maxval=100)
- EndSellZone = input.int(title="End Sell Zone %",defval=0,minval=0,maxval=100)
- StartNormalZone = input.int(title="Start Normal Zone %",defval=0,minval=0,maxval=100)
- EndNormalZone = input.int(title="End Normal Zone %",defval=100,minval=0,maxval=100)
- StartBuyZone = input.int(title="Start Buy Zone %",defval=100,minval=0)
- EndBuyZone = input.int(title="End Buy Zone %",defval=200,minval=0)
- bgcolor(dayofmonth == seperateDayOfMonth ? color.new(color.red, 70) : na,display=seperateDayOfMonth > 0 and seperateDayOfMonth < 32?display.all:display.none)
- //Sell
- lenSell = len
- srcSell = close
- offsetSell = 0
- multiSell = sellMulti
- maSell = ta.sma(srcSell*multiSell, lenSell)
- displayZone = zoneSwitch == 1?display.all:zoneSwitch == 2?display.pane:zoneSwitch == 3?display.data_window:zoneSwitch == 4?display.none:display.none
- plot(maSell*multiSell, color=#f23645, title="Sell", offset=offsetSell, linewidth=5,display=displayZone)
- //Strong Sell
- lenStrongSell = strongLen
- offsetStrongSell = 0
- multiStrongSell = strongSellMulti
- maStrongSell = ta.sma(maSell*multiStrongSell, lenStrongSell)
- plot(maStrongSell, color=#b22833, title="Strong Sell", offset=offsetStrongSell, linewidth=5,display=displayZone)
- //Buy
- lenBuy = len
- srcBuy = close
- offsetBuy = 0
- multiBuy = 1
- maBuy = ta.sma(srcBuy*multiBuy, lenBuy)
- plot(maBuy, color=color.green, title="Buy", offset=offsetBuy, linewidth=5,display=displayZone)
- //Strong Buy
- lenStrongBuy = strongLen
- offsetStrongBuy = 0
- multiStrongBuy = 1
- maStrongBuy = ta.sma(maBuy*multiStrongBuy, lenStrongBuy)
- plot(maStrongBuy, color=#1b5e20, title="Strong Buy", offset=offsetStrongBuy,linewidth=5,display=displayZone)
- //channel
- higherSell = maSell*multiSell > maStrongSell ? maSell*multiSell : maStrongSell
- lowerSell = maSell*multiSell < maStrongSell ? maSell*multiSell : maStrongSell
- higherBuy = maBuy > maStrongBuy ? maBuy : maStrongBuy
- gapSell = (higherSell - lowerSell)/10
- gapNormal = (lowerSell - higherBuy)/10
- gapBuy = (higherBuy - maStrongBuy)/10
- channelSell1 = higherSell - (gapSell*1)
- channelSell2 = higherSell - (gapSell*2)
- channelSell3 = higherSell - (gapSell*3)
- channelSell4 = higherSell - (gapSell*4)
- channelSell5 = higherSell - (gapSell*5)
- channelSell6 = higherSell - (gapSell*6)
- channelSell7 = higherSell - (gapSell*7)
- channelSell8 = higherSell - (gapSell*8)
- channelSell9 = higherSell - (gapSell*9)
- channelSell10 = higherSell - (gapSell*10)
- plot(channelSell1, color=#b22833, title="Strong Sell Channel 1", linewidth=1,display=displayZone)
- plot(channelSell2, color=#b22833, title="Strong Sell Channel 2", linewidth=1,display=displayZone)
- plot(channelSell3, color=#b22833, title="Strong Sell Channel 3", linewidth=1,display=displayZone)
- plot(channelSell4, color=#b22833, title="Strong Sell Channel 4", linewidth=1,display=displayZone)
- plot(channelSell5, color=#b22833, title="Strong Sell Channel 5", linewidth=1,display=displayZone)
- plot(channelSell6, color=#b22833, title="Strong Sell Channel 6", linewidth=1,display=displayZone)
- plot(channelSell7, color=#b22833, title="Strong Sell Channel 7", linewidth=1,display=displayZone)
- plot(channelSell8, color=#b22833, title="Strong Sell Channel 8", linewidth=1,display=displayZone)
- plot(channelSell9, color=#b22833, title="Strong Sell Channel 9", linewidth=1,display=displayZone)
- plot(channelSell10, color=#b22833, title="Strong Sell Channel 10", linewidth=1,display=displayZone)
- channelNormal1 = lowerSell - (gapNormal*1)
- channelNormal2 = lowerSell - (gapNormal*2)
- channelNormal3 = lowerSell - (gapNormal*3)
- channelNormal4 = lowerSell - (gapNormal*4)
- channelNormal5 = lowerSell - (gapNormal*5)
- channelNormal6 = lowerSell - (gapNormal*6)
- channelNormal7 = lowerSell - (gapNormal*7)
- channelNormal8 = lowerSell - (gapNormal*8)
- channelNormal9 = lowerSell - (gapNormal*9)
- channelNormal10 = lowerSell - (gapNormal*10)
- plot(channelNormal1, color=#f57f17, title="Normal Channel 1", linewidth=1,display=displayZone)
- plot(channelNormal2, color=#f57f17, title="Normal Channel 2", linewidth=1,display=displayZone)
- plot(channelNormal3, color=#f57f17, title="Normal Channel 3", linewidth=1,display=displayZone)
- plot(channelNormal4, color=#f57f17, title="Normal Channel 4", linewidth=1,display=displayZone)
- plot(channelNormal5, color=#f57f17, title="Normal Channel 5", linewidth=1,display=displayZone)
- plot(channelNormal6, color=#f57f17, title="Normal Channel 6", linewidth=1,display=displayZone)
- plot(channelNormal7, color=#f57f17, title="Normal Channel 7", linewidth=1,display=displayZone)
- plot(channelNormal8, color=#f57f17, title="Normal Channel 8", linewidth=1,display=displayZone)
- plot(channelNormal9, color=#f57f17, title="Normal Channel 9", linewidth=1,display=displayZone)
- plot(channelNormal10, color=#f57f17, title="Normal Channel 10", linewidth=1,display=displayZone)
- channelBuy1 = higherBuy - (gapBuy*1)
- channelBuy2 = higherBuy - (gapBuy*2)
- channelBuy3 = higherBuy - (gapBuy*3)
- channelBuy4 = higherBuy - (gapBuy*4)
- channelBuy5 = higherBuy - (gapBuy*5)
- channelBuy6 = higherBuy - (gapBuy*6)
- channelBuy7 = higherBuy - (gapBuy*7)
- channelBuy8 = higherBuy - (gapBuy*8)
- channelBuy9 = higherBuy - (gapBuy*9)
- channelBuy10 = higherBuy - (gapBuy*10)
- plot(channelBuy1, color=#1b5e20, title="Strong Buy Channel 1", linewidth=1,display=displayZone)
- plot(channelBuy2, color=#1b5e20, title="Strong Buy Channel 2", linewidth=1,display=displayZone)
- plot(channelBuy3, color=#1b5e20, title="Strong Buy Channel 3", linewidth=1,display=displayZone)
- plot(channelBuy4, color=#1b5e20, title="Strong Buy Channel 4", linewidth=1,display=displayZone)
- plot(channelBuy5, color=#1b5e20, title="Strong Buy Channel 5", linewidth=1,display=displayZone)
- plot(channelBuy6, color=#1b5e20, title="Strong Buy Channel 6", linewidth=1,display=displayZone)
- plot(channelBuy7, color=#1b5e20, title="Strong Buy Channel 7", linewidth=1,display=displayZone)
- plot(channelBuy8, color=#1b5e20, title="Strong Buy Channel 8", linewidth=1,display=displayZone)
- plot(channelBuy9, color=#1b5e20, title="Strong Buy Channel 9", linewidth=1,display=displayZone)
- plot(channelBuy10, color=#1b5e20, title="Strong Buy Channel 10", linewidth=1,display=displayZone)
- //---------------------------------------------------------------------------------------------------
- //------------------------------------------ Calculate ----------------------------------------------
- sell = close[1] > lowerSell
- halfBuy = close[1] < lowerSell and close[1] > higherBuy
- buy = close[1] < higherBuy
- percent = 0
- percentPeriodOfZoneSell = math.abs(StartSellZone - EndSellZone)/10
- percentPeriodOfZoneNormal = math.abs(StartNormalZone - EndNormalZone)/10
- percentPeriodOfZoneBuy = math.abs(StartBuyZone - EndBuyZone)/10
- if(sell)
- percent := close[1] > channelSell1 ? StartSellZone-(percentPeriodOfZoneSell*1) :
- close[1] > channelSell2 ? StartSellZone-(percentPeriodOfZoneSell*2) :
- close[1] > channelSell3 ? StartSellZone-(percentPeriodOfZoneSell*3) :
- close[1] > channelSell4 ? StartSellZone-(percentPeriodOfZoneSell*4) :
- close[1] > channelSell5 ? StartSellZone-(percentPeriodOfZoneSell*5) :
- close[1] > channelSell6 ? StartSellZone-(percentPeriodOfZoneSell*6) :
- close[1] > channelSell7 ? StartSellZone-(percentPeriodOfZoneSell*7) :
- close[1] > channelSell8 ? StartSellZone-(percentPeriodOfZoneSell*8) :
- close[1] > channelSell9 ? StartSellZone-(percentPeriodOfZoneSell*9) :
- close[1] > channelSell10 ? StartSellZone-(percentPeriodOfZoneSell*10) : StartSellZone-(percentPeriodOfZoneSell*10)
- else if(halfBuy)
- percent := close[1] > channelNormal1 ? StartNormalZone+(percentPeriodOfZoneNormal*1) :
- close[1] > channelNormal2 ? StartNormalZone+(percentPeriodOfZoneNormal*2) :
- close[1] > channelNormal3 ? StartNormalZone+(percentPeriodOfZoneNormal*3) :
- close[1] > channelNormal4 ? StartNormalZone+(percentPeriodOfZoneNormal*4) :
- close[1] > channelNormal5 ? StartNormalZone+(percentPeriodOfZoneNormal*5) :
- close[1] > channelNormal6 ? StartNormalZone+(percentPeriodOfZoneNormal*6) :
- close[1] > channelNormal7 ? StartNormalZone+(percentPeriodOfZoneNormal*7) :
- close[1] > channelNormal8 ? StartNormalZone+(percentPeriodOfZoneNormal*8) :
- close[1] > channelNormal9 ? StartNormalZone+(percentPeriodOfZoneNormal*9) :
- close[1] > channelNormal10 ? StartNormalZone+(percentPeriodOfZoneNormal*10) : StartNormalZone+(percentPeriodOfZoneNormal*10)
- else if(buy)
- percent := close[1] > channelBuy1 ? StartBuyZone+(percentPeriodOfZoneBuy*1) :
- close[1] > channelBuy2 ? StartBuyZone+(percentPeriodOfZoneBuy*2) :
- close[1] > channelBuy3 ? StartBuyZone+(percentPeriodOfZoneBuy*3) :
- close[1] > channelBuy4 ? StartBuyZone+(percentPeriodOfZoneBuy*4) :
- close[1] > channelBuy5 ? StartBuyZone+(percentPeriodOfZoneBuy*5) :
- close[1] > channelBuy6 ? StartBuyZone+(percentPeriodOfZoneBuy*6) :
- close[1] > channelBuy7 ? StartBuyZone+(percentPeriodOfZoneBuy*7) :
- close[1] > channelBuy8 ? StartBuyZone+(percentPeriodOfZoneBuy*8) :
- close[1] > channelBuy9 ? StartBuyZone+(percentPeriodOfZoneBuy*9) :
- close[1] > channelBuy10 ? StartBuyZone+(percentPeriodOfZoneBuy*10) : StartBuyZone+(percentPeriodOfZoneBuy*10)
- //---------------------------------------------------------------------------------------------------
- //-------------------------------------------- Display ----------------------------------------------
- labeltcolor = color.white
- textLabel = buy ? 'Buy' : halfBuy ? "Buy" : "Sell"
- labelbgcolor = buy ? color.green : halfBuy ? color.orange : color.red
- contibutingAmount = 0.0
- contibutingAmount := barstate.isfirst ? WaitingDCA : contibutingAmount[1]
- amountBuy = 0.0
- amountSell = 0.0
- if(percent > 100 and useContribution)
- amountBuy := (contibutingAmount*(percent - 100)/100) + (DCA*percent/100)
- else
- amountBuy := (DCA*percent/100)
- if(percent < 0 and useContribution)
- amountSell := ((contibutingAmount*(percent)/100) + (Invested*(percent)/100))/close[1]
- else
- amountSell := (Invested*(percent)/100)/close[1]
- currency = buy ? syminfo.currency : halfBuy ? syminfo.currency : syminfo.basecurrency
- textAmount = buy ? str.format("+{0}",amountBuy) : halfBuy ? str.format("+{0}",amountBuy) : str.format("-{0}",amountSell)
- waitingAmount = amountBuy <= DCA ? (DCA - amountBuy) : (WaitingDCA - amountBuy)
- textWaitingAmount = buy ? str.format("+{0}",waitingAmount) : halfBuy ? str.format("+{0}",waitingAmount) : str.format("-{0}",waitingAmount)
- l1 = label.new(bar_index, na,
- text=str.format("{0} {1}%\n amount {2} ({3})\n waiting amount {4} ({5})", textLabel, percent,textAmount,currency,textWaitingAmount,syminfo.currency),
- color=labelbgcolor,
- textcolor=labeltcolor,
- yloc=yloc.abovebar,
- style=label.style_label_down)
- label.delete(labelSwitch ? l1[1] : l1)
- //---------------------------------------------------------------------------------------------------
- //------------------------------------------ Strategy -----------------------------------------------
- qty = 0.0
- bar = barstate.isnew
- last_bar = barstate.islastconfirmedhistory
- //Calculate QTY and Waiting DCA
- if(buy)
- qty := amountBuy/close[1]
- contibutingAmount := percent > 100 and useContribution ? contibutingAmount * (percent-100)/100 : contibutingAmount + (DCA - amountBuy)
- else if(halfBuy)
- qty := amountBuy/close[1]
- contibutingAmount := percent > 100 and useContribution ? contibutingAmount * (percent-100)/100 : contibutingAmount + (DCA - amountBuy)
- else if(sell)
- qty := strategy.position_size * percent/100
- plot(contibutingAmount,title="contibutingAmount",display=display.data_window)
- plot(qty,title="qty",display=display.data_window)
- plot(percent,title="percent",display=display.data_window)
- plot(amountBuy/close[1],title="amountBuy",display=display.data_window)
- //Order
- if(strategySwitch)
- if(bar and buy)
- strategy.order(str.format("DCA Buy {0}% = {1} {2}",percent,qty,syminfo.basecurrency),strategy.long,qty=qty)
- else if(bar and halfBuy)
- strategy.order(str.format("DCA Normal {0}% = {1} {2}",percent,qty,syminfo.basecurrency),strategy.long,qty=qty)
- else if(bar and sell)
- strategy.order(str.format("DCA Sell {0}% = {1} {2}",percent,qty,syminfo.basecurrency),strategy.short,qty=qty)
- //END
- //if(last_bar)
- //strategy.close_all()
- //------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment