Please help!!!!Close current position when having a new signal

 

Hi guys,

Can you help me with my code, i need to close all current position when there is a new signal.

Example: i'm currently on a Sell position, when there's a buy signal, i want the sell signal to close at market price and open a new buy position. Thank you!

My code:

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
#include <Trade\Trade.mqh>
CTrade trade;
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
double Ask,Bid;
void OnTick()
  {
 
//---
   Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);  
   Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
   
//we calculate the ask price
   double ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   
// we calculate the bid price
   double bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   
//   create string for signal
      string signal = "";
      
      // create an array for the price data
      double myRSIArray[];
      
      //sort the array from the current candle downwards
      ArraySetAsSeries(myRSIArray,true);
      
      //difine the properties for RSI
      int myRSIDefinition = iRSI (_Symbol,_Period,18,PRICE_CLOSE);
      
      //defined EA, from current candle, for 3 candles, save in array
      CopyBuffer(myRSIDefinition,0,0,3,myRSIArray);
      
      //calculate the current RSI value
      double myRSIValue=NormalizeDouble(myRSIArray[0],2);
      
      if (myRSIValue>80)
      signal="sell";
      
      if (myRSIValue<20)
      signal="buy";
     
     //sell 0.8 lot
      if (signal=="sell" && PositionsTotal()<1)
         trade.Sell(0.80,NULL,Bid,0,0,NULL);
         
         
      //buy 0.8 lot
      if (signal=="buy" && PositionsTotal()<1)
         
         trade.Buy(0.80,NULL,Ask,0,0,NULL);
         
      //chart output
      Comment("The signal is now: ",signal);
      
      
  }

 
PhucMinh:

Hi guys,

Can you help me with my code, i need to close all current position when there is a new signal.

Example: i'm currently on a Sell position, when there's a buy signal, i want the sell signal to close at market price and open a new buy position. Thank you!

My code:

...
     //sell 0.8 lot
      if (signal=="sell")
      {
         CloseAll();
         trade.Sell(0.80,NULL,Bid,0,0,NULL);
      }
         
         
      //buy 0.8 lot
      if (signal=="buy")
      {
         CloseAll();
         trade.Buy(0.80,NULL,Ask,0,0,NULL);
      }
...
void CloseAll()
{
   for(int i=0;i<PositionsTotal();i++)
   {
      if(PositionSelectByTicket(PositionGetTicket(i)))
      {
         trade.PositionClose(PositionGetTicket(i), -1);
         i--;
      }
   }  
}

 
Yashar Seyyedin #:

Another thing is you should count buy and sell positions to decide to open a new position or close one.

 

Certainly, I'd be happy to help you with your code. Here's an example code snippet in MQL4 that should close all current positions and open a new position based on a new signal:

int start()
{
    // Check for new signal
    if (/*new signal is detected*/)
    {
        // Close all current positions
        int count = OrdersTotal();
        for (int i = count - 1; i >= 0; i--)
        {
            if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
            {
                if (OrderType() == OP_BUY || OrderType() == OP_SELL)
                {
                    OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), slippage, Blue);
                }
            }
        }
        
        // Open new position based on new signal
        if (/*buy signal*/)
        {
            OrderSend(OrderSymbol(), OP_BUY, lotsize, Ask, slippage, Ask - stoploss * Point, Ask + takeprofit * Point, "buy order", MagicNumber, 0, Green);
        }
        else if (/*sell signal*/)
        {
            OrderSend(OrderSymbol(), OP_SELL, lotsize, Bid, slippage, Bid + stoploss * Point, Bid - takeprofit * Point, "sell order", MagicNumber, 0, Red);
        }
    }
    
    return(0);
}



please let me know if it helps or not?
 
JordanBaker85 #:

Certainly, I'd be happy to help you with your code. Here's an example code snippet in MQL4 that should close all current positions and open a new position based on a new signal:



please let me know if it helps or not?
What's the point to provide mql4 code to someone asking for mql5 ?
 
Yashar Seyyedin #:


In the CloseAll() function there are i++ and i-- in a loop.

Then "i" will always have the same value.

Reason: