How to use TimeProfile() into a strategy

MDA7

New member
Hello everyone,

First of all, thank you for your time and help.
I have an issue when I want to build a strategy based on TPO.

Here is my code (strategy):
Code:
#
# TD Ameritrade IP Company, Inc. (c) 2010-2022
#

input pricePerRowHeightMode = {default TICKSIZE, AUTOMATIC, CUSTOM};
input customRowHeight = 1.0;
input timePerProfile = {default DAY, CHART, MINUTE, HOUR, WEEK, MONTH, "OPT EXP", BAR};
input multiplier = 1;
input onExpansion = no;
input profiles = 1000;
input showPointOfControl = yes;
input showValueArea = no;
input valueAreaPercent = 70;
input opacity = 20;

def period;
def yyyymmdd = getYyyyMmDd();
def seconds = secondsFromTime(0);
def month = getYear() * 12 + getMonth();
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 (timePerProfile) {
case CHART:
    period = 0;
case MINUTE:
    period = floor(seconds / 60 + day_number * 24 * 60);
case HOUR:
    period = floor(seconds / 3600 + day_number * 24);
case DAY:
    period = countTradingDays(Min(first(yyyymmdd), yyyymmdd), yyyymmdd) - 1;
case WEEK:
    period = floor(day_number / 7);
case MONTH:
    period = floor(month - first(month));
case "OPT EXP":
    period = exp_opt - first(exp_opt);
case BAR:
    period = barNumber() - 1;
}

def count = CompoundValue(1, if period != period[1] then (count[1] + period - period[1]) % multiplier else count[1], 0);
def cond = 0 < period - period[1];
def height;
switch (pricePerRowHeightMode) {
case AUTOMATIC:
    height = PricePerRow.AUTOMATIC;
case TICKSIZE:
    height = PricePerRow.TICKSIZE;
case CUSTOM:
    height = customRowHeight;
}

profile tpo = timeProfile("startnewprofile"=1);
tpo.Show("color"=Color.BLUE);
tpo.show();
def con = compoundValue(1, onExpansion, no);
def pc = if IsNaN(tpo.getPointOfControl()) and con then pc[1] else tpo.getPointOfControl();
def plotsDomain = IsNaN(close) == onExpansion;

def POC = if plotsDomain then pc else Double.NaN;


AddLabel(yes,tpo.getPointOfControl()[0]);
AddLabel(yes, POC[0]);
AddLabel(yes, pc[0]);

All the values for PC, POC, and tpo.getPointOfControl() are N/A
at line 57, I used this too:

profile tpo = timeProfile("startNewProfile" = cond, "onExpansion" = onExpansion, #"numberOfProfiles" = profiles, "pricePerRow" = height, "value area percent" = valueAreaPercent);

the result was the same.

I'll appreciate it, if you help me to get the POC's value inside a strategy, instead of a study.
I need to call TPO's POC value inside a strategy to test its trades.

Thanks
 
Last edited by a moderator:
Solution
Dear SleepyZ

Thank you for your follow-up.

I'll appreciate it if you share if you reach a result.

Thanks

Since you can use the profile code in a study, but not a TOS built-in Strategy, you may want to try to use the following snippet of order/profit/loss code that Linus created.

Ruby:
## V2.1  - 071715 - Dilbert - Correct calculation of profitLoss again
## V2    - 071515 - Dilbert - Correct calculation of profitLoss
## Profit/Loss label and bubbles, added by linus (2014-07-31)
## Note: Insert at the bottome of Mobius' RSI in Laguerre Time V02.07.2014 study.
input bubbles = {default OFF, POINTS, DOLLARS};
input PandL_Label = Yes;

def orderDir = compoundValue(1, if RSI > over_Bought then 1 else if RSI < over_Sold...

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

See if this helps

Dear SleepyZ,

Thank you for your comment.
I tried this and the returned value is still N/A

crq0W8h.jpg


Here is the code I tried:

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2010-2022
#

def tpopoc = TPOProfile("time per profile" = "DAY", "on expansion" = no).poc;
AddLabel(yes, tpopoc);
AddLabel(yes, tpopoc[1]);
 
Last edited:
I just wonder isn't there really a way to call the POC from a strategy?!!!

It is more than 2 weeks that no one has had any solution for this.

maybe not many people use profiles or are familiar with them. i don't use them.

i copied your post1 code, to a study, not sure what it is supposed to do,
but i have numbers, not N/A.
it draws a purple bar after the last bar. draws 3 labels, all with the same number.
tried it as a strategy, same, works.
typed in /hg , works.
changed input parameters, still worked
added sleepyz code, with a label and a plot. it works. draws a line with a daily value , and a label.

never saw N/A
sorry, don't know what to say.

Code:
input pricePerRowHeightMode = {default TICKSIZE, AUTOMATIC, CUSTOM};
input customRowHeight = 1.0;
input timePerProfile = {default DAY, CHART, MINUTE, HOUR, WEEK, MONTH, "OPT EXP", BAR};
input multiplier = 1;
input onExpansion = no;
input profiles = 1000;
input showPointOfControl = yes;
input showValueArea = no;
input valueAreaPercent = 70;
input opacity = 20;

def period;
def yyyymmdd = getYyyyMmDd();
def seconds = secondsFromTime(0);
def month = getYear() * 12 + getMonth();
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 (timePerProfile) {
case CHART:
    period = 0;
case MINUTE:
    period = floor(seconds / 60 + day_number * 24 * 60);
case HOUR:
    period = floor(seconds / 3600 + day_number * 24);
case DAY:
    period = countTradingDays(Min(first(yyyymmdd), yyyymmdd), yyyymmdd) - 1;
case WEEK:
    period = floor(day_number / 7);
case MONTH:
    period = floor(month - first(month));
case "OPT EXP":
    period = exp_opt - first(exp_opt);
case BAR:
    period = barNumber() - 1;
}

def count = CompoundValue(1, if period != period[1] then (count[1] + period - period[1]) % multiplier else count[1], 0);
def cond = 0 < period - period[1];
def height;
switch (pricePerRowHeightMode) {
case AUTOMATIC:
    height = PricePerRow.AUTOMATIC;
case TICKSIZE:
    height = PricePerRow.TICKSIZE;
case CUSTOM:
    height = customRowHeight;
}

profile tpo = timeProfile("startnewprofile"=1);
tpo.Show("color"=Color.BLUE);
tpo.show();
def con = compoundValue(1, onExpansion, no);
def pc = if IsNaN(tpo.getPointOfControl()) and con then pc[1] else tpo.getPointOfControl();
def plotsDomain = IsNaN(close) == onExpansion;

def POC = if plotsDomain then pc else Double.NaN;


AddLabel(yes,tpo.getPointOfControl()[0], color.yellow);
AddLabel(yes, POC[0], color.yellow);
AddLabel(yes, pc[0], color.yellow);

AddLabel(1, "  " , color.black);
def tpopoc = TPOProfile("time per profile" = "DAY", "on expansion" = no).poc;
plot z = tpopoc;
AddLabel(1, tpopoc, color.yellow);


HIiZ6h7.jpg
 
maybe not many people use profiles or are familiar with them. i don't use them.

i copied your post1 code, to a study, not sure what it is supposed to do,
but i have numbers, not N/A.
it draws a purple bar after the last bar. draws 3 labels, all with the same number.
tried it as a strategy, same, works.
typed in /hg , works.
changed input parameters, still worked
added sleepyz code, with a label and a plot. it works. draws a line with a daily value , and a label.

never saw N/A
sorry, don't know what to say.

Sorry, I have been in the process of moving and missed @MDA7 followup question. It appears that the 'profile indicators' cannot be referenced/used in TOS strategies coding, only studies where what code I previously posted works. I got the same "N/A" as @MDA7 did in a "TOS" strategy.

Also, I was unable to find anything in the Education Tab to explain this limitation or a TOS error message.
 
maybe not many people use profiles or are familiar with them. i don't use them.

i copied your post1 code, to a study, not sure what it is supposed to do,
but i have numbers, not N/A.
it draws a purple bar after the last bar. draws 3 labels, all with the same number.
tried it as a strategy, same, works.
typed in /hg , works.
changed input parameters, still worked
added sleepyz code, with a label and a plot. it works. draws a line with a daily value , and a label.

never saw N/A
sorry, don't know what to say.

Code:
input pricePerRowHeightMode = {default TICKSIZE, AUTOMATIC, CUSTOM};
input customRowHeight = 1.0;
input timePerProfile = {default DAY, CHART, MINUTE, HOUR, WEEK, MONTH, "OPT EXP", BAR};
input multiplier = 1;
input onExpansion = no;
input profiles = 1000;
input showPointOfControl = yes;
input showValueArea = no;
input valueAreaPercent = 70;
input opacity = 20;

def period;
def yyyymmdd = getYyyyMmDd();
def seconds = secondsFromTime(0);
def month = getYear() * 12 + getMonth();
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 (timePerProfile) {
case CHART:
    period = 0;
case MINUTE:
    period = floor(seconds / 60 + day_number * 24 * 60);
case HOUR:
    period = floor(seconds / 3600 + day_number * 24);
case DAY:
    period = countTradingDays(Min(first(yyyymmdd), yyyymmdd), yyyymmdd) - 1;
case WEEK:
    period = floor(day_number / 7);
case MONTH:
    period = floor(month - first(month));
case "OPT EXP":
    period = exp_opt - first(exp_opt);
case BAR:
    period = barNumber() - 1;
}

def count = CompoundValue(1, if period != period[1] then (count[1] + period - period[1]) % multiplier else count[1], 0);
def cond = 0 < period - period[1];
def height;
switch (pricePerRowHeightMode) {
case AUTOMATIC:
    height = PricePerRow.AUTOMATIC;
case TICKSIZE:
    height = PricePerRow.TICKSIZE;
case CUSTOM:
    height = customRowHeight;
}

profile tpo = timeProfile("startnewprofile"=1);
tpo.Show("color"=Color.BLUE);
tpo.show();
def con = compoundValue(1, onExpansion, no);
def pc = if IsNaN(tpo.getPointOfControl()) and con then pc[1] else tpo.getPointOfControl();
def plotsDomain = IsNaN(close) == onExpansion;

def POC = if plotsDomain then pc else Double.NaN;


AddLabel(yes,tpo.getPointOfControl()[0], color.yellow);
AddLabel(yes, POC[0], color.yellow);
AddLabel(yes, pc[0], color.yellow);

AddLabel(1, "  " , color.black);
def tpopoc = TPOProfile("time per profile" = "DAY", "on expansion" = no).poc;
plot z = tpopoc;
AddLabel(1, tpopoc, color.yellow);


HIiZ6h7.jpg
Dear halcyonguy
First of all, thank you for your comment and concern.
My code works as a study, what I need is to use it in a strategy.
When I put it into strategy code, the values changes to N/A.

 
Sorry, I have been in the process of moving and missed @MDA7 followup question. It appears that the 'profile indicators' cannot be referenced/used in TOS strategies coding, only studies where what code I previously posted works. I got the same "N/A" as @MDA7 did in a "TOS" strategy.

Also, I was unable to find anything in the Education Tab to explain this limitation or a TOS error message.
Dear SleepyZ

Thank you for your follow-up.

I'll appreciate it if you share if you reach a result.

Thanks
 
Dear SleepyZ

Thank you for your follow-up.

I'll appreciate it if you share if you reach a result.

Thanks

Since you can use the profile code in a study, but not a TOS built-in Strategy, you may want to try to use the following snippet of order/profit/loss code that Linus created.

Ruby:
## V2.1  - 071715 - Dilbert - Correct calculation of profitLoss again
## V2    - 071515 - Dilbert - Correct calculation of profitLoss
## Profit/Loss label and bubbles, added by linus (2014-07-31)
## Note: Insert at the bottome of Mobius' RSI in Laguerre Time V02.07.2014 study.
input bubbles = {default OFF, POINTS, DOLLARS};
input PandL_Label = Yes;

def orderDir = compoundValue(1, if RSI > over_Bought then 1 else if RSI < over_Sold then -1 else orderDir[1], 0);
def isOrder = orderDir crosses 0;

def orderCount = CompoundValue(1, if IsNaN(isOrder) then 0 else if isOrder then orderCount[1] + 1 else orderCount[1], 0);

def noBar = IsNaN(open[-1]);

def orderPrice = if isOrder then if noBar then close else open[-1] else orderPrice[1];
def profitLoss = if !isOrder or orderCount == 1
then 0
else if orderDir < 0 then orderPrice[1] - orderPrice
else if orderDir > 0 then orderPrice - orderPrice[1] else 0;
#else if orderDir > 0 then orderPrice[1] - orderPrice
#else if orderDir < 0 then orderPrice - orderPrice[1] else 0;
#def profitLoss = if !isOrder or orderCount == 1 then 0 else orderPrice - orderPrice[1];
def profitLossSum = CompoundValue(1, if IsNaN(isOrder) then 0 else if isOrder then profitLossSum[1] + profitLoss else profitLossSum[1], 0);

def orderWinners = compoundValue(1, if IsNaN(isOrder) then orderWinners[1] else if orderCount > 1 and profitLoss > 0 then orderWinners[1] + 1 else orderWinners[1], 0);

AddLabel(PandL_Label, orderCount + " orders (" + AsPercent(orderWinners/orderCount) + ") | P/L " + AsDollars((profitLossSum / TickSize()) * TickValue()), if profitLossSum > 0 then Color.GREEN else if profitLossSum < 0 then Color.RED else Color.GRAY);

AddChartBubble(bubbles and isOrder and orderDir > 0, RSI, if bubbles == bubbles.POINTS then "" + profitLoss else AsDollars((profitLoss / tickSize()) * tickValue()), if noBar then Color.LIGHT_GRAY else Color.GREEN, 0);

AddChartBubble(bubbles and isOrder and orderDir > 0, RSI, if bubbles == bubbles.POINTS then "" + orderPrice else AsDollars((orderPrice / tickSize()) * tickValue()), if noBar then Color.LIGHT_GRAY else Color.GREEN, 1);

AddChartBubble(bubbles and isOrder and orderDir < 0, RSI, if bubbles == bubbles.POINTS then "" + orderPrice else AsDollars((orderPrice / tickSize()) * tickValue()), if noBar then Color.GRAY else Color.RED, 0);
 
Solution
Hello everyone,

First of all, thank you for your time and help.
I have an issue when I want to build a strategy based on TPO.

Here is my code (strategy):
Code:
#
# TD Ameritrade IP Company, Inc. (c) 2010-2022
#

input pricePerRowHeightMode = {default TICKSIZE, AUTOMATIC, CUSTOM};
input customRowHeight = 1.0;
input timePerProfile = {default DAY, CHART, MINUTE, HOUR, WEEK, MONTH, "OPT EXP", BAR};
input multiplier = 1;
input onExpansion = no;
input profiles = 1000;
input showPointOfControl = yes;
input showValueArea = no;
input valueAreaPercent = 70;
input opacity = 20;

def period;
def yyyymmdd = getYyyyMmDd();
def seconds = secondsFromTime(0);
def month = getYear() * 12 + getMonth();
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 (timePerProfile) {
case CHART:
    period = 0;
case MINUTE:
    period = floor(seconds / 60 + day_number * 24 * 60);
case HOUR:
    period = floor(seconds / 3600 + day_number * 24);
case DAY:
    period = countTradingDays(Min(first(yyyymmdd), yyyymmdd), yyyymmdd) - 1;
case WEEK:
    period = floor(day_number / 7);
case MONTH:
    period = floor(month - first(month));
case "OPT EXP":
    period = exp_opt - first(exp_opt);
case BAR:
    period = barNumber() - 1;
}

def count = CompoundValue(1, if period != period[1] then (count[1] + period - period[1]) % multiplier else count[1], 0);
def cond = 0 < period - period[1];
def height;
switch (pricePerRowHeightMode) {
case AUTOMATIC:
    height = PricePerRow.AUTOMATIC;
case TICKSIZE:
    height = PricePerRow.TICKSIZE;
case CUSTOM:
    height = customRowHeight;
}

profile tpo = timeProfile("startnewprofile"=1);
tpo.Show("color"=Color.BLUE);
tpo.show();
def con = compoundValue(1, onExpansion, no);
def pc = if IsNaN(tpo.getPointOfControl()) and con then pc[1] else tpo.getPointOfControl();
def plotsDomain = IsNaN(close) == onExpansion;

def POC = if plotsDomain then pc else Double.NaN;


AddLabel(yes,tpo.getPointOfControl()[0]);
AddLabel(yes, POC[0]);
AddLabel(yes, pc[0]);

All the values for PC, POC, and tpo.getPointOfControl() are N/A
at line 57, I used this too:

profile tpo = timeProfile("startNewProfile" = cond, "onExpansion" = onExpansion, #"numberOfProfiles" = profiles, "pricePerRow" = height, "value area percent" = valueAreaPercent);

the result was the same.

I'll appreciate it, if you help me to get the POC's value inside a strategy, instead of a study.
I need to call TPO's POC value inside a strategy to test its trades.

Thanks
The reason you can not do a strategy with the Profile functions is because the Profile function output values are RETROACTIVE. In other words, prices in the present change the profile values in the past. Thus, any backtest -- ie, strategy -- would be using the wrong values, essentially "future" values, for its buy/sell conditions and would typically be invalid unless the market was barely moving. Hope this makes sense.
 
The reason you can not do a strategy with the Profile functions is because the Profile function output values are RETROACTIVE. In other words, prices in the present change the profile values in the past. Thus, any backtest -- ie, strategy -- would be using the wrong values, essentially "future" values, for its buy/sell conditions and would typically be invalid unless the market was barely moving. Hope this makes sense.
That's true for the current profile but prior period profile's values don't change
 
I found this script by Mobius

The script as a study does not plot the POC plots in a strategy
(saved as a strategy with addOrder(OrderType.BUY_AUTO, no);)



Volume Profile Modified for RTH and GlobeX


16:04 Mobius: Here is as far as I got:

1711072707489.png


# Volume Profile Modified for RTH and GlobeX
# Mobius
# V01
input showPointOfControl = yes;
input showValueArea = yes;
input opacity = 25;

def RTH = getTime() >= RegularTradingStart(getYYYYMMDD()) and
getTime() <= RegularTradingEnd(getYYYYMMDD());
def cond = RTH != RTH[1];
profile vol = volumeProfile(getSymbol(), TickSize(), cond, no, 2, 70);
def pc = if IsNaN(vol.getPointOfControl())
then pc[1]
else vol.getPointOfControl();
def hVA = if IsNaN(vol.getHighestValueArea())
then hVA[1]
else vol.getHighestValueArea();
def lVA = if IsNaN(vol.getLowestValueArea())
then lVA[1]
else vol.getLowestValueArea();
def hProfile = if IsNaN(vol.getHighest())
then hProfile[1]
else vol.getHighest();
def lProfile = if IsNaN(vol.getLowest())
then lProfile[1]
else vol.getLowest();
def PrevPOC = if cond
then pc[1]
else PrevPOC[1];
def PrevPOC2 = if cond
then PrevPoc[1]
else PrevPOC2[1];
def PrevPOC3 = if cond
then PrevPoc2[1]
else PrevPOC3[1];
plot POC = pc;
def x1 = if(cond, barNumber(), x1[1]);
def x2 = if(cond, x1[1], x2[1]);
def x3 = if(cond, x2[1], x3[1]);
def x4 = if(cond, x3[1], x4[1]);
plot POC2 = if barNumber() >= highestAll(x1)
then highestAll(if isNaN(close[-1])
then PrevPOC
else double.nan)
else double.nan;

plot POC3 = if barNumber() >= highestAll(x2)
then highestAll(if isNaN(close[-1])
then PrevPOC2
else double.nan)
else double.nan;
plot POC4 = if barNumber() >= highestAll(x3)
then highestAll(if isNaN(close[-1])
then PrevPOC3
else double.nan)
else double.nan;
plot ProfileHigh = hProfile;
plot ProfileLow = lProfile;
plot VAHigh = hVA;
plot VALow =lVA;

DefineGlobalColor("Profile", createColor(45, 135, 255));
DefineGlobalColor("Point Of Control", color.cyan);
DefineGlobalColor("Value Area", color.current);

vol.show(globalColor("Profile"), if showPointOfControl then globalColor("Point Of Control") else color.current, if showValueArea then globalColor("Value Area") else color.current, opacity);
POC.SetDefaultColor(globalColor("Point Of Control"));
POC.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
POC.SetLineWeight(2);
POC2.SetDefaultColor(CreateColor(25,150,250));
POC2.SetLineWeight(2);
POC3.SetDefaultColor(CreateColor(25,150,250));
POC3.SetLineWeight(2);
POC4.SetDefaultColor(CreateColor(25,150,250));
POC4.SetLineWeight(2);
VAHigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
VALow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
VAHigh.SetDefaultColor(color.orange);
VAHigh.SetLineWeight(2);
VALow.SetDefaultColor(color.orange);
VALow.SetLineWeight(2);
ProfileHigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ProfileLow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ProfileHigh.SetDefaultColor(color.Pink);
ProfileLow.SetDefaultColor(color.Pink);
ProfileHigh.hide();
ProfileLow.hide();



1711073705039.png
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
199 Online
Create Post

Similar threads

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.
Back
Top