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

Hello, can you please help me convert this indicator into strategy

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

    Hello, can you please help me convert this indicator into strategy

    I want it to trigger a trade and paint an arrow on the first tick of the incoming bar once the bar changes color. blue for up and red for down.

    /*
    Reversal Bar by
    This indicator will color a reversal bar grey.
    It is designed to be used on range charts.
    If you see a lot of grey bars then it could be consolidation.
    If you see a lot of colored bars then price is moving nicely.
    20100101 v1.0
    1st version. No alerts yet, I will add them later.
    20100106 v1.1
    Fixed problem with saving color parameters.
    Added purple bars for consolidation

    */

    #region Using declarations
    using System;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.ComponentModel;
    using System.Xml.Serialization;
    using NinjaTrader.Data;
    using NinjaTrader.Gui.Chart;
    #endregion

    namespace NinjaTrader.Indicator
    {
    [Description("ReversalBar_V2")] // Jan. 28, 2010, TheWizard added serialization to the colors so they will be saved if you change them & then save your template & re-load it
    [Gui.Design.DisplayName("ReversalBar_V2")]
    public class ReversalBar_V2 : Indicator // Changed the name to _V2 so as not to conflict with the original indicator posted by Cunparis.
    {
    private bool useAlerts = false;
    private bool paintbar = true;

    private Color colorUp = Color.Blue;
    private Color colorDown = Color.Red;
    private Color colorReversal = Color.Gray;
    private Color colorCongestion = Color.Purple;

    private int lastSoundBar = 0;
    private DataSeries BarColorData;

    protected override void Initialize()
    {
    Overlay = true;
    PriceTypeSupported = false;
    Panel = 1;

    BarColorData = new DataSeries(this);
    }

    protected void doSetBarColor() {
    if(Instrument.MasterInstrument.Compare(Close[1],Open[1]) == 1) {
    if(Instrument.MasterInstrument.Compare(Close[0], Open[0]) == 1) {
    BarColorData.Set(1);
    } else {
    BarColorData.Set(0);
    }
    } else if(Instrument.MasterInstrument.Compare(Close[1],Open[1]) == -1) {
    if(Instrument.MasterInstrument.Compare(Close[0], Open[0]) == -1) {
    BarColorData.Set(-1);
    } else {
    BarColorData.Set(0);
    }
    }

    bool overlap = false;
    if(MAX(High, 5)[0] - MIN(Low,5)[0] < 2 * Bars.Period.Value * TickSize) {
    //Print(Time[0] + " H=" + MAX(High, 5)[0] + " L=" + MIN(Low,5)[0]);
    overlap = true;
    }
    if(MAX(High, 4)[0] - MIN(Low,4)[0] < 1.5 * Bars.Period.Value * TickSize) {
    overlap = true;
    }

    if(BarColorData[0] == 1) {
    BarColor = colorUp;
    } else if(BarColorData[0] == -1) {
    BarColor = colorDown;
    } else if(BarColorData[0] == 0) {
    if(overlap) {
    BarColor = colorCongestion;
    } else {
    BarColor = colorReversal;
    }
    }
    }

    protected bool barOverlap(int i) {
    if(Math.Abs(High[i] - High[i-1]) <= 2*TickSize && Math.Abs(Low[0] - Low[i-1]) <= 2*TickSize) {
    return true;
    } else {
    return false;
    }
    }

    protected override void OnBarUpdate()
    {
    if(CurrentBar < 4) {
    return;
    }

    if(paintbar) {
    doSetBarColor();
    }

    }


    // [Description("Use Audio Alerts?")]
    // [Category("Parameters")]
    // public bool UseAlerts
    // {
    // get { return useAlerts; }
    // set { useAlerts = value; }
    // }

    [Description("Paint Bars?")]
    [Category("Visual")]
    [Gui.Design.DisplayName ("1. Paint Bars?")]
    public bool Paintbar
    {
    get { return paintbar; }
    set { paintbar = value; }
    }

    [XmlIgnore()]
    [Description("Color Up")]
    [Category("Visual")]
    [Gui.Design.DisplayName ("2. Color Up")]
    public Color ColorUp
    {
    get { return colorUp; }
    set { colorUp = value; }
    }
    // Serialization of the colors added by TheWizard January 28, 2010
    [Browsable(false)]
    public string colorUpSerialize
    {
    get { return NinjaTrader.Gui.Design.SerializableColor.ToString( colorUp); }
    set { colorUp = NinjaTrader.Gui.Design.SerializableColor.FromStrin g(value); }
    }
    [XmlIgnore()]
    [Description("Color Down")]
    [Category("Visual")]
    [Gui.Design.DisplayName ("3. Color Down")]
    public Color ColorDown
    {
    get { return colorDown; }
    set { colorDown = value; }
    }
    // Serialization of the colors added by TheWizard January 28, 2010
    [Browsable(false)]
    public string colorDownSerialize
    {
    get { return NinjaTrader.Gui.Design.SerializableColor.ToString( colorDown); }
    set { colorDown = NinjaTrader.Gui.Design.SerializableColor.FromStrin g(value); }
    }
    [XmlIgnore()]
    [Description("Color Reversal")]
    [Category("Visual")]
    [Gui.Design.DisplayName ("4. Color Reversal")]
    public Color ColorReversal
    {
    get { return colorReversal; }
    set { colorReversal = value; }
    }
    // Serialization of the colors added by TheWizard January 28, 2010
    [Browsable(false)]
    public string colorReversalSerialize
    {
    get { return NinjaTrader.Gui.Design.SerializableColor.ToString( colorReversal); }
    set { colorReversal = NinjaTrader.Gui.Design.SerializableColor.FromStrin g(value); }
    }
    [XmlIgnore()]
    [Description("Color Congestion")]
    [Category("Visual")]
    [Gui.Design.DisplayName ("5. Color Congestion")]
    public Color ColorCongestion
    {
    get { return colorCongestion; }
    set { colorCongestion = value; }
    }
    // Serialization of the colors added by TheWizard January 28, 2010
    [Browsable(false)]
    public string colorCongestionSerialize
    {
    get { return NinjaTrader.Gui.Design.SerializableColor.ToString( colorCongestion); }
    set { colorCongestion = NinjaTrader.Gui.Design.SerializableColor.FromStrin g(value); }
    }
    }
    }

    #region NinjaScript generated code. Neither change nor remove.
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    public partial class Indicator : IndicatorBase
    {
    private ReversalBar_V2[] cacheReversalBar_V2 = null;

    private static ReversalBar_V2 checkReversalBar_V2 = new ReversalBar_V2();

    /// <summary>
    /// ReversalBar_V2
    /// </summary>
    /// <returns></returns>
    public ReversalBar_V2 ReversalBar_V2()
    {
    return ReversalBar_V2(Input);
    }

    /// <summary>
    /// ReversalBar_V2
    /// </summary>
    /// <returns></returns>
    public ReversalBar_V2 ReversalBar_V2(Data.IDataSeries input)
    {
    if (cacheReversalBar_V2 != null)
    for (int idx = 0; idx < cacheReversalBar_V2.Length; idx++)
    if (cacheReversalBar_V2[idx].EqualsInput(input))
    return cacheReversalBar_V2[idx];

    lock (checkReversalBar_V2)
    {
    if (cacheReversalBar_V2 != null)
    for (int idx = 0; idx < cacheReversalBar_V2.Length; idx++)
    if (cacheReversalBar_V2[idx].EqualsInput(input))
    return cacheReversalBar_V2[idx];

    ReversalBar_V2 indicator = new ReversalBar_V2();
    indicator.BarsRequired = BarsRequired;
    indicator.CalculateOnBarClose = CalculateOnBarClose;
    #if NT7
    indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
    indicator.MaximumBarsLookBack = MaximumBarsLookBack;
    #endif
    indicator.Input = input;
    Indicators.Add(indicator);
    indicator.SetUp();

    ReversalBar_V2[] tmp = new ReversalBar_V2[cacheReversalBar_V2 == null ? 1 : cacheReversalBar_V2.Length + 1];
    if (cacheReversalBar_V2 != null)
    cacheReversalBar_V2.CopyTo(tmp, 0);
    tmp[tmp.Length - 1] = indicator;
    cacheReversalBar_V2 = tmp;
    return indicator;
    }
    }
    }
    }

    // This namespace holds all market analyzer column definitions and is required. Do not change it.
    namespace NinjaTrader.MarketAnalyzer
    {
    public partial class Column : ColumnBase
    {
    /// <summary>
    /// ReversalBar_V2
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.ReversalBar_V2 ReversalBar_V2()
    {
    return _indicator.ReversalBar_V2(Input);
    }

    /// <summary>
    /// ReversalBar_V2
    /// </summary>
    /// <returns></returns>
    public Indicator.ReversalBar_V2 ReversalBar_V2(Data.IDataSeries input)
    {
    return _indicator.ReversalBar_V2(input);
    }
    }
    }

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    public partial class Strategy : StrategyBase
    {
    /// <summary>
    /// ReversalBar_V2
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.ReversalBar_V2 ReversalBar_V2()
    {
    return _indicator.ReversalBar_V2(Input);
    }

    /// <summary>
    /// ReversalBar_V2
    /// </summary>
    /// <returns></returns>
    public Indicator.ReversalBar_V2 ReversalBar_V2(Data.IDataSeries input)
    {
    if (InInitialize && input == null)
    throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

    return _indicator.ReversalBar_V2(input);
    }
    }
    }
    #endregion

    #2
    Hello odmassion,

    You could keep the script as an indicator and set a custom series that the strategy can use as a signal.
    This would allow you to continue using the indicator separately if desired without having to run the strategy.

    Or you could put all of the code in the strategy if desired as well.

    As this script is for NinjaTrader 7, my advice is to start by creating as much of the script as possible in NinjaTrader 8 using the Strategy Builder (or Strategy Wizard) or Indicator Wizard.

    This will create the framework of the indicator or strategy which has large changes from NT7 to NT8.

    Then copy all code from the NT7 scripts OnBarUpdate to the OnBarUpdate of the NT8 script.

    This will introduce errors that will need to be resolved.

    Below I am including a link to a list of the code breaking changes from NT7 to NT8.


    The help guide will be the best way to see how things have changed from nt7 to nt8.

    You may also want to contact the vendor that creates the NT7 version and inquire if there is a new version available for NT8.

    You also mentioned the script would take an action on the first tick of the new bar.
    For this, you would want to set Calculate to .OnEachTick to trigger OnBarUpdate for each tick.


    Also, for creating a custom public series that you can use to pass triggers to a strategy I am including a link to the help guide on Series.


    The series needs to be public instead of private.
    For example:
    [Browsable(false)]
    [XmlIgnore]
    public Series<int> BarColorData
    { get; set; }

    Then in the strategy you would call:
    ReversalBar_V2(/*parameters here*/).BarColorData[0]

    You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our business development follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request.
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by f.saeidi, Today, 08:01 PM
    0 responses
    2 views
    0 likes
    Last Post f.saeidi  
    Started by Rapine Heihei, Today, 07:51 PM
    0 responses
    3 views
    0 likes
    Last Post Rapine Heihei  
    Started by frslvr, 04-11-2024, 07:26 AM
    5 responses
    96 views
    1 like
    Last Post caryc123  
    Started by algospoke, 04-17-2024, 06:40 PM
    6 responses
    49 views
    0 likes
    Last Post algospoke  
    Started by arvidvanstaey, Today, 02:19 PM
    4 responses
    11 views
    0 likes
    Last Post arvidvanstaey  
    Working...
    X