Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Passing parameters from Strategy to Indicator via Add()

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

    Passing parameters from Strategy to Indicator via Add()

    Hi,
    I am trying to add a custom indicator to a strategy and pass it some parameters derived from the strategy

    Unfortunately, when I run a print to Output of the value that the Indicator is receiving, the only value i get is 1.

    What am I doing wrong?

    Thanks.



    Strategy:-

    double param1;
    double param2;

    protected override void Initialize()
    {
    setParameters();

    Add(MyIndicator( param1, param2));
    }

    setParameters()
    {
    param1 = 0.9;
    param2 = 0.4;
    }

    Indicator:-

    public class MyIndicator : Indicator
    {
    #region Variables
    // Wizard generated variables
    private double param1 = 0.5; // Default setting for param1
    private double param2 = 0.5; // Default setting for param2
    // User defined variables (add any user defined variables below)
    double val;
    double lastDailyClose = 0.0;
    double LongStopIn;
    double ShortStopIn;
    #endregion

    /// <summary>
    /// This method is used to configure the indicator and is called once before any bar data is loaded.
    /// </summary>
    protected override void Initialize()
    {
    Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "LongEntry"));
    Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Line, "ShortEntry"));
    Overlay = true;
    Add(PeriodType.Day, 1);

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // 3 Days lookback
    if (CurrentBar < 4200) //700
    return;

    // OnBarUpdate() will be called on incoming tick events on all Bars objects added to the strategy
    // We only want to process events on our primary Bars object (main instrument) (index = 0) which
    // is set when adding the strategy to a chart
    if (BarsInProgress != 0)
    return;

    if (Bars.FirstBarOfSession)
    {
    val= calcVal();
    lastDailyClose = Closes[1][0];
    LongStopIn = lastDailyClose + (val*longStopEntry);
    ShortStopIn = lastDailyClose - (val*shortStopEntry);

    // Only ever see value 1.........
    string str = "INDICATOR------longStopEntry=" + longStopEntry;
    Print(str);
    str = "INDICATOR-------shortStopEntry=" + shortStopEntry;
    Print(str);
    }
    // Use this method for calculating your indicator values. Assign a value to each
    // plot below by replacing 'Close[0]' with your own formula.
    LongEntry.Set(LongStopIn);
    ShortEntry.Set(ShortStopIn);
    }

    #2
    abfc123, unfortunately strategies would not cross communicate to other scripts.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    46 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    28 views
    0 likes
    Last Post PaulMohn  
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    163 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    98 views
    1 like
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    158 views
    2 likes
    Last Post CaptainJack  
    Working...
    X