Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need a time stop with a condition

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

    Need a time stop with a condition

    Hello Programmers,

    in a NinjaScript, where I am using SetTrailStop and SetProfitTarget, I want to implement this in working code:

    "if the trade has after 3 days not made a profit in minimum of 1 percent, then close the trade"

    Code:
    protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"";
                    Name                                        = "InsideDay";
                    Calculate                                    = Calculate.OnPriceChange;
                    EntriesPerDirection                            = 10000;
                    EntryHandling                                = EntryHandling.UniqueEntries;
                    IsExitOnSessionCloseStrategy                = false;
                    ExitOnSessionCloseSeconds                    = 0;
                    IsFillLimitOnTouch                            = false;
                    MaximumBarsLookBack                            = MaximumBarsLookBack.Infinite;
                    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;
                }
                else if (State == State.Configure)
                {
                    //SetStopLoss("", CalculationMode.Percent, 0.02, true);
    [B]               SetTrailStop("", CalculationMode.Percent, 0.01, true);
                    SetProfitTarget("", CalculationMode.Percent, 0.1);[/B]
                }
            }
    I am thinking, this condition has to implement in the marked area.

    Can someome please write me the code?
    Thanks a lot.

    Best regards,
    Rainbowtrader

    #2
    Hello Rainbowtrader,

    Thank you for the post.

    While I wont be able to write the code for you, I can provide some details on what you would need to do to achieve this.

    The stop and target can be set from OnStateChange as you have highlighted, however if you wanted to later close the trade based on a condign that needs to go in OnBarUpdate.

    For this situation, you would need to store the time of your entry so you can later check it. One way to do that may be like the following:

    Code:
    private DateTime storedOrderTime;
    private double entryPrice; 
    protected override void OnBarUpdate()
    {
    
        if (CrossAbove(smaFast, smaSlow, 1))
        {
                    storedOrderTime = Time[0];
                    entryPrice = Close[0];
                    EnterLong();
        }
    }
    This stores the bar time at the time of the condition and the Close at that time.

    To find the difference in time from the starting point, you can subtract the stored time from the current bar time to get a difference:

    Code:
    TimeSpan diff = Time[0] - storedOrderTime;


    Once a timespan is calculated, you could check if the timespan is greater than a specified amount. You could additionally check if the entry price is the increase or decrease you wanted.

    In your condition checking the timespan, you can then use an Exit order to close your position and become flat.



    I look forward to being of further assistance.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    119 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    62 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    40 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    45 views
    0 likes
    Last Post TheRealMorford  
    Started by Mindset, 02-28-2026, 06:16 AM
    0 responses
    82 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Working...
    X