NexusFi: Find Your Edge


Home Menu

 





User defined function


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one calhawk01 with 4 posts (0 thanks)
    2. looks_two iq200 with 1 posts (1 thanks)
    3. looks_3 ABCTG with 1 posts (1 thanks)
    4. looks_4 ratfink with 1 posts (1 thanks)
    1. trending_up 1,646 views
    2. thumb_up 3 thanks given
    3. group 4 followers
    1. forum 6 posts
    2. attach_file 0 attachments




 
Search this Thread

User defined function

  #1 (permalink)
 calhawk01 
baltimore marylnd
 
Experience: Beginner
Platform: ninja
Trading: es
Posts: 91 since May 2013
Thanks Given: 5
Thanks Received: 11

Hi guys, I had a block of code that I’ve used in many strategies. I created a user defined function today so that I can just call it in my strategies. This way, anytime I need to make changes to the function.. I can amend the function and it changes it in all my strategies. One of those functions is simple... calculating position size

 
Code
//			public static void AccountCalcualtion(SystemPerformance systemperformance, double AccountSize, double RiskPercent, double Leverage, double fxToBuy1, double total_fundUSD)
//			{
//			double CumProfits= (systemperformance.AllTrades.TradesPerformance.Currency.CumProfit);
//			total_fundUSD= (AccountSize+CumProfits);
//			double dummyQ = (((int)Math.Floor((total_fundUSD*RiskPercent))) * Leverage); 
//			fxToBuy1 =  ((int)(dummyQ/1000))*1000;
           // NinjaTrader.Code.Output.ProcessfxToBuy1), PrintTo.OutputTab1);
Then I am calling this function in my strategy as

AccountCalculation();

It works, it prints out the correct position size that my strategy would had calculated.

Problem: I can’t seem to assign the values from this function to a variable within my strategy so that I can use it.

I keep getting an error “can’t convert void to double”

I just want to take the value that this function is calculating and then assign it to a variable for the position to purchase.... any help would be appreciate

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
Trade idea based off three indicators.
Traders Hideout
MC PL editor upgrade
MultiCharts
Exit Strategy
NinjaTrader
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
26 thanks
Diary of a simple price action trader
26 thanks
Tao te Trade: way of the WLD
23 thanks
My NQ Trading Journal
16 thanks
HumbleTraders next chapter
9 thanks
  #2 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,433 since Apr 2013
Thanks Given: 481
Thanks Received: 1,627

Hi calhawk01,

using void as the return type for a method specifies that the method doesn't return a value. You could change it to double and have the method return a double value (via the return keyword) when called.
You can find more information here: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/methods

Regards,

ABCTG

Follow me on Twitter Reply With Quote
Thanked by:
  #3 (permalink)
 
ratfink's Avatar
 ratfink 
Birmingham UK
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
Posts: 3,633 since Dec 2012
Thanks Given: 17,423
Thanks Received: 8,425


@calhawk01

Use return parameter as @ABCTG described above, or prefix your fxToBuy1 parameter with the 'out' keyword, see following for info:

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/out-parameter-modifier


[Note that when using public static for shared functions it is best to use a naming prefix to avoid namespace clashes, and also that compiling will cause links to be broken until all charts/strats are F5'd or reopened. (I do use this mechanism a lot and would like to find a good solution to that problem.)]

Another alternative is to use the 'partial class' mechanism for instanced funtions.

Travel Well
Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #4 (permalink)
 calhawk01 
baltimore marylnd
 
Experience: Beginner
Platform: ninja
Trading: es
Posts: 91 since May 2013
Thanks Given: 5
Thanks Received: 11


ABCTG View Post
Hi calhawk01,

using void as the return type for a method specifies that the method doesn't return a value. You could change it to double and have the method return a double value (via the return keyword) when called.
You can find more information here: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/methods

Regards,

ABCTG

Thank you.

 
Code
namespace NinjaTrader.NinjaScript.Strategies
{
		public partial class Strategy
		{
			
			public static double AccountCalcualtion(SystemPerformance systemperformance, double AccountSize, double RiskPercent, double Leverage, double fxToBuy1, double total_fundUSD)
			{
			double CumProfits= (systemperformance.AllTrades.TradesPerformance.Currency.CumProfit);
			total_fundUSD= (AccountSize+CumProfits);
			double dummyQ = (((int)Math.Floor((total_fundUSD*RiskPercent))) * Leverage); 
			fxToBuy1 =  ((int)(dummyQ/1000))*1000;
           // NinjaTrader.Code.Output.ProcessfxToBuy1), PrintTo.OutputTab1);
				
		
			
		}

}
}
I had tried using double. However, NT won't compile with a double.

the error:

 
Code
NinjaScript File	Error	Code	Line	Column
StrategyFunctions.cs	'NinjaTrader.NinjaScript.Strategies.Strategy.AccountCalcualtion(NinjaTrader.Cbi.SystemPerformance, double, double, double, double, double)': not all code paths return a value	CS0161	46	25

Started this thread Reply With Quote
  #5 (permalink)
 calhawk01 
baltimore marylnd
 
Experience: Beginner
Platform: ninja
Trading: es
Posts: 91 since May 2013
Thanks Given: 5
Thanks Received: 11

Still trying to get this to work :/

I'm not a programmer so i think the problem is pretty basic but b/c i don't have the background i'm stuck. i've been working with ninjascript for about 4 years and am well verse in creating strategies... just never done anything like this. would appreciate if someone can help! i think once i get this to work i should be good to go with various other functions that i need to create..

Started this thread Reply With Quote
  #6 (permalink)
 iq200 
London, UK
 
Experience: Intermediate
Platform: Ninjatrader, Tradestation
Broker: Kinetick, InteractiveBrokers
Trading: Equities, Futures
Posts: 408 since Jun 2010
Thanks Given: 145
Thanks Received: 278

The error is due to you declaring return of type double but not returning any double value from your function so you need eg:

return fxToBuy1;


Just put a return and then whatever variable value you need to pass back in your function.


Sent from my iPhone using futures.io

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #7 (permalink)
 calhawk01 
baltimore marylnd
 
Experience: Beginner
Platform: ninja
Trading: es
Posts: 91 since May 2013
Thanks Given: 5
Thanks Received: 11


iq200 View Post
The error is due to you declaring return of type double but not returning any double value from your function so you need eg:

return fxToBuy1;


Just put a return and then whatever variable value you need to pass back in your function.


Sent from my iPhone using futures.io

Thank you, iq200.

I finally had the chance to try your suggestion. I'm good to go!

Working code:

 
Code
namespace NinjaTrader.NinjaScript.Strategies
{
		public partial class Strategy
		{
			
			public static double fx_Position_Size(SystemPerformance systemperformance, double AccountSize, double RiskPercent, double Leverage)
			
			{
			double Total_Profit = systemperformance.AllTrades.TradesPerformance.Currency.CumProfit;
			double total_fundUSD= AccountSize+Total_Profit;
			double dummyQ = (((int)Math.Floor((total_fundUSD*RiskPercent))) * Leverage); 
			double fxToBuy =  ((int)(dummyQ/1000))*1000;
			return fxToBuy;
			}					
}
}

Started this thread Reply With Quote




Last Updated on September 8, 2018


© 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