NexusFi: Find Your Edge


Home Menu

 





Sine Weighted MA (SWMA)


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one Sam7768 with 8 posts (1 thanks)
    2. looks_two cory with 3 posts (1 thanks)
    3. looks_3 Big Mike with 2 posts (0 thanks)
    4. looks_4 trendisyourfriend with 1 posts (1 thanks)
    1. trending_up 7,779 views
    2. thumb_up 3 thanks given
    3. group 5 followers
    1. forum 16 posts
    2. attach_file 6 attachments




 
Search this Thread

Sine Weighted MA (SWMA)

  #11 (permalink)
 Sam7768 
Dallas, Texas, USA
 
Experience: Beginner
Platform: NT 6.5 & Tradestation
Trading: Equities & ES
Posts: 47 since Sep 2010
Thanks Given: 21
Thanks Received: 9


Sam7768 View Post
Thank you Cory. I will try to code and post it. Thanks again for your time & effort.

Here is the code for e-signal. Thjs MA is normalized and is extremely smooth and excellent indicator for direction...Spend weekend to code it in NT 6.5 and gave up.

Any one can try coding this to 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) );
}
}

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
MC PL editor upgrade
MultiCharts
NexusFi Journal Challenge - May 2024
Feedback and Announcements
Trade idea based off three indicators.
Traders Hideout
Quant vue
Trading Reviews and Vendors
REcommedations for programming help
Sierra Chart
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
What is Markets Chat (markets.chat) real-time trading ro …
70 thanks
Spoo-nalysis ES e-mini futures S&P 500
55 thanks
Bigger Wins or Fewer Losses?
24 thanks
Just another trading journal: PA, Wyckoff & Trends
24 thanks
The Program
20 thanks
  #12 (permalink)
 Sam7768 
Dallas, Texas, USA
 
Experience: Beginner
Platform: NT 6.5 & Tradestation
Trading: Equities & ES
Posts: 47 since Sep 2010
Thanks Given: 21
Thanks Received: 9


Sam7768 View Post
Thank you Cory. I will try to code and post it. Thanks again for your time & effort.

Here is the code for NT 6.5

 
Code
Sine Weighted Moving Average (SWMA). (I am new to NT hence do not know how to make it importable, so, please copy and paste it as an indicator)


#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

Started this thread Reply With Quote
  #13 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,460 since Jun 2009
Thanks Given: 33,234
Thanks Received: 101,655


Tip
If posting code snippets, please wrap it in the [code]put code in here[/code] tags. If posting entire indicators, please attach the file instead of cutting and pasting it.



Mike

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #14 (permalink)
 
cory's Avatar
 cory 
virginia
 
Experience: Intermediate
Platform: ninja
Trading: NQ
Posts: 6,098 since Jun 2009
Thanks Given: 877
Thanks Received: 8,090

not much diff from a SMA.

Reply With Quote
  #15 (permalink)
 Sam7768 
Dallas, Texas, USA
 
Experience: Beginner
Platform: NT 6.5 & Tradestation
Trading: Equities & ES
Posts: 47 since Sep 2010
Thanks Given: 21
Thanks Received: 9


cory View Post
not much diff from a SMA.

50 period Weighted MA (WMA) and 50 period Sine Weighted MA (SWMA) combination gives simple, yet effective trend indication. Throw in 10 period WMA and 10 period SWMA, you get almost precise entry and exits within the large trend indications.

Attached the screen shots...showing only 50 MAs...

Attached Thumbnails
Click image for larger version

Name:	ES 12-10  11_26_2010 (512 Tick).jpg
Views:	258
Size:	118.5 KB
ID:	26253  
Started this thread Reply With Quote
Thanked by:
  #16 (permalink)
 
trendisyourfriend's Avatar
 trendisyourfriend 
Quebec Canada
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: AMP/CQG
Trading: ES, NQ, YM
Frequency: Daily
Duration: Minutes
Posts: 4,527 since Oct 2009
Thanks Given: 4,176
Thanks Received: 6,020

Sam,

Using Moving Average is nice and *appears to work very well in most cases. I also like to use trend lines as they seem to remove the lag inherent to a MA. Your chart is a good example of what we have to deal with every day: pullback and reversal or continuation and change of direction.

I have added some lines to your chart just to compare both approaches :

Attached Thumbnails
Click image for larger version

Name:	SineWaveMA_WeightedMAjpg.jpg
Views:	265
Size:	131.0 KB
ID:	26255  
Reply With Quote
Thanked by:
  #17 (permalink)
 
trs3042's Avatar
 trs3042 
Holland, Michigan
 
Experience: None
Platform: ninjatrader
Broker: CQG
Trading: Acoustic Guitar
Posts: 1,617 since Jun 2009
Thanks Given: 23,764
Thanks Received: 5,616


Big Mike View Post
If there is a NT7 version, please attach it so it can be backported.

Mike

Here it is Mike.

Rick

Attached Files
Elite Membership required to download: SWMA.zip
Reply With Quote




Last Updated on November 28, 2010


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts