NexusFi: Find Your Edge


Home Menu

 





TOS Fold Loop to NT8 For Loop


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one elitecamper with 4 posts (1 thanks)
    2. looks_two zeller4 with 4 posts (1 thanks)
    3. looks_3 river72f with 1 posts (0 thanks)
    4. looks_4 hedgeplay with 1 posts (1 thanks)
    1. trending_up 4,649 views
    2. thumb_up 3 thanks given
    3. group 4 followers
    1. forum 10 posts
    2. attach_file 8 attachments




 
Search this Thread
  #1 (permalink)
 zeller4 
Orlando Florida
 
Experience: Intermediate
Platform: NT8
Trading: CL, NQ, ES, RTY
Posts: 478 since Jun 2009
Thanks Given: 1,419
Thanks Received: 404

Hello,
I'm trying to make sense of the fold loop in TOS thinkscript and convert to Ninjascript.

Here's a snippet of the TOS code (Mobius' TMO code):

 
Code
		# TMO ((T)rue (M)omentum (O)scilator)
# Mobius
# V01.05.2018
# hint: TMO calculates momentum using the delta of price. Giving a much better picture of trend, trend reversals and divergence than momentum oscillators using price.

declare Lower;

input length = 14;
input calcLength = 5;
input smoothLength = 3;

def o = open;
def c = close;
def data = fold i = 0 to length
           with s
           do s + (if c > getValue(o, i)
                   then 1
                   else if c < getValue(o, i)
                        then - 1
                        else 0);
def EMA5 = ExpAverage(data, calcLength);
plot Main = ExpAverage(EMA5, smoothLength);
plot Signal = ExpAverage(Main, smoothLength);
Here's what I have so far (_data and _ema05 are DataSeries, Main_Plot and Signal_Plot are plots):
 
Code
for (int idx=0;idx<Length;idx++)//14
			{
				
				double o = Open[idx];
				double c = Close[idx];
					
				_data[idx] = c > o ? 1 : c < o ? -1 : 0;
//				_ema05[idx] = EMA(_data, CalcLength)[idx];//5
			
//				Main_Plot[idx] = EMA(_ema05, SmoothLength)[idx];//3
//				Signal_Plot[idx] = EMA(Main_Plot, SmoothLength)[idx];//3
			
			}
			
				_ema05[0] = EMA(_data, CalcLength)[0];//5
			Main_Plot[0] = EMA(_ema05, SmoothLength)[0];//3
			Signal_Plot[0] = EMA(Main_Plot, SmoothLength)[0];//3
Where I'm stumped...
1) range of plots not the same from NT to TOS - extending to upper / lower bands
2) don't know how to use "fold", "with", "do", "getValue()".

TIA for any help and advice. Thanks.
kz


Attached Thumbnails
Click image for larger version

Name:	TOS_Screenshot 2022-06-27 104108.png
Views:	208
Size:	76.4 KB
ID:	325497   Click image for larger version

Name:	CL 08-22 (233 Tick) 2022_06_27 (10_37_19 AM).png
Views:	237
Size:	59.9 KB
ID:	325498  
Attached Files
Elite Membership required to download: TMOv01.cs
Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
requesting .nrd replay files from 2026 spy and qqq
The Elite Circle
No book, only a stair: journaling fills where price move …
Cryptocurrency
What we know about Binances $6.3bn hack - BBC News
Cryptocurrency
AI for Traders — Agents, Memory, Tools & Real-Worl …
Elite Quantitative GenAI/LLM
[WTS] ATAS Lifetime ULTRA license – EU buyers only – …
Platforms and Indicators
 
  #2 (permalink)
 elitecamper 
McAllen, TX
 
Experience: Advanced
Platform: Multicharts, Jigsaw
Broker: Ironbeam
Trading: ES, ZN, ZB, ZF
Posts: 106 since May 2013
Thanks Given: 108
Thanks Received: 138

Hello zeller4,

The double o, and double c, need to be declared before the for loop.
And they should not be variables but series objects instead.

Give it a try.


Visit my NexusFi Trade Journal Reply With Quote
  #3 (permalink)
 zeller4 
Orlando Florida
 
Experience: Intermediate
Platform: NT8
Trading: CL, NQ, ES, RTY
Posts: 478 since Jun 2009
Thanks Given: 1,419
Thanks Received: 404


Thanks elitecamper,

I made the o and c as DataSeries in the front end but still getting the same plot results.
I guess I still need to figure out how TOS "fold" gets translated to NT

 
Code
			_o[0] = Open[0];
			_c[0] = Close[0];
			
			for (int idx=0;idx<Length;idx++)//14
			{
				
				_data[idx] = _c[idx] > _o[idx] ? 1 : _c[idx] < _o[idx] ? -1 : 0;
				
			
			}


Attached Files
Elite Membership required to download: TMOv02.cs
Started this thread Reply With Quote
  #4 (permalink)
 elitecamper 
McAllen, TX
 
Experience: Advanced
Platform: Multicharts, Jigsaw
Broker: Ironbeam
Trading: ES, ZN, ZB, ZF
Posts: 106 since May 2013
Thanks Given: 108
Thanks Received: 138

The code should be,

 
Code
			_o[0] = Open[0];
			_c[0] = Close[0];
			
			for (int idx=0;idx<Length;idx++)//14
			{
				
				_data[idx] = _c[0] > _o[idx] ? 1 : _c[0] < _o[idx] ? -1 : 0;
				
			
			}

Try it.


Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #5 (permalink)
 elitecamper 
McAllen, TX
 
Experience: Advanced
Platform: Multicharts, Jigsaw
Broker: Ironbeam
Trading: ES, ZN, ZB, ZF
Posts: 106 since May 2013
Thanks Given: 108
Thanks Received: 138

I looked into it, and I don't think you need to add a series for the open and close after all. Here is a new one. I am uploading the code in cs file. If it doesn't work I will look into it later today.


Attached Files
Elite Membership required to download: TMOv01.cs
Visit my NexusFi Trade Journal Reply With Quote
  #6 (permalink)
 zeller4 
Orlando Florida
 
Experience: Intermediate
Platform: NT8
Trading: CL, NQ, ES, RTY
Posts: 478 since Jun 2009
Thanks Given: 1,419
Thanks Received: 404

EliteCamper,
Thanks for your input, I agree since I'm thinking Open and Close are already a series. In any case,
I played around with it some more. See code snippet and screenshot.

 
Code
                        _o[0] = Open[0];
			_c[0] = Close[0];
			
			for (int idx=0;idx<Length;idx++)//14
			{
				double s = 0;
//				_data[idx] = _c[0] > _o[idx] ? 1 : _c[0] < _o[idx] ? -1 : 0;
//				_data[idx] = _c[0] > _o[idx] ? (s+=1) : _c[0] < _o[idx] ? (s-=1) : 0;
				
				_data[0] = _c[0] > _o[idx] ? (s+=1) : _c[0] < _o[idx] ? (s-=1) : 0;
				
			
			}
Now I just have to work on the boundary since the TOS version plots toward 10 and -10. I think it has to do with the "with s" and "do s".
kz


Attached Thumbnails
Click image for larger version

Name:	CL 08-22 (233 Tick) 2022_06_28 (9_51_59 AM).png
Views:	177
Size:	69.6 KB
ID:	325530   Click image for larger version

Name:	Screenshot 2022-06-28 095713.png
Views:	193
Size:	74.2 KB
ID:	325531  
Started this thread Reply With Quote
  #7 (permalink)
 elitecamper 
McAllen, TX
 
Experience: Advanced
Platform: Multicharts, Jigsaw
Broker: Ironbeam
Trading: ES, ZN, ZB, ZF
Posts: 106 since May 2013
Thanks Given: 108
Thanks Received: 138

Zeller4, from my experience the boundaries are fixed plots, you plot a line a give it a fixed value. I don't think you will be needing "with" and "s" from thinkscript. Let me know how it goes.


Visit my NexusFi Trade Journal Reply With Quote
  #8 (permalink)
 zeller4 
Orlando Florida
 
Experience: Intermediate
Platform: NT8
Trading: CL, NQ, ES, RTY
Posts: 478 since Jun 2009
Thanks Given: 1,419
Thanks Received: 404

Moved the s out of the loop.

 
Code
double s = 0;
			
			for (int idx=0;idx<Length;idx++)//14
			{
				
//				_data[idx] = _c[0] > _o[idx] ? 1 : _c[0] < _o[idx] ? -1 : 0;
//				_data[idx] = _c[0] > _o[idx] ? (s+=1) : _c[0] < _o[idx] ? (s-=1) : 0;
				
				_data[0] = Close[0] > Open[idx] ? (s+=1) : Close[0] < Open[idx] ? (s-=1) : 0;
				
			
			}
Here's the result which matches closely. Now to make the draw region coloring.
Thanks for your help.


Attached Thumbnails
Click image for larger version

Name:	Screenshot 2022-06-28 104754.png
Views:	216
Size:	91.5 KB
ID:	325534  
Started this thread Reply With Quote
Thanked by:
  #9 (permalink)
river72f
Washington DC
 
Posts: 1 since Sep 2022
Thanks Given: 0
Thanks Received: 0

Could you please share the full code for NT8?


Reply With Quote
  #10 (permalink)
 hedgeplay 
Austin Texas / US
 
Experience: Intermediate
Frequency: Several times daily
Duration: Seconds
Posts: 219 since Dec 2019
Thanks Given: 162
Thanks Received: 243



zeller4 View Post
Hello,
I'm trying to make sense of the fold loop in TOS thinkscript and convert to Ninjascript.

TIA for any help and advice. Thanks.


@zeller4 and @elitecamper

Did you guys review this version?




Hedge


Reply With Quote
Thanked by:




Last Updated on June 9, 2023


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts