Does someone know what's wrong here because positions are not closing after senkou spanB crossing chinkou span from below for buy?
see what the error code is:
if(ChinkouVal >= SpanBVal){ for(int i = PositionsTotal()-1; i >= 0; i--){ ulong posTicket = PositionGetTicket(i); if(PositionSelectByTicket(posTicket)){ if(!Trade.PositionClose(posTicket)){ Print("Failed to close position: ", posTicket, " Error: ", GetLastError()); } } } }
There is a problem with the brackets, and your closing code is embedded within the opening condition, this is why it cannot close the trades. I have corrected your code this way:
//+------------------------------------------------------------------+ //| ProjectName | //| Copyright 2020, CompanyName | //| http://www.companyname.net | //+------------------------------------------------------------------+ #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(PositionsTotal() > 0) { if(ChinkouVal <= SpanBVal) { for(int i = PositionsTotal()-1; i >= 0; i--) { ulong Ticket = PositionGetTicket(i); if(PositionSelectByTicket(Ticket) && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) { Trade.PositionClose(Ticket); } } } if(ChinkouVal >= SpanBVal) { for(int i = PositionsTotal()-1; i >= 0; i--) { ulong posTicket = PositionGetTicket(i); if(PositionSelectByTicket(posTicket) && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) { Trade.PositionClose(posTicket); } } } } 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(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); } } } } //+------------------------------------------------------------------+
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register