Reset backtesting script to run as day trade

StockT8er

Member
VIP
This strategy was set up for swingtrading. As you can see I added EOD, I need to reset each day in order to run it as a DayTrade. Can you help?

Code:
#[email protected]

#positioning
input usetimefilter = yes;
input rthopen = 0930;
input rthclose = 1600;
def RTH = if usetimefilter == yes then if SecondsFromTime(rthopen) >= 0 and
               SecondsTillTime(rthclose) >= 0
            then 1
            else 0 else 1;

input avglength = 14;
input MTaverageType = AverageType.WILDERS;
input ATRLength = 5;
input MTprice = hlc3;
input MTreversalMode = {default ATRpercent, price};
input MTreversalAmount = .5;

##DayATR Value Calculation
def highPrice = FundamentalType.HIGH;
def closePrice = FundamentalType.CLOSE;
def lowPrice = FundamentalType.LOW;
def aggregationPeriod = AggregationPeriod.DAY;
rec AvgTrueRange = MovingAverage(AverageType.SIMPLE, TrueRange(Fundamental (highPrice, period = AggregationPeriod.DAY), Fundamental (closePrice, period = AggregationPeriod.DAY), Fundamental (lowPrice, period = AggregationPeriod.DAY) ), ATRLength);

##############
##ATR percent conversion calculation
rec DayATRReversalValue = MTreversalAmount * (AvgTrueRange / close) * 100;

def mode = if MTreversalMode == MTreversalMode.price then ZigZagTrendSign(price = MTprice, reversalAmount = MTreversalAmount) else ZigZagTrendPercent(price = MTprice, reversalAmount = DayATRReversalValue);
def MTinflection = if MTreversalMode == MTreversalMode.price then if !IsNaN(ZigZagSign(price = MTprice, reversalAmount = MTreversalAmount)) then 1 else 0 else if !IsNaN(ZigZagPercent(price = MTprice, reversalAmount = DayATRReversalValue)) then 1 else 0;
rec MTtrend = if MTinflection == 1 and mode == -1 then 1 else if MTinflection == 1 and mode == 1 then -1 else MTtrend[1];

rec MTupWaveVolume = if MTinflection == 1 and MTtrend == 1 and close > open then volume else if MTinflection == 1 and MTtrend == 1 and close <= open then 0 else if MTtrend == 1 or (MTinflection == 1 and MTtrend == -1 and close >= open) then MTupWaveVolume[1] + volume else 0;
rec MTdownWaveVolume = if MTinflection == 1 and MTtrend == -1 and close < open then volume else if MTinflection == 1 and MTtrend == -1 and close >= open then 0 else if MTtrend == -1 or (MTinflection == 1 and MTtrend == 1 and close <= open) then MTdownWaveVolume[1] + volume else 0;

plot BuySignal = MTinflection[1] and MTdownWaveVolume[2] > 0;
BuySignal.SetDefaultColor(Color.CYAN);
BuySignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BuySignal.SetLineWeight(4);

plot SellSignal = MTinflection[1] and MTupWaveVolume[2] > 0;
SellSignal.SetDefaultColor(Color.MAGENTA);
SellSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SellSignal.SetLineWeight(4);

def Buy_Time = if MTinflection[1] and MTdownWaveVolume[2] > 0 then 1 else 0;

def Buy_Signal = if RTH == 1 > SecondsTillTime(0930) and Buy_Time == 1 then 1 else 0;
def Sell_Signal = if MTinflection[1] and MTupWaveVolume[2] > 0 then 1 else 0;

def EOD =  RTH == 1 > SecondsTillTime(2000);
def orderPrice = open[-1]+((open[-1]+close[-1])/2);

AddOrder(OrderType.BUY_TO_OPEN, RTH and Buy_Signal, price = orderPrice, tickcolor = GetColor(0), arrowcolor = GetColor(0), name = "ST_LE");

AddOrder(OrderType.SELL_TO_CLOSE, RTH and Sell_Signal, price = orderPrice, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "ST_SE");

AddOrder(OrderType.SELL_TO_CLOSE, EOD, price = orderPrice, tickcolor = GetColor(9), arrowcolor = GetColor(7), name = "CDS_EOD");

##input MTVolumeAudible_Alert = yes;
def MTVolumeDeviation_Length = 60;
def MTVolumeDeviate = 2;
def MTvolumestdevlength = RelativeVolumeStDev(length = MTVolumeDeviation_Length);
def abovedev = MTvolumestdevlength >= MTVolumeDeviate;
def belowdev = MTvolumestdevlength <= MTVolumeDeviate;

def MTvolumeincrease = volume > volume[1];
def MTvolumedevincrease = MTvolumeincrease and abovedev;
def MTvolumedecrease = volume < volume[1];
def MTvolumedevdecrease = MTvolumedecrease and abovedev;

def O = open;
def H = high;
def C = close;
def L = low;
def V = volume;
def MTvolumeBuying = V * (C - L) / (H - L);
def MTvolumeSelling = V * (H - C) / (H - L);

input Show_Labels = yes;
AddLabel(Show_Labels, "Buy Vol = " + Round(MTvolumeBuying, 0), if MTvolumeBuying > MTvolumeSelling then Color.GREEN else Color.RED);
AddLabel(Show_Labels, "Sell Vol = " + Round(MTvolumeSelling, 0), if MTvolumeSelling > MTvolumeBuying then Color.GREEN else Color.RED);

Like below example due to market not open yet

RmaV7qC.png
 
Last edited by a moderator:

MatthewA

Active member
Hey @StockT8er If you don't want this to run 24/7 you might just pick a case where you let the code count the days...
You mix ADX & ATR cool I never thought of that??? Could that be used to warn of chop or consolidation??
Code:
input timeOfPhaseProfile = {Hour, Day, Week, Month, Year, Chart, "Opt Exp"};
input ValueAreaPercent = 50;
def period;
def yyyymmdd = GetYYYYMMDD();
def seconds = SecondsFromTime(0);
def month = GetYear() * 12 + GetMonth();
def year = GetYear();
def day_number = DaysFromDate(First(yyyymmdd)) + GetDayOfWeek(First(yyyymmdd));
def dom = GetDayOfMonth(yyyymmdd);
def dow = GetDayOfWeek(yyyymmdd - dom + 1);
def expthismonth = (if dow > 5 then 27 else 20) - dow;
def exp_opt = month + (dom > expthismonth);
switch (timeOfPhaseProfile) {
case Chart:
    period =0;#Entire Chart
case Hour:
    period = Floor(seconds / 3600 + day_number * 24);#Hour
case Day:
    period = CountTradingDays(Min(First(yyyymmdd), yyyymmdd), yyyymmdd) - 1;#Day
case Week:
    period = Floor(day_number / 7);#Week
case Month:
    period = Floor(month - First(month));#Month
case Year:
    period = Floor(year - First(year));#Year
case "Opt Exp":
    period = exp_opt - First(exp_opt);#option Exp
}
addLabel(ShowProfileSortingCounts4DEBUG, period + "period");
 

StockT8er

Member
VIP
Try changing this line

Code:
rec AvgTrueRange = MovingAverage(AverageType.SIMPLE, TrueRange(Fundamental (highPrice, period = AggregationPeriod.DAY), Fundamental (closePrice, period = AggregationPeriod.DAY), Fundamental (lowPrice, period = AggregationPeriod.DAY) ), ATRLength);

To this

Code:
rec AvgTrueRange = RTH and MovingAverage(AverageType.SIMPLE, TrueRange(Fundamental (highPrice, period = AggregationPeriod.DAY), Fundamental (closePrice, period = AggregationPeriod.DAY), Fundamental (lowPrice, period = AggregationPeriod.DAY) ), ATRLength);

I just hope the false signals are minimal. The first one did give false signals
 
Last edited:

BenTen

Administrative
Staff member
Staff
VIP
Lifetime
@Alex A good rule of thumb to identify whether an indicator will repaint or not is to look for false signals. If there isn't any, then most likely it's repainting. I added the backtesting script to my chart. It's giving me 100% win rate. Combine that with the fact that it is using "ZigZagTrendSign"
in the code.
 

Blue&Gold

New member
@Alex A good rule of thumb to identify whether an indicator will repaint or not is to look for false signals. If there isn't any, then most likely it's repainting. I added the backtesting script to my chart. It's giving me 100% win rate. Combine that with the fact that it is using "ZigZagTrendSign"
in the code.
What does "repaint" mean?
 

rad14733

Well-known member
VIP
What does "repaint" mean?
There is an entire forum topic on the subject but in short, repainting means that Buy and Sell signals will appear and then disappear as data continues to change... Therefore, secondary confirmation is the best practice if using repainting indicators...
 

Alex

Active member
VIP
@Alex A good rule of thumb to identify whether an indicator will repaint or not is to look for false signals. If there isn't any, then most likely it's repainting. I added the backtesting script to my chart. It's giving me 100% win rate. Combine that with the fact that it is using "ZigZagTrendSign"
in the code.
Exactly what I thought. The signals seemed too good to be true.
 

StockT8er

Member
VIP
I use EoD / RTH in every strategy, however I do have and input to control them.

Here is an alternate change

Code:
#rec DayATRReversalValue = MTreversalAmount * (AvgTrueRange / close) * 100;
rec DayATRReversalValue = RTH and MTreversalAmount * (AvgTrueRange / close) * 100;

Use either this one or the one posted earlier, however this change makes less trades

I was able to run this somewhat in OnDemand It defiantly repaints, Sell order appeared then disappeared after it started back up again

I have and idea, not necessary to eliminate false signals but to at lease reverse when they disappear. Anyone up for the challenge.
Link to latest updates
https://tos.mx/Qnj9wv1

By using this data could we do this?

Code:
def entryBarsTemp = if hasEntry
                    then bn
                    else nan;

def entryBarNum = if hasEntry and isNewEntry
                  then bn
                  else entryBarNum[1];

def isEntryBar = entryBarNum != entryBarNum[1];

def entryBarPL = if isEntryBar
                 then FPL
                 else entryBarPL[1];

def exitBarsTemp = if !hasEntry
                   and bn > entryBarsTemp[1]
                   then bn
                   else nan;

def exitBarNum = if !hasEntry and !IsNaN(exitBarsTemp)
                    then bn
                    else exitBarNum[1];

def isExitBar = exitBarNum != exitBarNum[1];

def exitBarPL = if isExitBar
                then FPL
                else exitBarPL[1];
 
Last edited:

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Top