Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Close-triggered stop (instead of touch triggered)

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

    Close-triggered stop (instead of touch triggered)

    So I'm trying to figure out how to code something that seems like it should be really easy, but can't figure it out. I'm trying to have a static stop (or any stop really) fire if the candle closes below the stop rather than simply touches the stop. I can't seem to get a variable EntryPrice to work nicely with the rest of the code.

    The desired trigger is:
    close[1] < EntryPrice * StopPrice. I've attached code. What am I doing wrong here? Bear in mind I'm not a programmer. I can get by in thinkscript, but C# is well beyond my skill set. Most of the code was generated by the wizard. Thanks in advance!

    HTML Code:
    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 RSI2PerLongCrossover : Strategy
    {
    private RSI RSI1;
    private double entryPrice;
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"2 Period RSI Crossover Trade";
    Name = "RSI2PerLongCrossover";
    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;
    RSI_OS = 10;
    RSI_OB = 85;
    RSI_Len = 2;
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    RSI1 = RSI(Close, Convert.ToInt32(RSI_Len), 3);
    }
    }
    
    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;
    
    if (CurrentBars[0] < 1)
    return;
    
    // Set 1
    if (CrossBelow(RSI1.Default, RSI_OS, 1))
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), @"RSICrossoverLongEntry");
    }
    
    // Set 2
    if ((CrossAbove(RSI1.Default, RSI_OB, 1)))
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), @"RSICrossoverLongExit", "");
    }
    
    //Set 3
    if (Close[0] <= entryPrice * 0.999)
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), @"RSICrossoverLongStop", "");
    }
    }
    
    region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="RSI_OS", Order=1, GroupName="Parameters")]
    public int RSI_OS
    { get; set; }
    
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="RSI_OB", Order=2, GroupName="Parameters")]
    public int RSI_OB
    { get; set; }
    
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="RSI_Len", Order=3, GroupName="Parameters")]
    public int RSI_Len
    { get; set; }
    #endregion
    
    }
    }

    Edit: Figured it out. EntryPrice isn't a valid function, or variable, or method, or call, or God knows whatever it is, but Position.AveragePrice is!
    Last edited by MW1983; 06-19-2024, 09:01 AM.

    #2
    Hello MW1983,

    Thank you for your post.

    To clarify, are you referring to implementing an actual stop loss, like using SetStopLoss() or ExitLongStopMarket()?

    Or are you just looking to exit via ExitLong() when the just closed bar has closed below a specified price?

    I look forward to your response.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    110 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    59 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    37 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    41 views
    0 likes
    Last Post TheRealMorford  
    Started by Mindset, 02-28-2026, 06:16 AM
    0 responses
    78 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Working...
    X