Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

StochRSI strategy help

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

    StochRSI strategy help

    Hi everyone,

    Beginner programming involved here.I'm using the strategy wizard to make a basic StochRSI strategy. My entries are working right when I want to enter above 0.75 or below 0.25 however when i'm in a position and it stochrsi crosses through those thresholds (0.25 < StochRsi < 0.75), it doesn't exit out. It prints to the output window for every if statement but doesn't actually exit out of positions to no position Please see code below. Thanks for any help, much appreciated.



    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    /// <summary>
    /// Nov 3/12
    /// </summary>
    [Description("Nov 3/12")]
    public class StochrsiStrat : Strategy
    {
    #region Variables
    // Wizard generated variables
    private int sharesinplay = 100; // Default setting for Sharesinplay
    private double longrsinum = 0.75; // Default setting for Longrsinum
    private double shortrsinum = 0.25; // Default setting for Shortrsinum
    private int noposition = 0; // Default setting for Noposition
    private int shortposition = 1; // Default setting for Shortposition
    private int longposition = 2; // Default setting for Longposition
    // User defined variables (add any user defined variables below)
    #endregion

    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {
    Add(StochRSI(14));
    Add(StochRSI(14));
    Add(StochRSI(14));
    Add(StochRSI(14));

    CalculateOnBarClose = false;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (Variable0 == Noposition
    && StochRSI(14)[0] > Longrsinum)
    {
    EnterLong(Sharesinplay, "Long");
    Variable0 = Longposition;
    PrintWithTimeStamp("IN Long");
    }

    // Condition set 2
    if (Variable0 == Noposition
    && StochRSI(14)[0] < Shortrsinum)
    {
    EnterShort(Sharesinplay, "Short");
    Variable0 = Shortposition;
    PrintWithTimeStamp("In Short");
    }

    // Condition set 3
    if (Variable0 == Longposition
    && CrossBelow(StochRSI(14), Longrsinum, 1))
    {
    ExitLong("", "Out");
    Variable0 = Noposition;
    PrintWithTimeStamp("Out");
    }

    // Condition set 4
    if (Variable0 == Shortposition
    && CrossAbove(StochRSI(14), Shortrsinum, 1))
    {
    ExitShort("", "Out");
    Variable0 = Noposition;
    PrintWithTimeStamp("Out");
    }
    }

    #region Properties
    [Description("")]
    [GridCategory("Parameters")]
    public int Sharesinplay
    {
    get { return sharesinplay; }
    set { sharesinplay = Math.Max(100, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public double Longrsinum
    {
    get { return longrsinum; }
    set { longrsinum = Math.Max(0.75, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public double Shortrsinum
    {
    get { return shortrsinum; }
    set { shortrsinum = Math.Max(0.25, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public int Noposition
    {
    get { return noposition; }
    set { noposition = Math.Max(0, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public int Shortposition
    {
    get { return shortposition; }
    set { shortposition = Math.Max(1, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public int Longposition
    {
    get { return longposition; }
    set { longposition = Math.Max(2, value); }
    }
    #endregion
    }
    }

    #2
    djoyce854, it looks like the signal name tagging is not correct for your exit codes, please ensure to set the FromEntrySignal string parameter to your Entry Signal names assigned (so the "Long" and "Short").

    Comment


      #3
      Thanks Bertrand! Ya I swithced it up using Position.MarketPosition == MarketPositon.Flat or something like that and It worked.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      657 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      373 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      109 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      574 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      579 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X