Converting Indicator to EA

 

I have this indicator. It is writing order info on chart. 

I want to turn this indicator into an EA that will close that order when i click on text.

Is there a way to do this, any help will be appreciated..

int OnInit()

  {

//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

   for(int j=0; j<OrdersTotal(); j++)

     {

      if(OrderSelect(j,SELECT_BY_POS))

        {

         if(Symbol()==OrderSymbol() && OrderType()==OP_BUY)

           {

            string name=IntegerToString(OrderTicket());

            ObjectDelete(0,name);

            TextCreate(0,name,0,Time[20],OrderOpenPrice(),StringConcatenate("Buy = ",OrderLots(),"    Profit = ",DoubleToStr(OrderProfit(),2)),"Tahoma",10,Color(OrderProfit()<0,clrRed,clrGreen));

           }

         if(Symbol()==OrderSymbol() && OrderType()==OP_SELL)

           {

            string name=IntegerToString(OrderTicket());

            ObjectDelete(0,name);

            TextCreate(0,name,0,Time[20],OrderOpenPrice(),StringConcatenate("Sell = ",OrderLots(),"    Profit = ",DoubleToStr(OrderProfit(),2)),"Tahoma",10,Color(OrderProfit()<0,clrRed,clrGreen));

           }

        }

     }

   return(rates_total);

  }

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

bool TextCreate(const long              chart_ID=0,               

                const string            name="Text",             

                const int               sub_window=0,            

                datetime                time=0,                  

                double                  price=0,                 

                const string            text="Text",             

                const string            font="Tahoma",           

                const int               font_size=10,            

                const color             clr=clrRed,              

                const double            angle=0.0,               

                const ENUM_ANCHOR_POINT anchor=ANCHOR_LOWER,     

                const bool              back=false,              

                const bool              selection=false,         

                const bool              hidden=true,             

                const long              z_order=0)               

  {

   ResetLastError();

   if(!ObjectCreate(chart_ID,name,OBJ_TEXT,sub_window,time,price))

     {

      Print(__FUNCTION__,

            "",GetLastError());

      return(false);

     }

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);

   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);

   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);

   ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);

   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);

   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);

   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);

   return(true);

  }

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

color Color(bool P,color a,color b)

  {

   if(P) return(a);

   else return(b);

  }

//------------------------------------------------------------------
Step on New Rails: Custom Indicators in MQL5
Step on New Rails: Custom Indicators in MQL5
  • www.mql5.com
Finally we've got an opportunity to try the new trade terminal - MetaTrader 5. No doubt, it is noteworthy and has many new features as compared to its predecessor. The important advantages of this platform among others are: Essentially modified language allowing now to use the object-oriented programming, still allowing to use the rich...
Files:
 
Mehmet Cak: I want to turn this indicator into an EA t
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. Don't try to do that. There are no buffers, no IndicatorCounted() or prev_calculated. No way to know if older bars have changed or been added (history update.)

    Just get the value(s) of the indicator(s) into EA/indicator (using iCustom) and do what you want with it.

    You should encapsulate your iCustom calls to make your code self-documenting.
              Detailed explanation of iCustom - MQL4 programming forum

    But, since your indicator doesn't have any buffers you can't do that. All you can do is read the objects created.

  3. The indicator just creates text objects of opened orders. It never deletes them.

  4. Mehmet Cak: close that order when i click on text.
    Just enable ToolsOptions (control+O) → Charts and enable Show trade levels and then you can right click and close any order; no code required.

  5. Otherwise, go write your EA. Go through the text objects and see if one is selected, then close.
 
William Roeder:
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. Don't try to do that. There are no buffers, no IndicatorCounted() or prev_calculated. No way to know if older bars have changed or been added (history update.)

    Just get the value(s) of the indicator(s) into EA/indicator (using iCustom) and do what you want with it.

    You should encapsulate your iCustom calls to make your code self-documenting.
              Detailed explanation of iCustom - MQL4 programming forum

    But, since your indicator doesn't have any buffers you can't do that. All you can do is read the objects created.

  3. The indicator just creates text objects of opened orders. It never deletes them.

  4. Just enable ToolsOptions (control+O) → Charts and enable Show trade levels and then you can right click and close any order; no code required.

  5. Otherwise, go write your EA. Go through the text objects and see if one is selected, then close.

Thank you. Actually there is no need for this but i wanted this to use on tester. MT4 tester is inefficient in so many ways. I think i should find a new way  to see order profit on chart. Any way thank a lot..