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
use variable in a different strategy
Collapse
X
-
use variable in a different strategy
hi nt8 support, how do I make a variable available for use in another strategy. Thank youTags: None
-
Hello Austiner87,
A hosted indicator can set a value to a public variable. If bar information is used, Update() will need to be called before the return. The Browsable(false) attribute may also be used so the property does not appear in the UI, and XmlIgnore() so the property is not saved with the workspace or template.
Chelsea B.NinjaTrader Customer Service
-
Hi ChelseaB,
Thank you! this was extremely helpful. I got it to work for one test by another strategy printing the TestSharedVariable from this one, and then I began getting an error and I tried a lot of things to fix it and can't. The error is "Strategy 'a1TestCreatesVariable': Error on calling 'OnBarUpdate' method on bar 1: Index was outside the bounds of the array."
My code is below (it is a very simple test code and should be extremely easy to fix) How am I supposed to fix it?
Thank you!
#region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Gui.Tools;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.Indicators;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion
//This namespace holds Strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies
{
public class a1TestCreatesVariable : Strategy
{
private double testSharedVariable;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "a1TestCreatesVariable";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 20;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
testSharedVariable = 0;
}
else if (State == State.Configure)
{
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 1)
return;
// Set 1
if (Close[0] != Open[0])
{
testSharedVariable = Convert.ToInt32(Close[0]);
Value[0] = Close[0];
}
}
public double TestSharedVariable
{
get
{
//call OnBarUpdate before returning variable
Update();
return testSharedVariable;
}
}
}
}
Comment
-
Hello Austiner87,
The public variable should be in an indicator not in a strategy. Strategies are not supported to be called from other scripts. Indicators can be called from other scripts.
Below is a link to the reference sample that demonstrates using Update() on a property in an indicator and making this property public to a host script.
If you are trying to share information between strategies, this is not officially supported.
However, it is possible to write to a file and have other strategy instances read from that file.
Or, using undocumented code with static classes information can be shared between strategies. As a heads up, that will break backtesting.
As a tip, to export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:- Click Tools -> Export -> NinjaScript...
- Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
- Click the 'Export' button
- Enter a unique name for the file in the value for 'File name:'
- Choose a save location -> click Save
- Click OK to clear the export location message
- (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
http://ninjatrader.com/support/helpG...-us/export.htmLast edited by NinjaTrader_ChelseaB; 11-09-2020, 09:57 AM.Chelsea B.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by kaywai, 09-01-2023, 08:44 PM
|
5 responses
601 views
0 likes
|
Last Post Today, 12:32 AM | ||
Started by xiinteractive, 04-09-2024, 08:08 AM
|
6 responses
22 views
0 likes
|
Last Post Today, 12:31 AM | ||
Started by Pattontje, Yesterday, 02:10 PM
|
2 responses
16 views
0 likes
|
Last Post
by Pattontje
Yesterday, 11:49 PM
|
||
Started by flybuzz, 04-21-2024, 04:07 PM
|
17 responses
230 views
0 likes
|
Last Post
by TradingLoss
Yesterday, 11:45 PM
|
||
Started by agclub, 04-21-2024, 08:57 PM
|
3 responses
17 views
0 likes
|
Last Post
by TradingLoss
Yesterday, 11:42 PM
|
Comment