Download MetaTrader 5

EA Should Open trade Once per Signal.

To add comments, please log in or register
Have you installed MetaTrader 5? Find out more about it!
Arav007
412
Arav007 2014.04.29 16:37 

I have been trying to make it work but not getting it right. Probably my logic isn't right at all.

The goal is: EA Should open trade only Once when it gets a New Signal. Even though the opened

trades got closed, it wont open any New trade unless it gets the Opposite Signal.

So for this what I have tried:

//Universal Variable
Static bool Buy_Once, Sell_Once;

//Start Function
Buy_Once=0;
Sell_Once=0;

if (Buy Condition Met)
{
 if(!Buy_Once)
  {
   Buy=Open Buy Trade function;
    if( Buy>0)
     {
       Print("Buy Order Opened");
        Buy_Once=1;
     }
    }
   }

if (Sell Condition Met)
{
 if(!Sell_Once)
  {
   Sell=Open Sell Trade function;
    if( Buy>0)
     {
       Print("Sell Order Opened");
        Sell_Once=1;
     }
    }
   }

But this is not working.

So please show me the way thus I can implement this successfully.

Thanks

Alain Verleyen
Moderator
30755
Alain Verleyen 2014.04.29 16:44  
Arav007:

I have been trying to make it work but not getting it right. Probably my logic isn't right at all.

The goal is: EA Should open trade only Once when it gets a New Signal. Even though the opened

trades got closed, it wont open any New trade unless it gets the Opposite Signal.

So for this what I have tried:

But this is not working.

So please show me the way thus I can implement this successfully.

Thanks

Of course it's not working, you reset the value every time this code is executed :

//Start Function
Buy_Once=0;
Sell_Once=0;

Following what you want, where these values need to be reset to false ? Also if "it wont open any New trade unless it gets the Opposite Signal." where do you have to use Buy_Once and Sell_Once ?

By the way, you declared them as bool, so you have to set them to true/false, not 0/1.

Arav007
412
Arav007 2014.04.29 18:23  
angevoyageur:

Of course it's not working, you reset the value every time this code is executed :

Following what you want, where these values need to be reset to false ? Also if "it wont open any New trade unless it gets the Opposite Signal." where do you have to use Buy_Once and Sell_Once ?

By the way, you declared them as bool, so you have to set them to true/false, not 0/1.

Hello,

I used to know for Boolean, True=1, False=0. That's why I used like that. Is it wrong?

I thought 'Static' wont let them change every time.

They need to be reset to False when an Opposite Signal is occurring. Like at first both are False.

Then say Buy Signals occurred. So after opening Buy Order, Buy_Once has become True. So when the EA will check on the Next tick, it'll find Other Buy Conditions met but Buy_Once=True instead of False. So it wont open any new Buy trade. Same for Sell.

Now, after Buy, Sell signal occurred. So EA will open Sell trade. At a point, again Buy signal should occur. Then Buy_Once has to be False again thus the EA can open New Buy order. And after this Buy signal, when a new Sell Signal will occur, Sell_Once has to be False too.

I tried something like this:

Buy=Buy order function;
if(Buy>0)
 {
  Print("Buy order Opened");
   Buy_Once=1; //setting Buy_Once to True
    Sell_Once=0; //Over writing the previous value thus when the Next Sell signal will occur, it'll open trade
  }

Sell=Sell order function;
if(Sell>0)
 {
  Print("Sell order Opened");
   Sell_Once=1; //setting Sell_Once to True
    Buy_Once=0; //Over writing the previous value thus when the Next Buy signal will occur, it'll open trade
  }

The reason behind such thinking was:

Say the first signal is Buy. So it'll open a Buy order and set Buy_Once=True and Sell_Once=False. So it is keeping Sell Condition Alive as Sell_Once=False now and will remain until next Sell order opens.

The EA will open next trade only when the Sell Signal will be Valid. After opening Sell trade, it'll set Sell_Once=True and Buy_Once=False. Thus the EA is opening a Sell trade and along with it converting Buy_Once from True (which was set during opening previous Buy order) to False. So when next time the Buy Signal will become Valid, the EA will get Buy_Once=False and open trade.

I'm not sure how much this Logic make sense. And of course, the way I thought static will work proved wrong.

So how this can be done?

Alain Verleyen
Moderator
30755
Alain Verleyen 2014.04.29 19:39  
Arav007:

Hello,

I used to know for Boolean, True=1, False=0. That's why I used like that. Is it wrong?

Yes, it's wrong and can lead to hidden bugs very difficult to detect.

I thought 'Static' wont let them change every time.

Static means the value is kept between each call to your code, but here you are assigning explicitly a value of 0.

They need to be reset to False when an Opposite Signal is occurring. Like at first both are False.

Then say Buy Signals occurred. So after opening Buy Order, Buy_Once has become True. So when the EA will check on the Next tick, it'll find Other Buy Conditions met but Buy_Once=True instead of False. So it wont open any new Buy trade. Same for Sell.

Now, after Buy, Sell signal occurred. So EA will open Sell trade. At a point, again Buy signal should occur. Then Buy_Once has to be False again thus the EA can open New Buy order. And after this Buy signal, when a new Sell Signal will occur, Sell_Once has to be False too.

I tried something like this:

The reason behind such thinking was:

Say the first signal is Buy. So it'll open a Buy order and set Buy_Once=True and Sell_Once=False. So it is keeping Sell Condition Alive as Sell_Once=False now and will remain until next Sell order opens.

The EA will open next trade only when the Sell Signal will be Valid. After opening Sell trade, it'll set Sell_Once=True and Buy_Once=False. Thus the EA is opening a Sell trade and along with it converting Buy_Once from True (which was set during opening previous Buy order) to False. So when next time the Buy Signal will become Valid, the EA will get Buy_Once=False and open trade.

I'm not sure how much this Logic make sense. And of course, the way I thought static will work proved wrong.

So how this can be done?

You almost get it. Put it all together.
Arav007
412
Arav007 2014.04.30 04:25  
angevoyageur:
Yes, it's wrong and can lead to hidden bugs very difficult to detect.Static means the value is kept between each call to your code, but here you are assigning explicitly a value of 0.
You almost get it. Put it all together.


Still not working.

I'm not understanding how to use the Static to keep the variable's value unchanged.

Another thing is, if I put the printing functions inside the if condition, they work but if I put them at the end of the function (as in the attached code), they don't work. Why?

    if (BuyOrder>0) //Checking if the order was opened or not
      { 
      sLog_CheckBuyConditions = sLog_CheckBuyConditions + sNL + "    Buy order sent successfully. Ticket=" + BuyOrder;
      Buy_Once=False;
      Sell_Once=True;
Print("After Opening a BUY trade, the value of Buy_Once is:",Buy_Once);
Print("After Opening a BUY trade, the value of Sell_Once is:",Sell_Once);

      }

Please give me some direct explanations/directions. I'm not capable of interpreting Hints.

Regards

Files:
Alain Verleyen
Moderator
30755
Alain Verleyen 2014.04.30 08:40  
Arav007:


Still not working.

I'm not understanding how to use the Static to keep the variable's value unchanged.

Another thing is, if I put the printing functions inside the if condition, they work but if I put them at the end of the function (as in the attached code), they don't work. Why?

Please give me some direct explanations/directions. I'm not capable of interpreting Hints.

Regards

Something like this :

  Static bool Buy_Once=false,Sell_Once=false;

   if(Buy Condition Met && !Buy_Once)
     {
      Buy=Open Buy Trade function;
      if(Buy>0)
        {
         Print("Buy Order Opened");
         Buy_Once=true;
         Sell_Once=false;
        }
     }
   if(Sell Condition Met && !Sell_Once)
     {
      Sell=Open Sell Trade function;
      if(Buy>0)
        {
         Print("Sell Order Opened");
         Sell_Once=true;
         Buy_Once=false;
        }
     }

Arav007
412
Arav007 2014.04.30 14:35  
angevoyageur:

Something like this :




Hello,

You wont believe it but honestly, I had tried exactly this yesterday! If I could show you.

But I used 0/1 instead of True/False. As a result it didn't work.

Anyway, Thanks a lot. I have learned something new.

The code is working as expected now.

I need another suggestion please.

I am trying another thing. That is, the EA will open trade Once per 'BAR'.

After searching I found this piece of code:

extern bool EnterOpenBar = true; 
int CurrentTime;

int init(){
CurrentTime= Time[0];
return(0);
}
int start(){

if (EnterOpenBar) = true)
{
if(CurrentTime != Time[0]){
// first tick of new bar found
// buy and sell conditions here
CurrentTime= Time[0];
return(0);
}
}

So far it is working. By using this in the EA, the EA will open trade on the very first Tick of a New Bar. From 2nd Tick, it wont do anything unless another bar forms.

But I'm thinking that the EA might Fail to open trade on the first Tick. So it should attempt on the next Tick to open the trade.

How can I do it? So far what I have understood from this piece of code:

At first we are initializing the time of current bar to CurrentTime.

Then it is going to check if CurrentTime is equal to the Time of Current bar or Not. If the Time doesn't match, then it assumes a New Bar got Formed.

Then it sets the Time of current bar to CurrentTime at the end of the code.

Am I right?

If I could use the Ticks of a Bar to Detect the New Bar then I could think of a logic to make it works for few more Ticks.

But not sure how to do it with Time comparison.

What if I run a Loop just before the 'OrderSend' few times to re-send request if the Order doesn't get opened at the first run?

Something like this will do the work?

for(int i=0; i<5; i++)
{
Buy=OrderSend();

if(Buy>0)
 {
  Print("Buy Order Opened Successfully");
 }
Break;
}

So if the Order Open was successful it has to Quit the Loop immediately.

Will this work? (Asking this because I don't know how to make a order Fail to open other than naturally)

Regards

Alain Verleyen
Moderator
30755
Alain Verleyen 2014.04.30 17:46  
Arav007:


...

Will this work? (Asking this because I don't know how to make a order Fail to open other than naturally)

Regards

You need to put the "break;" statement inside the "if" otherwise it will be executed even if there is an error. Also you have to check and process the error in case OrderSend fails. If you have an error, it's not worth to try to send the error 5 times without any change. And finally add a Sleep(100) or similar inside your loop.

About new bar if order fails to be send, you can add something like that in case of error :

CurrentTime= Time[1];

so next tick will be processed as if it's a new bar.

Arav007
412
Arav007 2014.04.30 17:58  
angevoyageur:

You need to put the "break;" statement inside the "if" otherwise it will be executed even if there is an error. Also you have to check and process the error in case OrderSend fails. If you have an error, it's not worth to try to send the error 5 times without any change. And finally add a Sleep(100) or similar inside your loop.

About new bar if order fails to be send, you can add something like that in case of error :

so next tick will be processed as if it's a new bar.


Hello,

Thanks for the direction and explaining things in detail.

Actually so far I have not experienced any permanent failure with OrderSend in my code. But sometime it fails to open order because of error: Invalid Price.

Also sometime the EA fails to close an existing order because of error: Off Quotes.

I am not sure how to Fix them. That is why I'm thinking of re-trying. Is there way I can fix those errors?

So the code should be like this?

for(int i=0; i<5; i++)
{
Buy=OrderSend();

if(Buy>0)
 {
  Print("Buy Order Opened Successfully");
   Break;
 }
Sleep(100);
}

Does Time[1] refer to the 2nd Tick or 2nd Bar's time?

If it is referring to the Ticks then it will be a bit easy.

Like then I can set Time[5], so it'll go upto 5 Ticks of a Newly formed bar.

Regards

Alain Verleyen
Moderator
30755
Alain Verleyen 2014.04.30 18:03  
Arav007:


Hello,

Thanks for the direction and explaining things in detail.

Actually so far I have not experienced any permanent failure with OrderSend in my code. But sometime it fails to open order because of error: Invalid Price.

Also sometime the EA fails to close an existing order because of error: Off Quotes.

I am not sure how to Fix them. That is why I'm thinking of re-trying. Is there way I can fix those errors?

So the code should be like this?

Does Time[1] refer to the 2nd Tick or 2nd Bar's time?

If it is referring to the Ticks then it will be a bit easy.

Like then I can set Time[5], so it'll go upto 5 Ticks of a Newly formed bar.

Regards

for(int i=0; i<5; i++)
{
Buy=OrderSend();

if(Buy>0)
 {
  Print("Buy Order Opened Successfully");
   Break;
 }
else
 { 
  // Error processing
  ...
  RefreshRates();
 }
Sleep(100);
}

Invalid price comes from error in your code. Off quotes maybe also, or the slippage is too high, resend the order with updated price.

EDIT : Setting CurrentTime to Time[1] is just a way for the next tick to be processed as if it was a tick of a new bar. Time[1] refers to the opening time of the last closed candle. Time[5] is for the 5th closed candle in the past. Time[0] is current candle.

Arav007
412
Arav007 2014.04.30 18:08  
angevoyageur:
Invalid price comes from error in your code. Off quotes maybe also, or the slippage is too high, resend the order with updated price.


Thanks for updating the code.

If there is problem in my code, then wont I get the Error:Invalid Price with every order?

I'm using Slippage=3. Is it too high?

Regards

12
To add comments, please log in or register