Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Change from Calculate.OnPriceChange to .OnBarClose makes script freeze

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

    Change from Calculate.OnPriceChange to .OnBarClose makes script freeze

    Can´t figure out what makes my script freeze and the chart stops updating without giving an error message.

    The script uses two Bars series and starts with Calculate.OnBarClose then changes to .OnPriceChange when the order is placed. I notice that the problem happens when I revert back to .OnBarClose when the order is closed with the .OnExectionUpdate method



    Here is my code:




    #region Variables
    private Order EntryOrder = null;
    #endregion

    protected override void OnStateChange(){
    if (State == State.SetDefaults)
    {
    Description = @"";
    Name = "AddSeriesTest";
    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 = 0;
    IsInstantiatedOnEachOptimizationIteration = true;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;

    }

    ///////// Adds secondary series, primary series is ES 03-14 loaded by chart
    else if (State == State.Configure)
    {
    AddDataSeries("ES 06-14", Data.BarsPeriodType.Minute, 2, Data.MarketDataType.Last);
    }
    }

    protected override void OnBarUpdate(){

    ////////// Checks which Bars is calling the bar update based on the day

    if( BarsInProgress == 1 && ToDay(Time[0]) < 20140314) //ES 06-14
    return;

    ////////// Submits order on a specific bar in the asigned Bars series and sets Calculate method to OnPriceChange
    if(BarsInProgress == 0 && ToDay(Time[0]) == 20140312 && ToTime(Time[0]) == ToTime(14,28,00) )
    {
    SetProfitTarget(CalculationMode.Ticks,6);
    SetStopLoss(CalculationMode.Ticks,9);
    EntryOrder = EnterShortLimit(0,true, 1, Close[0],"Entry");
    Calculate = Calculate.OnPriceChange;
    }
    }


    protected override void OnExecutionUpdate (Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time){

    if(execution.Order.Name == "Entry")
    {
    Print(ToDay(Time[0]) + @" " + ToTime(Time[0]) + @" " + execution.Order.OrderState + @" in full By Bars: " + BarsInProgress);
    }


    ///////// Reverts back to .OnBarClose on either target or stoploss
    if(execution.Order.Name == "Profit target")
    {
    Calculate = Calculate.OnBarClose;
    Print(@"W in Bars: " + BarsInProgress);
    }

    if(execution.Order.Name == "Stop loss")
    {
    Calculate = Calculate.OnBarClose;
    Print(@"L in Bars: "+ BarsInProgress);
    }
    }

    }

    }

    #2
    Hello Orden33,

    While it is not explicitly stated in the help guide, the Calculate property should not be changed after the State becomes State.DataLoaded. This should only be set in OnStateChange when the State property is either State.SetDefaults, or State.Configure.

    I am sending a request to our product management to update the help guide so that this is mentioned.

    Please change the logic so that the Calculate value is not changed after the State becomes State.DataLoaded. (This means the Calculate setting cannot be changed in OnBarUpdate or other data driven methods).
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea B.,

      Just a note about doc update:

      I was please to find your clarification here about when "Calculate" could be changed. Because it appears, today(1/21/23) the online documentation still does not reflect this.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Today, 05:17 AM
      0 responses
      52 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      130 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      70 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      43 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      47 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X