NexusFi: Find Your Edge


Home Menu

 





Newbie scripter needs help scripting MT4 LWMA indicator


Discussion in Platforms and Indicators

Updated
    1. trending_up 2,547 views
    2. thumb_up 0 thanks given
    3. group 1 followers
    1. forum 1 posts
    2. attach_file 0 attachments




 
Search this Thread

Newbie scripter needs help scripting MT4 LWMA indicator

  #1 (permalink)
 kashix 
Los Angeles California/USA
 
Experience: Beginner
Platform: NinjaTrader
Trading: Currency Futures
Posts: 3 since Nov 2011
Thanks Given: 0
Thanks Received: 0

I'm using a strategy that uses the 2 period LWMA to get into trades as price will oscillate around this band. I want to make an indicator that plots the 4 hour LWMA on the 15 minute chart. There are 16 15 minute candles in 1 4 hour candle, therefore, 2 periods would make it 32 candles. Because it's LWMA (Linear Weighted), I can't just change the period to 32 as it will look different.


LMWA Calculation from investopedia

"For example, in a 15-day linearly-weighted moving average, today's closing price is multiplied by 15, yesterday's by 14, and so on until day 1 in the period's range is reached. These results are then added together and divided by the sum of the multipliers (15 + 14 + 13 + ... + 3 + 2 + 1 = 120)."


So a 2-four hour linearly-weighted moving average would be:
([close the 32nd candle] * 2) + ([close of the 16th candle] * 1) / (2+1))

EDIT: I want to use the LOW and HIGH instead of the close. So it would be the low of every block of X candles.


I would need a command that returns the low or high of every block of X candles.
I would also need a way to plot the return value on the chart.

Below is the moving average coding for LWMA. I figure I could take the plotting part from that as it would be easiest. And then make changes for the high and lows of every block of x candles. Any assistance would be appreciated as I have no MT4 coding experience. I'll work on breaking down the code for now. Hopefully someone can lend me a hand.

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
What broker to use for trading palladium futures
Commodities
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
How to apply profiles
Traders Hideout
MC PL editor upgrade
MultiCharts
About a successful futures trader who didnt know anythin …
Psychology and Money Management
 
  #2 (permalink)
 kashix 
Los Angeles California/USA
 
Experience: Beginner
Platform: NinjaTrader
Trading: Currency Futures
Posts: 3 since Nov 2011
Thanks Given: 0
Thanks Received: 0

(Metaquotes MA Indicator)

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- indicator parameters
extern int MA_Period=13;
extern int MA_Shift=0;
extern int MA_Method=0;
//---- indicator buffers
double ExtMapBuffer[];
//----
int ExtCountedBars=0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
int draw_begin;
string short_name;
//---- drawing settings
SetIndexStyle(0,DRAW_LINE);
SetIndexShift(0,MA_Shift);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
if(MA_Period<2) MA_Period=13;
draw_begin=MA_Period-1;
//---- indicator short name
switch(MA_Method)
{
case 1 : short_name="EMA("; draw_begin=0; break;
case 2 : short_name="SMMA("; break;
case 3 : short_name="LWMA("; break;
default :
MA_Method=0;
short_name="SMA(";
}
IndicatorShortName(short_name+MA_Period+")");
SetIndexDrawBegin(0,draw_begin);
//---- indicator buffers mapping
SetIndexBuffer(0,ExtMapBuffer);
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
if(Bars<=MA_Period) return(0);
ExtCountedBars=IndicatorCounted();
//---- check for possible errors
if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
if (ExtCountedBars>0) ExtCountedBars--;
//----
switch(MA_Method)
{
case 0 : sma(); break;
case 1 : ema(); break;
case 2 : smma(); break;
case 3 : lwma();
}
//---- done
return(0);
}

//-- cases 0-2 truncated from code

//+------------------------------------------------------------------+
//| Linear Weighted Moving Average |
//+------------------------------------------------------------------+
void lwma()
{
double sum=0.0,lsum=0.0;
double price;
int i,weight=0,pos=Bars-ExtCountedBars-1;
//---- initial accumulation
if(pos<MA_Period) pos=MA_Period;
for(i=1;i<=MA_Period;i++,pos--)
{
price=Close[pos];
sum+=price*i;
lsum+=price;
weight+=i;
}
//---- main calculation loop
pos++;
i=pos+MA_Period;
while(pos>=0)
{
ExtMapBuffer[pos]=sum/weight;
if(pos==0) break;
pos--;
i--;
price=Close[pos];
sum=sum-lsum+price*MA_Period;
lsum-=Close[i];
lsum+=price;
}
//---- zero initial bars
if(ExtCountedBars<1)
for(i=1;i<MA_Period;i++) ExtMapBuffer[Bars-i]=0;
}
//+------------------------------------------------------------------+

Started this thread Reply With Quote




Last Updated on April 27, 2012


© 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