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

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 cre8able, Today, 01:01 PM
      0 responses
      1 view
      0 likes
      Last Post cre8able  
      Started by manitshah915, Today, 12:59 PM
      0 responses
      2 views
      0 likes
      Last Post manitshah915  
      Started by ursavent, Today, 12:54 PM
      0 responses
      2 views
      0 likes
      Last Post ursavent  
      Started by Mizzouman1, Today, 07:35 AM
      3 responses
      17 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by RubenCazorla, Today, 09:07 AM
      2 responses
      13 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Working...
      X