Functions return different value in Indicator and strategy

Questions about MultiCharts and user contributed studies.
Mac Chen
Posts: 5
Joined: 10 Sep 2020

Functions return different value in Indicator and strategy

Postby Mac Chen » 10 Sep 2020

Hi,

I'm using TTMSquzee for my strategy. To simplify my strategy, I modify TTMSquzee code into function. Code is listed as followed:

Code: Select all

Inputs: Price(numericseries ), Length(numericseries ), { Length for Average True Range (ATR) & Std. Deviation (SD) Calcs } nKelt(numericseries ), { Keltner Channel ATRs from Average } BBStd(Numeric ), { Bollinger Band Std. Devs. from Average } AlertLevel( Numeric ), { BBS_Index level at which to issue alerts } Squzee( TrueFalseRef); Variables: {---------------------------------------------} intrabarpersist ATR(0), { Average True Range } intrabarpersist SDev(0), { Standard Deviation } intrabarpersist BBS_Ind(0), { Bollinger Band Squeeze Indicator } intrabarpersist Denom(0), intrabarpersist LHMult(0); if ( barnumber=1 ) then Begin If minmove <> 0 then LHMult = pricescale/minmove; end; { if barnumber = 1 and alertTextID = -1 then alertTextID = Text_New(date,time,0,"dummy"); } {-- Calculate BB Squeeze Indicator ----------------------} ATR = AvgTrueRange(Length); SDev = StandardDev(Price, Length, 1); Denom = (nKelt*ATR); If Denom <> 0 then BBS_Ind = (BBStd * SDev) /Denom; //If BBS_Ind < AlertLevel then If BBS_Ind < AlertLevel then Squzee = True else Squzee = False; {-- Plot delta of price from Donchian mid line ----------} value2 = LinearRegValue(price-((Highest(H, Length)+Lowest(L, Length))/2 + xAverage(c,Length))/2,Length,0); TTMSquzeeFunc = value2*LHMult; print("TTMSquzeeFunc Value ->", TTMSquzeeFunc);
The question is it return different values when I use it in indicator and strategy. The log is listed in following :

Code: Select all

{Value output for Indicator. These values are right.} TTMSquzeeFunc Value -> 6.58 $TTMSquzee Indicator -> 6.58 TTMSquzeeFunc Value -> 4.75 $TTMSquzee Indicator -> 4.75 TTMSquzeeFunc Value -> 5.11 $TTMSquzee Indicator -> 5.11 TTMSquzeeFunc Value -> 6.06 $TTMSquzee Indicator -> 6.06 {Value output for Strategy. These values are wild.} TTMSquzeeFunc Value ->-1541.01 TTMSquzee Strategy ->-1541.01 TTMSquzeeFunc Value ->-1541.76 TTMSquzee Strategy ->-1541.76 TTMSquzeeFunc Value ->-1542.53 TTMSquzee Strategy ->-1542.53 TTMSquzeeFunc Value ->-1543.34 TTMSquzee Strategy ->-1543.34
I try to add [IntrabarOrderGeneration = True/False] in front of my strategy, but it makes no difference. Add/Remove intrabarpersist in function variables make no difference too.

Does anyone what causes it? And how to fix it?

Regards
Mac Chen

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Functions return different value in Indicator and strategy

Postby JoshM » 11 Sep 2020

I got the same results for the indicator and for the signal.

I copied your function code into a new function file on my end, which I set to series. Then in the indicator I did (this and no more code than this):

Code: Select all

if currentbar > 1000 then begin condition1 = false; Print(PrintDateTime(0), "Indicator TTM squeeze value: ", TTMSqueeze(close, 10, 2, 2, 1, condition1)); end;
Which prints:

Code: Select all

TTMSquzeeFunc Value -> 77.84 11-09_21:50:00 Indicator TTM squeeze value: 77.84 TTMSquzeeFunc Value -> 69.19 11-09_21:55:00 Indicator TTM squeeze value: 69.19 TTMSquzeeFunc Value -> 54.65 11-09_22:00:00 Indicator TTM squeeze value: 54.65 TTMSquzeeFunc Value -> 31.58 11-09_22:05:00 Indicator TTM squeeze value: 31.58 TTMSquzeeFunc Value -> 15.36 11-09_22:10:00 Indicator TTM squeeze value: 15.36 TTMSquzeeFunc Value -> 8.27 11-09_22:15:00 Indicator TTM squeeze value: 8.27 TTMSquzeeFunc Value -> 1.95 11-09_22:20:00 Indicator TTM squeeze value: 1.95 TTMSquzeeFunc Value -> 0.41 11-09_22:25:00 Indicator TTM squeeze value: 0.41 TTMSquzeeFunc Value -> -8.93 11-09_22:30:00 Indicator TTM squeeze value: -8.93 TTMSquzeeFunc Value -> -13.70 11-09_22:35:00 Indicator TTM squeeze value: -13.70 TTMSquzeeFunc Value -> -3.53 11-09_22:40:00 Indicator TTM squeeze value: -3.53 TTMSquzeeFunc Value -> 7.67 11-09_22:45:00 Indicator TTM squeeze value: 7.67 TTMSquzeeFunc Value -> 20.37 11-09_22:50:00 Indicator TTM squeeze value: 20.37 TTMSquzeeFunc Value -> 26.16 11-09_22:55:00 Indicator TTM squeeze value: 26.16 TTMSquzeeFunc Value -> 28.71 11-09_23:00:00 Indicator TTM squeeze value: 28.71
In the signal I did (this and nothing more):

Code: Select all

if currentbar > 1000 then begin condition1 = false; Print(PrintDateTime(0), "Signal TTM squeeze value: ", TTMSqueeze(close, 10, 2, 2, 1, condition1)); end;
Which gives this output:

Code: Select all

TTMSquzeeFunc Value -> 77.84 11-09_21:50:00 Signal TTM squeeze value: 77.84 TTMSquzeeFunc Value -> 69.19 11-09_21:55:00 Signal TTM squeeze value: 69.19 TTMSquzeeFunc Value -> 54.65 11-09_22:00:00 Signal TTM squeeze value: 54.65 TTMSquzeeFunc Value -> 31.58 11-09_22:05:00 Signal TTM squeeze value: 31.58 TTMSquzeeFunc Value -> 15.36 11-09_22:10:00 Signal TTM squeeze value: 15.36 TTMSquzeeFunc Value -> 8.27 11-09_22:15:00 Signal TTM squeeze value: 8.27 TTMSquzeeFunc Value -> 1.95 11-09_22:20:00 Signal TTM squeeze value: 1.95 TTMSquzeeFunc Value -> 0.41 11-09_22:25:00 Signal TTM squeeze value: 0.41 TTMSquzeeFunc Value -> -8.93 11-09_22:30:00 Signal TTM squeeze value: -8.93 TTMSquzeeFunc Value -> -13.70 11-09_22:35:00 Signal TTM squeeze value: -13.70 TTMSquzeeFunc Value -> -3.53 11-09_22:40:00 Signal TTM squeeze value: -3.53 TTMSquzeeFunc Value -> 7.67 11-09_22:45:00 Signal TTM squeeze value: 7.67 TTMSquzeeFunc Value -> 20.37 11-09_22:50:00 Signal TTM squeeze value: 20.37 TTMSquzeeFunc Value -> 26.16 11-09_22:55:00 Signal TTM squeeze value: 26.16 TTMSquzeeFunc Value -> 28.71 11-09_23:00:00 Signal TTM squeeze value: 28.71
Both outputs are the same so I don't have your problem.

Did you perhaps called the function in a different way, or did you have other code inside the indicator and signal? Or is your function set to 'simple'? That might also be something worth experimenting with.

Mac Chen
Posts: 5
Joined: 10 Sep 2020

Re: Functions return different value in Indicator and strategy

Postby Mac Chen » 13 Sep 2020

I got the same results for the indicator and for the signal.

I copied your function code into a new function file on my end, which I set to series. Then in the indicator, I did (this and no more code than this):

emmited....
It is odd. I try with Length > 1000 and still have the same problem. Following is my code:
TTMSquzeeFunc

Code: Select all

Inputs: {------------------------------------------------} Price(NumericSeries ), Length(NumericSimple), { Length for Average True Range (ATR) & Std. Deviation (SD) Calcs } nKelt(NumericSimple), { Keltner Channel ATRs from Average } BBStd(NumericSimple), { Bollinger Band Std. Devs. from Average } AlertLevel(NumericSimple), { BBS_Index level at which to issue alerts } //TTMCutOffStd(numericseries ), {HiLo BB Band} Squzee( TrueFalseRef); Variables: {---------------------------------------------} ATR(0), { Average True Range } SDev(0), { Standard Deviation } BBS_Ind(0), { Bollinger Band Squeeze Indicator } Denom(0), LHMult(0); if ( barnumber=1 ) then Begin If minmove <> 0 then LHMult = pricescale/minmove; end; { if barnumber = 1 and alertTextID = -1 then alertTextID = Text_New(date,time,0,"dummy"); } {-- Calculate BB Squeeze Indicator ----------------------} if BarNumber > Length then begin ATR = AvgTrueRange(Length); SDev = StandardDev(Price, Length, 1); Denom = (nKelt*ATR); If Denom <> 0 then BBS_Ind = (BBStd * SDev) /Denom; //If BBS_Ind < AlertLevel then If BBS_Ind < AlertLevel then Squzee = True else Squzee = False; {-- Plot delta of price from Donchian mid line ----------} value2 = LinearRegValue(price-((Highest(H, Length)+Lowest(L, Length))/2 + ema(c,Length))/2,Length,0); TTMSquzeeFunc = value2*LHMult; print("TTMSquzeeFunc Value ->", TTMSquzeeFunc); end else begin TTMSquzeeFunc = 0; Squzee =False; end;
TTM Squzee Indicator

Code: Select all

Inputs: {------------------------------------------------} Price(Close), Length(20), { Length for Average True Range (ATR) & Std. Deviation (SD) Calcs } nK(1.5), { Keltner Channel ATRs from Average } BBStd(2), { Bollinger Band Std. Devs. from Average } TTMBB(1.382), {HiLo BB Band} AlertLine( 1 ), { BBS_Index level at which to issue alerts } NormalColor( Green ), { Normal color for BBS_Ind } AlertlColor( Red ); { Color for BBS_Ind below alert line } Variables: {---------------------------------------------} ATR(0), { Average True Range } SDev(0), { Standard Deviation } BBS_Ind(0), { Bollinger Band Squeeze Indicator } alertTextID(-1), Denom(0), LHMult(0), Squzee( False ), { BBS_Index level at which to issue alerts } TTMValue(0), TTMUpBand(0), TTMDnBand(0), color(0); TTMValue = TTMSquzeeFunc(Price, Length, nk, BBStd, AlertLine, Squzee); TTMUpBand = BollingerBand(TTMValue, Length, TTMBB); TTMDnBand = BollingerBand(TTMValue, Length, -TTMBB); print("$TTMSquzee Indicator ->", TTMValue); if TTMValue > 0 then begin if TTMValue >= TTMUpBand then begin if TTMValue >= TTMValue [1] then color= cyan else color = darkcyan; end; if TTMValue < TTMUpBand then if TTMValue > TTMValue [1] then color = lightgray else color = darkgray; end; if TTMValue <= 0 then begin if TTMValue <= TTMDnBand then begin if TTMValue <= TTMValue [1] then color= magenta else color = darkmagenta; end; if TTMValue > TTMDnBand then if TTMValue < TTMValue [1] then color = lightgray else color = darkgray; end; Plot1(TTMValue , "TTM", color); If Squzee then SetPlotColor(2, AlertlColor) else SetPlotColor(2, NormalColor); {-- Plot the Index & Alert Line -------------------------} Plot2(0, "BBS_Ind");
Simple TTMSquzee Strategy

Code: Select all

//[IntrabarOrderGeneration = True] inputs: Strength(2), Error(3); variables: {TTMSquzee Function Variables} Price(Close), Length(20), { Length for Average True Range (ATR) & Std. Deviation (SD) Calcs } nK(1.5), { Keltner Channel ATRs from Average } nBB(2), { Bollinger Band Std. Devs. from Average } AlertLevel( 1 ), { BBS_Index level at which to issue alerts } Squzee(False), {TTM Squzee Status} TTMValue(0); TTMValue = TTMSquzeeFunc(Price, Length, nk, nbb, AlertLevel, Squzee); print("TTMSquzee Strategy ->", TTMValue);
Following is the data log for E-Mini 15-min bar:
eMini 15m log output - Last 8 lines

Code: Select all

TTMSquzeeFunc Value -> -26.66 $TTMSquzee Indicator -> -26.66 TTMSquzeeFunc Value -> -16.13 $TTMSquzee Indicator -> -16.13 TTMSquzeeFunc Value -> -6.63 $TTMSquzee Indicator -> -6.63 TTMSquzeeFunc Value -> 3.12 $TTMSquzee Indicator -> 3.12 TTMSquzeeFunc Value -> 130.28 TTMSquzee Strategy -> 130.28 TTMSquzeeFunc Value -> 131.45 TTMSquzee Strategy -> 131.45 TTMSquzeeFunc Value -> 133.70 TTMSquzee Strategy -> 133.70 TTMSquzeeFunc Value -> 134.92 TTMSquzee Strategy -> 134.92


Return to “MultiCharts”