Custom indicators do not generate trades

 

I am fairly new to EAs. I have been writing some simple EA code to teach myself some about the system and language. I now came to the point where I wanted to create some custom indicators. After getting those to work, I started to put some simple example EAs together to see how they work.


For my first test, I merely created a variable of the custom indicator at Index=0 and another at Index=1. Let's call the variables Indicator0 and Indicator1. I then told the EA to buy when Indicator0>Indicator1 and sell when Indicator0<Indicator1. I tested on H4 USDCHF for about a week worth of time. In that week there were several times where the trading criteria were met but there were no trades executed.

When the Tester ran in visual mode, values for my custom indicator were generated and the line was drawn on the chart


As a sanity check then, I substituted the iCustom indicator in the code with a standard iMA indicator keeping everything else the same. When I re-ran, the tester executed quite a few trades.


Is there something specific or different that needs to be done to custom indicators to be able to trade using them as compared to the standard set of indicators? What step could I be missing?


Thanks!

 
ter42 wrote >>

What step could I be missing?

Showing us the code in question :)

-BB-

 
BarrowBoy:

Showing us the code in question :)

-BB-

Here is the EA sample code. Like I said, when I run this, the iCustom indicator is being plotted. I just don't know why it isn't trading when the conditions are met:


//+------------------------------------------------------------------+
//| Sample.mq4 |
//| |
//| |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""

extern double TakeProfit = 500;
extern double Lots = 0.1;
extern double TrailingStop = 30;
extern double StopLoss=50;
extern double Custom1=20;



//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
//int init()
// {
//----

//----
// return(0);
// }
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
//int deinit()
// {
//----

//----
// return(0);
// }
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
double CustomLive, CustomPrevious;
int cnt, ticket, total;

//----
// initial data checks
// it is important to make sure that the expert works with a normal
// chart and the user did not make any mistakes setting external
// variables (Lots, StopLoss, TakeProfit,
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}
//----
// to simplify the coding and speed up access
// data are put into internal variables
CustomLive=iCustom(NULL,0,"CustomInd",Custom1,1,0);
CustomPrevious=iCustom(NULL,0,"CustomInd",Custom1,1,1);

total=OrdersTotal();
if(total<1)
{
// no opened orders identified
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}

// check for long position (BUY) possibility

if(CustomLive>CustomPrevious)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"CustomTest",16384,0,Green);
//EntryBarTime = Time[0];
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
// check for short position (SELL) possibility

if(CustomLive<CustomPrevious)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"CustomTest",16384,0,Red);
//EntryBarTime = Time[0];
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
}




//----
return(0);
}
//+------------------------------------------------------------------+

Reason: