Download MetaTrader 5

Trade only once when conditions met

To add comments, please log in or register
Use the Pocket to paste your content. It is convenient!
Ivaylo
30
Ivaylo 2013.12.22 17:08 
Hello, my code is opening and closing trades as long as indicator1 > indicator2. I want to make it trade only once, in the beggining, when the conditions are met. Then wait for the conditions to be different, and when they are met again, it should start trading again. Please help me with this.
Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2013.12.22 17:13  
ivoes:
Hello, My code is opening and closing trades as long as indicator1 > indicator2. I want to make it trade only once, in the beggining, when the conditions are met. Then wait for the conditions to be different, and when they are met again, it should start trading again. Please help me with this.
Check if there is already an open trade before you open another . . .
Tjipke de Vries
6315
Tjipke de Vries 2013.12.22 17:21  
or check if closing position is also true moment opening position is true
Ivaylo
30
Ivaylo 2013.12.22 17:25  

I have already made a max order function. My point is different. It should trade only once per indication. Currently when the trade is closed it opens another trade, because the conditions are still met. This should not happen.

Here is what I tried:

int TradedSell = False;

if ((TradedSell == False) && (indicator1 < indicator2))
        {
                 if (total < MaxOrders || MaxOrders == 0){
         ticket_sell=OrderSend(Symbol(),OP_SELL,volume_weak,Bid,slippage,Bid+StopLoss*Point,
                      Bid-TakeProfit*Point,comment,magic,0,arrow_colorSell);
         return (ticket_sell);
                 TradedSell = True;
         }
        }

if ((TradedSell == True) && (indicator1 > indicator2))
        {
        TradedSell = False;
        }
Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2013.12.22 17:34  
ivoes:

I have already made a max order function. My point is different. It should trade only once per indication. Currently when the trade is closed it opens another trade, because the conditions are still met. This should not happen.

What should happen if the trading condition becomes unset and then shortly after the same condition become set again ? would that mean another trade should be opened ? if not why not ? what condition means that a trade should not be opened ? answer these questions and your code will be closer to being done.
Ivaylo
30
Ivaylo 2013.12.22 17:43  

RaptorUK:
What should happen if the trading condition becomes unset and then shortly after the same condition become set again ? would that mean another trade should be opened ? if not why not ? what condition means that a trade should not be opened ? answer these questions and your code will be closer to being done.


What should happen if the trading condition becomes unset and then shortly after the same condition become set again ?

- Well, maybe, there has to be a few points difference in the indicators. Or some other way of getting around this. I have to think about it. If the condition is met again after a few bars, yes, it should open another trade.

what condition means that a trade should not be opened ?

- indicator1 > indicator2

Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2013.12.22 17:59  
ivoes:

What should happen if the trading condition becomes unset and then shortly after the same condition become set again ?

- Well, maybe, there has to be a few points difference in the indicators. Or some other way of getting around this. I have to think about it. If the condition is met again after a few bars, yes, it should open another trade.

what condition means that a trade should not be opened ?

- indicator1 > indicator2

OK, so the situation is simple ? when indicator1 is less than indicator2 and if OKToPlaceTrade is true you place a trade, then you set OKToPlaceTrade = false, when indicator1 is greater than indicator2 you set OKToPlaceTrade = true
Ivaylo
30
Ivaylo 2013.12.23 10:37  

:( Here is the code, and it`s not working. Maybe I`m doing something wrong. bool TradeBuy and bool TradeSell are before the init and start, so it`s not setting them true on every tick.

bool TradeBuy = True;
bool TradeSell = True;

        if  ((TradeBuy == True) && (Buy == True) && (TMA1 > ichiSpanA) && (TMA2 > ichiSpanA) && (TMA3 > ichiSpanA) && (TMA1 > ichiSpanB) && (TMA2 > ichiSpanB) && (TMA3 > ichiSpanB))
        {
                 if (total < MaxOrders || MaxOrders == 0)
                 {
         ticket_buy=OrderSend(Symbol(),OP_BUY,volume_weak,Ask,slippage,Ask-StopLoss*Point,
                      Ask+TakeProfit*Point,comment,magic,0,arrow_colorBuy);
         PlaySound("alert.wav");
         return (ticket_buy);
                 TradeBuy = False;
                 return (TradeBuy);
         }
        }
        
        if ((TradeBuy == False) && (TMA2 < ichiSpanA) && (TMA2 < ichiSpanB))
        {
        TradeBuy = True;
        return (TradeBuy);
        }
        


        if ((TradeSell == True) && (Sell == True) && (TMA1 < ichiSpanA) && (TMA2 < ichiSpanA) && (TMA3 < ichiSpanA) && (TMA1 < ichiSpanB) && (TMA2 < ichiSpanB) && (TMA3 < ichiSpanB))
        {
                 if (total < MaxOrders || MaxOrders == 0){
         ticket_sell=OrderSend(Symbol(),OP_SELL,volume_weak,Bid,slippage,Bid+StopLoss*Point,
                      Bid-TakeProfit*Point,comment,magic,0,arrow_colorSell);
         PlaySound("alert.wav");
         return (ticket_sell);
                 TradeSell = False;
                 return (TradeSell);
         }
        }
        
        if ((TradeSell == False) && (TMA3 > ichiSpanA) && (TMA3 > ichiSpanB))
        {
        TradeSell = True;
        return (TradeSell);
        }
Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2013.12.23 12:03  
ivoes:

:( Here is the code, and it`s not working. Maybe I`m doing something wrong. bool TradeBuy and bool TradeSell are before the init and start, so it`s not setting them true on every tick.

Now add plenty of Print() statements to find the values of the variables you are using to make your decisions and find out what is going wrong . . .
Keith Watford
Moderator
9626
Keith Watford 2013.12.24 20:39  

You return (ticket_buy) so the 2 lines of code after that will never run.

         return (ticket_buy);
                 TradeBuy = False;
                 return (TradeBuy);
jabeen ali
14
jabeen ali 2013.12.26 05:33  
ivoes:
Hello, my code is opening and closing trades as long as indicator1 > indicator2. I want to make it trade only once, in the beggining, when the conditions are met. Then wait for the conditions to be different, and when they are met again, it should start trading again. Please help me with this.

You should to check the opening and closing condition of trade. It will help to You in business market.
To add comments, please log in or register