Creating a function to condense code - NinjaTrader Programming | futures io social day trading
futures io futures trading


Creating a function to condense code
Updated: Views / Replies:2,045 / 10
Created: by brightredmegaphone Attachments:4

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors Ė all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you donít need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

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

Reply
 4  
 
Thread Tools Search this Thread
 

Creating a function to condense code

  #1 (permalink)
Elite Member
Brisbane, Australia
 
Futures Experience: Advanced
Platform: Ninja, MT4
Favorite Futures: FX
 
Posts: 31 since Sep 2010
Thanks: 2 given, 13 received

Creating a function to condense code

How difficult is it to create a method for time series, that I can call?

Rather than have 20 condition sets, there must be a way of a) shortening and b) automating to some degree.

see attached as simple example:

if (Position.MarketPosition !=MarketPosition.Flat) return;
// Condition set 1 for short
if (High[1] > High[0]
&& Close[1] < hmav[2]
&& ToTime(Time[0]) > ToTime(1, 0, 0)
&& ToTime(Time[0]) < ToTime(8, 0, 0))
{
DrawDiamond("My diamond" + CurrentBar, false, 0, Low[0] + 400 * TickSize, Color.LimeGreen);
GoShort();
}

// Condition set 2 for short
if (High[1] > High[0]
&& Close[1] < hmav[2]
&& ToTime(Time[0]) > ToTime(9, 0, 0)
&& ToTime(Time[0]) < ToTime(21, 0, 0))
{
DrawDiamond("My diamond" + CurrentBar, false, 0, Low[0] + 400 * TickSize, Color.LimeGreen);
GoShort();
}

I want to condense the time conditions, as I may have 10 condition sets before even start to use time.

thanks

Reply With Quote
 
  #2 (permalink)
Site Administrator
Manta, Ecuador
 
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
 
Big Mike's Avatar
 
Posts: 46,238 since Jun 2009
Thanks: 29,350 given, 83,214 received

I moved your post to a new thread since it is a different subject.

What you want are functions. You can read about them in most any C# book for beginners.

 
Code
                            


void bool MyCondition1
(int val1int val2)
{

bool _retval false;

if 
val1 val2 then _retval true;

return 
_retval;

}

// onbarupdate

if (MyCondition(Close[0], Close[1]))
{
Print(
"True");

Typed freehand, but you get the idea.

Mike

Due to time constraints, please do not PM me if your question can be resolved or answered on the forum.

Need help?
1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first.
2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses.
3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make.
4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance.
5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers.
6)
Help using the forum? Watch this video to learn general tips on using the site.

If you want
to support our community, become an Elite Member.

Reply With Quote
The following user says Thank You to Big Mike for this post:
 
  #3 (permalink)
Elite Member
Brisbane, Australia
 
Futures Experience: Advanced
Platform: Ninja, MT4
Favorite Futures: FX
 
Posts: 31 since Sep 2010
Thanks: 2 given, 13 received


I put together the time validation as a separate strategy (ValidtimesforTrades) that I could modify simply & call in any other strategy.

It is is the attached 5patt strategy, line 122 but doesn't seem to call. Any clues?

Thanks

Attached Files
Register to download File Type: zip 5patt3profit.zip (9.2 KB, 8 views)
Register to download File Type: zip ValidTimestoTrade.zip (825 Bytes, 7 views)
Reply With Quote
 
  #4 (permalink)
Site Administrator
Manta, Ecuador
 
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
 
Big Mike's Avatar
 
Posts: 46,238 since Jun 2009
Thanks: 29,350 given, 83,214 received


brightredmegaphone View Post
I put together the time validation as a separate strategy (ValidtimesforTrades) that I could modify simply & call in any other strategy.

It is is the attached 5patt strategy, line 122 but doesn't seem to call. Any clues?

Thanks

I would suggest first getting the example I gave to work before separating the function out. I do not use NT any more, so can't test, but pretty sure you can't put the function in a remote strategy file, it needs to be in an indicator. But walk before you run, just keep it in the same strategy you are calling it from first.

Mike

Due to time constraints, please do not PM me if your question can be resolved or answered on the forum.

Need help?
1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first.
2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses.
3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make.
4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance.
5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers.
6)
Help using the forum? Watch this video to learn general tips on using the site.

If you want
to support our community, become an Elite Member.

Reply With Quote
 
  #5 (permalink)
Elite Member
Brisbane, Australia
 
Futures Experience: Advanced
Platform: Ninja, MT4
Favorite Futures: FX
 
Posts: 31 since Sep 2010
Thanks: 2 given, 13 received


Big Mike View Post
I moved your post to a new thread since it is a different subject.

What you want are functions. You can read about them in most any C# book for beginners.

 
Code
                            

void bool MyCondition1
(int val1int val2)
{

bool _retval false;

if 
val1 val2 then _retval true;

return 
_retval;

}

// onbarupdate

if (MyCondition(Close[0], Close[1]))
{
Print(
"True");

Typed freehand, but you get the idea.

Mike

Where do I put the 'void bool'function Mike?

protected override void Initialize()
{
CalculateOnBarClose = true;
EntryHandling = EntryHandling.UniqueEntries;
TraceOrders = true;
}


public bool ValidTimestoTrade (DateTime barTime)

{
//If the current bar times are equal to these, then call them in another strategy
if (ToTime(barTime) > ToTime(2,0,0) && ToTime(barTime) < ToTime (4,0,0))
if (ToTime(barTime) > ToTime(8,0,0) && ToTime(barTime) < ToTime (15,0,0))
if (ToTime(barTime) > ToTime(17,0,0) && ToTime(barTime) < ToTime (23,0,0))
return true;

return false;
}

thx

Reply With Quote
 
  #6 (permalink)
Site Administrator
Manta, Ecuador
 
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
 
Big Mike's Avatar
 
Posts: 46,238 since Jun 2009
Thanks: 29,350 given, 83,214 received

Look at this advanced Murrey Math strategy I posted, it has lots of functions in it being called from OnBarUpdate and should be plenty to make it clear.

https://futures.io/elite-group-trading-methods/5701-murrey-math-automated-strategy-ninjatrader.html

My C# is a bit rusty, haven't written anything for NT in a long time now. It is private bool, not void bool, a mistake on my part in my original post above (void meaning nothing to return, and here you are returning a bool).

Mike

Due to time constraints, please do not PM me if your question can be resolved or answered on the forum.

Need help?
1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first.
2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses.
3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make.
4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance.
5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers.
6)
Help using the forum? Watch this video to learn general tips on using the site.

If you want
to support our community, become an Elite Member.

Reply With Quote
 
  #7 (permalink)
Elite Member
Brisbane, Australia
 
Futures Experience: Advanced
Platform: Ninja, MT4
Favorite Futures: FX
 
Posts: 31 since Sep 2010
Thanks: 2 given, 13 received

Still can't seem to get this to call in the condition set; the initial idea was to put together a function where I could change times more simply.

the method is at the bottom of OnBarUpdate:

private bool TimestoTrade(DateTime barTime)
{
if (ToTime(barTime) > ToTime(2,0,0) && ToTime(barTime) < ToTime (8,0,0)

|| (ToTime(barTime) > ToTime(8,0,0) && ToTime(barTime) < ToTime (15,0,0))

|| (ToTime(barTime) > ToTime(17,0,0) && ToTime(barTime) < ToTime (23,0,0)))

return true;

else return false;

}

OnBarUpdate then reads:

protected override void OnBarUpdate()
{
EntryHandling = EntryHandling.UniqueEntries;
HMA hmav = HMA(hmaperiod);

ManageOrders();
{
if (Position.MarketPosition !=MarketPosition.Flat) return;

// Condition set 1 for short
if (High[1] > High[0]
&& Close[1] < hmav[2])
&& (TimestoTrade(Time[0])==true))
{
GoShort();
Print(Time[0]+"Time is (bartime)");
}
}
}

I did move the method up to initialise but no effect; I appreciate my limited coding skills, now I am stuck. Any further thoughts? The irony is this was originally taken from NT code base.

Thx

Reply With Quote
 
  #8 (permalink)
Elite Member
UK
 
Futures Experience: Intermediate
Platform: Ninja
Broker/Data: Mirus/Zen
Favorite Futures: Futures - bonds, currencies, index
 
Posts: 288 since Oct 2010
Thanks: 70 given, 267 received

You need to be a bit clearer


brightredmegaphone View Post
Still can't seem to get this to call in the condition set

What does this mean? What exactly doesn't work and what do you see?

 
Code
I did move the method up to initialise but no effect;
To Initialise? What inside it? just after it?

 
Code
the method is at the bottom of OnBarUpdate:
You mean contained in but before the end? Or after? The method, or the method call?

By the way, your ToTime proc can be much simplified by something like

int h = barTime.Hour

if (h > 1 && h < 8 etc etc

Reply With Quote
 
  #9 (permalink)
Elite Member
Brisbane, Australia
 
Futures Experience: Advanced
Platform: Ninja, MT4
Favorite Futures: FX
 
Posts: 31 since Sep 2010
Thanks: 2 given, 13 received

hi Xeno
I am trying to set up a simple function that allows me to trigger different times [hours] of the day to trade. Rather than add those times to each separate entry condition I have tried to set this up with a simple function.

I have now run a debugged version of the code, which prints out true/false as you would expect, but when I run it in the actual strategy the function no longer seems to work. So, the strategy and the function in isolation seem to work, but not together.

Debugged is attached.

Attached Files
Register to download File Type: cs Swingtick5patt3profitlongshort (2).cs (8.8 KB, 5 views)
Reply With Quote
 
  #10 (permalink)
Elite Member
Brisbane, Australia
 
Futures Experience: Advanced
Platform: Ninja, MT4
Favorite Futures: FX
 
Posts: 31 since Sep 2010
Thanks: 2 given, 13 received



brightredmegaphone View Post
hi Xeno
I am trying to set up a simple function that allows me to trigger different times [hours] of the day to trade. Rather than add those times to each separate entry condition I have tried to set this up with a simple function.

I have now run a debugged version of the code, which prints out true/false as you would expect, but when I run it in the actual strategy the function no longer seems to work. So, the strategy and the function in isolation seem to work, but not together.

Debugged is attached.

Then if I add back some conditions, function seems to stop functioning.

Attached Files
Register to download File Type: cs Swingtick5patt3profitlongshortthree.cs (8.8 KB, 8 views)
Reply With Quote

Reply



futures io > > > > > Creating a function to condense code

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

Linda Bradford Raschke: Reading The Tape

Elite only

Adam Grimes: TBA

Elite only

NinjaTrader: TBA

January

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
Ema2 Function in NinjaScript? srgtroy NinjaTrader Programming 3 June 29th, 2011 01:00 AM
Reference another study that's not a function vegasfoster Sierra Chart Programming 3 June 23rd, 2011 02:41 PM
using advance search function aquarian1 Traders Hideout 5 December 26th, 2010 04:47 PM
Losing Edit Function, looking for help! alex123 Traders Hideout 4 September 15th, 2009 08:32 PM


All times are GMT -4. The time now is 02:14 AM.

Copyright © 2017 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
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.
no new posts
Page generated 2017-12-11 in 0.14 seconds with 20 queries on phoenix via your IP 54.226.34.209