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 Mindset, 04-21-2026, 06:46 AM
    0 responses
    91 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    137 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    68 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    121 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    72 views
    0 likes
    Last Post PaulMohn  
    Working...
    X