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

Exit on close is closing position on previous day

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

    Exit on close is closing position on previous day

    Trying to test a strategy which generates signals using Daily TF and enters and exits on same day.
    Below is my code:
    Code:
    public class Entry01TradeThePullback : Strategy { SessionIterator sessionIterator; Order sessionOrder; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Entry #1 – Trade The Pullback"; Name = "Entry01TradeThePullback"; 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; } else if (State == State.Configure) { AddDataSeries(Data.BarsPeriodType.Minute, 5); } else if (State == State.DataLoaded) { sessionIterator = new SessionIterator(Bars); } } protected override void OnBarUpdate() { if (CurrentBar < BarsRequiredToTrade) return; if (BarsInProgress == 0) //Daily bars, check on open previous value { if (High[0] < High[1] && High[1] > High[2]) sessionOrder=EnterShort(); if (Low[0] > Low[1] && Low[1] < Low[2]) sessionOrder= EnterLong(); } if (Bars.IsLastBarOfSession) { if ((sessionOrder != null) && (sessionOrder.OrderState == OrderState.Filled)) { if (sessionOrder.OrderAction == OrderAction.Buy) ExitLong("eXit", sessionOrder.Name); if ((sessionOrder.OrderAction == OrderAction.Sell) || (sessionOrder.OrderAction == OrderAction.SellShort)) ExitShort("eXit", sessionOrder.Name); } else if (sessionOrder != null) CancelOrder(sessionOrder); } } }
    Attaching screenshot.
    Attached Files

    #2
    Hello ravividap,

    Thank you for your note.

    This is occurring because you're including a more granular series, but aren't actually using it for submitting your orders or your session iterator. It's also partially because you are using Exit On Session Close.

    In a multi-series strategy, you have to specifically tell the strategy which series it should be calculating values from and which it's supposed to be submitting orders on. I would recommend reviewing this page on considerations for working with multi-series scripts:



    In this case, to achieve the results you're wanting, you'd need to ensure you're calculating whether to enter from the primary series, but actually entering and exiting should be done when the secondary 5 minute series is processing.

    Here's an example of how you might revise the code:

    Code:
    public class Entry01TradeThePullback: Strategy {
    SessionIterator sessionIterator;
    Order sessionOrder;
    protected override void OnStateChange() {
    if (State == State.SetDefaults) {
    Description = @"Entry #1 – Trade The Pullback";
    Name = "Entry01TradeThePullback";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = false;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = true;
    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) {
    AddDataSeries(Data.BarsPeriodType.Minute, 5);
    
    }
    else if (State == State.DataLoaded) {
    //make sure our session iterator is calculating on the added data series by using BarsArray[1]
    sessionIterator = new SessionIterator(BarsArray[1]);
    
    }
    
    }
    protected override void OnBarUpdate() {
    //make sure we have at least 20 daily bars to calculate with
    if (CurrentBars[0] < BarsRequiredToTrade) return;
    // only calculate the following on the secondary series
    if (BarsInProgress == 1)
    {
    // do the following on the first 5 minute bar of the session
    if (BarsArray[1].IsFirstBarOfSession)
    {
    //Daily bars, check on open previous value
    if (Highs[0][0] < Highs[0][1] && Highs[0][1] > Highs[0][2])
    sessionOrder = EnterShort();
    if (Lows[0][0] > Lows[0][1] && Lows[0][1] < Lows[0][2])
    sessionOrder = EnterLong();
    }
    
    // do the following on the last 5 minute bar of the session
    if (BarsArray[1].IsLastBarOfSession)
    {
    // if we have an order, exit it
    if ((sessionOrder != null) && (sessionOrder.OrderState == OrderState.Filled))
    {
    if (sessionOrder.OrderAction == OrderAction.Buy)
    ExitLong("eXit", sessionOrder.Name);
    if ((sessionOrder.OrderAction == OrderAction.Sell) || (sessionOrder.OrderAction == OrderAction.SellShort))
    ExitShort("eXit", sessionOrder.Name);
    
    }
    else if (sessionOrder != null)
    CancelOrder(sessionOrder);
    
    }
    }
    }
    }
    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Thanks Kate, I had to make
      IsExitOnSessionCloseStrategy = true, along with above changes then It worked as I expected. Thanks for the help.
      Last edited by ravividap; 10-03-2020, 06:24 AM.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by wzgy0920, 04-20-2024, 06:09 PM
      2 responses
      26 views
      0 likes
      Last Post wzgy0920  
      Started by wzgy0920, 02-22-2024, 01:11 AM
      5 responses
      32 views
      0 likes
      Last Post wzgy0920  
      Started by wzgy0920, Yesterday, 09:53 PM
      2 responses
      49 views
      0 likes
      Last Post wzgy0920  
      Started by Kensonprib, 04-28-2021, 10:11 AM
      5 responses
      191 views
      0 likes
      Last Post Hasadafa  
      Started by GussJ, 03-04-2020, 03:11 PM
      11 responses
      3,230 views
      0 likes
      Last Post xiinteractive  
      Working...
      X