coding my first EA with icustom. however buy is to late. sell to late. close order to late. could someone take a look and help?

 

i am trying to make an EA with the slope direction indicator. not on hedging or scalping.

but i would like to start a buy when color goes from red to blue and close the order when lines changes from blue to red and start a sell. and then so on.

now i have come pretty far. except that it starts the buy to late, sell to late and close order to late.


if i start the EA on a timeframe of 15 minutes it will close the order 15 minutes after the line has changed if i start the Ea on 1 hour timeframe it will close the order 1 hour after the line has changed.

if possible could someone look into the code and could explain to me how to fix it. indicator attached as well.

thank you

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

//| sample.EA.indicatortest.mq4

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

// External Varibales

extern double LotSize = 1.0;

extern double StopLoss = 0;

extern double TakeProfit = 0;


extern int Slippage = 5;

extern int MagicNumber = 123;

extern int SlopePeriod = 80;

extern int SlopeMethod = 3;

extern int SlopePrice = 0;

extern bool bRichard=true;


//Global varibales


int BuyTicket;

int SellTicket;


double UsePoint;

int UseSlippage;



// init function


int init()

{

UsePoint = PipPoint(Symbol());

UseSlippage = GetSlippage(Symbol(),Slippage);

}


// Start Function


int start()

{


//moving averages


double SlopeUp = iCustom(NULL,0,"Slope Direction Line",SlopePeriod,SlopeMethod,SlopePrice,0,1);

double SlopeUp2 = iCustom(NULL,0,"Slope Direction Line",SlopePeriod,SlopeMethod,SlopePrice,0,2);

double SlopeDown = iCustom(NULL,0,"Slope Direction Line",SlopePeriod,SlopeMethod,SlopePrice,1,1);

double SlopeDown2 = iCustom(NULL,0,"Slope Direction Line",SlopePeriod,SlopeMethod,SlopePrice,1,2);



//Buy order

if( SlopeUp != SlopeDown && SlopeDown2 == EMPTY_VALUE && BuyTicket == 0 ) // BUY

{



OrderSelect(SellTicket,SELECT_BY_TICKET);



//Close OrderClose

if(OrderCloseTime() == 0 && SellTicket > 0)

{

double CloseLots = OrderLots();

double ClosePrice = Ask;


bool Closed = OrderClose(SellTicket,CloseLots,ClosePrice,UseSlippage,Red);

}


double OpenPrice = Ask;


//Calculate Stop loss and take profit

if(StopLoss > 0) double BuyStopLoss = OpenPrice - (StopLoss * UsePoint);

if(TakeProfit > 0) double BuyTakeProfit = OpenPrice + (TakeProfit * UsePoint);


// Open Buy Order

BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,OpenPrice,UseSlippage,

BuyStopLoss,BuyTakeProfit,"Buy Order",MagicNumber,0,Green);


SellTicket = 0;


}


// Sell Order


if(SlopeUp == SlopeDown && SlopeUp2 != EMPTY_VALUE && SellTicket == 0) // Sell

{

OrderSelect(BuyTicket,SELECT_BY_TICKET);

if(OrderCloseTime() == 0 && BuyTicket > 0)

{

CloseLots = OrderLots();

ClosePrice =Bid;


Closed = OrderClose(BuyTicket,CloseLots,ClosePrice,UseSlippage,Red);

}


OpenPrice = Bid;


if(StopLoss > 0) double SellStopLoss = OpenPrice + (StopLoss * UsePoint);

if(TakeProfit > 0) double SellTakeProfit = OpenPrice - (TakeProfit * UsePoint);


SellTicket = OrderSend(Symbol(),OP_SELL,LotSize,OpenPrice,UseSlippage,SellStopLoss,SellTakeProfit,"Sell Order",MagicNumber,0,Red);


BuyTicket = 0;


}


return(0);


}


// Pip Point Function

double PipPoint(string Currency)

{

int CalcDigits = MarketInfo(Currency,MODE_DIGITS);

if (CalcDigits == 2 || CalcDigits == 3) double CalcPoint = 0.01;

else if(CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001;

return(CalcPoint);

}


// Get Slippage Function


int GetSlippage(string Currency,int SlippagePips)


{

int CalcDigits = MarketInfo(Currency,MODE_DIGITS);

if(CalcDigits == 2 || CalcDigits == 4) double CalcSlippage = SlippagePips;

else if(CalcDigits == 3 || CalcDigits == 5) CalcSlippage = SlippagePips * 10;

return(CalcSlippage);


}

//+-----------------------------END----------------------------------+

Files:
 

  1. double SlopeUp = iCustom(NULL,0,"Slope Direction Line", SlopePeriod, 
                             SlopeMethod, SlopePrice, 0, 1);
    
    double SlopeUp2 = iCustom(NULL,0,"Slope Direction Line", SlopePeriod, 
                             SlopeMethod, SlopePrice, 0, 2);
    You are looking at the indicator's output one and two bars ago. If you look at the current value "..., 0)" you won't have any delay.
    But, you may get more reversals.
 
WHRoeder:

  1. You are looking at the indicator's output one and two bars ago. If you look at the current value "..., 0)" you won't have any delay.
    But, you may get more reversals.


i did that, but it didnt work i added

double SlopeUp = iCustom(NULL,0,"Slope Direction Line",SlopePeriod,SlopeMethod,SlopePrice,0,0);
double SlopeUp2 = iCustom(NULL,0,"Slope Direction Line",SlopePeriod,SlopeMethod,SlopePrice,0,0);
double SlopeDown = iCustom(NULL,0,"Slope Direction Line",SlopePeriod,SlopeMethod,SlopePrice,1,0);
double SlopeDown2 = iCustom(NULL,0,"Slope Direction Line",SlopePeriod,SlopeMethod,SlopePrice,1,0);

however it still didnt work. on the M15 time line still bought 15 minutes to late, sold and close 15 minutes to late as well. did not do the actions on the line.

any other suggestions?

thanks inadvance

 
WHRoeder:

  1. You are looking at the indicator's output one and two bars ago. If you look at the current value "..., 0)" you won't have any delay.
    But, you may get more reversals.

Hi WHRoeder,  

I reopen an old thread but on a similar note that is what I did in order to have consolidated values...

 I was trying a simple close condition when the Slope Direction Line changes from going downwards to going upwards.

In some cases, it does not work, and I really do not understand why.


I get the value of the Slope (shifted by 1 to have a consolidated value from the last bar), and compare it to the already stored value from the iteration before.

The prints say values are correct, but this is definitely what I expect (see image).


Parts of the code:

double slopeSLOW_Plus = iCustom (Symbol(), PERIOD_CURRENT, "Slope_Direction_Line", SlopeSLOW_Period, SlopeSLOW_Method, SlopeSLOW_Price, 0, 1);

double slopeSLOW_Minus = iCustom (Symbol(), PERIOD_CURRENT, "Slope_Direction_Line", SlopeSLOW_Period, SlopeSLOW_Method, SlopeSLOW_Price, 1, 1);

double slopeFAST_Plus = iCustom (Symbol(), PERIOD_CURRENT, "Slope_Direction_Line", SlopeFAST_Period, SlopeFAST_Method, SlopeFAST_Price, 0, 1);

double slopeFAST_Minus = iCustom (Symbol(), PERIOD_CURRENT, "Slope_Direction_Line", SlopeFAST_Period, SlopeFAST_Method, SlopeFAST_Price, 1, 1);

...

if ( (slopeFAST_Minus_OLD!=EMPTY_VALUE) && (slopeFAST_Plus!=EMPTY_VALUE) )
{
CLOSE_TRADE
}

...

slopeSLOW_Plus_OLD = slopeSLOW_Plus;
slopeSLOW_Minus_OLD = slopeSLOW_Minus;
   
slopeFAST_Plus_OLD = slopeFAST_Plus;
slopeFAST_Minus_OLD = slopeFAST_Minus;

 

Results are (in some cases) misleading:
Definitely not the exit condition  

 

 

And in one case it also BOTH opened AND closed the trade where there was NO slope change:
Whaaaat?

What can it be?