Question from a newbie code learner

 

Dear Programmars

I am a super newbie who started coding about a week ago, trying to self-learn and make up an EA with my trading strategy base on Heikin Ashi and multi-time frame.

I was so happy to see my codes finally complies, however there are some error while running on the tester, such as similar problem like this https://www.mql5.com/en/forum/279663 and the trades did not go as how I wanted to be.

Doing my best to understand the mql language and modifing the codes from all sources online, here is my work along this week and hoping for somebody can point out anything that I might have missed or wrong in logic that caursed the errors.


Thank you so much for every helping hands, your reply will lead me to a big improvement. Thanks indeed.

//+------------------------------------------------------------------+
//|                                                ForFreedom001.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "ForFreedom001"
#property link      ""
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Input                 宣告                                       |
//+------------------------------------------------------------------+
input string _="//--- General Setting ---//";
input int               Magic_number                         = 5678912;
input string            Open_Order_Comment                   = "ForFreedom001";
input int               Spread_Filter                        = 20;         // Spread_Filter (0=Disable)

input string __="//--- Entry Setting ---//";
input double            Position_One_Lot_Sizes               = 1.0;
input double            Position_Two_Lot_Sizes               = 0.8;
input double            Position_Three_Lot_Sizes             = 0.6;
input double            Position_Four_Lot_Sizes              = 0.4;
input double            Position_Five_Lot_Sizes              = 0.2;
input int               Max_Number_Of_Position               = 5;

input string ___="//--- Exit Setting ---//";
input int               Stop_Loss_In_Points                  = 400;




//+------------------------------------------------------------------+
//| Variables             自定義變數                                 |
//+------------------------------------------------------------------+

double Point_Value = 0;
int    Number_Of_Buy_Order = 0;
int    Number_Of_Sell_Order = 0;
double Open_Price_Of_Buy_Order = 0;
double Open_Price_Of_Sell_Order = 0;
bool   Close_Order_Status = true ;
int    Open_Order_Status = 0;


//+------------------------------------------------------------------+
//|       Functions             其他自定義變數                       |
//+------------------------------------------------------------------+

//----------------------------------------------------------------------------------------------------------BUY/SELL單一方向全部結單

void Close_Single_Direction_All_Orders(int Operation_Type)
  {
       for(int i=OrdersTotal()-1; i>=0; i--)// loop所有order
     
       if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))//選擇訂單
         
          if(OrderMagicNumber()==Magic_number && OrderSymbol()== Symbol() && OrderType()==Operation_Type)
            (
             Close_Order_Status=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,Green) //結束
            );
 };

//------------------------------------------------------------------------------------------------------------查看LOT SIZE

double Trade_Lot_Size()
 {
  double Lot_Size = 0;
 
   if(Number_Of_Buy_Order ==0 || Number_Of_Sell_Order ==0)
    {Lot_Size = Position_One_Lot_Sizes;}
     else if(Number_Of_Buy_Order ==1 || Number_Of_Sell_Order ==1)
           {Lot_Size = Position_Two_Lot_Sizes;}
            else if (Number_Of_Buy_Order ==2 || Number_Of_Sell_Order==2)
                  {Lot_Size = Position_Three_Lot_Sizes;}
                  else if (Number_Of_Buy_Order ==3 || Number_Of_Sell_Order==3)
                        {Lot_Size = Position_Four_Lot_Sizes;}
                        else if (Number_Of_Buy_Order ==4 || Number_Of_Sell_Order ==5)
                              {Lot_Size = Position_Five_Lot_Sizes;}
                              else Alert("Full Position");
   return(Lot_Size);
 }




//-------------------------------------------------------------------------------------------------------一支trend confirmation bar (4H)
 
bool NewBar_PERIOD_H4()
         {
          static datetime prevTime_H4    = 0;
          datetime        currentTime_H4 = iTime(Symbol(),PERIOD_H4,0);
          
           if(currentTime_H4!= prevTime_H4)
           {
            prevTime_H4=currentTime_H4;
            return(true);
            };
            return(false);       
          };  

//------------------------------------------------------------------------------------------------------- Heikin Ashi Value


double Prev_HA_Open = iCustom(NULL,PERIOD_H4,"heikinashi",2,1);
double Prev_HA_Close = iCustom(NULL,PERIOD_H4,"heikinashi",3,1);
double HA_Open = (Prev_HA_Open+Prev_HA_Close)/2;
double HA_Close = (iOpen(Symbol(),PERIOD_H4,1)+iHigh(Symbol(),PERIOD_H4,1)+iLow(Symbol(),PERIOD_H4,1)+iClose(Symbol(),PERIOD_H4,1))/4;
double Current_Low = iLow(Symbol(),PERIOD_H4,1);
double Current_High = iHigh(Symbol(),PERIOD_H4,1);  


double HA_Low = MathMin(
                        MathMin(Current_Low, 
                                HA_Open),
                        HA_Close
                       );

double HA_High = MathMax(
                         MathMin(Current_High,
                                 HA_Open),
                         HA_Close
                        );



//+------------------------------------------------------------------+
//| Check_Opened_Orders                                              |
//+------------------------------------------------------------------+
void Check_Opened_Orders()
  {
  
      Number_Of_Buy_Order        =0;
      Number_Of_Sell_Order       =0;
      Open_Price_Of_Buy_Order    =0;
      Open_Price_Of_Sell_Order   =0;
      
   for(int i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
       {
        if(OrderMagicNumber()==Magic_number && OrderSymbol()==Symbol())
         {
          if(OrderType()==OP_BUY)
           {
            Number_Of_Buy_Order++;
            Open_Price_Of_Buy_Order += OrderOpenPrice();
           }
          if(OrderType()==OP_SELL)
           {
            Number_Of_Sell_Order++;
            Open_Price_Of_Sell_Order += OrderOpenPrice();
            }
          }
        }
      }
  }   

//+------------------------------------------------------------------+
//| Check_For_Close                                                  |
//+------------------------------------------------------------------+

//-----------------------------------------------------------------------------------------------------------查看BUY單是否符合條件平倉

void Check_For_Close()
  {
   if(Number_Of_Buy_Order>0)  //如果有BUY單
    {
     if(Number_Of_Buy_Order ==1 && Stop_Loss_In_Points>0 && Bid<=Open_Price_Of_Buy_Order-Stop_Loss_In_Points*Point_Value)  //----------- BUY單止蝕
      {
       Close_Single_Direction_All_Orders(OP_BUY); // Close 所有BUY單
      }
       else if(Number_Of_Buy_Order >=2 && Stop_Loss_In_Points>0 && Bid<=Open_Price_Of_Buy_Order-Stop_Loss_In_Points*Point_Value)
             {
              Close_Single_Direction_All_Orders(OP_BUY);
             }
     }
//-------------------------------------------------------------------------------------------------------------查看SELL單是否符合條件平倉
    if(Number_Of_Sell_Order>0)  //如果有SELL單
     {
      if(Number_Of_Sell_Order ==1 && Stop_Loss_In_Points>0 && Ask>=Open_Price_Of_Sell_Order+Stop_Loss_In_Points*Point_Value) //----------- SELL 單止蝕
       {
        Close_Single_Direction_All_Orders(OP_SELL);  // ------------------------------------------------------Close 所有SELL單
       }
       else if( Number_Of_Sell_Order >=2 && Stop_Loss_In_Points>0 && Ask>=Open_Price_Of_Sell_Order+Stop_Loss_In_Points*Point_Value) //----------- SELL 單止蝕
             {
              Close_Single_Direction_All_Orders(OP_SELL); // End 如果有SELL單
             }   
      }      
  } //Check_For_Close 完結



//+------------------------------------------------------------------+
//| Check_For_Open                                                   |
//+------------------------------------------------------------------+



void Check_For_Open()     //-----------------------------------------------------------------------查看户口是否符合條件開倉
  {
    if(OrdersTotal()<Max_Number_Of_Position)
     {
      if(NewBar_PERIOD_H4()) //------------------------------------------------------------------只是每一支4H HA bar 完成才run 一次
       {
         if(Spread_Filter>=MarketInfo(Symbol(), MODE_SPREAD) || Spread_Filter==0)  //---------------------Check Spread_Filter
          {
           if(HA_Open == HA_Low)  //在4H Heikin Ashi Chart, 上一支Bar 是沒有下影線
            {       
             Open_Order_Status=OrderSend(Symbol(),OP_BUY,Trade_Lot_Size(),Ask,0,0,0,Open_Order_Status,clrGreen);
            }
          
         
           if(HA_Open == HA_High)       
            {
             Open_Order_Status=OrderSend(Symbol(),OP_SELL,Trade_Lot_Size(),Bid,0,0,0,Open_Order_Status,clrRed);
            }
          } // End Spread Filter
       return;} // End NewBar_PERIOD_4H
     }     
  } //Check_For_Open 完結



//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   Point_Value=MarketInfo(Symbol(),MODE_POINT); //------------計算這户口用的Point Value 是多少, 0.0001 還是0.00001
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   Check_Opened_Orders();
   Check_For_Close();
   Check_For_Open();
  }
//+------------------------------------------------------------------+
                 


Cannot Open File - iCustom Error
Cannot Open File - iCustom Error
  • 2018.09.17
  • www.mql5.com
I am trying to return a value from an indicator buffer via iCustom, but I keep getting the error 'cannot open file': '2018.09.17 11:09:53...
 
Orders are orders. But you would probably want Positions....

Check the docs.
https://www.mql5.com/en/docs/trading

Another assumption you are doing:
iCustom does not return a value to be related to your usage!

https://www.mql5.com/en/docs/indicators/icustom

It's a handle.

Earliest call for iCustom should be in OnInit.

Do not init within global space.

 
A general hint for coding.

Warnings should be understood and not be ignored.
 
Thank you so much for the comments and advices.

They do gave me a great push to move on and improve.

Cheers
 
Dominik Egert:
Orders are orders. But you would probably want Positions....

Check the docs.
https://www.mql5.com/en/docs/trading

Another assumption you are doing:
iCustom does not return a value to be related to your usage!

https://www.mql5.com/en/docs/indicators/icustom

It's a handle.

Earliest call for iCustom should be in OnInit.

Do not init within global space.

Please note that the OP is using MQL4 (not MQL5) and as such there are only Orders (no Positions) and iCustom returns a value and not a handle.
 
Walter Tang: Dear Programmars...

Please confirm, so we can be sure to provide you accurate assistance - are you using MetaTrader/MQL v4 or MetaTrader/MQL v5?

Your source code seems to indicate MQL4, but just so we can be certain, please confirm!

 
Fernando Carreiro:

Please confirm, so we can be sure to provide you accurate assistance - are you using MetaTrader/MQL v4 or MetaTrader/MQL v5?

Your source code seems to indicate MQL4, but just so we can be certain, please confirm!

I would say that there is no doubt that it is MQL4.

Usually, I would have moved it, but I must have missed this one.

I will wait for confirmation from the OP and move the topic and delete some posts if necessary.

 
Schemas...

Sorry for posting an answer to a question that is not a solution.

I keep assuming questions in MQL5 section are related to MQL5 and do not sort out MQL4.


But aside of this, how do you determine it is MQL4? I cannot see a hint in the source.

Edit:
The only hint I found is OrderSelect() and OrderSend()

Ok. It's obvious now.
 
Dominik Egert:
Schemas...

Sorry for posting an answer to a question that is not a solution.

I keep assuming questions in MQL5 section are related to MQL5 and do not sort out MQL4.


But aside of this, how do you determine it is MQL4? I cannot see a hint in the source.

Edit:
The only hint I found is OrderSelect() and OrderSend()

Ok. It's obvious now.

The first and most obvious clue is ...

//+------------------------------------------------------------------+
//|                                                ForFreedom001.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+

However, confirmation is needed, just in case he is trying to compile MQL4 code in an MQL5 environment and is not able to get it to work, but since he stated that it compiles, then is most probably a MQL4 environment and not MQL5.

 

Thank you for all the comments.

I am working on MQL4.

Please let me know if I put this post on a incorrect section which focuing on MQL5, sorry for any incoveinenct casued.

I appreciated with all the advice indeed.

 
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.
Reason: