Trade only once when conditions met

 
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.
 
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 . . .
 
or check if closing position is also true moment opening position is true
 

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;
        }
 
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.
 

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

 
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
 

:( 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);
        }
 
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 . . .
 

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

         return (ticket_buy);
                 TradeBuy = False;
                 return (TradeBuy);
 
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.
Reason: