Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

use variable in a different strategy

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    use variable in a different strategy

    hi nt8 support, how do I make a variable available for use in another strategy. Thank you

    #2
    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.
    https://ninjatrader.com/support/help...eattribute.htm
    https://ninjatrader.com/support/help...eattribute.htm
    https://ninjatrader.com/support/help...nt8/update.htm
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      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


        #4
        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.
        https://ninjatrader.com/support/help...alues_that.htm


        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.
        https://ninjatrader.com/support/help...o_write_to.htm
        https://ninjatrader.com/support/help...o_read_fro.htm

        Or, using undocumented code with static classes information can be shared between strategies. As a heads up, that will break backtesting.
        https://ninjatrader.com/support/foru...245#post712245


        As a tip, to export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
        1. Click Tools -> Export -> NinjaScript...
        2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
        3. Click the 'Export' button
        4. Enter a unique name for the file in the value for 'File name:'
        5. Choose a save location -> click Save
        6. Click OK to clear the export location message
        By default your exported file will be in the following location:
        • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
        Below is a link to the help guide on Exporting NinjaScripts.
        http://ninjatrader.com/support/helpG...-us/export.htm
        Last 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 sinpeople, Today, 07:20 AM
        0 responses
        1 view
        0 likes
        Last Post sinpeople  
        Started by Frank1000, Today, 03:05 AM
        0 responses
        3 views
        0 likes
        Last Post Frank1000  
        Started by ntdev, Today, 01:14 AM
        4 responses
        16 views
        0 likes
        Last Post ntdev
        by ntdev
         
        Started by ct, Today, 02:22 AM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_Jason  
        Started by yodaler, Yesterday, 06:12 PM
        4 responses
        13 views
        0 likes
        Last Post NinjaTrader_Jason  
        Working...
        X