Ninjatrader BetterRenko

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #108274 quote
    Vicari0us
    Participant
    Average

    Hi Nicolas,

    Was wondering if you could convert a Ninjatrader script to PRT?

    I like using this Renko indicator on Ninjatrader when I trade futures.

    I have attached the code below that I obtained from the CS file and also a Pic as to how it looks like.
    Thanks, I appreciate your help.

    Regards

    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    
    #endregion
    //
    // BetterRenkoBarsType
    //
    // written by aslan
    //
    // 20100807 - created BetterRenko to address issues with other types of Renko bars
    // 20101118 - changed initial brick alignment to brick size
    // 20150719 - DaleBru converted to NT8
    // 20160508 - antonma fixed the SessionIterator compiler error NJ8 8.0.0.9
    //
    //This namespace holds Bars types in this folder and is required. Do not change it. 
    namespace NinjaTrader.NinjaScript.BarsTypes
    {
    	public class BetterRenkoBarsType : BarsType
    	{
    		
    		private enum RenkoState { BarComplete, BarAccumulating };
    		private RenkoState barRenkoState = RenkoState.BarComplete;
    
    		private double renkoHigh;
    		private double renkoLow;		
    		
    		protected override void OnStateChange()
    		{
    			if (State == State.SetDefaults)
    			{
    				Description					= @"NT8 translation of BetterRenko by aslan";
    				Name						= "BetterRenko";
    				BarsPeriod					= new BarsPeriod { BarsPeriodType = (BarsPeriodType) 17, BarsPeriodTypeName = "BetterRenko(17)", Value = 1 };
    				BuiltFrom					= BarsPeriodType.Tick;
    				DaysToLoad					= 3;
    				IsIntraday					= true;
    				DefaultChartStyle			= Gui.Chart.ChartStyleType.CandleStick; 
    				
    			}
    			else if (State == State.Configure)
    			{
                    Name = string.Format(Core.Globals.GeneralOptions.CurrentCulture, "{0} BetterRenko{1}", BarsPeriod.Value, (BarsPeriod.MarketDataType != MarketDataType.Last ? string.Format(" - {0}", Core.Globals.ToLocalizedObject(BarsPeriod.MarketDataType, Core.Globals.GeneralOptions.CurrentUICulture)) : string.Empty));
    
                    Properties.Remove(Properties.Find("BaseBarsPeriodType",			true));
    				Properties.Remove(Properties.Find("BaseBarsPeriodValue",		true));
    				Properties.Remove(Properties.Find("PointAndFigurePriceType",	true));
    				Properties.Remove(Properties.Find("ReversalType",				true));
    				Properties.Remove(Properties.Find("Value2",						true));
    
    				SetPropertyName("Value", "BrickSize");
                }			
    			
    		}
    
    		public override int GetInitialLookBackDays(BarsPeriod barsPeriod, TradingHours tradingHours, int barsBack)
    		{
    			return 3;
    		}
    
    		protected override void OnDataPoint(Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isBar, double bid, double ask)
    		{
                var brickSize = bars.Instrument.MasterInstrument.RoundToTickSize(bars.BarsPeriod.Value * bars.Instrument.MasterInstrument.TickSize);  // #ticks per brick * tickSize						
    			
    			// build a session iterator from the bars object being updated
      			if (SessionIterator == null)
        			SessionIterator = new SessionIterator(bars);			
    			
    			if (SessionIterator.IsNewSession(time, isBar))
    			{
    				SessionIterator.GetNextSession(time, isBar);
    			}
    			
                if (bars.Count == 0 || bars.IsResetOnNewTradingDay && SessionIterator.IsNewSession(time, isBar))
                {
                    if (bars.Count > 0)
                    {
                        // Close out last bar in session and set open == close
                        double lastBarClose = bars.GetClose(bars.Count - 1);
                        DateTime lastBarTime = bars.GetTime(bars.Count - 1);
                        long lastBarVolume = bars.GetVolume(bars.Count - 1);
                        bars.RemoveLastBar();
                        AddBar(bars, lastBarClose, lastBarClose, lastBarClose, lastBarClose, lastBarTime, lastBarVolume);
                    }
                    barRenkoState = RenkoState.BarAccumulating;
                    double mod = bars.Instrument.MasterInstrument.RoundToTickSize(close % brickSize);
                    double mid = bars.Instrument.MasterInstrument.Compare(mod, brickSize) == 0 ? close : close - mod;
                    renkoHigh = mid + brickSize;
                    renkoLow = mid - brickSize;
                    AddBar(bars, close, close, close, close, time, volume);
                    bars.LastPrice = close;
                    return;
                }
                if (barRenkoState == RenkoState.BarComplete)
                {
                    // this tick creates a new bar
                    AddBar(bars, close, close, close, close, time, volume);
                    if (RangeExceeded(bars.Instrument.MasterInstrument, close))
                    {
                        MoveLimits(bars.Instrument.MasterInstrument, close, brickSize);
                    }
                }
                else
                {
                    if (RangeExceeded(bars.Instrument.MasterInstrument, close))
                    {
                        AddBar(bars, close, close, close, close, time, volume);
                        MoveLimits(bars.Instrument.MasterInstrument, close, brickSize);
                    }
                    else
                    {
                        var barHigh = bars.GetHigh(bars.Count - 1);
                        var barLow = bars.GetLow(bars.Count - 1);
                        UpdateBar(bars, (close > barHigh ? close : barHigh), (close < barLow ? close : barLow), close, time, volume);
                    }
                }
                CheckBarComplete(bars.Instrument.MasterInstrument, close, brickSize);
                bars.LastPrice = close;
            }
    
    		public override void ApplyDefaultBasePeriodValue(BarsPeriod period)
    		{
    			
    		}
    
    		public override void ApplyDefaultValue(BarsPeriod period)
    		{
    			period.BarsPeriodTypeName = "BetterRenko";
    			period.Value = 2;
    		}
    
    		public override string ChartLabel(DateTime dateTime)
    		{
    			return dateTime.ToString("T", Core.Globals.GeneralOptions.CurrentCulture);
    		}
    
    		public override double GetPercentComplete(Bars bars, DateTime now)
    		{
    			return 1.0d; //replace with your bar percent logic
    		}
    
            private void MoveLimits(MasterInstrument masterInstrument, double price, double brickSize)
    		{
    			if (masterInstrument.Compare(price, renkoHigh) >= 0)
    			{
    				do
    				{
    					renkoHigh += brickSize;
    					renkoLow  = renkoHigh - 3.0 * brickSize;
    				} while (masterInstrument.Compare(price, renkoHigh) > 0);  // stops if price in range, including edge
    			}
    			else
    			{
    				do
    				{
    					renkoLow -= brickSize;
    					renkoHigh = renkoLow + 3.0 * brickSize;
    				} while (masterInstrument.Compare(price, renkoLow) < 0);
    			}
    		}
    
            private void CheckBarComplete(MasterInstrument masterInstrument, double price, double brickSize)
    		{
    			int cmpHi = masterInstrument.Compare(price, renkoHigh);
    			int cmpLo = masterInstrument.Compare(price, renkoLow);
    
    			if (cmpHi == 0 || cmpLo == 0)
    			{
    				barRenkoState = RenkoState.BarComplete;
    				MoveLimits(masterInstrument, price, brickSize);  // will move limits once since equal
    			}
    			else barRenkoState = RenkoState.BarAccumulating;
    		}
    
    		private bool RangeExceeded(MasterInstrument masterInstrument, double price)
    		{
    			int cmpHi = masterInstrument.Compare(price, renkoHigh);
    			int cmpLo = masterInstrument.Compare(price, renkoLow);
    
    			return (cmpHi > 0 || cmpLo < 0);
    		}
    	}
    }
    
    #108275 quote
    Vicari0us
    Participant
    Average

    Pic is here. Forgot to add it 🙂

    Better-Renko.png Better-Renko.png
    #108283 quote
    Nicolas
    Keymaster
    Master

    Why not using the default renko from the platform? Which is probably the same I think.

    #188792 quote
    MnA
    Participant
    New

    VicariOus, quick one for you…In Ninja trader…are you able to trade from alerts?

    …I create the signal, attach an alert which triggers a market order.done.

    But proreal is fairly slow to hit the market and I get alot of slippage.

    Can you trade off alerts when using Ninja Trader? I dont think so but maybe you know otherwise?

    #188948 quote
    Khaled
    Participant
    Veteran

    You cannot call the native Renko chart in an Automated Trading System. I tried my best to convert a code found on TradingView, which you can embed in a Strategy. Here is the link https://www.prorealcode.com/topic/renk-prt-renko-prc/

    MnA thanked this post
    #188978 quote
    Nicolas
    Keymaster
    Master

    Same exact renko bricks as the one from the platform, but on a regular chart, that you can CALL to create automated strategies: https://market.prorealcode.com/product/prt-renko/

    #189227 quote
    MnA
    Participant
    New

    £100 is not too bad a price to pay if it works….Im sure it can paint the chart like a renko chart but can it be used to trade..I mean…I think proreal sends auto trading orders only at the close of a bar…I want to send the order as soon as a brick changes colour (for example)…immediately. not at the next close price.. I want to use it to buy or sell at the change of brick colour …like attaching a market order to an alert. Is this possible with the indicator you mentioned? Thank you v much .alex

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.

Ninjatrader BetterRenko


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Vicari0us @vicari0us Participant
Summary

This topic contains 6 replies,
has 3 voices, and was last updated by MnA
3 years, 11 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 09/23/2019
Status: Active
Attachments: 1 files
Logo Logo
Loading...