Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Order: Keep alive for x number of bars?

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

    Order: Keep alive for x number of bars?

    i have create a strategy but i want to keep the order x number of bars aktive.


    iam not i coder so i dont have find any solutiont that i can implement here in the forum.

    if somebody can help me to make it that would be very nice.

    here is the code ;


    public class BBBands : Strategy
    {
    private NinjaTrader.NinjaScript.Indicators.new.BBbands BBBands1;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "BBBands";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlatSynchronizeAccount;
    TimeInForce = TimeInForce.Day;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.IgnoreAllErrors;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    Target = 30;
    Stop = 15;
    BBHalf = 66;
    BBDevi = 2.8;
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    BBbands = BBbands(Close, Convert.ToInt32(BBHalf), NinjaTrader.Data.PriceType.close, BBdevi, true);
    BBbands1.Plots[0].Brush = Brushes.Red;
    BBbands1.Plots[1].Brush = Brushes.DarkGray;
    BBbands1.Plots[2].Brush = Brushes.Lime;
    BBbands1.Plots[3].Brush = Brushes.Red;
    BBbands1.Plots[4].Brush = Brushes.Lime;
    AddChartIndicator(BBbands1);
    SetStopLoss(CalculationMode.Ticks, Stop);
    SetProfitTarget("", CalculationMode.Ticks, Target);
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1)
    return;

    // Set 1
    if ((Position.MarketPosition == MarketPosition.Flat)
    && (BBbands1.BullishMarker[0] != 0))
    {
    ExitShort(Convert.ToInt32(DefaultQuantity), "", "");
    BackBrush = Brushes.Lime;
    EnterLong(Convert.ToInt32(DefaultQuantity), "");
    }

    // Set 2
    if ((Position.MarketPosition == MarketPosition.Flat)
    && (BBbands1.BearishMarker[0] != 0))
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
    BackBrush = Brushes.Crimson;
    EnterShort(Convert.ToInt32(DefaultQuantity), "");
    }

    // Set 3
    if ((Position.MarketPosition == MarketPosition.Short)
    && (BBbands1.BullishMarker[0] != 0))
    {
    ExitShort(Convert.ToInt32(DefaultQuantity), "", "");
    BackBrush = Brushes.Lime;
    EnterLong(Convert.ToInt32(DefaultQuantity), "");
    }

    // Set 4
    if ((Position.MarketPosition == MarketPosition.Long)
    && (BBbands1.BearishMarker[0] != 0))
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
    BackBrush = Brushes.Crimson;
    EnterShort(Convert.ToInt32(DefaultQuantity), "");
    }

    }

    region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="Target", Order=1, GroupName="Parameters")]
    public int Target
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="Stop", Order=2, GroupName="Parameters")]
    public int Stop
    { get; set; }

    [NinjaScriptProperty]
    [Range(0, int.MaxValue)]
    [Display(Name="BBHalf", Description="BBbands Einstellung 1", Order=3, GroupName="Parameters")]
    public int BBHalf
    { get; set; }

    [NinjaScriptProperty]
    [Range(0, double.MaxValue)]
    [Display(Name="BBdevi", Description="BBbands Einstellung 2", Order=4, GroupName="Parameters")]
    public double BBdevi
    { get; set; }
    #endregion

    }
    }​

    #2
    Hello nadire.sadiki60,

    Thanks for your post.

    First, I see you are calling Exit methods and Entry methods in the same Set in your strategy. Entry methods and Exit methods should not be called within the same set. To reverse the position of an order, you could simply call the Entry method in the opposite direction as stated on the Managed Approach help guide page below.

    "Entry() methods will reverse the position automatically. For example if you are in a 1 contract long position and now call EnterShort() -> you will see 2 executions, one to close the prior long position and the other to get you into the desired 1 contract short position."

    Managed Approach: https://ninjatrader.com/support/help...d_approach.htm

    Also from the help guide page linked above: "By default, orders submitted via Entry() and Exit() methods automatically cancel at the end of a bar if not re-submitted"

    You could keep orders alive by unlocking the script from the Strategy Builder and using the isLiveUntilCancelled overload. This would not be possible using the Strategy Builder and would require manually coding your logic.

    See this reference sample demonstrating how to keep orders alive: https://ninjatrader.com/support/help...ders_alive.htm

    You could then check if BarsSinceEntryExecution is greater than x, where x equals the number of bars since the entry was placed, and call your Exit order method to exit the position.

    BarsSinceEntryExecution: https://ninjatrader.com/support/help...yexecution.htm

    Or, you could use CancelOrder() to cancel the order instead of using an Exit method.

    See this reference sample demonstrating how to use CancelOrder(): https://ninjatrader.com/support/help...thod_to_ca.htm

    Please let me know if I may assist further.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    65 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    139 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    75 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    45 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    50 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X