Date Arithmetic

 

Hello

I have a fn which calcs missed daily, weekly and monthly pivots, and plots lines.

Due to the lines being triggered by buttons, the calc is done in an EA, not an indicator.  I need to plot the lines to start level with the first bar of the day (daily), week (weekly, i.e. Monday 0000) and month (1st of the month).

When the daily button is hit, I use

string lineStart = TimeToString(TimeCurrent(),TIME_DATE);

to plot the daily - easy.  For the weekly, I'm struggling with working out the most efficient way to do this.  My man will be changing the timeframe, so the line will be redrawn for the selected period() on next tick.  Worse, for the 1st day of the month, this may be a saturday, so I may need to then adjust to the Sunday evening first bar of the week.

I've not found anything really useful on the web so far, I'd appreciate advice on the matter.  Thanks

 
strontiumDog:

Due to the lines being triggered by buttons, the calc is done in an EA, not an indicator. 

FYI: buttons work fine in an indicator the same as an EA.

I'm not entirely sure what you need, but if you're working off the broker (server) times the easiest way to retrieve the start of the day / week / month is using iTime

Once you have the time, you can use iBarShift to figure out which bar that is on your current chart timeframe... But if you're drawing a line, you won't need to know the shift because the parameters are price and time.

Maybe this will help, based on the thread title? It just illustrates how you can add to (or subtract from) datetimes to get what you need (e.g. add offsets to broker time in order to get local time etc)

   #define WEEK 604800 // seconds in a week
   datetime StartWk=iTime(NULL,PERIOD_W1,0),
            EndWk  =StartWk+WEEK-1;   
   string name="MyLine";
   ObjectCreate(0,name,OBJ_TREND,0,StartWk,High[0],EndWk,High[0]);
   ObjectSetInteger(0,name,OBJPROP_RAY,false);
 

Actually, I hadn't thought of iTime with a period of whatever and a shift of 0 to return the datetime of the first bar, but of course, it does.

Using an indicator for buttons effectively means writing scripts and triggering assigned hotkeys which call them.  I already did it that way, but an EA is more self contained, and no need for hotkeys.

Thanks for the reply.

 

Glad to help.

Do you mean you want the buttons to perform trade operations? If so, yes the EA is the way to go.

 
honest_knave:

Glad to help.

Do you mean you want the buttons to perform trade operations? If so, yes the EA is the way to go.

The EA performs trade functions, and a couple of bespoke line drawing ops also.  Works now, cheers!
Reason: