Download MetaTrader 5

Why am I getting zero values in the function

To add comments, please log in or register
macpee
507
macpee  
      

int window = 0;
double NewProfit;
datetime CurrTime;
ChartXYToTimePrice(0, 320, 410, window, CurrTime, NewProfit);
Why am I getting zero values for CurrTime and NewProfit?
Keith Watford
Moderator
10533
Keith Watford  
macpee:
      

int window = 0;
double NewProfit;
datetime CurrTime;
ChartXYToTimePrice(0, 320, 410, window, CurrTime, NewProfit);
Why am I getting zero values for CurrTime and NewProfit?
Because you have not assigned any value to CurrTime or NewProfit
Alain Verleyen
Moderator
35038
Alain Verleyen  
macpee:
      

int window = 0;
double NewProfit;
datetime CurrTime;
ChartXYToTimePrice(0, 320, 410, window, CurrTime, NewProfit);
Why am I getting zero values for CurrTime and NewProfit?

Check the returned value of ChartXYToTimePrice().

Return Value

Returns true if successful, otherwise false. To get information about the error, call the GetLastError() function.

Alain Verleyen
Moderator
35038
Alain Verleyen  
Keith Watford:
Because you have not assigned any value to CurrTime or NewProfit
They are supposed to be set by ChartXYToTimePrice().
macpee
507
macpee  
Keith Watford:
Because you have not assigned any value to CurrTime or NewProfit
But the values are there: 320 and 410, respectively.  This values are meant to be converted into date and price and put in CurrTime and NewProfit by C
hartXYToTimePrice
Marco vd Heijden
Moderator
6137
Marco vd Heijden  
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//--- Show the event parameters on the chart
   Comment(__FUNCTION__,": id=",id," lparam=",lparam," dparam=",dparam," sparam=",sparam);
//--- If this is an event of a mouse click on the chart
   if(id==CHARTEVENT_CLICK)
     {
      //--- Prepare variables
      int      x     =(int)lparam;
      int      y     =(int)dparam;

      datetime dt    =0;
      double   price =0;
      int      window=0;
      //--- Convert the X and Y coordinates in terms of date/time
      if(ChartXYToTimePrice(0,x,y,window,dt,price))
        {
         PrintFormat("Window=%d X=%d  Y=%d  =>  Time=%s  Price=%G",window,x,y,TimeToString(dt),price);
         //--- Perform reverse conversion: (X,Y) => (Time,Price)
         if(ChartTimePriceToXY(0,window,dt,price,x,y))
            PrintFormat("Time=%s  Price=%G  =>  X=%d  Y=%d",TimeToString(dt),price,x,y);
         else
            Print("ChartTimePriceToXY return error code: ",GetLastError());
         //--- delete lines
         ObjectDelete(0,"V Line");
         ObjectDelete(0,"H Line");
         //--- create horizontal and vertical lines of the crosshair
         ObjectCreate(0,"H Line",OBJ_HLINE,window,dt,price);
         ObjectCreate(0,"V Line",OBJ_VLINE,window,dt,price);
         ChartRedraw(0);
        }
      else
         Print("ChartXYToTimePrice return error code: ",GetLastError());
      Print("+--------------------------------------------------------------+");
     }
  }
Keith Watford
Moderator
10533
Keith Watford  
Keith Watford:
Because you have not assigned any value to CurrTime or NewProfit
Sorry, I had a few beers last night. I don't know  what I must have been thinking!
honest_knave
Moderator
2342
honest_knave  
macpee:
      

int window = 0;
double NewProfit;
datetime CurrTime;
ChartXYToTimePrice(0, 320, 410, window, CurrTime, NewProfit);
Why am I getting zero values for CurrTime and NewProfit?

Works fine for me:

int window = 0;
double NewProfit;
datetime CurrTime;
if(!ChartXYToTimePrice(0, 320, 410, window, CurrTime, NewProfit)) printf("Error: %i",GetLastError());
printf("NewProfit: %f",NewProfit);
printf("CurrTime: %s",TimeToStr(CurrTime));


2017.02.03 23:21:58.453 TestScript Ger30Sb317,M1: CurrTime: 2017.02.03 17:51
2017.02.03 23:21:58.453 TestScript Ger30Sb317,M1: NewProfit: 11642.833572
macpee
507
macpee  
Thanks folks
macpee
507
macpee  
macpee:
Thanks folks
The purpose seems defeated. I realize that the coordinates is for the objects on the window and not for the chart, while the price and date are for the chart and not for the objects on the window. They work similar to label and Text on the window. I want the coordinates that will move alongside the chart (price and dates) as well. Any suggestion please?
honest_knave
Moderator
2342
honest_knave  

I don't really understand what you are trying to achieve.

Some objects are set by time / price    e.g. OBJ_RECTANGLE and OBJ_TEXT

Some objects are set by x / y     e.g. OBJ_RECTANGLE_LABEL and OBJ_LABEL 

You will need to pick the object type that suits your needs. 

Perhaps a screenshot will help us to understand?

12
To add comments, please log in or register