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

Sine Weighted MA (SWMA)

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

  • Harry
    replied
    You may replace the SWMA with a simple TMA. They are mostly identical. The TMA is preferable over the SWMA, as it puts a lower toll on CPU, particularly when using it with Calculate.OnPriceChange.

    Leave a comment:


  • pjsmith
    replied
    Hi - I stumbled across this whilst doing a little research on SWMA. I figured I'd re-code it with the improvements Harry suggested above. Seems to work ok. Figured I'd post it here in case it is of use to anyone else.
    Attached Files

    Leave a comment:


  • Harry
    replied
    Just a little suggestion: I do not think that the indicator code is already optimized.

    There is no point to perform the calculation below for every single bar.

    Code:
    _nVal2 = 0;
    
      for (int x = 1; x < Period; x++)
      {
         _nVal2 += Math.Sin((x + 1) * _nFac);
      }
    The result is always the same and it is sufficient to do it once in State == State.Historical.

    Furthermore the coefficients

    Code:
    Math.Sin((x + 1) * _nFac)
    should also be calculated in State == State.HIstorical, where they can be written to an array. Again, no need to perform this calculation for every bar.

    Leave a comment:


  • Sim22
    replied
    NT8 version

    Here is a code-optimized NT8 version......
    Attached Files

    Leave a comment:


  • Sam7768
    replied
    Ok. Working now. Thank you very much...

    Posting the code here for the benefit of others...

    Sine Wave Moving Average (SWMA) for NT 6.5.
    ____________________________


    #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

    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    /// <summary>
    /// The SWMA is an indicator
    /// </summary>
    [Description("The SWMA is an indicator that shows the average value of a security's price over a period of time.")]
    public class SWMA : Indicator
    {
    #region Variables
    private int period = 14;
    #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.Orange, "SWMA"));

    Overlay = true;
    PriceTypeSupported = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick).
    /// </summary>
    protected override void OnBarUpdate()
    {
    if (CurrentBar < Period+1) return;
    double nFac = Math.PI / ( Period+1 );
    double nVal2 = 0;
    for ( int x=0; x<Period; x++ )
    {
    nVal2 += Math.Sin( (x+1) * nFac );
    }
    double nVal1 = 0;
    for ( int x=0; x<Period; x++ )
    {
    nVal1 += Math.Sin( (x+1) * nFac ) * Close[x];
    }
    Value.Set(nVal1/nVal2);
    }

    #region Properties
    /// <summary>
    /// </summary>
    [Description("Numbers of bars used for calculations")]
    [Category("Parameters")]
    public int Period
    {
    get { return period; }
    set { period = Math.Max(1, value); }
    }
    #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 SWMA[] cacheSWMA = null;

    private static SWMA checkSWMA = new SWMA();

    /// <summary>
    /// The SWMA is an indicator that shows the average value of a security's price over a period of time.
    /// </summary>
    /// <returns></returns>
    public SWMA SWMA(int period)
    {
    return SWMA(Input, period);
    }

    /// <summary>
    /// The SWMA is an indicator that shows the average value of a security's price over a period of time.
    /// </summary>
    /// <returns></returns>
    public SWMA SWMA(Data.IDataSeries input, int period)
    {
    checkSWMA.Period = period;
    period = checkSWMA.Period;

    if (cacheSWMA != null)
    for (int idx = 0; idx < cacheSWMA.Length; idx++)
    if (cacheSWMA[idx].Period == period && cacheSWMA[idx].EqualsInput(input))
    return cacheSWMA[idx];

    SWMA indicator = new SWMA();
    indicator.BarsRequired = BarsRequired;
    indicator.CalculateOnBarClose = CalculateOnBarClose;
    indicator.Input = input;
    indicator.Period = period;
    indicator.SetUp();

    SWMA[] tmp = new SWMA[cacheSWMA == null ? 1 : cacheSWMA.Length + 1];
    if (cacheSWMA != null)
    cacheSWMA.CopyTo(tmp, 0);
    tmp[tmp.Length - 1] = indicator;
    cacheSWMA = tmp;
    Indicators.Add(indicator);

    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>
    /// The SWMA is an indicator that shows the average value of a security's price over a period of time.
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.SWMA SWMA(int period)
    {
    return _indicator.SWMA(Input, period);
    }

    /// <summary>
    /// The SWMA is an indicator that shows the average value of a security's price over a period of time.
    /// </summary>
    /// <returns></returns>
    public Indicator.SWMA SWMA(Data.IDataSeries input, int period)
    {
    return _indicator.SWMA(input, period);
    }

    }
    }

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    public partial class Strategy : StrategyBase
    {
    /// <summary>
    /// The SWMA is an indicator that shows the average value of a security's price over a period of time.
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.SWMA SWMA(int period)
    {
    return _indicator.SWMA(Input, period);
    }

    /// <summary>
    /// The SWMA is an indicator that shows the average value of a security's price over a period of time.
    /// </summary>
    /// <returns></returns>
    public Indicator.SWMA SWMA(Data.IDataSeries input, int period)
    {
    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.SWMA(input, period);
    }

    }
    }
    #endregion

    Leave a comment:


  • Trader.Jon
    replied
    change the lines 'grid category ' in the code ..

    [GridCategory("Parameters")]

    . you might have to change them to '[category "parameters" ] ' sorry

    [Category("Parameters")]

    .. I havent used 6.5 for a while but that should get you to see what has to be done ..

    Have a good night
    Jon
    Last edited by Trader.Jon; 11-27-2010, 10:00 PM.

    Leave a comment:


  • Sam7768
    replied
    Thank You Trader Jon. I did as per the instructions, and when compiled, the error code says..

    "type or namespace name 'GridCategory' could not be found (are you missing a using directive or an assembly reference?)"

    Code CS0246, line 60, column 4

    Also, in the zip file, under info file...

    <NinjaTrader>
    - <Export>
    <Version>7.0.0.24</Version>
    </Export>
    </NinjaTrader>

    So, I guess the code is correct, but not compatible with NT 6.5. And I am using NT 6.5.

    So, How to convert this code to NT 6.5 compatibility?.

    Leave a comment:


  • Trader.Jon
    replied
    Originally posted by Sam7768 View Post
    Thank you Bertrand.

    Is it for NT 7? it says import failed. "Version required to extract....(45)"

    I am using NT 6.5

    Pl advice.
    Some assist to help ...

    Hi max-td, When I go .... Edit NinjaScript > Indicator > Edit Indicator The file jtEconNews2 already exists I doubleclick and it opens ... I then run compile and resave as jtEconNews2 However when I go onto Chart and select indicators (CTrl I) it doesnt appear in my list of indicators At no stage do I get any errors. sleepy :sarcastic: Sleepy did you find it? I had the same problem but found it. When you go to add an indicator to your chart I found it as a blank white indicator (no name) at the very beginning &#8230;


    BTW, SWMA tracks very closely with WMA

    Leave a comment:


  • Sam7768
    replied
    Originally posted by NinjaTrader_Bertrand View Post
    Please give the file attached a try, works well for me.
    Thank you Bertrand.

    Is it for NT 7? it says import failed. "Version required to extract....(45)"

    I am using NT 6.5

    Pl advice.

    Leave a comment:


  • NinjaTrader_Bertrand
    replied
    Please give the file attached a try, works well for me.
    Attached Files

    Leave a comment:


  • Sam7768
    replied
    Thank you Hegh2000. I am getting error...here is the code I am trying to..I am not sure what could be error. Any advice?



    #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

    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    /// <summary>
    /// The SWMA is an indicator
    /// </summary>
    [Description("The SWMA is an indicator that shows the average value of a security's price over a period of time.")]
    public class SWMA : Indicator
    {
    #region Variables
    private int period = 14;
    #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.Orange, "SWMA"));

    Overlay = true;
    PriceTypeSupported = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick).
    /// </summary>
    protected override void OnBarUpdate()
    {
    if (CurrentBar < Period+1) return;
    double nFac = Math.PI / ( Period+1 );
    double nVal2 = 0;
    for ( int x=0; x<Period; x++ )
    {
    nVal2 += Math.Sin( (x+1) * nFac );
    }
    double nVal1 = 0;
    for ( int x=0; x<Period; x++ )
    {
    nVal1 += Math.Sin( (x+1) * nFac ) * Close[x];
    }
    SWMA.Set(nVal1/nVal2);
    }
    #endregion

    Leave a comment:


  • hegh2000
    replied
    hi,

    if (CurrentBar < Period+1) return;
    double nFac = Math.PI / ( Period+1 );
    double nVal2 = 0;
    for ( int x=0; x<Period; x++ )
    {
    nVal2 += Math.Sin( (x+1) * nFac );
    }
    double nVal1 = 0;
    for ( int x=0; x<Period; x++ )
    {
    nVal1 += Math.Sin( (x+1) * nFac ) * Close[x];
    }
    SWMA.Set(nVal1/nVal2);

    Leave a comment:


  • Sam7768
    started a topic Sine Weighted MA (SWMA)

    Sine Weighted MA (SWMA)

    /************************************************** ******************
    Hello,

    I could not locate Sine Weighted MA indicator for NT. I have used it in eSignal and it is normalized and is very smooth. Below is code in esignal. Can someone help code this in NT 6.5?
    _______________________________________


    Title: Sine Weighted MA for eSignal 7.x
    By: Chris D. Kryza (Divergence Software, Inc.)
    Email: [email protected]
    Incept: 02/17/2004
    Version: 1.0.0


    ================================================== ===================
    Fix History:

    02/17/2004 - Initial Release
    1.0.0

    ================================================== ===================
    Project Description:

    Sine Weighted Moving Average plot.

    Dislaimer: For educational purposes only! Obviously, no guarantees
    whatsoever and use at your own risk.

    ************************************************** ********************/


    //External Variables
    var nBarCounter = 0;
    var nFac = null;
    var nVal2 = null;
    var aFPArray = new Array();
    var bInitialized = false;

    //== PreMain function required by eSignal to set things up
    function preMain() {
    var x;

    setPriceStudy(true);
    setStudyTitle("Sine Weighted MA");
    setCursorLabelName("SineMA", 0);
    setDefaultBarFgColor( Color.blue, 0 );
    setShowTitleParameters( false );


    //initialize formula parameters
    x=0;
    aFPArray[x] = new FunctionParameter( "Period", FunctionParameter.NUMBER);
    with( aFPArray[x] ) {
    setLowerLimit( 2 );
    setUpperLimit( 125 );
    setDefault( 15 );
    }
    x++;
    aFPArray[x] = new FunctionParameter( "Price", FunctionParameter.STRING);
    with( aFPArray[x] ) {
    addOption( "Close" );
    addOption( "HL/2" );
    addOption( "HLC/3" );
    addOption( "OHLC/4" );
    addOption( "High" );
    addOption( "Low" );
    setDefault( "Close" );
    }
    x++;
    aFPArray[x] = new FunctionParameter( "Color", FunctionParameter.COLOR);
    with( aFPArray[x] ) {
    setDefault( Color.blue );
    }
    x++;
    aFPArray[x] = new FunctionParameter( "Thickness", FunctionParameter.NUMBER);
    with( aFPArray[x] ) {
    setLowerLimit( 1 );
    setUpperLimit( 10 );
    setDefault( 2 );
    }

    }

    //== Main processing function
    function main( Period, Price, Color, Thickness ) {
    var x;

    //script is initializing
    if ( getBarState() == BARSTATE_ALLBARS ) {
    return null;
    }

    if ( bInitialized == false ) {

    setDefaultBarFgColor( Color, 0 );
    setDefaultBarThickness( Math.round( Thickness ), 0 );

    nFac = Math.PI / ( Period+1 );

    nVal2 = 0;
    //calculate the denominator. This won't change
    for ( x=0; x<Math.round(Period); x++ ) {
    nVal2 += Math.sin( (x+1) * nFac );
    }

    bInitialized = true;
    }

    //called on each new bar
    if ( getBarState() == BARSTATE_NEWBAR ) {
    nBarCounter++;
    }

    nVal1 = 0;
    nMax = Math.round( Period );
    for ( x=0; x<nMax; x++ ) {
    nVal1 += Math.sin( (x+1) * nFac ) * getPrice( Price, x );
    }

    nPlot = nVal1/nVal2;

    return( nPlot );

    }


    /*************************************************
    SUPPORT FUNCTIONS
    **************************************************/

    //== gID function assigns unique identifier to graphic/text routines
    function gID() {
    grID ++;
    return( grID );
    }


    //== return price type selected by user
    function getPrice( Price, nOffset ) {

    if ( Price == "Close" ) {
    return( close(-nOffset ) );
    }
    else if ( Price == "HL/2" ) {
    return( (high(-nOffset)+low(-nOffset)) / 2 );
    }
    else if ( Price == "HLC/3" ) {
    return( (high(-nOffset)+low(-nOffset)+close(-nOffset)) / 3 );
    }
    else if ( Price == "OHLC/4" ) {
    return( (open(-nOffset)+high(-nOffset)+low(-nOffset)+close(-nOffset)) / 4 );
    }
    else if ( Price == "Low" ) {
    return( low(-nOffset) );
    }
    else if ( Price == "High" ) {
    return( high(-nOffset) );
    }
    }
    __________________________

    Thanks much

    Sam

Latest Posts

Collapse

Topics Statistics Last Post
Started by judysamnt7, 03-13-2023, 09:11 AM
4 responses
59 views
0 likes
Last Post DynamicTest  
Started by ScottWalsh, Today, 06:52 PM
4 responses
36 views
0 likes
Last Post ScottWalsh  
Started by olisav57, Today, 07:39 PM
0 responses
7 views
0 likes
Last Post olisav57  
Started by trilliantrader, Today, 03:01 PM
2 responses
22 views
0 likes
Last Post helpwanted  
Started by cre8able, Today, 07:24 PM
0 responses
10 views
0 likes
Last Post cre8able  
Working...
X