closing positions

 
how can i write a code so i could close positions after crossing two ichimoku line?
 

You first create a function to retrieve the values of the Tenkan-sen and Kijun-sen lines:

void GetIchimokuValues(string symbol, int timeframe, double &tenkanSen, double &kijunSen, int shift)
{
    // Ichimoku handle
    int ichimokuHandle = iIchimoku(symbol, timeframe, 9, 26, 52);

    // Buffers for Tenkan-sen (buffer 1) and Kijun-sen (buffer 2)
    double tenkanBuffer[], kijunBuffer[];

    // Copy values from the indicator buffer
    if (CopyBuffer(ichimokuHandle, 0, shift, 1, tenkanBuffer) <= 0 ||
        CopyBuffer(ichimokuHandle, 1, shift, 1, kijunBuffer) <= 0)
    {
        Print("Failed to copy Ichimoku buffers");
        return;
    }

    // Assign the values to the referenced variables
    tenkanSen = tenkanBuffer[0];
    kijunSen = kijunBuffer[0];
}

Next, you can modify the function that detects the crossing of the Tenkan-sen and Kijun-sen lines:

bool IsIchimokuCross(string symbol, int timeframe)
{
    double tenkanCurrent, kijunCurrent;
    double tenkanPrevious, kijunPrevious;

    // Get current and previous Tenkan-sen and Kijun-sen values
    GetIchimokuValues(symbol, timeframe, tenkanCurrent, kijunCurrent, 0);
    GetIchimokuValues(symbol, timeframe, tenkanPrevious, kijunPrevious, 1);

    // Detect cross
    if ((tenkanPrevious < kijunPrevious && tenkanCurrent > kijunCurrent) ||
        (tenkanPrevious > kijunPrevious && tenkanCurrent < kijunCurrent))
    {
        return true;
    }

    return false;
}

Finally, use the IsIchimokuCross function to check for a cross and close all positions if a cross is detected:

void CloseAllPositions(string symbol)
{
    for(int i = PositionsTotal() - 1; i >= 0; i--)
    {
        ulong ticket = PositionGetTicket(i);
        if(PositionSelectByTicket(ticket))
        {
            if(PositionGetString(POSITION_SYMBOL) == symbol)
            {
                if(!PositionClose(PositionGetInteger(POSITION_TICKET)))
                {
                    Print("Failed to close position ", PositionGetInteger(POSITION_TICKET));
                }
            }
        }
    }
}

void OnTick()
{
    string symbol = _Symbol;
    int timeframe = PERIOD_M15;

    // Check for Ichimoku cross
    if(IsIchimokuCross(symbol, timeframe))
    {
        // Close all positions
        CloseAllPositions(symbol);
    }
}
 
Do you konw how can i change that so it would close after senkou spanB crossing chinkou span from below for buy because it isn't closing positions?


#include <Indicators/Trend.mqh>
CiIchimoku*Ichimoku;

#include <Trade\Trade.mqh>
#include <Trade\PositionInfo.mqh>

input int Magic = 400;
input double lot_size = 0.1;
input int TimeStartHour = 1;
input int TimeStartMin = 0;
input int TimeStopHour = 20;
input int TimeStopMin = 00;
input int points_sl = 500;
input int points_tp = 500;

CTrade Trade;
CPositionInfo PositionInfo;

void OnInit() {

   Trade.SetExpertMagicNumber(Magic);

   Ichimoku = new CiIchimoku();
   Ichimoku.Create(_Symbol,PERIOD_CURRENT,9,26,52);
  }

void OnTick() { 

   Ichimoku.Refresh(-1);
   double TenkanVal = Ichimoku.TenkanSen(0);
   double KijunVal = Ichimoku.KijunSen(0);
   double SpanAVal = Ichimoku.SenkouSpanA(26);
   double SpanBVal = Ichimoku.SenkouSpanB(26);
   double ChinkouVal = Ichimoku.ChinkouSpan(26);

   double TenkanValPrevious = Ichimoku.TenkanSen(1);
   double KijunValPrevious = Ichimoku.KijunSen(1);
   double SpanAValPrevious = Ichimoku.SenkouSpanA(27);
   double SpanBValPrevious = Ichimoku.SenkouSpanB(27);
   double ChinkouValPrevious = Ichimoku.ChinkouSpan(27);
   
   double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits); //ask price
   double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits); //bid price
   
   MqlDateTime structTime;
   TimeCurrent(structTime);
   structTime.sec = 0;
   
   structTime.hour = TimeStartHour;
   structTime.min = TimeStartMin;
   datetime timeStart = StructToTime(structTime);
   
   structTime.hour = TimeStopHour;
   structTime.min = TimeStopMin;
   datetime timeStop = StructToTime(structTime);
   
   if(TimeCurrent() >= timeStart && TimeCurrent() < timeStop) {
   if(PositionsTotal() == 0) {
   if(ChinkouValPrevious < SpanBValPrevious && ChinkouVal > SpanBVal) {
      Comment("The trend is up","\n",
              "Tenkan Sen Value is: ",TenkanVal,"\n",
              "Kijun Sen Value is: ",KijunVal,"\n",
              "Senkou Span A Value is: ", SpanAVal,"\n",
              "Senkou Span B Value is: ",SpanBVal,"\n",
              "Chikou Span Value is: ",ChinkouVal);
  
     double tp = Ask + points_tp * SymbolInfoDouble(_Symbol,SYMBOL_POINT);  
     double sl = Ask - points_sl * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
  
     Trade.Buy(lot_size,_Symbol,Ask,sl,tp); 
      
    
/*     if(ChinkouVal <= SpanBVal){
     for(int i = PositionsTotal()-1; i >= 0; i--){
     ulong posTicket = PositionGetTicket(i);
     if(PositionSelectByTicket(posTicket)){
     if(Trade.PositionClose(posTicket)){
     Print(__FUNCTION__," > #",posTicket,"was closed");
    
      }}}}  
*/   
   
      }
     } 
    }     
    
 
   if(TimeCurrent() >= timeStart && TimeCurrent() < timeStop) {
   if(PositionsTotal() == 0) {
   if(ChinkouValPrevious > SpanBValPrevious && ChinkouVal < SpanBVal) {
      Comment("The trend is down","\n",
              "Tenkan Sen Value is: ",TenkanVal,"\n",
              "Kijun Sen Value is: ",KijunVal,"\n",
              "Senkou Span A Value is: ", SpanAVal,"\n",
              "Senkou Span B Value is: ",SpanBVal,"\n",
              "Chikou Span Value is: ",ChinkouVal);
              
    double tp = Bid - points_tp * SymbolInfoDouble(_Symbol,SYMBOL_POINT); 
    double sl = Bid + points_sl * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
    
    Trade.Sell(lot_size,_Symbol,Bid,sl,tp);
    
    
/*   if (ChinkouValPrevious > SpanBValPrevious && ChinkouVal < SpanBVal) {
   for (int i = PositionsTotal()-1; i >= 0; i--) {
   ulong ticket = PositionGetTicket(i);
   if (ticket > 0 && PositionSelectByTicket(ticket)) {
   int posType = (int)PositionGetInteger(POSITION_TYPE);
   if (posType == POSITION_TYPE_BUY) {
   Trade.PositionClose(ticket);
   posType = 0;
   }}}}
 

You must add code for buy first.

ChinkouVal <= SpanBVal or ChinkouVal >= SpanBVal I suppose but your code does not compile I cannot help