Help with EA

 

I have written the following code to test a simple strategy. However, the trades shown are nowhere near what I want. Can anyone tell me what I'm doing wrong?

//---- input parameters
extern int EntryBars = 17;
extern int EffRatioLength1 = 10;
extern int FastAvgLength1 = 7;
extern int SlowAvgLength1 = 30;
extern int EffRatioLength2 = 10;
extern int FastAvgLength2 = 11;
extern int SlowAvgLength2 = 30;
extern int TradeSize = 1;

//---- variables
int longticket = 0, shortticket = 0, mp = 0;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

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

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

double ma1 = iCustom(NULL,0,"AdaptiveMovAvg",EffRatioLength1,FastAvgLength1,SlowAvgLength1,0,0);
double ma2 = iCustom(NULL,0,"AdaptiveMovAvg",EffRatioLength2,FastAvgLength2,SlowAvgLength2,0,0);
double hh = High[iHighest(NULL,0,MODE_HIGH,EntryBars,0)];
double ll = Low[iLowest(NULL,0,MODE_LOW,EntryBars,0)];

if (Low[0]>ma2 && mp!=1)
{
longticket=OrderSend(Symbol(),OP_BUYSTOP,TradeSize,hh,0,0,0,"LE",0,iTime(NULL,0,-1),Blue);

// debug
if(longticket!=0)
{
if(OrderSelect(longticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
}

if (High[0]<ma2 && mp!=-1)
{
shortticket=OrderSend(Symbol(),OP_SELLSTOP,TradeSize,ll,0,0,0,"SE",1,iTime(NULL,0,-1),Red);

// debug
if(shortticket!=0)
{
if(OrderSelect(shortticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
}

// it is important to enter the market correctly,
// but it is more important to exit it correctly...
int total=OrdersTotal()-1;
for(int cnt=total;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if(OrderType()==OP_BUY) // long position is opened
{
// should it be closed?
if(Close[0]<=ma1)
{
OrderClose(OrderTicket(),OrderLots(),Bid,0,Magenta); // close position
longticket=0;
mp=0;
}
else mp=1;

}
else // go to short position
{
// should it be closed?
if(Close[0]>=ma1)
{
OrderClose(OrderTicket(),OrderLots(),Ask,0,Magenta); // close position
shortticket=0;
mp=0;
}
else mp=-1;
}
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
 

 
toptrader:

I have written the following code to test a simple strategy. However, the trades shown are nowhere near what I want. Can anyone tell me what I'm doing wrong?


First explain what you want and what you get, maybe then it will possible to help.
 
It's pretty simple. When the low is above ma2, I want to enter a buy stop at a price of hh. When the high is below ma2, I want to enter a sell stop at a price of ll. I then want to use ma1 as a trailing stop for any open positions. When I test the strategy, I am getting tons of sell orders, but not at the proper time/price.
 

if((OrderType()==OP_SELL||OrderType()==OP_BUY) && // check for opened position
OrderSymbol()==Symbol())
// check for symbol

 

Ok,

First you need to check whether order is already at the place, when you try to open:

if ( longticket  <= 0 ) { OrderSend(); }
if ( shortticket <= 0 ) { OrderSend(); }

This will reduce the number of pendings, I believe=)

Next, you need to check if levels that you want to use are ok to use, I mean check the Stop Level distance.

Reason: