Click here to Skip to main content
15,887,404 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
//+------------------------------------------------------------------+
//|                            	                        my_oop_ea.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
I am building an oop EA to trade 40 dollars:Below is my code://+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
"(#include<my_expert_class_mqh>)"
 //input parameters
input int STP=30;
input int TakeProfit=100;
input int ADX_Period=14;
input int MA_Period=10;
input int EA_Margic=12345;
input double Adx_Min=22.0;
input double lot=0.1;
input int Margin_Chk=0;
input double Trd_percent=15.0;
//Other parameters
int STP,TKP;
//Create an object of our class
class myExpert{
 CExpert myExpert;
//Expert initilization function
//
 int onInit()
 { 
  Cexpert.doInit(ADX_Period,MA_Period);
 //
 CExpert.setPeriod(_Period);
 CExpert.setSymbol(_Symbol);
 CExpert.setMagic(EA_Magic);
 CExpert.setadxmin(Adx_Min);
 Eexpert.setLots(Lot);
 CExpert.setchkMAG(Margin_Chk);
 CExpert.setTRpct(Trd_percent);
 //
 STP=STopLoss;
 TKP=TakeProfit;
 (Digits==5 || _Digits==3)
 {
 STL=STL*10;
 TKP=TKP*10;
    }
   //
  ;
   
   //
   //
   //
   void OnDoInit(const int reason)
   //
   CExpert.doUninit();
   //
   //
   //
   void OnTick()
  
   //
   int Mybars=Bars(Symbol, _Period);
   if(Mybars<60)
    {
    Alert("We have less than 60 bars,EA will now exit!!");
    return;
    }
    //
    MqlTick latest_price;
    MqlRates mrate[];
    /*
    
    */
    //
    ArraySetAsSeries(mrate,true);
    //
    if(!SymbolInfoTick(_Symbol,latest_price))
    {
    Alert("Error getting the latest price quote-error:",GetLastErroror(),"!!");
    return;
    }
    //
    if(CopyRates (_Symbol,_Period,0,3,mrate)<0)
    {
    Alert("Error copying rates/history data -error:",GetLastError(),"!!");
    return;
    }
    //
    //
    static datetime Prev_time;
    //
    datetime Bar_time[1];
    //
    Bar_time[0]=mrate[0],time;
    //
    if(Prev_time==Bar_time[0])
    { 
    return;
    }
    //
    Prev_time=Bar_time[0];
    //
    //
    bool Buy_opned=false,Sell_opened=false;
    if(PositionSelect _Symbol)==true)
    {
    if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
    {
    Buy_opened=true;
    }
    else if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
    {
    Sell_opened=true;
    }
  }
  //
  Cexpert,setCloseprice(mrate[1].close);
  //
  if(Cexpert,checkBuy()==true)
  {
  //
  if(Buy_opened)
  {
  Alert("We already have a buy position");
  return;
  }
  double askprice=NormalizeDouble(latest_price.ask,_Digits);
  double stl=NormalizeDoublelatest_price,ask-STP*Point,_Digits);
  double tkp=NormalizeDouble(latest_price,ask+TKP*_Point,_Digits);
  int mdev=100;
  //
  Cexpert.openBuy(ORDER_TYPE_BUY,aprice,stl.tkp,mdev);
  }
  //
  if(Cexpert.checkSell()==true)
  {
  //
  if(Sell=opened)
  {
  Alert("We already have a sell position!!!");
  return;
  }
  double bprice=NormalizeDouble(latest_price.bid,_Digits);
  double bstl=NormalizeDouble(latest_price.bid+STP*_Point,_Digits);
  double btkp=NormalizeDouble(latest_price.bid-TKP*_Point,_Digits);
  int bdev=100;
  //
  Cexpert,openSell(ORDER_TYPE_SELL,bprice,bstl,btkp,bdev);K
  
   exit

What I have tried:

What could be the problem with the above code?I have tried to fix the 5 errors but I am unable.
5 Errors and 0 warnings are:
1.see previous declaration of variable 'STP':Line 10 column 1
2.'CExpert'-unexpected token probably type is missing:Line 23 column 2
3.'myExpert'-Semicolon expected:Line 23 column 10
4.unexpected end of program:Line 148 column 9
5.'{' -unbalanced parentheses:Line 6 column 1
Posted
Updated 16-Oct-22 21:33pm

C++
//Create an object of our class
class myExpert{
 CExpert myExpert;

You declare a class called myExpert, and inside it you try to instantiate a CExpert (where is that class defined?) object and name it myExpert. None of which makes any sense. And the following lines
C++
 Cexpert.doInit(ADX_Period,MA_Period);
//
CExpert.setPeriod(_Period);

are similarly not anywhere near correct. And the rest of the code looks like it was just randomly thrown together. You need to throw this away and go back to the C++ documentation, and learn how to write class definitions, and implementations.

Sorry that sounds harsh but that is the reality I am afraid.
 
Share this answer
 
Take your errors one by one, and read the messages.
For example:
previous declaration of variable 'STP':Line 10 column 1

So look at your code, and find "STP":
C++
input int STP=30;
...
int STP,TKP;
You are trying to declare the same variable twice: so which one of them should the system use when you reference them later? Which one is later code expecting? Should they be the same variable, or two different ones?

The compiler doesn't know your intent: it just looks at your code and says "I need help with this, what did you mean to write?" And I can't tell you what to do either, because I don't know any more than the compiler does what you intended to do with each variable, or that the second is just spurious.

So fix that, and compile again.

You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you look at each compilation error and fix it!
 
Share this answer
 
Comments
[no name] 17-Oct-22 2:40am    
Thanks OG
OriginalGriff 17-Oct-22 2:44am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900