MA cross support for trading

 

Hello all,

I am newcomer and first programming, please kindly help me to review and correct this code, it is error report after being compiled. 



Thank you for your helps,



#property version   "1.00"

#include <Trade/Trade.mqh>

CTrade trade;


int OnInit() 
{
     return(INIT_SUCCEEDED);
  }


void OnDeinit(const int reason)
  
{

  }


void OnTick()
  {
   static datetime timestamp=0;
   datetime time = iTime(_Symbol,PERIOD_CURRENT,0);
   result        = false;
   if(timestamp != time)
   {
    timestamp = time; 
    result = true;
    }
   
   
   
   static int handleSlowMa = iMA(_Symbol,PERIOD_CURRENT,200,0,MODE_SMA,PRICE_CLOSE);
   double slowMaArray[];
   CopyBuffer(handleSlowMa,0,1,2,slowMaArray);
   ArraySetAsSeries(slowMaArray,true);
   
   static int handleFastMa = iMA(_Symbol,PERIOD_CURRENT,20,0,MODE_SMA,PRICE_CLOSE);
   double fastMaArray[];
   CopyBuffer(handleFastMa,0,1,2,fastMaArray);
   ArraySetAsSeries(fastMaArray,true);
   
   if(fastMaArray[0] > slowMaArray[0] && fastMaArray [1] < slowMaArray[1])
   {
   Print(" higher ");
   double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
   double sl = ask - 100 * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
   double tp = ask + 100 * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
   trade.Buy(0.01,_Symbol,ask,sl,tp,"Buy")
   }
   
   if(fastMaArray[0] < slowMaArray[0] && fastMaArray [1] > slowMaArray[1])
   {
   Print(" lower ");
   double ask = SymbolInfoDouble(_Symbol,SYMBOL_BID);
   double sl = bid + 100 * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
   double tp = bid - 100 * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
   trade.Sell(0.01,_Symbol,bid,sl,tp,"Sell")
   }
   
   
   
   Comment ("\nslowMaArray[0]: ",slowMaArray[0],
            "\nslowMaArray[1]: ",slowMaArray[1],
            "\nfastMaArray[0]: ",fastMaArray[0],
            "\nfastMaArray[1]: ",fastMaArray[1]);


  }
 

#property version   "1.00"

#include <Trade/Trade.mqh>

CTrade trade;


int OnInit() 
{
     return(INIT_SUCCEEDED);
  }


void OnDeinit(const int reason)
  
{

  }


void OnTick()
  {
   static datetime timestamp=0;
   datetime time = iTime(_Symbol,PERIOD_CURRENT,0);
   result        = false;
   if(timestamp != time)
   {
    timestamp = time; 
    result = true;
    }
   
   
   
   static int handleSlowMa = iMA(_Symbol,PERIOD_CURRENT,200,0,MODE_SMA,PRICE_CLOSE);
   double slowMaArray[];
   CopyBuffer(handleSlowMa,0,1,2,slowMaArray);
   ArraySetAsSeries(slowMaArray,true);
   
   static int handleFastMa = iMA(_Symbol,PERIOD_CURRENT,20,0,MODE_SMA,PRICE_CLOSE);
   double fastMaArray[];
   CopyBuffer(handleFastMa,0,1,2,fastMaArray);
   ArraySetAsSeries(fastMaArray,true);
   
   if(fastMaArray[0] > slowMaArray[0] && fastMaArray [1] < slowMaArray[1])
   {
   Print(" higher ");
   
   double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
   double sl = ask - 100 * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
   double tp = ask + 100 * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
   trade.Buy(0.01,_Symbol,ask,sl,tp,"Buy")
   }
   
   if(fastMaArray[0] < slowMaArray[0] && fastMaArray [1] > slowMaArray[1])
   {
   Print(" lower ");
   double ask = SymbolInfoDouble(_Symbol,SYMBOL_BID);
   double sl = bid + 100 * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
   double tp = bid - 100 * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
   trade.Sell(0.01,_Symbol,bid,sl,tp,"Sell")
   }
   
   
   
   Comment ("\nslowMaArray[0]: ",slowMaArray[0],
            "\nslowMaArray[1]: ",slowMaArray[1],
            "\nfastMaArray[0]: ",fastMaArray[0],
            "\nfastMaArray[1]: ",fastMaArray[1]);


  }
//I would like to update code, however I want to ask that how to edit to open 1 trade when fastMa crosses to slowMa?
#property
version   "1.00" #include <Trade/Trade.mqh> CTrade trade; int OnInit() {      return(INIT_SUCCEEDED);   } void OnDeinit(const int reason)    {   } void OnTick()   {    static datetime timestamp=0;    datetime time = iTime(_Symbol,PERIOD_CURRENT,0);    result        = false;    if(timestamp != time)    {     timestamp = time;     result = true;     }             static int handleSlowMa = iMA(_Symbol,PERIOD_CURRENT,200,0,MODE_SMA,PRICE_CLOSE);    double slowMaArray[];    CopyBuffer(handleSlowMa,0,1,2,slowMaArray);    ArraySetAsSeries(slowMaArray,true);       static int handleFastMa = iMA(_Symbol,PERIOD_CURRENT,20,0,MODE_SMA,PRICE_CLOSE);    double fastMaArray[];    CopyBuffer(handleFastMa,0,1,2,fastMaArray);    ArraySetAsSeries(fastMaArray,true);       if(fastMaArray[0] > slowMaArray[0] && fastMaArray [1] < slowMaArray[1])    {    Print(" higher ");    double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);    double sl = ask - 100 * SymbolInfoDouble(_Symbol,SYMBOL_POINT);    double tp = ask + 100 * SymbolInfoDouble(_Symbol,SYMBOL_POINT);    trade.Buy(0.01,_Symbol,ask,sl,tp,"Buy");    }       if(fastMaArray[0] < slowMaArray[0] && fastMaArray [1] > slowMaArray[1])    {    Print(" lower ");    double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);    double sl = bid + 100 * SymbolInfoDouble(_Symbol,SYMBOL_POINT);    double tp = bid - 100 * SymbolInfoDouble(_Symbol,SYMBOL_POINT);    trade.Sell(0.01,_Symbol,bid,sl,tp,"Sell");    }             Comment ("\nslowMaArray[0]: ",slowMaArray[0],             "\nslowMaArray[1]: ",slowMaArray[1],             "\nfastMaArray[0]: ",fastMaArray[0],             "\nfastMaArray[1]: ",fastMaArray[1]);   }
 

Traders and coders are working for free:

  • if it is interesting for them personally, or
  • if it is interesting for many members on this forum.

Freelance section of the forum should be used in most of the cases.

NOTE: some details in your code are common for AI-generated, so it can be hardly helped (would need considerable human review/corrections before being usable in a live trading environment).

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2025.05.24
  • www.mql5.com
The largest freelance service with MQL5 application developers
 
Thank you for your comment, I am newcomer, so I need a specific tutorial, 
 
AIM2233 #:
Thank you for your comment, I am newcomer, so I need a specific tutorial, 

Tip: for help, bring a clear, specific question and show your own effort. Be ready to dig into documentation and examples.

Otherwise Freelance section is your best friend - that’s where you can pay people to be your personal tutor.

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2025.05.24
  • www.mql5.com
The largest freelance service with MQL5 application developers
 

I am new trader , and I would like to code a strategy robot to support my trading. 

1. This bot will open a position when it checks crossover signals every interval seconds . Example, MA20 crosses above  MA200, opens a buy position and opposite opens a sell position when MA20 is below MA200. Close that position when opposite signal detected , and open new position right after.

2. Avoid duplicate trade, ensures that only one position is active at all times. 

3. GUI dashboard to control and modify inputs for actual situation.

Thank you


 
AIM2233 #:

Thank you for sharing your interest in developing a trading robot. It's great to see new traders taking the initiative to learn.

However, please understand that this forum is not meant for supervising or fully developing someone's strategy.

We are here to help with specific technical questions, issues in your code, and to provide guidance, not to fulfill general wishes or carry out full projects.

So far, you've mostly described what you would like your robot to do, but you haven't asked clear, focused questions. If you want effective help, try to present a small, concrete issue.

 
Miguel Angel Vico Alba #:

Thank you for sharing your interest in developing a trading robot. It's great to see new traders taking the initiative to learn.

However, please understand that this forum is not meant for supervising or fully developing someone's strategy.

We are here to help with specific technical questions, issues in your code, and to provide guidance, not to fulfill general wishes or carry out full projects.

So far, you've mostly described what you would like your robot to do, but you haven't asked clear, focused questions. If you want effective help, try to present a small, concrete issue.

The issue is, I have coded as the instructions, however the bot opened many positions and cutloss very continually, that lead to swipe out account. Therefore I want to set to avoid duplicate trade, ensures that only one position is active at all times.  

Thank you for your reply.

 
AIM2233 #The issue is, I have coded as the instructions, however the bot opened many positions and cutloss very continually, that lead to swipe out account. Therefore I want to set to avoid duplicate trade, ensures that only one position is active at all times.

Check if there's already an open trade before placing a new one. Something like:

if (PositionsTotal() == 0)
    trade.Buy(...);
 
You calculate the handler in OnInit() and use the CopyBuffer in OnCalculate() function.