Gjol, Thanks for trying to help me figure this one out. I saw the same thing that youre pointing out about the final value being correct. But I'm pretty sure i need to use "shift" in some way to stretch that Daily MA out on the 4hr chart so it looks correct.
I think I may have figured it out, I'm at least getting closer. My goal is once I get this working properly is to be able to use the daily MA (10,20,50,100) as a trade signal dependent on their order and slope.
//+------------------------------------------------------------------+ //| _MA + IB Ind.mq4 | //| Copyright © 2010, MetaQuotes Software Corp. | //| http://www.metaquotes.net | //+------------------------------------------------------------------+ #property copyright "Copyright © 2010, MetaQuotes Software Corp." #property link "http://www.metaquotes.net" //+------------------------------------------------------------------+ #property indicator_chart_window #property indicator_buffers 8 #property indicator_color1 Red #property indicator_color2 LightBlue #property indicator_color3 Red #property indicator_color4 CornflowerBlue #property indicator_color5 Red #property indicator_color6 Blue #property indicator_color7 Red #property indicator_color8 DarkViolet extern int PeriodMA1 = 10; //10 day MA extern int PeriodMA2 = 20; //20 day MA extern int PeriodMA3 = 50; //20 day MA extern int PeriodMA4 = 100; //20 day MA extern int applied_price = 0; extern int iTimeFrame = PERIOD_D1; //Moving Average Timeframe //---- Buffers double MA_10[]; double MA_10Smooth[]; double MA_20[]; double MA_20Smooth[]; double MA_50[]; double MA_50Smooth[]; double MA_100[]; double MA_100Smooth[]; int iBarsPerPeriod; /* PERIOD_M1 1 1 minute PERIOD_M5 5 5 minute PERIOD_M15 15 15 minute PERIOD_M30 30 30 minute PERIOD_H1 60 1 hour PERIOD_H4 240 4 hour PERIOD_D1 1440 1 day PERIOD_W1 10080 PERIOD_MN1 43200 */ //+------------------------------------------------------------------+ int init() { if (iTimeFrame<Period()) iTimeFrame=Period(); //if Moving average period is less than current period, set timeframe to current period. //---- indicator lines SetIndexStyle(0,DRAW_LINE); SetIndexBuffer(0,MA_10); SetIndexStyle(1,DRAW_LINE); SetIndexBuffer(1,MA_10Smooth); SetIndexStyle(2,DRAW_LINE); SetIndexBuffer(2,MA_20); SetIndexStyle(3,DRAW_LINE); SetIndexBuffer(3,MA_20Smooth); SetIndexStyle(4,DRAW_LINE); SetIndexBuffer(4,MA_50); SetIndexStyle(5,DRAW_LINE); SetIndexBuffer(5,MA_50Smooth); SetIndexStyle(6,DRAW_LINE); SetIndexBuffer(6,MA_100); SetIndexStyle(7,DRAW_LINE); SetIndexBuffer(7,MA_100Smooth); //---- iBarsPerPeriod = iTimeFrame/Period(); //Number of bars within each Timeframe (hours per day) - smooth average Print("iBarsPerPeriod = ",iBarsPerPeriod); return(0); } //+------------------------------------------------------------------+ int start() { int counted_bars=IndicatorCounted(); if(counted_bars>0) counted_bars--; int limit=Bars-counted_bars; for(int i=0; i<limit; i++) { MA_10[i] = iMA(NULL,iTimeFrame,PeriodMA1,0,MODE_EMA,PRICE_TYPICAL,iBarShift(NULL,iTimeFrame,Time[i],false)); MA_20[i] = iMA(NULL,iTimeFrame,PeriodMA2,0,MODE_EMA,PRICE_TYPICAL,iBarShift(NULL,iTimeFrame,Time[i],false)); MA_50[i] = iMA(NULL,iTimeFrame,PeriodMA3,0,MODE_EMA,PRICE_TYPICAL,iBarShift(NULL,iTimeFrame,Time[i],false)); MA_100[i] = iMA(NULL,iTimeFrame,PeriodMA4,0,MODE_EMA,PRICE_TYPICAL,iBarShift(NULL,iTimeFrame,Time[i],false)); } for(i=0; i<limit; i++) { MA_10Smooth[i] = iMAOnArray(MA_10,0,iBarsPerPeriod,0,MODE_EMA,i); MA_20Smooth[i] = iMAOnArray(MA_20,0,iBarsPerPeriod,0,MODE_EMA,i); MA_50Smooth[i] = iMAOnArray(MA_50,0,iBarsPerPeriod,0,MODE_EMA,i); MA_100Smooth[i] = iMAOnArray(MA_100,0,iBarsPerPeriod,0,MODE_EMA,i); } return(0); } //+------------------------------------------------------------------+![]()

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
So I'm trying to write this as an indicator first. I want to be able to use the current daily Moving Average as a trade signal but it doesn't seem to be giving me the correct results. Any ideas on what I need to change to get this to work? Thanks.