Dislikedwhat am i doing wrong in this code void A() { double ar = iRSI(Symbol(),0,14,PRICE_CLOSE,0); string s = DoubleToStr(ar,0); ObjectCreate(0,"R",OBJ_ARROW_RIGHT_PRICE,0,0,ar); ObjectSetText("R",s,10,"Tahoma",Yellow); }Ignored
- The Time1 field is 0, so the object is getting created at time=1970.01.01, far to the left of all the bars on the chart
- The Price1 field is the value of RSI, which is a range 0<ar<100. As a *price* on a chart, the RSI value makes no sense
- The choice of "OBJ_ARROW_RIGHT_PRICE" does not seem suited for what I think you're trying to do, which is to display RSI value somewhere.
Better to use "OBJ_TEXT" or "OBJ_LABEL" - Even if the ObjectCreate of an OBJ_TEXT (or Arrow) were to work, it would set the time and price only ONCE. (A 2nd ObjectCreate of an existing object does nothing to update the fields, rather these must be updated separately)
Here is an a simple indicator example that displays the RSI value as either an OBJ_TEXT near the current bar and close price; or as an OBJ_LABEL.
Notice also that it has an option to run no more than once-per-second (but only when a tick arrives); or it can run with every tick.
It only creates the object once, thereafter updating the value, and -- if OBJ_TEXT -- it updates the location.
If it's an OBJ_LABEL, the label can be manually moved (as always), but it will stay where you put it if you just change the Timeframe of the chart.
Consider this as a template (** but see P.S.) that could be used to display any value(s) of interest, this one showing RSI(n):
Inserted Code
/* A simple indicator example that displays the current RSI(n) value as a Text (near the current bar and price), or as a fixed-location Label. Ref: https://www.forexfactory.com/thread/post/14144187#post14144187 2022-SEP-20 v1.0 by pips4life */ #property version "1.0" #property strict // strict mode is *optional*, but using it gives more compiler warnings/errors that may help to write cleaner code #property indicator_chart_window // Use if an indicator. //#property show_inputs // Use if this is a *script*, and if you want it to popup the Input variable(s) enum useObject { Draw_Label, Draw_Text}; // Unless manually set differently, the 1st argument Draw_Label==0, the 2nd Draw_Text==1 input useObject DrawText_vs_Label = Draw_Text; input int RSI_period = 14; input bool runNoMoreThanOncePerSecond = true; input color objectColor = clrRed; string objName = "myRSI"; datetime bars_forward; int OnInit() { bars_forward = 3*PeriodSeconds(); // Why in OnInit? This does the multiplication only once, rather than every time the value is updated. return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { // By NOT deleting this when it's an OBJ_LABEL just for a CHARTCHANGE (e.g. changing the Timeframe), an OBJ_LABEL that was manually moved will stay where it is when changing TF's! if(DrawText_vs_Label == Draw_Text || reason != REASON_CHARTCHANGE) ObjectDelete(objName); } //+-------------------------------------------------------------------------+ //| Indicator program start function (old, pre-build-600 MQL4 style) | //| One can change to OnCalculate(...) {} if necessary | //+-------------------------------------------------------------------------+ void start() { //--- if(runNoMoreThanOncePerSecond) { static datetime last_TimeCurrent = TimeCurrent(); if( TimeCurrent() == last_TimeCurrent) return; last_TimeCurrent = TimeCurrent(); } getRsi(); } //+------------------------------------------------------------------+ void getRsi() { double ar = iRSI(Symbol(),0,RSI_period,PRICE_CLOSE,0); string desc = "RSI("+IntegerToString(RSI_period)+")="+DoubleToStr(ar,0); // Original code: //ObjectCreate(0,objName,OBJ_ARROW_RIGHT_PRICE,0,0,ar); // Doesn't work right because: // The time=0, therefore 1970.01.01, so it's far to the left of any bars. // The price=ar, which for an iRSI value is 0<ar<100. As a *price*, value of 'ar' makes no sense. // The TIME1 and PRICE1 fields ONLY update once when created, but never update again. // A little better, not acceptable yet: //ObjectCreate(0,objName,OBJ_ARROW_RIGHT_PRICE,0,Time[0],Close[0]); // However, a PRICE arrow displays the price at which the arrow is placed, not the value of 'ar' // Again, the TIME1 and PRICE1 fields ONLY update once when created, but never update again. // Below will display a Text(if true) or a Label(if false) that will update the value each time it runs. if(DrawText_vs_Label) // or equivalent: if(DrawText_vs_Label == Draw_Text) // where Draw_Text=1 { // A choice of OBJ_TEXT: if(ObjectCreate(0,objName,OBJ_TEXT,0,0,0) ) { // The object is created only once, and then one-time properties are set here ObjectSetInteger(0,objName,OBJPROP_ANCHOR,ANCHOR_LEFT); } // Since this is an OBJ_TEXT, update the time,price location every time this runs: ObjectSetInteger(0,objName,OBJPROP_TIME1,Time[0]+bars_forward ); ObjectSetDouble(0,objName,OBJPROP_PRICE1,Close[0]); } else { // A choice of an OBJ_LABEL at an arbitrary location on the chart: if(ObjectCreate(0,objName,OBJ_LABEL,0,0,0) ) { // The object is created only once, and then one-time properties are set here ObjectSetInteger(0,objName,OBJPROP_CORNER,CORNER_RIGHT_UPPER); ObjectSetInteger(0,objName,OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER); ObjectSetInteger(0,objName,OBJPROP_XDISTANCE,100); ObjectSetInteger(0,objName,OBJPROP_YDISTANCE,50); // Note, by setting the location only once upon creation, the label can be selected and moved anywhere. } } // Update the value: ObjectSetText(objName,desc,10,"Tahoma",objectColor); }
Good luck.
Kent (pips4life)
P.S. My code example attempts to run "ObjectCreate" every time. This only creates the object once, but every other attempt is wasted effort in the code, though it still works as-is.
An even better example would have moved the ObjectCreate operations into the OnInit() section, and then have the 'getRsi()' routine ONLY update the value and location (if OBJ_TEXT). As a coding exercise...
- See if you can move JUST the ObjectCreate commands into OnInit, and leave only the minimum necessary real-time updates in 'getRsi()'.
- When it displays an OBJ_TEXT, notice that text is right on top of the current Bid price line, which is not ideal. What would you change in this code to make the Text appear (when updated, at least) just *above* the Bid price line? (It's a single property value).
Attached File(s)
Display_RSI.mq4
4 KB
|
189 downloads