SAME EXACT CODE but the one that I wrote just dont work

 

(Just started learning couple days ago,apologies for the sloppy coding)

Just wrote an EA which long when  (FastMA2<=SlowMA2&&FastMA1>SlowMA1) and short when (FastMA2>=SlowMA2&&FastMA1<SlowMA1). Only one position at a time.

I tried using OrderModify to close the trade just to get used to the function.

When I compile, It says    ` `-declaration without type  -     //extern int FastMA_Period = 10;  ,   `SlowMA_Period` - undeclared identifier   -    double SlowMA1= iMA(NULL,0,SlowMA_Period,0,MODE_SMA,PRICE_CLOSE,1);

and now when I COPY PASTE the exact same code extern int FastMA_Period = 10; 
                                                                           extern int SlowMA_Period = 40;  there was no error. even tho it is the exact same code.

(u can see its exactly the same //extern int FastMA_Period = 10; 
                                               //extern int SlowMA_Period = 40;   written above. I wonder what what cause this to happen? even tho its the same code.

Pls help this crueless newbie. I really dont know why this happen n feels like I need to be always recompilling every time i wrote a line in order for me to check if copy/paste from the same code works.


#define MAGIC 2020 

//extern int FastMA_Period = 10; 
//extern int SlowMA_Period = 40;  

extern int FastMA_Period = 10; 
extern int SlowMA_Period = 40;


extern double Lot= 3;
extern int Slippage= 3;

int start()
   { 
     
      if(Volume[0]>1||IsTradeAllowed()== false) return(0);
     
      
      double FastMA1= iMA(NULL,0,FastMA_Period,0,MODE_SMA,PRICE_CLOSE,1);
      double SlowMA1= iMA(NULL,0,SlowMA_Period,0,MODE_SMA,PRICE_CLOSE,1);
      double FastMA2= iMA(NULL,0,FastMA_Period,0,MODE_SMA,PRICE_CLOSE,2);
      double SlowMA2= iMA(NULL,0,SlowMA_Period,0,MODE_SMA,PRICE_CLOSE,2);
     
      
      for(int i=0;i<OrdersTotal();i++)
         { 
            if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)== false) break;
            if(OrderMagicNumber()!=MAGIC||OrderSymbol()!=Symbol()== false) continue;
            
           
            if(OrderType()==OP_BUY)
               {
                 OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-50*Point,OrderOpenPrice()+80*Point,0,White);
                 break;
                 }
             if(OrderType()==OP_SELL)
               {    
                 OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+50*Point,OrderOpenPrice()-80*Point,0,White);
                 break;
                 }   
                     }
                 
                  
       
      if(FastMA2<=SlowMA2&&FastMA1>SlowMA1)
      {
        OrderSend(Symbol(),OP_BUY,Lot,Ask,Slippage,0,0,"BuYY",MAGIC,0,Blue);
        return(0);
      }
      
      
      if(FastMA2>=SlowMA2&&FastMA1<SlowMA1)
      {
        OrderSend(Symbol(),OP_SELL,Lot,Bid,Slippage,0,0,"SeLL",MAGIC,0,Red);
        return(0);
      }
         } 
            
    
                 
              
           
 
 

MichaelSoros:

I tried using OrderModify to close the trade just to get used to the function.

and now when I COPY PASTE the exact same code there was no error.

  1. Modify modifies a trade (you set TP/SL) it doesn't close trades.
  2. It obviously wasn't the same exact code, or you would have gotten the same result. You didn't post the original, no one can help. There are no mind readers here. But these three are not the same, the first will not compile.
    extern int FastMA_Period = 10;  ,   SlowMA_Period = 40;
    
    extern int FastMA_Period = 10   ,   SlowMA_Period = 40;
    
    extern int FastMA_Period = 10;
    extern int SlowMA_Period = 40; 

  3. if(Volume[0]>1||IsTradeAllowed()== false) return(0);
    
    Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 forum.) Always use time. New candle - MQL4 forum
    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
  4. for(int i=0;i<OrdersTotal();i++){ 
       if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)== false) break;
       if(OrderMagicNumber()!=MAGIC||OrderSymbol()!=Symbol()== false) continue;
    
    You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool).
  5. In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading)
  6. OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-50*Point,OrderOpenPrice()+80*Point,0,White);
    OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+50*Point,OrderOpenPrice()-80*Point,0,White);
    OrderSend(Symbol(),OP_BUY,Lot,Ask,Slippage,0,0,"BuYY",MAGIC,0,Blue);
    
    Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  7. You are using slippage of 3. On a 5 digit broker that is 0.3 pips. On common pairs try 3 pip. You are not adjusting SL, TP, and slippage; for 4/5 digit brokers and for JPY pairs.
    double   pip          = StringFind(_Symbol,"JPY") < 00.010.0001;
    int      pipsToPoints = int(pip / _Point);
    int      pipDigits    = (int)MathLog10(pipsToPoints);
    int      slippage     = 3 * pipsToPoints;

  8. int start(){ 
    Stop using the pre-build 600 (February 3, 2014) type code. Use the new event calls.
 
Marco vd Heijden:

Yes, when I use the the

extern int FastMA_Period = 10;
extern int SlowMA_Period = 40; it works some how n the one with //( i did it to show the exact same code)  shows up errors even tho its the same


//extern int FastMA_Period = 10; 
//extern int SlowMA_Period = 40;  

extern int FastMA_Period = 10;
extern int SlowMA_Period = 40;

 
whroeder1:
  1. Modify modifies a trade (you set TP/SL) it doesn't close trades.
  2. It obviously wasn't the same exact code, or you would have gotten the same result. You didn't post the original, no one can help. There are no mind readers here. But these three are not the same, the first will not compile.

  3. Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 forum.) Always use time. New candle - MQL4 forum
    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
  4. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool).
  5. In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading)
  6. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  7. You are using slippage of 3. On a 5 digit broker that is 0.3 pips. On common pairs try 3 pip. You are not adjusting SL, TP, and slippage; for 4/5 digit brokers and for JPY pairs.
    double   pip          = StringFind(_Symbol,"JPY") < 00.010.0001;
    int      pipsToPoints = int(pip / _Point);
    int      pipDigits    = (int)MathLog10(pipsToPoints);
    int      slippage     = 3 * pipsToPoints;

  8. Stop using the pre-build 600 (February 3, 2014) type code. Use the new event calls.

Thank you so much for the feed back. I use alot of the same code from the book example that I read and do it line by line.

I apologies for the late n short response to ur wonderful feedback, Tokyo was around 23:00 n I fell asleep / n I will work n figure out one by one(1-8) what u meant n how I can debug,improve this code that i originally wrote.

I see that you have been helping out the community, this was my first time ever to post a programming question so I really really do appreciate it. and will continue to improve n refine my code. Thank you. I will reply once more when I refine my code n figure out what you have just thought me. If you have the time, please take a look and give me a feedback. All in all, I really appreciate thge help. Thank you whoeder1.

 
whroeder1:
  1. Modify modifies a trade (you set TP/SL) it doesn't close trades.
  2. It obviously wasn't the same exact code, or you would have gotten the same result. You didn't post the original, no one can help. There are no mind readers here. But these three are not the same, the first will not compile.

  3. Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 forum.) Always use time. New candle - MQL4 forum
    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
  4. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool).
  5. In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading)
  6. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  7. You are using slippage of 3. On a 5 digit broker that is 0.3 pips. On common pairs try 3 pip. You are not adjusting SL, TP, and slippage; for 4/5 digit brokers and for JPY pairs.
    double   pip          = StringFind(_Symbol,"JPY") < 00.010.0001;
    int      pipsToPoints = int(pip / _Point);
    int      pipDigits    = (int)MathLog10(pipsToPoints);
    int      slippage     = 3 * pipsToPoints;

  8. Stop using the pre-build 600 (February 3, 2014) type code. Use the new event calls.

It took me quite awhile to get this, there are still many errors.

1st and foremost, I appreciate all the information that you gave me. It really helped me along the way. 

      Im using Alpari Demo account(if that helps)

Im making an EA that only have one position at a time. Long&Short. n want it to modifies the original OrderSend() function with OrderModify().

1. The problem is that when I backtested it from M1-H4, some trades wont Modify and just tp/sl at the original pips that I set on OrderSend. The OrderModify never works on the Daily chart.

2. It also holds more than one position at a time.

3.I used this  piece of code that I found on https://www.cashbackforex.com/en-us/school/tabid/426/ID/435704/auto-define-slippage-point-values-for-5-digit-brokers  .

Am I using it right?  I had a hard time understanding this code.    When I compiled it, there was 3 "possible loss of data due to type conversion" warning sign which was all from the vSlippage. I want to check out if the slippage works but I cant seem to find it in the terminal or a function that will let me know if the slippage works. Is this the reason it have more than ONE position at a time ? due to slippage issue(if it didnt work)

double   pip          = StringFind(_Symbol,"JPY") < 00.010.0001;            --StringFind(  1st part finds a symbol(currency pair) when i backtest, second part detect if its a JPY pair. but what is x.000?)  < 00.010.0001;   ---I never seen this code before. and it was hard       to find an explanation about how this works.
int      pipsToPoints = int(pip / _Point);                   --0.001/0.0001=10
int      pipDigits    = (int)MathLog10(pipsToPoints);      --why is () needed? I never seen an (int) in it before and eager to learn from it, how does this part contribute to doing the pipstopoint?
int      slippage     = 3 * pipsToPoints;

//Global Variables, used for Adjusting 5Digits brokers
double vPoint;
double Slippage;

int OnInit()
   {
      //Detect 3/5 Digit Brokers for Point and Slippage
      if(Point==0.00001)
         {  
         vPoint= 0.0001; vSlippage= Slippage*10; 
             }
         else
         {  
         if(Point==0.001)
         {
            vPoint= 0.01; vSlippage= Slippage*10;  
            }
            else vPoint= Point; vSlippage=Slippage;
            }  return(0);
            }  

4. This code allows me to count a previous bar before the Current one. But I cant grapsh how this code allows TimePre to count be counted in the previous bar and not the current one since its been defined timePre= timeCur

I know this code works and I tested it in the log. How is by simply adding a Time[0] after that allows TimePre to be counted as the previous one and not the current one?

Also i assume thhat //once per bar is

void OnTick(){
   static datetime timeCur; datetime timePre = timeCur; timeCur=Time[0];
   bool isNewBar = timeCur != timePre;
   if(isNewBar){ 
     : // Once per bar
   }
   : // every tick
}

                      



5. Am I using RefreshRates correctly?

6. Lastly, Its been a week since the last post n I think I have spent at rougly around 60-70hours on this trying to figure out.

When I see a new function(), or new variable type,I will go do some research and after that I will a make many types of simple program just for that function and Print it out in the log to get to know the function, variable types etc. (exp. down below).

Is my approach wrong? Should i just simply write as many codes as I can instead of doing these? and debugging? Or should I spend more time just rewritting code and see if it works?(but that feels like Im writting without knowing the tools)I just started out a week and half ago (new to programming) and it feels like this approach and debugging is taking way too long.Any advice,recommendation and critisim is welcome. I just wannt get better and put in the work.



Here is the whole code

#define MAGIC 2020 
#property strict

extern int FastMA_Period = 10; 
extern int SlowMA_Period = 40;
extern double Lot= 3; //Lot
extern int vSlippage= 3;
//Global Variables, used for Adjusting 5Digits brokers
double vPoint;
double Slippage;

int OnInit()
   {
      //Detect 3/5 Digit Brokers for Point and Slippage
      if(Point==0.00001)
         {  
         vPoint= 0.0001; vSlippage= Slippage*10; 
             }
         else
         {  
         if(Point==0.001)
         {
            vPoint= 0.01; vSlippage= Slippage*10;  
            }
            else vPoint= Point; vSlippage=Slippage;
            }  return(0);
            }  
            
   void OnTick()
   {     
      //Check open 1st tick of the open bar
      //if(Volume[0]>1||IsTradeAllowed()== false) return(0);
      static datetime timeCur; datetime timePre= timeCur; timeCur= Time[0]; 
      bool isNewBar= timeCur!= timePre;
      if(isNewBar )
      {             
         
      //AdjustSlippage
           
      //移動平均1、2バー前の計算
      //double FastMA1= iMA(NULL,0,FastMA_Period,0,MODE_SMA,PRICE_CLOSE,1);       
      //double SlowMA1= iMA(NULL,0,SlowMA_Period,0,MODE_SMA,PRICE_CLOSE,1);
      //double FastMA2= iMA(NULL,0,FastMA_Period,0,MODE_SMA,PRICE_CLOSE,2);
      //double SlowMA2= iMA(NULL,0,SlowMA_Period,0,MODE_SMA,PRICE_CLOSE,2);  }     //Once Per Bar?
         
      int PositionIndex;   //Variable is the index used for the loop                //From here n down, Every Tick??
      int TotalNumbersOfOrders;   //This variable will hold the number of orders currently in the trade pool
      TotalNumbersOfOrders= OrdersTotal();
     
      
        for(PositionIndex= TotalNumbersOfOrders-1;PositionIndex>= 0;PositionIndex--)   
         { 
            if(!OrderSelect(PositionIndex,SELECT_BY_POS,MODE_TRADES)) 
            {
            Print("Order select failed,error==",OrderSelect(PositionIndex,SELECT_BY_POS,MODE_TRADES) );
            
            continue;  }   
             
            if(OrderMagicNumber()!=MAGIC|| OrderSymbol()!=Symbol()) break;
            
            //Check order type n Modifies the profit/stoploss pips
            if(OrderType()==OP_BUY)
               {
                 bool ModifyBUY= OrderModify(OrderTicket(),0,OrderOpenPrice()-50*vPoint,OrderOpenPrice()+80*vPoint,0,White); 
                 
                 Print("true or false?==",ModifyBUY,"GetLastError",GetLastError());
                 
                 break;    }
                 
             if(OrderType()==OP_SELL)
               {    
                 bool ModifySELL= OrderModify(OrderTicket(),0,OrderOpenPrice()+50*vPoint,OrderOpenPrice()-80*vPoint,0,White);
                 
                 Print("true or false?==",ModifySELL,"GetLastError",GetLastError());
                 
                 break;
                 }      
                     }  
                     
      double FastMA1= iMA(NULL,0,FastMA_Period,0,MODE_SMA,PRICE_CLOSE,1);       
      double SlowMA1= iMA(NULL,0,SlowMA_Period,0,MODE_SMA,PRICE_CLOSE,1);
      double FastMA2= iMA(NULL,0,FastMA_Period,0,MODE_SMA,PRICE_CLOSE,2);
      double SlowMA2= iMA(NULL,0,SlowMA_Period,0,MODE_SMA,PRICE_CLOSE,2);          
           //Buy Signal
      if(FastMA2<=SlowMA2&&FastMA1>SlowMA1)
      { 
        RefreshRates(); 
        int OrderBUY= OrderSend(Symbol(),OP_BUY,Lot,Ask,vSlippage,Ask-30*vPoint,Ask+40*vPoint,"BuYY",MAGIC,0,Blue);
        if(OrderBUY>0)
            {
            Print("OrderBUY placed==",OrderBUY);
            }
            else
            {
               Print("OrderBUY failed, error==",GetLastError());  
               
               }
        //return(0);   
        return;
        }
      
      
      //Short Signal
      if(FastMA2>=SlowMA2&&FastMA1<SlowMA1)
      { 
        RefreshRates();
        int OrderSELL= OrderSend(Symbol(),OP_SELL,Lot,Bid,vSlippage,Bid+30*vPoint,Bid-40*vPoint,"SeLL",MAGIC,0,Red);
        if(OrderSELL>0)
        {
        Print("OrderSELL placed==",OrderSELL);
        }
        else
        {
         Print("OrderSELL Failed, error==",GetLastError());
         }
         
         return;//return(0);
      }   
        return; }  } //return(0);    }   
         
Define Slippage and Point Values for 5-Digit Brokers | Forex Razor
  • CashBackForex
  • www.cashbackforex.com
The Slippage value, found in the fourth parameter of the OrderSend() function, represents the maximum difference in pips for the order to go through.
 
MichaelSoros:double   pip          = StringFind(_Symbol,"JPY") < 00.010.0001;            --StringFind(  1st part finds a symbol(currency pair) when i backtest, second part detect if its a JPY pair. but what is x.000?)  < 00.010.0001;   ---I never seen this code before. and it was hard       to find an explanation about how this works.
  1. Don't double post.
  2. Perhaps you should read the manual. Ternary Operator ?: - Operators - Language Basics - MQL4 Reference and StringFind - String Functions - MQL4 Reference
 
Thank you. Will keep working on it.
Reason: