NexusFi: Find Your Edge


Home Menu

 





Creating a function to condense code


Discussion in NinjaTrader

Updated
    1. trending_up 4,919 views
    2. thumb_up 1 thanks given
    3. group 2 followers
    1. forum 10 posts
    2. attach_file 4 attachments




 
Search this Thread

Creating a function to condense code

  #1 (permalink)
 brightredmegaphone 
Brisbane, Australia
 
Experience: Advanced
Platform: Ninja, MT4
Trading: FX
Posts: 30 since Sep 2010
Thanks Given: 2
Thanks Received: 13

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

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
REcommedations for programming help
Sierra Chart
Quant vue
Trading Reviews and Vendors
How to apply profiles
Traders Hideout
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
MC PL editor upgrade
MultiCharts
 
  #2 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,454 since Jun 2009
Thanks Given: 33,227
Thanks Received: 101,645

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

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #3 (permalink)
 brightredmegaphone 
Brisbane, Australia
 
Experience: Advanced
Platform: Ninja, MT4
Trading: FX
Posts: 30 since Sep 2010
Thanks Given: 2
Thanks Received: 13


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
Elite Membership required to download: 5patt3profit.zip
Elite Membership required to download: ValidTimestoTrade.zip
Started this thread Reply With Quote
  #4 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,454 since Jun 2009
Thanks Given: 33,227
Thanks Received: 101,645


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

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #5 (permalink)
 brightredmegaphone 
Brisbane, Australia
 
Experience: Advanced
Platform: Ninja, MT4
Trading: FX
Posts: 30 since Sep 2010
Thanks Given: 2
Thanks Received: 13


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

Started this thread Reply With Quote
  #6 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,454 since Jun 2009
Thanks Given: 33,227
Thanks Received: 101,645

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.



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

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #7 (permalink)
 brightredmegaphone 
Brisbane, Australia
 
Experience: Advanced
Platform: Ninja, MT4
Trading: FX
Posts: 30 since Sep 2010
Thanks Given: 2
Thanks Received: 13

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

Started this thread Reply With Quote
  #8 (permalink)
 Xeno 
UK
 
Experience: Intermediate
Platform: Ninja
Broker: Mirus/Zen
Trading: Futures - bonds, currencies, index
Posts: 288 since Oct 2010
Thanks Given: 70
Thanks Received: 274

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)
 brightredmegaphone 
Brisbane, Australia
 
Experience: Advanced
Platform: Ninja, MT4
Trading: FX
Posts: 30 since Sep 2010
Thanks Given: 2
Thanks Received: 13

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
Elite Membership required to download: Swingtick5patt3profitlongshort (2).cs
Started this thread Reply With Quote
  #10 (permalink)
 brightredmegaphone 
Brisbane, Australia
 
Experience: Advanced
Platform: Ninja, MT4
Trading: FX
Posts: 30 since Sep 2010
Thanks Given: 2
Thanks Received: 13



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
Elite Membership required to download: Swingtick5patt3profitlongshortthree.cs
Started this thread Reply With Quote




Last Updated on December 16, 2010


© 2024 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 - Privacy Policy - Downloads - Top
no new posts