MT4: My EA exits soon after taking position...

 

I want to make EA like this EA takes position when price during above 20ma and does not exit before price once goes down below 20ma then exits when price comes back above 20ma. But my EA exits soon after taking position because price is above 20ma. If someone knows how to fix this, It would be very helpful. 

 int ShortNum=ShortPosition();
            
    int sum = 0;
         int Shortsum = 0;
         int CloseStrtagy2=Shortsum;
         double Ma3=iMA(NULL,PERIOD_M5,1,0,0,0,0);
         double Ma4=iMA(NULL,PERIOD_M5,5,0,0,0,0);
      
      if(ShortNum!=0 && Ma3>Ma4 )sum = 1;

      if(sum=1 && Ma3<Ma4 )Shortsum = 1;

   
     if(Shortsum!=0 && ( CloseStrtagy2!=-1 )) 
         {
                 ExitSell=1;
                 ShortNum=0;
         }



Thanks

Ken

 
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I have moved your topic to the MQL4 and Metatrader 4 section.
 
Keith Watford:
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I have moved your topic to the MQL4 and Metatrader 4 section.
Thanks Keith! I am quite new here. I appreciate your help.
 
jok kok:

I want to make EA like this EA takes position when price during above 20ma and does not exit before price once goes down below 20ma then exits when price comes back above 20ma. But my EA exits soon after taking position because price is above 20ma. If someone knows how to fix this, It would be very helpful. 

It seems to me that it makes no sense that the exit condition should be basically the same as the entry condition.

 
Keith Watford:

It seems to me that it makes no sense that the exit condition should be basically the same as the entry condition.

Yeah you are right. but if it works only making Short position, I thought it would be better to avoid many lose trades. Thanks though.

 
Your code
 Result
int sum = 0;
     int Shortsum = 0;
     int CloseStrtagy2=Shortsum;
     double Ma3=iMA(NULL,PERIOD_M5,1,0,0,0,0);
     double Ma4=iMA(NULL,PERIOD_M5,5,0,0,0,0);
      
  if(ShortNum!=0 && Ma3>Ma4 )sum = 1;

  if(sum=1 && Ma3<Ma4 )Shortsum = 1;

   
 if(Shortsum!=0 && ( CloseStrtagy2!=-1 )) 
         {
                 ExitSell=1;
                 ShortNum=0;
         }
sum is zero.
Shortsum is zero.
CloseStrtagy2 is zero.
irrevalent. Don't use zeros, use the proper
enumerations: ENUM_MA_METHOD ENUM_APPLIED_PRICE
      
ShortNum is zero, line does nothing.

sum is zero, line does nothing.

   
Shortsum is zero, lines do nothing.




Reason: