How do I put an SMA on a Spread Differential Indicator to plot/code together for TS?

clbradley17

Junior member
Messages
25
Likes
0
How can I put a simple moving average on a spread differential indicator on the same plot underneath price, so the SMA isn't the SMA of the price, but of the spread differential indicator? The SpreadDif and SMA have to be in the same code too, don't they? This is the code for TS's Spread Differential:

inputs: DataSeries1( Close of data1 ), DataSeries2( Close of data2 ) ;

Plot1( DataSeries1 - DataSeries2, "SprdDiff" ) ;

and for the Mov Avg 1 line:

inputs: Price( Close ), Length( 9 ), Displace( 0 ) ;
variables: Avg( 0 ) ;

Avg = AverageFC( Price, Length ) ;

if Displace >= 0 or CurrentBar > AbsValue( Displace ) then
begin
Plot1[Displace]( Avg, "Avg" ) ;
end;

Just not sure how to paste these together to put them in the same code. Thanks for any help.
 
How can I put a simple moving average on a spread differential indicator on the same plot underneath price, so the SMA isn't the SMA of the price, but of the spread differential indicator? The SpreadDif and SMA have to be in the same code too, don't they? This is the code for TS's Spread Differential:

inputs: DataSeries1( Close of data1 ), DataSeries2( Close of data2 ) ;

Plot1( DataSeries1 - DataSeries2, "SprdDiff" ) ;

and for the Mov Avg 1 line:

inputs: Price( Close ), Length( 9 ), Displace( 0 ) ;
variables: Avg( 0 ) ;

Avg = AverageFC( Price, Length ) ;

if Displace >= 0 or CurrentBar > AbsValue( Displace ) then
begin
Plot1[Displace]( Avg, "Avg" ) ;
end;

Just not sure how to paste these together to put them in the same code. Thanks for any help.

Something like:


inputs: DataSeries1( Close of data1 ), DataSeries2( Close of data2 ) ,
Length( 9 ), Displace( 0 ) ;

Vars: sprdiff(0), Avg(0);

sprdiff = DataSeries1 - DataSeries2;

Plot1( sprdiff, "SprdDiff" ) ;

Avg = AverageFC( sprdiff, Length ) ;

if Displace >= 0 or CurrentBar > AbsValue( Displace ) then
begin
Plot2[Displace]( Avg, "Avg" ) ;
end;

What you are doing is assigning the results of the spreaddiff caculation to a variable. Then you use that variable instead of price within the Avg calculation. You then plot that avg on a second plot Plot2. Hopefully I have not made any syntax errors there, but you can see the principle of the thing

Charlton
 
Thanks Charlton. Your code did verify successfully for Tradestation. Was also trying to put an SMA on the same sub-graph/in the same code as ADX.
The very short code for that is:
inputs:
Length( 14 ) ;
variables:
ADXValue( 0 ) ;
ADXValue = ADX( Length ) ;
Plot1( ADXValue, "ADX" ) ;

I figure that Plot 2 would be the SMA, but I'm not sure what to do at the top for inputs or in variables, since Length(14) in the ADX code inputs uses the length term also, same as for a moving average. Thanks again for for your earlier great answer, won't post this follow-up in the other thread, but sometimes people just look at certain sections, and would've missed it in one place or the other.
 
Thanks Charlton. Your code did verify successfully for Tradestation. Was also trying to put an SMA on the same sub-graph/in the same code as ADX.
The very short code for that is:
inputs:
Length( 14 ) ;
variables:
ADXValue( 0 ) ;
ADXValue = ADX( Length ) ;
Plot1( ADXValue, "ADX" ) ;

I figure that Plot 2 would be the SMA, but I'm not sure what to do at the top for inputs or in variables, since Length(14) in the ADX code inputs uses the length term also, same as for a moving average. Thanks again for for your earlier great answer, won't post this follow-up in the other thread, but sometimes people just look at certain sections, and would've missed it in one place or the other.

If the length for the SMA will always be the same as the length used for the ADX then you can use the same input, so the formula would be something like

inputs:
Length( 14 ) ;
variables:
ADXValue( 0 ), SMA(0) ;
ADXValue = ADX( Length ) ;
Plot1( ADXValue, "ADX" ) ;

SMA = AVERAGE(ADXValue,length);

Plot2(SMA, "SMA");

If however you wanted different values for the SMA and the ADX you have to create another input and use that one in the SMA calculation so it would become


inputs:
ADX_LEN( 14 ), SMA_LEN(9) ;
variables:
ADXValue( 0 ), SMA(0) ;
ADXValue = ADX( ADX_LEN) ;
Plot1( ADXValue, "ADX" ) ;

SMA = AVERAGE(ADXValue, SMA_LEN);

Plot2(SMA, "SMA");

Remember that the figures in brackets wtihin the inputs section of the code are only the default lengths and that these can be set to anything you like when running the code using the format > indicator option

Charlton
 
Thanks again so much for the speedy and accurate reply, Charlton. That code verifies perfectly in Tradestation as the first one for the Spread Dif + SMA did. I needed to use different lengths for the ADX and SMA, so thanks for showing both options. I'm guessing that if I wanted to use an exponential moving average instead of simple, just exchange EMA for SMA in either ADX code. In the first code, I'm pretty sure xAverage can replace AverageFC in line 6 to use an exponential instead of a simple MA in the Spread Dif + SMA code.
 
Thanks again so much for the speedy and accurate reply, Charlton. That code verifies perfectly in Tradestation as the first one for the Spread Dif + SMA did. I needed to use different lengths for the ADX and SMA, so thanks for showing both options. I'm guessing that if I wanted to use an exponential moving average instead of simple, just exchange EMA for SMA in either ADX code. In the first code, I'm pretty sure xAverage can replace AverageFC in line 6 to use an exponential instead of a simple MA in the Spread Dif + SMA code.

Correct - average or averageFC is the Easylanguage function for a simple moving average. xaverage is for an exponential moving average. Obviously you might want to change the names of the variables, inputs and plots to reflect that these are EMAs instead.

Charlton
 
Charlton, thanks so much for your help. Is there any way to code Bollinger Bands around 2 of the codes above, the spread differential + SMA at the top of the page, and the ADX + SMA ( the 2nd one with different lengths)? I'm pretty sure the upper and lower bands would be plotted on plot 3 and 4, or 3, 4 & 5, and most of the input and variables from the Bollinger Bands code could remain the same? Here is TS's BB code:

inputs:
BollingerPrice( Close ),
TestPriceUBand( Close ),
TestPriceLBand( Close ),
Length( 20 ),
NumDevsUp( 2 ),
NumDevsDn( -2 ),
Displace( 0 ) ;

variables:
Avg( 0 ),
SDev( 0 ),
LowerBand( 0 ),
UpperBand( 0 ) ;

Avg = AverageFC( BollingerPrice, Length ) ;
SDev = StandardDev( BollingerPrice, Length, 1 ) ;
UpperBand = Avg + NumDevsUp * SDev ;
LowerBand = Avg + NumDevsDn * SDev ;

if Displace >= 0 or CurrentBar > AbsValue( Displace ) then
begin
Plot1[Displace]( UpperBand, "UpperBand" ) ;
Plot2[Displace]( LowerBand, "LowerBand" ) ;
Plot3[Displace]( Avg, "MidLine" ) ;

end;

Thanks again for your help, everything has verified perfectly in TS.
 
Charlton, thanks so much for your help. Is there any way to code Bollinger Bands around 2 of the codes above, the spread differential + SMA at the top of the page, and the ADX + SMA ( the 2nd one with different lengths)? I'm pretty sure the upper and lower bands would be plotted on plot 3 and 4, or 3, 4 & 5, and most of the input and variables from the Bollinger Bands code could remain the same? Here is TS's BB code:

inputs:
BollingerPrice( Close ),
TestPriceUBand( Close ),
TestPriceLBand( Close ),
Length( 20 ),
NumDevsUp( 2 ),
NumDevsDn( -2 ),
Displace( 0 ) ;

variables:
Avg( 0 ),
SDev( 0 ),
LowerBand( 0 ),
UpperBand( 0 ) ;

Avg = AverageFC( BollingerPrice, Length ) ;
SDev = StandardDev( BollingerPrice, Length, 1 ) ;
UpperBand = Avg + NumDevsUp * SDev ;
LowerBand = Avg + NumDevsDn * SDev ;

if Displace >= 0 or CurrentBar > AbsValue( Displace ) then
begin
Plot1[Displace]( UpperBand, "UpperBand" ) ;
Plot2[Displace]( LowerBand, "LowerBand" ) ;
Plot3[Displace]( Avg, "MidLine" ) ;

end;

Thanks again for your help, everything has verified perfectly in TS.

Hello clbradley17,

Based on your request, please find the code you are requesting below. It would be best to use two pieces of code in order to differentiate the inputs of the Bollinger Bands. If you need the two indicators as one code set, please contact us and we will go ahead and provide it to you. In addition, the alerts have been set to notify you when either the ADX moving average or spread differential moving average is crossing back into the Bollinger Bands. If you need another setting, please let us know.

Bollinger Bands around spread differential moving average:

Inputs: DataSeries1( Close of data1 ), DataSeries2( Close of data2 ) ,
AvgLength( 9 ), Displace( 0 ), SDLength( 20 ), NumDevsUp( 2 ),NumDevsDn( -2 );

Vars: sprdiff(0), AvgSpread(0), LoBandSpreadSMA( 0 ), UpBandSpreadSMA( 0 ), SDev(0);

sprdiff = DataSeries1 - DataSeries2;
AvgSpread = AverageFC( sprdiff, AvgLength ) ;
SDev = StandardDev( AvgSpread, SDLength, 1 ) ;
UpBandSpreadSMA = AvgSpread + NumDevsUp * SDev ;
LoBandSpreadSMA = AvgSpread + NumDevsDn * SDev ;

if Displace >= 0 or CurrentBar > AbsValue( Displace ) then
begin
Plot1[Displace](AvgSpread, "AvgSprd");
Plot2[Displace](UpBandSpreadSMA, "UpBandSprd");
Plot3[Displace](LoBandSpreadSMA, "LoBandSprd");
end;

{ Alert criteria }
if Displace <= 0 then
begin
if Sprdiff crosses over LoBandSpreadSMA then
Alert( "Price crossing over lower price band" )
else if SprDiff crosses under UpBandSpreadSMA then
Alert( "Price crossing under upper price band" );
end ;

Bollinger Bands around ADX moving average:

Inputs: Displace( 0 ), ADX_LEN( 14 ), SMA_LEN(9), SDLength( 20 ),
NumDevsUp( 2 ),NumDevsDn( -2 );

Vars: ADXValue(0), SMA(0), AvgSpread( 0 ),
SDev( 0 ), UpBandADXSMA(0), LoBandADXSMA(0);

ADXValue = ADX( ADX_Len ) ;
SMA = AverageFC(ADXValue, SMA_LEN);
SDev = StandardDev(SMA, SDLength, 1 ) ;
UpBandADXSMA = SMA + NumDevsUp * SDev ;
LoBandADXSMA = SMA + NumDevsDn * SDev;

if Displace >= 0 or CurrentBar > AbsValue( Displace ) then
begin
Plot1(SMA, "SMAADX");
Plot2[Displace](UpBandADXSMA, "UpBandADX");
Plot3[Displace](LoBandADXSMA, "LoBandADX");

{ Alert criteria }
if Displace <= 0 then
begin
if ADXValue crosses over LoBandADXSMA then
Alert( "Price crossing over lower price band" )
else if ADXValue crosses under UpBandADXSMA then
Alert( "Price crossing under upper price band" ) ;
end ;
end ;

I encourage you to visit the TradeStation EasyLanguage Support Forums if you have any additional EasyLanguage questions in the future. The forum is located at: https://www.tradestation.com/Discussions/Forum.aspx?Forum_ID=213

Regards,

Alexandra Guevara
Market Technician

TradeStation Securities, Inc.
A Wholly-owned subsidiary of
TradeStation Group, Inc. (Nasdaq: TRAD)
http://www.tradestation.com
 
Thanks so much for your help with the codes, TS Securities-Alexandra. I have asked the questions at the start of this thread and several other ?s at Tradestation, and rarely get a reply, and have seen other questions and threads started by others frequently there, that die on the vine with no reply.

Had given up on a reply here after a couple weeks with no response to the last question, or would've thanked you earlier if I had checked back and seen your answer. Thanks again for your help, but after looking at the indicators, what I was looking for was Bollinger Bands plotted in the same subgraph around an ADX with a separate but related SMA, same with the spread differential, but somehow the ADX and SMA, and the Spread Dif and SMA are melded together in these codes. In the codes further up the page the SMAs are plotted apart but in relation to the indicators, not price, and was looking to see them remain that way, just with BBs around them. What would be the code for a spread ratio (instead of differential) with an SMA on it, or how the SMAs and indicators can be shown relative but apart in the BB codes? Thanks.
 
Last edited:
Thanks so much for your help with the codes, TS Securities-Alexandra. I have asked the questions at the start of this thread and several other ?s at Tradestation, and rarely get a reply, and have seen other questions and threads started by others frequently there, that die on the vine with no reply.

Had given up on a reply here after a couple weeks with no response to the last question, or would've thanked you earlier if I had checked back and seen your answer. Thanks again for your help, but after looking at the indicators, what I was looking for was Bollinger Bands plotted in the same subgraph around an ADX with a separate but related SMA, same with the spread differential, but somehow the ADX and SMA, and the Spread Dif and SMA are melded together in these codes. In the codes further up the page the SMAs are plotted apart but in relation to the indicators, not price, and was looking to see them remain that way, just with BBs around them. What would be the code for a spread ratio (instead of differential) with an SMA on it, or how the SMAs and indicators can be shown relative but apart in the BB codes? Thanks.

Hello clbradley17,

Can you please clarify if you want to plot a simple moving average of the ADX and spread differential or if you want to plot the simple moving average of the price? It seemed as though in your original request, you were looking to code Bollinger Bands around the simple moving average of the ADX and around the simple moving average of the spread differential.

If you are looking to plot the simple moving average of the price, the simple moving average of the price plus two standard deviations of the ADX, and the simple moving average of the price minus two standard deviations of the ADX, then simply change the code from:

SMA = AverageFC(ADXValue, SMA_LEN);
SDev = StandardDev(ADX, SDLength, 1 ) ;

To:

SMA = AverageFC(Close, SMA_LEN);
SDev = StandardDev(ADXValue, SDLength, 1 ) ;

The same would apply for the Spread Differential Bollinger Bands, simply change the code from:

AvgSpread = AverageFC( SprDiff, AvgLength ) ;
SDev = StandardDev( AvgSpread, SDLength, 1 ) ;

To:

AvgSpread = AverageFC( Close, AvgLength ) ;
SDev = StandardDev( SprDiff, SDLength, 1 ) ;

In regards to the spread ratio, please see the code provided below:

inputs: DataSeries1( Close of data1 ), DataSeries2( Close of data2 ), AvgLen(130) ;
Vars: SpreadR(0), AvgRatio(0);

SpreadR = DataSeries1 / DataSeries2;
AvgRatio = Average(SpreadR, AvgLen);

if DataSeries2 <> 0 then
Plot1(SpreadR , "SprdRatio" ) ;
Plot2(AvgRatio, "Avg");

The above is a built-in indicator in TradeStation called Spread – Ratio.

Regards,

TradeStation Securities, Inc.
A Wholly-owned subsidiary of
TradeStation Group, Inc. (Nasdaq: TRAD)
http://www.tradestation.com
 
Top