Trading Articles
Article Categories
Article Tools
Higher high since event?
Updated March 13, 2010
trending_up
3,323 views
thumb_up
1 thanks given
group
0 followers
forum
10 posts
attach_file
1 attachments
Welcome to futures io: the largest futures trading community on the planet, with well over 125,000 members
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to
register in order to view the content of the threads and start contributing to our community.
It's free and simple.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
(login for full post details)
#1 (permalink )
Asia
Experience: Beginner
Platform: NinjaTrader, TOS
Posts: 798 since Jun 2009
Thanks: 109 given,
799
received
Hi all,
I'm trying to code something in Ninja based on a paper I read. It's all straight forward to me except for one rule:
If the Close is above the MA, that is the setup bar. The next close above the MA that is higher than the close of that first setup bar is your long entry point.
Sounds simple but I'm scratching my head on this one. My fear is that it needs something like the ZigZag code.
Am I missing a simple way to do this?
Thanks!
Best Threads (Most Thanked) in the last 7 days on futures io
(login for full post details)
#3 (permalink )
Site Administrator Swing Trader Data Scientist & DevOps
Manta, Ecuador
Experience: Advanced
Platform: My own custom solution
Trading: Emini Futures
Posts: 49,745 since Jun 2009
Thanks: 32,292 given,
97,494
received
MXASJ
Hi all,
I'm trying to code something in Ninja based on a paper I read. It's all straight forward to me except for one rule:
If the Close is above the MA, that is the setup bar. The next close above the MA that is higher than the close of that first setup bar is your long entry point.
Sounds simple but I'm
scratching my head on this one. My fear is that it needs something like the ZigZag code.
Am I missing a simple way to do this?
Thanks!
Perhaps just a flag would do it
Code
#var private int flagbar ; #onbarupdate if ( Close [ 0 ] > ma [ 0 ] and flagbar == 0 ) flagbar = CurrentBar ; else flagbar = 0 ; if ( flagbar > 0 && Close [ 0 ] > Close [ CurrentBar - flagbar ] and Close [ 0 ] > ma [ 0 ]) DoYourThing ();
You'll need to reset flagbar back to 0 somewhere too, like once in a position or once a setup fails, whatever...
Mike
The following user says Thank You to Big Mike for this post:
(login for full post details)
#4 (permalink )
Asia
Experience: Beginner
Platform: NinjaTrader, TOS
Posts: 798 since Jun 2009
Thanks: 109 given,
799
received
Thanks Mike!
I think I'm lost at the reset thing. This is where I am now:
Code
private void HhLl () { if ( Close [ 0 ] > EMA ( fastMA )[ 0 ] && Close [ 0 ] > EMA ( slowMA )[ 0 ] && flagbarlong == 0 ) flagbarlong = CurrentBar ; else { flagbarlong = 0 ; } if ( Close [ 0 ] < EMA ( fastMA )[ 0 ] && Close [ 0 ] < EMA ( slowMA )[ 0 ] && flagbarshort == 0 ) flagbarshort = CurrentBar ; else { flagbarshort = 0 ; } } protected override void OnBarUpdate () ///////////////////////////////////////////////////////////////// { HhLl (); if ( Close [ 0 ] > EMA ( fastMA )[ 0 ] && Close [ 0 ] > EMA ( slowMA )[ 0 ] && flagbarlong > 0 ) GoLong (); if ( Close [ 0 ] < EMA ( fastMA )[ 0 ] && Close [ 0 ] < EMA ( slowMA )[ 0 ] && flagbarshort > 0 ) GoShort ();
If I get a close above/below the the two MAs, it will enter on the next bar even if that bar below/above the two MAs. Ideas?
(login for full post details)
#5 (permalink )
Site Administrator Swing Trader Data Scientist & DevOps
Manta, Ecuador
Experience: Advanced
Platform: My own custom solution
Trading: Emini Futures
Posts: 49,745 since Jun 2009
Thanks: 32,292 given,
97,494
received
MXASJ
Thanks Mike!
I think I'm lost at the reset thing. This is where I am now:
Code
private void HhLl () { if ( Close [ 0 ] > EMA ( fastMA )[ 0 ] && Close [ 0 ] > EMA ( slowMA )[ 0 ] && flagbarlong == 0 ) flagbarlong = CurrentBar ; else { flagbarlong = 0 ; } if ( Close [ 0 ] < EMA ( fastMA )[ 0 ] && Close [ 0 ] < EMA ( slowMA )[ 0 ] && flagbarshort == 0 ) flagbarshort = CurrentBar ; else { flagbarshort = 0 ; } } protected override void OnBarUpdate () ///////////////////////////////////////////////////////////////// { HhLl (); if ( Close [ 0 ] > EMA ( fastMA )[ 0 ] && Close [ 0 ] > EMA ( slowMA )[ 0 ] && flagbarlong > 0 ) GoLong (); if ( Close [ 0 ] < EMA ( fastMA )[ 0 ] && Close [ 0 ] < EMA ( slowMA )[ 0 ] && flagbarshort > 0 ) GoShort ();
If I get a close above/below the the two MAs, it will enter on the next bar even if that bar below/above the two MAs. Ideas?
Yes, you must remember:
Code
Close [ 0 ] > Close [ CurrentBar - flagbar ]
</span></span>In other words, remember to compare to [CurrentBar - flagbar], so we look xx bars back for that closing price. You are looking at 'now' (0).
As for reset, the thing is that if flagbar != 0 then the condition will never again reset the flagbar itself. So if it happened on bar 500, then it would never happen again past bar 500.
What you need to do is create a situation where flagbar is reset to 0 so it can start evaluating again. That could either be a failure of some sort, or it could be taking a position. If you only do it when you take a position just keep in mind there could be a bunch of bars between the flag bar and the position bar, so much so it could be irrelevant. I would probably recommend something like a 'max conditional bars' reference, something like:
Code
if ( flagbar + 10 > CurrentBar ) flagbar = 0 ;
This means that if 10 bars passed since the flagbar, we'll give up and start looking for a new setup.
Mike
(login for full post details)
#6 (permalink )
Asia
Experience: Beginner
Platform: NinjaTrader, TOS
Posts: 798 since Jun 2009
Thanks: 109 given,
799
received
I'm still scratching my head on this one. Tried a few different variants, and the only thing that comes close is neither elegant nor 100% correct in following the original trading rule. Here is where I am. Commented out code is various things I tried in various combinations. FWIW I'm trying to code and reproduce the the results of the low-frequency macro strategy outlined by Klienman in his 2005 book "Trading Commodities and Financial Futures." I'll post it in the Elite methods section if I can reproduce his results.
Code
private void HhLl () { if ( Close [ 0 ] > EMA ( fastMA )[ 0 ] && Close [ 0 ] > EMA ( slowMA )[ 0 ] && flagbarlong == 0 ) flagbarlong = CurrentBar ; //if (flagbarlong + 10 > CurrentBar) flagbarlong = 0; else { flagbarlong = 0 ; } if ( Close [ 0 ] < EMA ( fastMA )[ 0 ] && Close [ 0 ] < EMA ( slowMA )[ 0 ] && flagbarshort == 0 ) flagbarshort = CurrentBar ; //if (flagbarshort + 10 > CurrentBar) flagbarshort = 0; else { flagbarshort = 0 ; } } protected override void OnBarUpdate () ///////////////////////////////////////////////////////////////// { //HhLl(); if ( Close [ 0 ] > EMA ( fastMA )[ 0 ] && Close [ 0 ] > EMA ( slowMA )[ 0 ] && Close [ 1 ] > EMA ( fastMA )[ 1 ] && Close [ 1 ] > EMA ( slowMA )[ 1 ] //&& Close[2] > EMA(fastMA)[2] && Close[2] > EMA(slowMA)[2] && Close [ 0 ] > Close [ 1 ] // || Close[0] > Close [2] //&& Close[0] > Close[CurrentBar-flagbarlong] //&& flagbarlong > 0 ) GoLong (); if ( Close [ 0 ] < EMA ( fastMA )[ 0 ] && Close [ 0 ] < EMA ( slowMA )[ 0 ] && Close [ 1 ] < EMA ( fastMA )[ 1 ] && Close [ 1 ] < EMA ( slowMA )[ 1 ] //&& Close[2] < EMA(fastMA)[2] && Close[2] < EMA(slowMA)[2] && Close [ 0 ] < Close [ 1 ] // || Close[0] < Close [2] //&& Close[0] < Close[CurrentBar-flagbarshort] //&& flagbarshort > 0 ) GoShort ();
(login for full post details)
#7 (permalink )
Site Administrator Swing Trader Data Scientist & DevOps
Manta, Ecuador
Experience: Advanced
Platform: My own custom solution
Trading: Emini Futures
Posts: 49,745 since Jun 2009
Thanks: 32,292 given,
97,494
received
You still aren't checking for a higher close against the flagbar.
Code
if ( Close [ 0 ] > EMA ( fastMA )[ 0 ] && Close [ 0 ] > EMA ( slowMA )[ 0 ] && Close [ 1 ] > EMA ( fastMA )[ 1 ] && Close [ 1 ] > EMA ( slowMA )[ 1 ] && Close [ 0 ] > Close [ 1 ] ) GoLong ();
There is nothing there for the flagbar, so it's like the flagbar doesn't exist.
I don't understand the rules you need exactly, so I will keep it simple:
Code
if ( Close [ 0 ] > Close [ CurrentBar - flagbar ] && flagbar > 0 ) GoLong ();
This is the same thing I've said before Let me know if it doesn't work. Let me make it clear --- Close[CurrentBar - flagbar ] .... very important...
Mike
(login for full post details)
#8 (permalink )
Asia
Experience: Beginner
Platform: NinjaTrader, TOS
Posts: 798 since Jun 2009
Thanks: 109 given,
799
received
Mike thanks for your input. I'm feeling very thick as I can't get this to trade. Feel free to give up on me, but this is where it stands:
Code
private void HhLl () { if ( Close [ 0 ] > EMA ( fastMA )[ 0 ] && Close [ 0 ] > EMA ( slowMA )[ 0 ] && flagbarlong == 0 ) flagbarlong = CurrentBar ; //if (flagbarlong + 10 > CurrentBar) flagbarlong = 0; else flagbarlong = 0 ; if ( Close [ 0 ] < EMA ( fastMA )[ 0 ] && Close [ 0 ] < EMA ( slowMA )[ 0 ] && flagbarshort == 0 ) flagbarshort = CurrentBar ; //if (flagbarshort + 10 > CurrentBar) flagbarshort = 0; else flagbarshort = 0 ; if ( flagbarlong + 10 > CurrentBar ) flagbarlong = 0 ; if ( flagbarshort + 10 > CurrentBar ) flagbarshort = 0 ; } protected override void OnBarUpdate () ///////////////////////////////////////////////////////////////// { HhLl (); if ( //Close[0] > EMA(fastMA)[0] && Close[0] > EMA(slowMA)[0] //&& Close[1] > EMA(fastMA)[1] && Close[1] > EMA(slowMA)[1] //&& Close [ 0 ] > Close [ CurrentBar - flagbarlong ] && flagbarlong > 0 ) GoLong (); if ( //Close[0] < EMA(fastMA)[0] && Close[0] < EMA(slowMA)[0] //&& Close[1] < EMA(fastMA)[1] && Close[1] < EMA(slowMA)[1] //&& Close [ 0 ] < Close [ CurrentBar - flagbarshort ] && flagbarshort > 0 ) GoShort (); }
The trade logic is as follows:
Code
public class Kleinman : Strategy //Notes: Klienman refers to this strategy in Chapter 10. It has two basic rules: //Using daily charts, a close above the 23 and 30 day SMA is the long set-up signal. If the market excedes the //high of the set-up bar, enter long. Opposite for shorts. The example in the book is "always in"... you are either //long or short the market. There is a suggestion that identifying range-bound markets and sitting on the sidelines will reduce losses. //Revisions:
It's that "exceding the high of the setup bar" while it's still above/below the SMA that is giving me the agro. The closest I get using more brute-force coding (whats been commented out above) makes me want to pursue getting this right as it has some interesting results in backtesting .
(login for full post details)
#9 (permalink )
Site Administrator Swing Trader Data Scientist & DevOps
Manta, Ecuador
Experience: Advanced
Platform: My own custom solution
Trading: Emini Futures
Posts: 49,745 since Jun 2009
Thanks: 32,292 given,
97,494
received
MX,
The code you provided is not taking trades?
If so, please add some Print(Time[0] + " (" + CurrentBar + "): flagbar = " + flagbarlong) code and paste results.
Mike
(login for full post details)
#10 (permalink )
Asia
Experience: Beginner
Platform: NinjaTrader, TOS
Posts: 798 since Jun 2009
Thanks: 109 given,
799
received
OK. Since it is not taking trades I commented/uncommented out some code so it would take trades and Print() accordingly, as follows:
Code
private void HhLl () { if ( Close [ 0 ] > EMA ( fastMA )[ 0 ] && Close [ 0 ] > EMA ( slowMA )[ 0 ] && flagbarlong == 0 ) flagbarlong = CurrentBar ; //if (flagbarlong + 10 > CurrentBar) flagbarlong = 0; else flagbarlong = 0 ; if ( Close [ 0 ] < EMA ( fastMA )[ 0 ] && Close [ 0 ] < EMA ( slowMA )[ 0 ] && flagbarshort == 0 ) flagbarshort = CurrentBar ; //if (flagbarshort + 10 > CurrentBar) flagbarshort = 0; else flagbarshort = 0 ; if ( flagbarlong + 10 > CurrentBar ) flagbarlong = 0 ; if ( flagbarshort + 10 > CurrentBar ) flagbarshort = 0 ; } protected override void OnBarUpdate () ///////////////////////////////////////////////////////////////// { HhLl (); if ( Close [ 0 ] > EMA ( fastMA )[ 0 ] && Close [ 0 ] > EMA ( slowMA )[ 0 ] && Close [ 1 ] > EMA ( fastMA )[ 1 ] && Close [ 1 ] > EMA ( slowMA )[ 1 ] //&& //Close[0] > Close[CurrentBar-flagbarlong] && flagbarlong > 0 ) GoLong (); Print( Time [ 0 ] + " (" + CurrentBar + "): flagbar = " + flagbarlong ); if ( Close [ 0 ] < EMA ( fastMA )[ 0 ] && Close [ 0 ] < EMA ( slowMA )[ 0 ] && Close [ 1 ] < EMA ( fastMA )[ 1 ] && Close [ 1 ] < EMA ( slowMA )[ 1 ] //&& //Close[0] < Close[CurrentBar-flagbarshort] && flagbarshort > 0 ) GoShort (); Print( Time [ 0 ] + " (" + CurrentBar + "): flagbar = " + flagbarshort ); }
All of the print statements in the look like this (but with different date and time stamps:
2010-03-12 04:00:00 (695): flagbar = 0
None of the printed flagbar values != 0. The first one was (20) and the last (695).
(login for full post details)
#11 (permalink )
Site Administrator Swing Trader Data Scientist & DevOps
Manta, Ecuador
Experience: Advanced
Platform: My own custom solution
Trading: Emini Futures
Posts: 49,745 since Jun 2009
Thanks: 32,292 given,
97,494
received
Ok so the problem is here
Code
if ( Close [ 0 ] > EMA ( fastMA )[ 0 ] && Close [ 0 ] > EMA ( slowMA )[ 0 ] && flagbarlong == 0 ) flagbarlong = CurrentBar ; //if (flagbarlong + 10 > CurrentBar) flagbarlong = 0; else flagbarlong = 0 ;
</span></span>
That is not evaluating to true, ever.
When you defined flagbarlong in #var , you defined it as an int and set it to 0 yes?
Try removing the && flagbarlong == 0 from above, just to make sure the condition starts working.
Sorry I can't just run the code on my side, but I haven't even opened NinjaTrader in 2 weeks and it feels good
Mike
Last Updated on March 13, 2010
Right now
Ongoing
Right now
February
Coming soon
March