Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.
    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by maybeimnotrader, Yesterday, 05:46 PM
    2 responses
    20 views
    0 likes
    Last Post maybeimnotrader  
    Started by adeelshahzad, Today, 03:54 AM
    5 responses
    32 views
    0 likes
    Last Post NinjaTrader_BrandonH  
    Started by stafe, 04-15-2024, 08:34 PM
    7 responses
    32 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by merzo, 06-25-2023, 02:19 AM
    10 responses
    823 views
    1 like
    Last Post NinjaTrader_ChristopherJ  
    Started by frankthearm, Today, 09:08 AM
    5 responses
    22 views
    0 likes
    Last Post NinjaTrader_Clayton  
    Working...
    X