Want to make an ADR without using Period_D1 - MetaQuotes MetaTrader MQL Programming | futures io social day trading
futures io futures trading


Want to make an ADR without using Period_D1
Updated: Views / Replies:817 / 3
Created: by stearno Attachments:2

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors Ė all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you donít need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

Reply
 
Thread Tools Search this Thread
 

Want to make an ADR without using Period_D1

  #1 (permalink)
Elite Member
Muscat, Oman
 
Futures Experience: Intermediate
Platform: Ninja and MT4
Favorite Futures: Forex
 
Posts: 17 since Apr 2012
Thanks: 4 given, 3 received

Want to make an ADR without using Period_D1

I am relatively new to programming and trying to learn by deciding what I want to do and then researching and investigating how to do that one thing. I learn a lot and find it easier to learn with real world application.

I want the ADR(20) in the buffer for each candle, for a defined session. In my thinking right now session and day are interchangeable, because my intention in the way I think I am writing this indicator is so I could easily modify it to be for market sessions, not only days, in the future. Also, I don't want to use Period_D1, because then it will not work in backtesting, which is the main reason I need it at this time (otherwise I just use an existing ADR indicator, or use iATR with Period_D1). But I can easily see the need (or maybe it is just nice to have) this indicator to see the average range for a particular session (e.g. first 2 hours of the London open) and compare that to other periods of time.

I have looked at many examples of ADR indicator code on internet, but almost every one of them uses Period_D1 (which by using that makes the job easy, but does not meet my requirements).

Where I have gotten is I have calculated the previous day's range (a session length and the session end hour are defined in the indicator).
The difficulty I am having is getting the 20 session periods' ranges summed and then averaging them by 20.

I have used a loop, but it just does not compare to when I use Period_D1 as a sanity check. So I figured there is a bug in my code.
How can I fix what I have? Any suggestions?

Here is a piece of my code:
 
Code
 int mVcnt, ADR2, ADR1,ADR0, aVcnt, BarCnt,NumForSessionCloseBar, ShiftToDayClosebar, BarsForthePeriod,i;  
datetime aTime;      
int SesionLength = LengthofSessioninHours * 60;  
 
if(Time[0] > aTime)  // update only once per bar
 {    
  for(mVcnt=0; mVcnt<=NumBarsBack; mVcnt++)
   {  
    if(TimeHour(Time[mVcnt]) > SessionHourClosed)    
     {
       NumForSessionCloseBar           = (((TimeHour(Time[mVcnt])- SessionHourClosed)*60) + TimeMinute(Time[mVcnt]))/ ChartPeriod1; //calculate how many bars away is the close of the session.
       ShiftToDayClosebar          = (NumForSessionCloseBar + mVcnt); // adds the current candle's bar to the number of bars so can shift to the session close bar        BarsForthePeriod           = (SesionLength / ChartPeriod1); // Number of bars from the beginning of the session to the end of the seesion if 24 hours         
       //Using this below two lines as it shows the ADR for the previous session     
      ADR0 =  ((iHigh(NULL,0,iHighest(NULL,0,MODE_HIGH,BarsForthePeriod,ShiftToDayClosebar))  - iLow(NULL,0,iLowest(NULL,0,MODE_LOW,BarsForthePeriod,ShiftToDayClosebar)))/point/10);
       PreviousSessionRange[mVcnt]   =  ADR0;                                  
 
        for(i=1;i<=ADRAvg;i++,ShiftToDayClosebar += BarsForthePeriod )                  
          ADR1   +=  ((iHigh(NULL[/color],0,iHighest(NULL,0,MODE_HIGH,BarsForthePeriod,ShiftToDayClosebar))  - iLow(NULL,0,iLowest(NULL,0,MODE_LOW,BarsForthePeriod,ShiftToDayClosebar)))/point);                
 
      ADR1 = ADR1/ADRAvg;      
      AvgDayRange[mVcnt] = ADR1;
[Note: below is a little explanation of the above code, so can know what I was trying to do with each line. Note2: there might be typo errors in the above, because I had to delete out all the color codes. So I might have deleted a something important by mistake. But it is all solid in the attached.]

This piece of code is used when the current candle is after the end of the last session hour.

At the first 'if' statement, i find out where the current candle is compared to the session close hour. If the current candle time is after the session's close, then it goes here.

First I calculate the number bars away the beginning of the session is from the current candle. This then gives me the shift I need to get to the sessions' close.

Then I can calculate how many bars are in the session by dividing the number of hours of the session length by the chart period.

Then I use the shift and the session length for the ihighest and ilowest, within a loop. The loop continues 20 times. I then take that and sum at the end of the loop and divide by 20. Then I place that in the buffer and it goes on to the next candle in the orignal loop.

So as I said, the ADR does not appear to be correct.

I appreciate any guidance, explanations, example code, etc.

I am first to admit that my method of code might not be the best, cleanest, nor the quickest way. But it is how my thought process was and the extent of my understanding so far to this point. If I may be so bold to presume to ask, but could you focus your guidance and advice to help me fix my current way of doing it. If it is completely wrong and there is no way for me to accomplish my goal, then sure, I am ready to hear that and learn the proper way. But if I can do it with making some changes with what I have, I would like that more.

I thank you in advance.

-Stearno

Attached Files
Register to download File Type: mq4 adrbcalculationa-bdraftu1.mq4 (6.3 KB, 2 views)
Reply With Quote
 
  #2 (permalink)
Quick Summary
Quick Summary Post

Quick Summary is created and edited by users like you... Add FAQ's, Links and other Relevant Information by clicking the edit button in the lower right hand corner of this message.

 
  #3 (permalink)
Elite Member
Muscat, Oman
 
Futures Experience: Intermediate
Platform: Ninja and MT4
Favorite Futures: Forex
 
Posts: 17 since Apr 2012
Thanks: 4 given, 3 received


I tried a different approach, that I think is cleaner than my first attempt.

But now, the buffer results in the previous session's range. It does not average them across the 20 sessions. How do I fix this?

 
Code
//+------------------------------------------------------------------+
//|                             ASR Any Timeframe-called.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
 

#property copyright "Stearno"
#property indicator_buffers 1
#property indicator_chart_window

extern string  Sess1Beg = "00:00";
extern string  Sess1End = "24:00";
extern int  	NumBarsBack = 2000;
extern int 		ASRAvg = 20;

double   AvgSessionRange[];

int  	         ChartPeriod1;
string 	      symbol;  
int    	      digits, period, mult = 1; 
double 	      point;
 
//+------------------------------------------------------------------+
int init()
{
   period       =  Period() ;    
   symbol       =  Symbol() ;
   digits       =  Digits ;   
   point        =  Point ;
 
   if(digits == 5) { mult = 10; digits = digits - 1 ; point = point; }   
   if(digits == 3) { mult = 10; digits = digits - 1 ;point = point * 1 ;}

  int Poin = Point;
	//Checking for unconvetional Point digits number
   if ((Point == 0.00001) || (Point == 0.001))
   {
      Poin *= 10;
   }

    SetIndexBuffer(0,AvgSessionRange);
    SetIndexEmptyValue(0,0.0);
    SetIndexLabel(0,"AvgSessionRange");
    
     if(period == PERIOD_M1) {ChartPeriod1 = 1;}
     if(period == PERIOD_M5) {ChartPeriod1 = 5;}
     if(period == PERIOD_M15) {ChartPeriod1 = 15;}
     if(period == PERIOD_M30) {ChartPeriod1 = 30;}
     
  return(0);
   }

//+------------------------------------------------------------------+
int deinit()
{

   return(0);
}
   
//+------------------------------------------------------------------+
int start()
{
	 //------------------------------------------Day's ADR Calculation-----------------------------------------
   int mVcnt;
   datetime aTime;
   
if(Time[0] > aTime)                                // update only once per bar
{     
 for(mVcnt=1; mVcnt<=NumBarsBack; mVcnt++)         //go back each bar so the average is on each bar
 {
  int j  =  mVcnt, i;                                 // makes the j bar equal to the current bar in the loop count
  double SumSessionRange;
    
  for(i=1 ; i<=ASRAvg ; i++)                       // loop to find the last 20 sessions
 	{ 
 	  int iTimeInSeconds =  Time[j + (1440/ChartPeriod1)];                                     //finds the time in seconds from the j bar + number of bars to go to previous day
     int iDateTimeBegin   = StrToTime(TimeToStr(iTimeInSeconds, TIME_DATE) + " " + Sess1Beg);   //TimeToStr gives the date from the time in seconds.  So yeilds mm.dd.yyyy + blank space + hour string
     int iDateTimeEnd     = StrToTime(TimeToStr(iTimeInSeconds, TIME_DATE) + " " + Sess1End);
     int iBeginsBarShift  = iBarShift(NULL, 0, iDateTimeBegin);                                 //finds the barshift for the datetime of begin and end
         //Print("BegBarShift","",i,"",iBeginsBarShift);
     int iEndsBarShift    = iBarShift(NULL, 0, iDateTimeEnd);
         //Print("EndBarshift","",i,"",iEndsBarShift);
     double iHighestHigh  = High[iHighest(NULL, 0, MODE_HIGH, iBeginsBarShift - iEndsBarShift, iEndsBarShift)];
         //Print("High","",i,"",iHighestHigh);
     double iLowestLow    = Low[iLowest(NULL, 0, MODE_LOW, iBeginsBarShift - iEndsBarShift, iEndsBarShift)];
         //Print("Low","",i,"",iLowestLow);
	  double SessionRange   =  (iHighestHigh - iLowestLow);
	  SumSessionRange +=  SessionRange;
	      
	} // do the above 20 times for each session - so need to add a session count down    
  AvgSessionRange[mVcnt] = SumSessionRange / ASRAvg;

 } // bar-by-bar for statement
 Comment(AvgSessionRange[0]+" "+AvgSessionRange[1]+" "+AvgSessionRange[2]+" "+AvgSessionRange[3]+" "+AvgSessionRange[4]+" "+AvgSessionRange[5]);
   aTime = Time[0]; //this sets ntime's value as the current time, which means this will not run again until next bar. 
} // end of if statement   
 //------------------------------------------End of Day's ADR Calculation-----------------------------------------
 return(0);   
 }
Thanks.

Stearno

Reply With Quote
 
  #4 (permalink)
Elite Member
Muscat, Oman
 
Futures Experience: Intermediate
Platform: Ninja and MT4
Favorite Futures: Forex
 
Posts: 17 since Apr 2012
Thanks: 4 given, 3 received

Okay, new attempt. I am using the arrrays to add the values, so I could troubleshoot this. I then using the buffers to show the array values.

So the array values are all the same.

What is wrong with my "for" cycle or with my assigning the value to the array element?

-Stearno

Attached Files
Register to download File Type: mq4 ASR Calculation - draft 10.mq4 (4.8 KB, 4 views)
Reply With Quote

Reply



futures io > > > > > Want to make an ADR without using Period_D1

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

Jigsaw Trading: TBA

Elite only

FuturesTrader71: TBA

Elite only

NinjaTrader: TBA

Jan 18

RandBots: TBA

Jan 23

GFF Brokers & CME Group: Futures & Bitcoin

Elite only

Adam Grimes: TBA

Elite only

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
Average Daily Range (ADR) cory The Elite Circle 119 March 5th, 2016 04:05 PM
Do educated traders make it harder to make a profit randyf Beginners and Introductions 47 March 2nd, 2014 06:33 AM
Calculating ADR of Future indexes...... mdsvtr Emini Index Futures Trading 3 November 18th, 2013 02:51 PM
How to make when you have got none dutchstew Beginners and Introductions 1 July 8th, 2013 04:06 PM
Rockwell ADR stop and target indicaor - not calculating correctly Jason11 NinjaTrader Programming 3 March 19th, 2011 07:53 PM


All times are GMT -4. The time now is 05:21 PM.

Copyright © 2017 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
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.
no new posts
Page generated 2017-12-17 in 0.09 seconds with 20 queries on phoenix via your IP 54.163.210.170