// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © tresduno66794 //@version=4 //With code of @robswc strategy(title="Dynamic DCA Grid", shorttitle="DDcaG", initial_capital=1000, overlay=true) CloseSource = close openSrc = open volPercent = input(17, "Percent Volatility Length") smProfitPercent = input(title="% SM Profit", defval=0.24) smLongPercent = input(title="% SM Long", defval=0.47) mdLongPercent = input(title="% MD Long", defval=0.8) smAmount = input(10, "SM Amount")/10 mdAmount = input(10, "MD Amount")/10 lgAmount = input(10, "LG Amount")/10 smAmountReal = (smAmount*100)/close/10 mdAmountReal = (mdAmount*100)/close/10 lgAmountReal = (lgAmount*100)/close/10 startDate = input(timestamp("2021-01-01T00:00:00"), type = input.time) endDate = input(timestamp("2021-01-31T00:00:00"), type = input.time) inTimeRange = time >= startDate and time <= endDate // DEFAULT SLEEPING HOURS 12PM - 9AM EST sleepingHoursValue = input(title="Sleeping Hours (0430-1400) UTC", defval="0400-1100") sleepingHours = time(timeframe.period, sleepingHoursValue) bgcolor(not na(sleepingHours) ? #FFA50007 : na) // STUDY LENGTH defval=10days = 500 candles studyLength = input(title="Study Length (30mx500=10days)", defval=500) floorCeilingRation = input(3.5, "Ratio") // GET CEILING STUDYING THE LAST "studyLength (defval=10days)" CANDLES get_high_average() => firstValue = openSrc[1] highestValue = 0.0 for n = 2 to studyLength if openSrc[n] > highestValue highestValue := openSrc[n] highestValue // GET FLOOR STUDYING THE LAST "studyLength" CANDLES TIMES RATIO get_low_average() => firstValue = openSrc[1] lowestValue = 9999999999999999999999.0 for n = 2 to studyLength/floorCeilingRation if openSrc[n] < lowestValue lowestValue := openSrc[n] lowestValue // MOVING AVERAGE avg_of_indv = avg(openSrc[1], openSrc[1]) tenpercent = round(openSrc[1]/(get_high_average()/10))*(get_high_average()/10) get_average(avg_src, avg_len) => ret_val = 0.0 for i = 1 to avg_len ret_val := ret_val + avg_src[i] ret_val/avg_len // SETTING VARIABLES avg_of_func = ema(openSrc, 16) avg_of_func2 = get_average(openSrc, 20) super_high_average = get_high_average() super_low_average = get_low_average() Difference = ((close - open)/((close + open)/2) * 100) percentChange = sma(Difference, volPercent) // LINE LIMITS smProfit = (volPercent*smProfitPercent/100) smLong = (volPercent*smLongPercent)/100 mdLong = (volPercent*mdLongPercent)/100 lgLong = volPercent/100 lgmdLong = avg(lgLong,mdLong) // DRAWING plot(avg_of_func,offset=5) plot(super_high_average, color=color.red, linewidth=2) plot(super_low_average, color=color.green, linewidth=2) plot(super_high_average-(super_high_average*smProfit), color=color.red, linewidth=1) plot(super_high_average-(super_high_average*smLong), color=color.green, linewidth=1) plot(super_high_average-(super_high_average*mdLong), color=color.green, linewidth=1) plot(super_high_average-(super_high_average*lgLong), color=color.green, linewidth=1) // POSITION CONDITIONALS smOpenLong = inTimeRange and crossunder(hl2,super_high_average-(super_high_average*smLong)) smCloseLong = inTimeRange and cross(close, super_high_average) smCloseLong2 = inTimeRange and cross(close, super_high_average) mdOpenLong = inTimeRange and crossunder(hl2,super_high_average-(super_high_average*mdLong)) mdCloseLong = inTimeRange and cross(close, super_high_average-(super_high_average*(smProfit))) mdCloseLong2 = inTimeRange and cross(close, super_high_average) lgOpenLong = inTimeRange and crossunder(close, super_low_average) lgCloseLong = inTimeRange and cross(close, super_high_average-(super_high_average*(smLong))) lgCloseLong2 = inTimeRange and crossunder(hl2,super_high_average-(super_high_average*mdLong)) smPos = 0 p1 = close / 100 / syminfo.mintick // SETTING POSITION strategy.order("smLong", strategy.long, smAmountReal, when = smOpenLong) // strategy.order("smLong2", strategy.long, smAmountReal, when = smOpenLong) strategy.close("smLong", when = smCloseLong) // strategy.close("smLong2", when = smCloseLong2) alertcondition(smOpenLong, "SM Buy {{ticker}}", "{{ticker}}, price = {{close}}") alertcondition(smCloseLong, "SM Sell 1 {{ticker}}", "{{ticker}}, price = {{close}}") // alertcondition(smCloseLong2, "SM Sell 2 {{ticker}}", "{{ticker}}, price = {{close}}") strategy.order("mdLong", strategy.long, mdAmountReal, when = mdOpenLong) // strategy.order("mdLong2", strategy.long, mdAmountReal, when = mdOpenLong) strategy.close("mdLong", when = mdCloseLong) // strategy.close("mdLong2", when = mdCloseLong2) alertcondition(mdOpenLong, "MD Buy {{ticker}}", "{{ticker}}, price = {{close}}") alertcondition(mdCloseLong, "MD Sell 1 {{ticker}}", "{{ticker}}, price = {{close}}") // alertcondition(mdCloseLong2, "MD Sell 2 {{ticker}}", "{{ticker}}, price = {{close}}") strategy.order("lgLong", strategy.long, lgAmountReal, when = lgOpenLong) // strategy.order("lgLong2", strategy.long, lgAmountReal, when = lgOpenLong) strategy.close("lgLong", when = lgCloseLong) // strategy.close("lgLong2", when = lgCloseLong2) alertcondition(lgOpenLong, "LG Buy {{ticker}}", "{{ticker}}, price = {{close}}") alertcondition(lgCloseLong, "MD Sell 1 {{ticker}}", "{{ticker}}, price = {{close}}") // alertcondition(lgCloseLong2, "MD Sell 2 {{ticker}}", "{{ticker}}, price = {{close}}") //strategy.exit("TP1", "smLong", profit = smLongPercent/10 * p1, qty_percent = 50) //strategy.exit("TP2", "smLong", profit = volPercent/10 * p1, qty_percent = 50) //strategy.exit("TP1.0", "mdLong", profit = mdLongPercent/10 * p1, qty_percent = 50) //strategy.exit("TP2.0", "mdLong", profit = volPercent/10 * p1, qty_percent = 50) //strategy.exit("TP1.00", "lgLong", profit = mdLongPercent/10 * p1, qty_percent = 50) //strategy.exit("TP2.00", "lgLong", profit = volPercent/10 * p1, qty_percent = 50) //if barssince(mdOpenLong) < 5