Stochastic system - Error

 

I created a simple EA based on 15 minutes chart..The sistem is the seguent:

- when Stochastic cross over its Moving Average then buy a lot

- when Stochastic cross under its Moving Average then close the long position

- when Stochastic cross under its Moving Average then sell short a lot

- when Stochastic cross over its Movinga Average then close the short position

But I saw that when it close a position, it attend the next bar to open a contrary position...It's possibile to make that it close the position and AT THE SAME MOMENT it open a contrary position? (Stop and Reverse)

Where is my error in the code?

 

It's possible to close and then open another order, provided that you give 6 seconds pause before opening. If you open immediately after you close then Metatrader will give "too frequent" error.

Code like this:

1. Close the order

2. Sleep(6000)

3. Open another order

 

I tried to modify my EA with your suggestion, but it doesn't work..I attached the original EA and the modified EA: can you tell me where is my error?

Files:
EAsleep.mq4  10 kb
 

I'm working on it, but I can't solve the problem..I don't understand why it doesn't reverse the position..The code seems ok, isn't it?

 
tradetrade:
I tried to modify my EA with your suggestion, but it doesn't work..I attached the original EA and the modified EA: can you tell me where is my error?

I add a new question..Hoping someone answer to me..(it's very difficult to me, these are my first steps with EAs..): how can I modify the attached file (in precedent post named EA.mq4) if I want open 2 lots and close the first lot when profit is 10 pips and leave run other lot?

I tried to write:

if (Bid > (OrderOpenPrice()+TakeProfit2*Point)&& ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

OrderClose(OrderTicket(), OneLot, Bid, Slippage, MediumSeaGreen);

where TakeProfit2 = 10, but it doesn't work..When I open Metatrader it is "grey"...Can anyone help me?

 

What I am about to say reminds of computer tech support when they ask you if you turned on the computer... Now as basic as this may sound, did you try clicking the debug function in the MT4 Editor?... I noticed a lot of people skip that step, it will point out if you mis-wrote something.

 
gazuz:
What I am about to say reminds of computer tech support when they ask you if you turned on the computer... Now as basic as this may sound, did you try clicking the debug function in the MT4 Editor?... I noticed a lot of people skip that step, it will point out if you mis-wrote something.

Sorry but I don't understand..What does it mean?

I usually write an ea and then I save the file as metatrader file..I miss some steps? (others eas work..)

 

Hey

If sleep() doesn't work, you should replace all standard ordering functions with my extended functions. Just add Ex after SelectOrder, OrderSend, OrderClose, OrderDelete, OrderModify. Example: SelectOrderEx, OrderSendEx, etc. This will eliminate many ordering problems.

And, add below code to your expert:


//+------------------------------------------------------------------+
//| Extended order execution functions for used in multiple pairs |
//| with automatic retry attempts. |
//+------------------------------------------------------------------+
int OrderSendEx(string symbol, int cmd, double volume, double price, int slippage, double stoploss, double takeprofit, string comment, int magic, datetime expiration=0, color arrow_color=CLR_NONE) {
if(!WaitWhileBusy())
{
Print("Error in OrderSendEx(): Timeout encountered");
return(-1);
}
SetBusyState();
int ticket = OrderSend(symbol, cmd, volume, price, slippage, stoploss, takeprofit, comment, magic, expiration, arrow_color);
Sleep(6000);
ReleaseBusyState();
return(ticket);
}


bool OrderCloseEx(int ticket, double lots, double price, int slippage, color Color=CLR_NONE)
{
if(!WaitWhileBusy())
{
Print("Error in OrderCloseEx(): Timeout encountered");
return(false);
}
SetBusyState();
bool ret = OrderClose(ticket, lots, price, slippage, Color);
Sleep(6000);
ReleaseBusyState();
return(ret);
}

bool OrderDeleteEx(int ticket)
{
if(!WaitWhileBusy())
{
Print("Error in OrderDeleteEx(): Timeout encountered");
return(false);
}
SetBusyState();
bool ret = OrderDelete(ticket);
Sleep(6000);
ReleaseBusyState();
return(ret);
}

bool OrderModifyEx( int ticket, double price, double stoploss, double takeprofit, datetime expiration, color arrow_color=CLR_NONE)
{
if(!WaitWhileBusy())
{
Print("Error in OrderModifyEx(): Timeout encountered");
return(false);
}
SetBusyState();
bool ret = OrderModify(ticket, price, stoploss, takeprofit, expiration, arrow_color);
if(ret)
{
Sleep(6000);
}else{
Print("Error in OrderModifyEx(): ", LastErrorText());
}
ReleaseBusyState();
return(ret);
}


bool OrderSelectEx(int index, int select, int pool = MODE_TRADES)
{
if (OrderSelect(index,select,pool)==true)
{
return(true);
}else{
Print("Error: Order #", index ," cannot be selected. ", LastErrorText());
}
}


//+------------------------------------------------------------------+
//| Calling state functions |
//+------------------------------------------------------------------+
bool WaitWhileBusy()
{
datetime OldCurTime;
int timeoutsec=6;

OldCurTime=CurTime();
while (GlobalVariableCheck("InTrade") || !IsTradeAllowed()) {
if(OldCurTime + timeoutsec <= CurTime()) {
return(false);
}
Sleep(1000);
}
return(true);
}


void SetBusyState()
{
GlobalVariableSet("InTrade", CurTime()); // set lock indicator
}

void ReleaseBusyState()
{
GlobalVariableDel("InTrade"); // clear lock indicator
}

 
tradetrade:
I add a new question..Hoping someone answer to me..(it's very difficult to me, these are my first steps with EAs..): how can I modify the attached file (in precedent post named EA.mq4) if I want open 2 lots and close the first lot when profit is 10 pips and leave run other lot? I tried to write: if (Bid > (OrderOpenPrice()+TakeProfit2*Point)&& ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {OrderClose(OrderTicket(), OneLot, Bid, Slippage, MediumSeaGreen);where TakeProfit2 = 10, but it doesn't work..When I open Metatrader it is "grey"...Can anyone help me?

The sequence of the code is:

OrderSelectEx(...)

If (Bid > OrderOpenPrice() + (TakeProfitPips * Point))

{

OrderClose(ticket, OneLot, Price, Slippage, Color);

}

Look at the bold text, especially Price. For buy order, it's Bid. For sell order, it's Ask.

 
scorpion:
The sequence of the code is: OrderSelectEx(...) If (Bid > OrderOpenPrice() + (TakeProfitPips * Point)) { OrderClose(ticket, OneLot, Price, Slippage, Color); } Look at the bold text, especially Price. For buy order, it's Bid. For sell order, it's Ask.

Sorry but I can't apply this to my EA..For example, I didn't find "OderSelecEx"..what is it? The code I use to close position is the seguent: how I have to modify it in order to your suggest?

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

//| Signal Begin(Exit Buy) |

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

if (CloseBuy1_1 < CloseBuy1_2) Order = SIGNAL_CLOSEBUY;

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

//| Signal End(Exit Buy) |

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

if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");

if (!EachTickMode) BarCount = Bars;

IsTrade = False;

continue;

I tried this, but obviously it doesn't work (...):

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

//| Signal Begin(Exit Buy) |

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

if (CloseBuy1_1 < CloseBuy1_2) Order = SIGNAL_CLOSEBUY;

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

//| Signal End(Exit Buy) |

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

if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

OrderClose(OrderTicket(), 1, Bid, Slippage, MediumSeaGreen);

If (Bid > OrderOpenPrice() + (Range6 * Point))

{OrderClose(ticket, 1, Bid, 3, Red);}

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");

if (!EachTickMode) BarCount = Bars;

IsTrade = False;

continue;

 

Read post #8 man. Add the code of extended functions and replace your normal functions with the extended ones. Example of replacing to extended functions:

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

//| Signal End(Exit Buy) |

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

if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

OrderCloseEx(OrderTicket(), 1, Bid, Slippage, MediumSeaGreen);

Reason: