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

Renko bar strategy

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

    Renko bar strategy

    Hi, I created a strategy to buy at bar opening. When i backtested, i see all the buy orders taken at low of the bar and sell orders are taken at high of the bar, exactly as i expected. But when i run realtime trades, i see trades are taken randomnly, not at low of the bar.

    I used ninzaRenko 40/1. Below is the code i used. Can anyone guide me why backtest is filling orders correctly but real time trades orders are filling differently.

    Attachment1 - real time trades where orders are filling incorrectly.
    Attachment2 - backtest trades working correctly.


    Code:
    protected override void OnStateChange()
    {
        if (State == State.SetDefaults)
        {
            Description                                    = @"Enter the description for your new custom Strategy here.";
            Name                                        = "HTScalper";
            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;
            ProfitTarget                    = 36;
            StopLoss                    = 100;
            Lot                    = 1;
            lastTradeTaken = TradeType.None;
            TradeOnlyNWSession = true;
        }
        else if (State == State.Configure)
        {
        }
        else if (State == State.DataLoaded)
        {                
            HalfTrend1                = HalfTrend(Close, 2, 2, 100, false, true, 10);
            ninZaHalfTrendPro1 = ninZaHalfTrendPro(Close, 3, ninZa_MAType.LinReg, true, ninZa_MAType.EMA, 3, 1, 100);
            SetProfitTarget("", CalculationMode.Ticks, ProfitTarget);
            SetStopLoss("", CalculationMode.Ticks, StopLoss, false);
        }
    }
    
    protected override void OnBarUpdate()
    {
        if (BarsInProgress != 0)
            return;
    
        if (CurrentBars[0] < 1)
            return;
    
    
        // Set 1
        if (ninZaHalfTrendPro1.Signal_Trend[0] == 1)
        {
            EnterLong(Convert.ToInt32(Lot), "");
        }
        
         // Set 2
        if (ninZaHalfTrendPro1.Signal_Trend[0] == -1)
        {
            EnterShort(Convert.ToInt32(Lot), "");
        }
    }​
    Attached Files

    #2
    Hello visvabalaji,

    Live orders are filled on an exchange with a trading partner on an agreed upon price based on market dynamics. Backtest orders are not using these market dynamics. Instead these are filled based on logical rules from processing historical data.
    • When in historical data, only the Open, High, Low, and Close will be available and there will be no intra-bar data.
      • This means actions cannot happen intra-bar, fills cannot happen intra-bar. All prices and actions come from and occur when the bar closes as this is all the information that is known
      • Because of this, OnBarUpdate will only update 'On bar close' as it does not have the intra-bar information necessary for 'On price change' or 'On each tick' and the script will not have the intra-bar information to accurately fill an order at the exact price and time.

    With standard Renko bars, a backtest is very challenging because the bars have a function that removes the last bar and replaces it with a new Open price. This is difficult to simulate historically vs. how it occurs in real time. Since you are using renko bars from a third party, I am unable to say if this is also the case with these bars since I don't have any visibility on how these are built.



    In any case, to understand why the script is behaving as it is, such as placing orders or not placing orders when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

    in order to better understand how the code is working, it will be necessary to use Print to see how the conditions are evaluating and enable TraceOrders to see if orders are being submitted, ignored, rejected, or cancelled.

    Below is a link to a forum post that demonstrates using prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.


    The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very helpful to include labels and operators in the print to understand what is being compared in the condition sets.

    It is also helpful to set TraceOrders to true in State.Configure as well as print the order object in OnOrderUpdate().

    TraceOrders will output to the NinjaScript Output window a message when orders are being submitted, ignored, cancelled, or rejected.

    Printing the order object in OnOrderUpdate() will allow you to track the progression of the order from submitted, to working, to filled, cancelled, or rejected.

    These tools will let you know what happens to the order.

    TraceOrders - https://ninjatrader.com/support/help...raceorders.htm
    OnOrderUpdate() - https://ninjatrader.com/support/help...rderupdate.htm

    I'm also including a link to a forum post with further suggestions on debugging a script.
    https://ninjatrader.com/support/foru...956#post671956​​

    Let me know if you need any assistance creating a print or enabling TraceOrders.

    Save the output from the output window to a text file and provide this with your reply.

    I'll be happy to assist with analyzing the output.​
    Gaby V.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by geddyisodin, 04-25-2024, 05:20 AM
    8 responses
    60 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by jxs_xrj, 01-12-2020, 09:49 AM
    4 responses
    3,287 views
    1 like
    Last Post jgualdronc  
    Started by Option Whisperer, Today, 09:55 AM
    0 responses
    5 views
    0 likes
    Last Post Option Whisperer  
    Started by halgo_boulder, 04-20-2024, 08:44 AM
    2 responses
    22 views
    0 likes
    Last Post halgo_boulder  
    Started by mishhh, 05-25-2010, 08:54 AM
    19 responses
    6,189 views
    0 likes
    Last Post rene69851  
    Working...
    X