How can I expose values from a custom indicator inside of a strategy?
Announcement
Collapse
Looking for a User App or Add-On built by the NinjaTrader community?
Visit NinjaTrader EcoSystem and our free User App Share!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less
Partner 728x90
Collapse
NinjaTrader
exposing values of Custom indicator in a strategy
Collapse
X
-
exposing values of Custom indicator in a strategy
Hi There
How can I expose values from a custom indicator inside of a strategy?Tags: None
-
Hello GKonheiser,
The value can be exposed from the indicator as a public variable.
However, a strategy cannot be called from another script. Should the strategy receive a value from an indicator, this can be used in the strategy but would not be something that could be called from a different script.
Exposing a value in the indicator can by done by creating a public variable that uses a get and set to return a private variable within the script.
Please take a look at the SMA indicator included with NinjaTrader.
Tools -> Edit NinjaScript -> Indicator... -> SMA -> OK
On line 27 the private variable is created for the period. On lines 61 to 67 this private variable period is exposed as a public variable.
The attribute tag [GridCategory("Parameters")] is optional and allows the variable to appear in the overloads as well as the Indicator window. When not used, the value is accessible from calling the script and value. IndicatorName().PublicVariableNameChelsea B.NinjaTrader Customer Service
- Likes 1
-
Hi Chelsea,
Ok so in my indicator I have a Get Set:-
Code:private double plus2SD; public double Plus2SD { get { return plus2SD; } set { plus2SD = value; } } private double minus2SD; public double Minus2SD { get { return minus2SD; } set { minus2SD = value; } }
Code:protected override void OnBarUpdate() { if( CurrentBars[0] < sDPeriod) return; avg = Average(averageBase, sDPeriod); sndDev = Deviation(deviationBase, sDPeriod); Values[0].Set( avg ); Values[1].Set( avg + 2*sndDev ); Values[2].Set( avg - 2*sndDev ); Values[3].Set( avg + 2.5*sndDev ); Values[4].Set( avg - 2.5*sndDev ); Values[5].Set( avg + 3*sndDev ); Values[6].Set( avg - 3*sndDev ); Values[7].Set( avg + 4*sndDev ); Values[8].Set( avg - 4*sndDev ); Values[9].Set( avg + 5*sndDev ); Values[10].Set( avg - 5*sndDev ); Values[11].Set( avg + 6*sndDev ); Values[12].Set( avg - 6*sndDev ); Values[13].Set( avg + 7*sndDev ); Values[14].Set( avg - 7*sndDev ); plus2SD = Values[1][0]; minus2SD = Values[2][0]; // Print("Date" + Time[0].Day + "/"+ Time[0].Month +" Time " + Time[0].Hour + " " + Time[0].Minute + " Current Bar 10 min " + CurrentBars[0] + " +2SD " + Values[1] + " -2SD " + Values[2]); }
I then access the value from my Strategy but it returns 0:
Code:Print("2SD from indic " + StDev(averageBase, deviationBase, sdPeriod).Plus2SD);
Comment
-
Originally posted by NinjaTrader_ChelseaB View PostHello GKonheiser,
The value can be exposed from the indicator as a public variable.
However, a strategy cannot be called from another script. Should the strategy receive a value from an indicator, this can be used in the strategy but would not be something that could be called from a different script.
Exposing a value in the indicator can by done by creating a public variable that uses a get and set to return a private variable within the script.
Please take a look at the SMA indicator included with NinjaTrader.
Tools -> Edit NinjaScript -> Indicator... -> SMA -> OK
On line 27 the private variable is created for the period. On lines 61 to 67 this private variable period is exposed as a public variable.
The attribute tag [GridCategory("Parameters")] is optional and allows the variable to appear in the overloads as well as the Indicator window. When not used, the value is accessible from calling the script and value. IndicatorName().PublicVariableName
Hi ChelseaB,
I created an Indicator script with custom button that allowed me to place an ATM order, with great help from Calonious. However, now I need to change the Stop and Target (2 each). Is there a way for me to do this inside indicator script? OR passing the new Stop levels, Target levels, atmStrategyID and orderID from indicator to strategy to do?
Comment
-
Hello chrishyewll,
In general, the Atm Strategy Template is where all settings for modifying orders should go.
If you are wanting to have custom control over the movements of protective orders, I would not recommend using an Atm Strategy. Instead, I would recommend submitting the protective orders and modifying them.
Below is a link to an example.
There is no documentation or support for this, but you might try getting a collection of the stop orders with <atmStrategy>.GetStopOrders() and try an account.Change() on the order.
Or you may try monitoring all orders on the <account>.OrderUpdate event (or looping through all of the <account>.Orders) and change working stop orders.Chelsea B.NinjaTrader Customer Service
Comment
-
Originally posted by NinjaTrader_ChelseaB View PostHello chrishyewll,
In general, the Atm Strategy Template is where all settings for modifying orders should go.
If you are wanting to have custom control over the movements of protective orders, I would not recommend using an Atm Strategy. Instead, I would recommend submitting the protective orders and modifying them.
Below is a link to an example.
There is no documentation or support for this, but you might try getting a collection of the stop orders with <atmStrategy>.GetStopOrders() and try an account.Change() on the order.
Or you may try monitoring all orders on the <account>.OrderUpdate event (or looping through all of the <account>.Orders) and change working stop orders.
Thanks for the reference above. I forgot to mention that I am using NT7 while the example in the link is using NT8. Do you have NT7 example?
Thanks ahead.
Comment
-
Hello chrishyewll,
I overlooked this is an NT7 thread.
Unfortunately no, I don't have any suggestions for NinjaTrader 7.
With NT7 placing orders or using the account object outside of a NinjaScript Strategy in any other type of script is far outside of the area of NS support.
This thread will remain open for any community members that would like to assist.
You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our business development follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request.Chelsea B.NinjaTrader Customer Service
Comment
-
Originally posted by NinjaTrader_ChelseaB View PostHello chrishyewll,
I overlooked this is an NT7 thread.
Unfortunately no, I don't have any suggestions for NinjaTrader 7.
With NT7 placing orders or using the account object outside of a NinjaScript Strategy in any other type of script is far outside of the area of NS support.
This thread will remain open for any community members that would like to assist.
You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our business development follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request.
Thanks for you infor. I managed to find ways to go around it by creating a few different template with different preset stoploss and targets. I use script to decide which template to use so that it won't immediately get stopped out the moment I enter the market. Thanks!
Comment
-
Hello nothingbutprofits,
There is now a code sample in the NinjaTrader 8 help guide under Update().
https://ninjatrader.com/support/help...nt8/update.htm
Please let me know if you need this example copied and pasted into an indicator.Chelsea B.NinjaTrader Customer Service
Comment
-
Originally posted by NinjaTrader_ChelseaB View PostHello nothingbutprofits,
There is now a code sample in the NinjaTrader 8 help guide under Update().
https://ninjatrader.com/support/help...nt8/update.htm
Please let me know if you need this example copied and pasted into an indicator.
No overload for method 'Indicator' takes 0 arguments
Code://Strategy //------------------- protected override void OnBarUpdate() { base.OnBarUpdate(); Print("Indicator tripleValue: " + Indicator().tripleValue.ToString() ); //error message here..."No overload for method 'Indicator' takes 0 arguments" if (CurrentBars[0] < BarsRequiredToTrade) return; }
Comment
-
Hello nothingbutprofits,
Calling Indicator() would be the issue. You would need to actually use your indicators name. There are no indicators called Indicator (as Indicator is a base class not an actual indicator).
Attached is the example from the help guide copied into a script (ExposedValIndiTest) and a host script (HostExposedValIndiTest) that calls it.Attached FilesChelsea B.NinjaTrader Customer Service
Comment
-
Originally posted by NinjaTrader_ChelseaB View PostHello nothingbutprofits,
Attached is the example from the help guide copied into a script (ExposedValIndiTest) and a host script (HostExposedValIndiTest) that calls it.
Both HostExposedValIndiTest and ExposedValIndiTest indicators are working well.
Now for this step I'm trying to make it work:
I added ExposedValIndiTest to NT8, and then copied Print("ExposedValIndiTest().TripleValue: " + ExposedValIndiTest().TripleValue ); to my strategy.
MACDCustom Indicator:
============================
Code:protected override void OnBarUpdate() { tripleValue = SMA(20)[0] * 3;//added here for testing to be read by strategy if (CurrentBar == 0) { fastEma[0] = Input[0]; slowEma[0] = Input[0]; macdSeries[0] = 0; macdAverage[0] = 0; return; } // Calculate MACD first CalculateMACD(); UpdateBarsLines(); UpdateBackgroundPanelColors(); CrossingMarker(); UpdateDirection(); UpdateMACDPosition(); } //added for testing on allowing for tripleValue variable to read by strategy on every OnBarUpdate() public double TripleValue { get { //call OnBarUpdate before returning tripleValue Update(); return tripleValue; } }
AutoStrategy Strategy:
============================
Code:protected override void OnBarUpdate() { base.OnBarUpdate(); Print("ExposedValIndiTest().TripleValue: " + ExposedValIndiTest().TripleValue ); // <- this one works fine. Print("MACDCustom().TripleValue: " + MACDCustom().TripleValue );// <- this one has "No overload for method 'MACDCustom' takes 0 arguments." error. if (CurrentBars[0] < BarsRequiredToTrade) return; if (MACDCustom(........) EnterLong(); }
Comment
-
Hello nothingbutprofits,
Are there other public variables (possibly using the NinjaScriptProperty attribute) within the scope of the MACDCustom indicator class?
Overloads are how you would call the indicator based on the public properties using the NinjaScriptProperty attribute.
Below is a link to the help guide on NinjaScriptProperty Attribute.
https://ninjatrader.com/support/help...yattribute.htm
Like SMA(14), this requires a Period overload parameter (declared publicly within the SMA class and uses the NinjaScriptProperty attribute).
https://ninjatrader.com/support/help...simple_sma.htm
MACD(2,14,3), this requires a Fast, Slow, and Smooth overload parameters (also public using NinjaScriptProperty).
https://ninjatrader.com/support/help...gence_macd.htmLast edited by NinjaTrader_ChelseaB; 10-05-2021, 02:13 PM.Chelsea B.NinjaTrader Customer Service
- Likes 1
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by AttiM, 02-14-2024, 05:20 PM
|
12 responses
213 views
0 likes
|
Last Post
by DrakeiJosh
Today, 11:17 AM
|
||
Started by cre8able, 02-11-2023, 05:43 PM
|
3 responses
237 views
0 likes
|
Last Post
by rhubear
Today, 10:46 AM
|
||
Started by frslvr, 04-11-2024, 07:26 AM
|
8 responses
117 views
1 like
|
Last Post Today, 10:30 AM | ||
Started by stafe, 04-15-2024, 08:34 PM
|
10 responses
47 views
0 likes
|
Last Post
by stafe
Today, 10:29 AM
|
||
Started by rocketman7, Today, 09:41 AM
|
3 responses
12 views
0 likes
|
Last Post Today, 10:07 AM |
Comment