[WARNING CLOSED!] Any newbie question, so as not to clutter up the forum. Professionals, don't go by. Can't go anywhere without you. - page 797

 

Fellow comrades! This is my attempt #2 to create a miracle tool that reads ROC AUDUSD and GBPUSD to trade on GBPAUD!) It's a bit primitive, but I really wanted to try it!!! When I run it in the tester without optimizing it, it freezes, but keeps loading successfully... and in the end - not a single trade. Well clearly I screwed up, but where, plz, tell me, knowledgeable people!!! :)) This is an EA code. It takes data from a normal custom ROC.

//+------------------------------------------------------------------+
//|                                                    Robot_Rocky_Rich |
//|                                                          JonsonAlla |
//|                                                    deep_ampik@bk.ru |
//+------------------------------------------------------------------+

extern double TakeProfit = 700;
extern double Sl = 200;
extern double Lots = 0.01;
             
//+------------------------------------------------------------------+
int start()
 {
  int cnt, ticket;
  double x1= iCustom("AUDUSD",PERIOD_H1,"ROC",12,1,0);
  double x2= iCustom("GBPUSD",PERIOD_H1,"ROC",12,1,0); 
  
  
     
  /**total=OrdersTotal();
  if(total<1)//проверка количества ордеров 
   {
   if(AccountFreeMargin()<(1000*Lots))
    {
     Print("Недостаточно средств = ", AccountFreeMargin());
     return(0);  
    }**/
  if (x1<x2)
   {
    ticket=OrderSend("GBPAUD",OP_BUY,Lots,Ask,30,Bid-Sl*Point,Ask+TakeProfit*Point,"-",0,0,Green);
    if(ticket>0)
     {
      if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("открыта позиция BUY : ",OrderOpenPrice());
     }
    else Print("Ошибка при открытии BUY позиции : ",GetLastError());          
    return(0);
   }
  if (x1>x2) 
   {
    ticket=OrderSend("GBPAUD",OP_SELL,Lots,Bid,30,Ask+Sl*Point,Bid-TakeProfit*Point,"-",0,0,Red);
    if(ticket>0)
     {
      if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("открыта позиция SELL : ",OrderOpenPrice());
     }
    else Print("Ошибка при открытии SELL позиции : ",GetLastError());
    return(0); 
   }
   return(0);
  }
  
 
Vinin:

IndicatorCounted() does not work in EAs. It is only intended for indicators. Maybe we should widen the problem a bit. There are different ways of solving it. The easiest one is to optimize the indicator (but not always possible), you can transfer the calculations to the Expert Advisor, but again not always possible. You actually have to look at the indicator

I want to transfer the calculation code to an EA, in an indicator the calculation code starts with a loop...how can I replace it with a more correct one7

int CountedBars=IndicatorCounted();
if(CountedBars< 0) CountedBars= 0;
if(CountedBars> 0) CountedBars--;
cnt = Bars - CountedBars;

for(int i = 0; i < cnt ;i++)
 
obla4ko:

Fellow comrades! This is my attempt #2 to create a miracle tool that reads ROC AUDUSD and GBPUSD to trade on GBPAUD!) It's a bit primitive, but I really wanted to try it!!! When I run it in the tester without optimizing it, it freezes, but keeps loading successfully... and in the end - not a single trade. Well clearly I screwed up, but where, plz, tell me, knowledgeable people!!! :)) This is code of so called Expert Advisor, it takes data from usual custom ROC.


Try with primitive history loading control

Files:
 
Vinin:


Try with primitive control of history loading

Maybe it's not correct just to compare x1 and x2, but should be attached to something, say, to the last bar? By indicator logic, EA should open at least one position and not close it, because closing is not implemented in the program :)), and ROC of one currency is always greater than ROC of the other ...

 

Maybe the indicator call isn't quite right. I'll have to look for it myself. It just seems that the buffer should be zero, not first.

 

I don't have such an indicator

 
T-G:

I want to transfer the calculation code to the Expert Advisor, the calculation code in the indicator starts with a loop...how can I replace it with a more correct one7

1. Why?????????????????????

But, if you really need to, then

2. Why the fuck am I answering to nothing? Told you - there's an article by Kositsyn on this in the articles (it's right here, link above). How many times must I repeat it to you? Maybe you have something else to do?

 
Vinin:

I don't have such an indicator

Here is the code for the ROC indicator

//+------------------------------------------------------------------+
//|                                                          ROC.mq4 |
//|                                    Copyright © 2006, Robert Hill |
//+------------------------------------------------------------------+

#property  copyright "Copyright © 2006, Robert Hill"
//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 1
#property  indicator_color1  Red
//---- indicator parameters
extern int RPeriod = 12;
extern bool UsePercent = true;
//---- indicator buffers
double RateOfChange[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   SetIndexStyle(0, DRAW_LINE);
   SetIndexDrawBegin(0, RPeriod);
   IndicatorDigits(Digits + 1);
//---- indicator buffers mapping
   if(!SetIndexBuffer(0, RateOfChange))
       Print("cannot set indicator buffers!");
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("ROC(" + RPeriod + ")");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   double ROC, CurrentClose, PrevClose;
   int counted_bars = IndicatorCounted();
//---- check for possible errors
   if(counted_bars < 0) 
       return(-1);
//---- last counted bar will be recounted
   if(counted_bars > 0) 
       counted_bars--;
   limit = Bars - counted_bars - RPeriod;
//---- ROC calculation
   for(int i = 0; i < limit; i++)
     {
       CurrentClose = iClose(NULL, 0, i);
       PrevClose = iClose(NULL, 0, i + RPeriod);
       ROC = CurrentClose - PrevClose;
       //----
       if(UsePercent)
         {
           if(PrevClose != 0)
               RateOfChange[i] = 100 * ROC / PrevClose;
         }
       else
           RateOfChange[i] = ROC;
     }   
//---- done
   return(0);
  }
//+------------------------------------------------------------------+
 

"Doesn't anyone need to mop the floors? I'll come and wash them! " (с)

Here is the transfer of the indicator code to the Expert code.

 
obla4ko:

Here is the code for the ROC indicator


There is an error in the indicator code.