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

Spread Indicator code

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

    Spread Indicator code

    Hello
    This Bid/Ask Spread indicator is plotting a dot on candle close. There some body can change
    it to calculate on the High and Low please.

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

    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    /// <summary>
    /// Spread between the Bid and Ask prices
    /// </summary>
    [Description("Spread between the Bid and Ask prices")]
    public class BidAskSpread : Indicator
    {
    #region Variables
    #endregion

    /// <summary>
    /// This method is used to configure the indicator and is called once before any bar data is loaded.
    /// </summary>
    protected override void Initialize()
    {
    Add(new Plot(Color.PaleTurquoise, PlotStyle.Dot, "Bid"));
    Add(new Plot(Color.HotPink, PlotStyle.Dot, "Ask"));
    Add(new Plot(Color.Empty, "spreadSeries"));
    Overlay = true;
    CalculateOnBarClose = false;
    AutoScale = false;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    if (Historical)
    return;

    double spread = (GetCurrentAsk() - GetCurrentBid());

    if (Instrument.MasterInstrument.InstrumentType == InstrumentType.Currency)
    {
    string[] yenword = { "J", "P", "Y" };
    string name = Instrument.FullName;
    int lettersfound = 0;

    for (int j = 0; j < yenword.Length; j++)
    {
    for (int i = 0; i < name.Length; i++)
    {
    string letter = name[i].ToString();
    if (letter == yenword[j])
    lettersfound++;
    }
    }

    if (lettersfound >= yenword.Length)
    {
    DrawTextFixed("spread", (Math.Round((spread * 100), 1).ToString()) + " pip spread", TextPosition.TopRight, Color.Black, new Font("Arial", 14), Color.Black, Color.AntiqueWhite, 3);
    spreadSeries.Set((Math.Round((spread * 100), 1)));
    }
    else
    {
    DrawTextFixed("spread", (Math.Round((spread * 10000), 1).ToString()) + " pip spread", TextPosition.TopRight, Color.Black, new Font("Arial", 14), Color.Black, Color.AntiqueWhite, 3);
    spreadSeries.Set((Math.Round((spread * 10000), 1)));
    }
    }
    else if (Instrument.MasterInstrument.InstrumentType == InstrumentType.Future)
    {
    DrawTextFixed("spread", (Math.Round((spread / TickSize), 1).ToString()) + " tick spread", TextPosition.TopRight, Color.Black, new Font("Arial", 14), Color.Black, Color.AntiqueWhite, 3);
    spreadSeries.Set((Math.Round((spread / TickSize), 1)));
    }
    Bid.Set(GetCurrentBid());
    Ask.Set(GetCurrentAsk());
    }



    #region Properties
    [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
    [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
    public DataSeries Bid
    {
    get { return Values[0]; }
    }

    [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
    [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
    public DataSeries Ask
    {
    get { return Values[1]; }
    }

    [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
    [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
    public DataSeries spreadSeries
    {
    get { return Values[2]; }
    }

    #endregion
    }
    }

    #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 BidAskSpread[] cacheBidAskSpread = null;

    private static BidAskSpread checkBidAskSpread = new BidAskSpread();

    /// <summary>
    /// Spread between the Bid and Ask prices
    /// </summary>
    /// <returns></returns>
    public BidAskSpread BidAskSpread()
    {
    return BidAskSpread(Input);
    }

    /// <summary>
    /// Spread between the Bid and Ask prices
    /// </summary>
    /// <returns></returns>
    public BidAskSpread BidAskSpread(Data.IDataSeries input)
    {
    if (cacheBidAskSpread != null)
    for (int idx = 0; idx < cacheBidAskSpread.Length; idx++)
    if (cacheBidAskSpread[idx].EqualsInput(input))
    return cacheBidAskSpread[idx];

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

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

    BidAskSpread[] tmp = new BidAskSpread[cacheBidAskSpread == null ? 1 : cacheBidAskSpread.Length + 1];
    if (cacheBidAskSpread != null)
    cacheBidAskSpread.CopyTo(tmp, 0);
    tmp[tmp.Length - 1] = indicator;
    cacheBidAskSpread = 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>
    /// Spread between the Bid and Ask prices
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.BidAskSpread BidAskSpread()
    {
    return _indicator.BidAskSpread(Input);
    }

    /// <summary>
    /// Spread between the Bid and Ask prices
    /// </summary>
    /// <returns></returns>
    public Indicator.BidAskSpread BidAskSpread(Data.IDataSeries input)
    {
    return _indicator.BidAskSpread(input);
    }
    }
    }

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

    /// <summary>
    /// Spread between the Bid and Ask prices
    /// </summary>
    /// <returns></returns>
    public Indicator.BidAskSpread BidAskSpread(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.BidAskSpread(input);
    }
    }
    }
    #endregion

    #2
    Vittorino, if noone takes this up as a project, you could also contact one of the NinjaScript consultants to make this change for you -

    BertrandNinjaTrader Customer Service

    Comment


      #3
      How to serialize a string to display like an asterisk. For example, my string is a password and I want it to be displayed in the setup page as an asterisk.

      Comment


        #4
        [QUOTE=Vittorino;319277]Hello
        This Bid/Ask Spread indicator is plotting a dot on candle close. There some body can change
        it to calculate on the High and Low please.

        /// </summary>
        [Description("Spread between the Bid and Ask prices")]
        .......................

        Unfortunaly the code in your message cannot work for historical data. The reason for it is described in Ninja help:

        "GetCurrentBid()
        Print this Topic Previous pageReturn to chapter overview Next page
        Definition
        Returns the current bid price.


        Method Return Value

        A double value representing the current bid price. Note: When accessed during a historical backtest, the close price of the evaluated bar is substituted."


        I'm attaching a working indicator , exported. It works , provided you have bid and ask prices.

        I wonder why Ninja developers choose such implementation and caused things to be complicated. Any answer from Ninja ?

        7/4/13:
        Corrected version - adds bid and ask correctly for PeriodType and interval.
        See attached.
        Attached Files
        Last edited by xTrader1; 04-07-2013, 01:40 AM.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by algospoke, Yesterday, 06:40 PM
        2 responses
        18 views
        0 likes
        Last Post algospoke  
        Started by ghoul, Today, 06:02 PM
        3 responses
        14 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by jeronymite, 04-12-2024, 04:26 PM
        3 responses
        44 views
        0 likes
        Last Post jeronymite  
        Started by Barry Milan, Yesterday, 10:35 PM
        7 responses
        20 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by AttiM, 02-14-2024, 05:20 PM
        10 responses
        180 views
        0 likes
        Last Post jeronymite  
        Working...
        X