Guys what am i missing here?

 

 i am fairly new to coding. its been two days since i have been working on this.  compiling it brings 2 errors, i have checked the trade mqh file for help. but made more errors on the way.

here is the code.


#include<Trade\Trade.mqh>

// Create a Ctrade instance
void OnTick()
  {
   // create an empty string
   string entry="";
   
   // Get the Ask Price
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   
      // Get the Ask Price
   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits); 
   
   // create an array for several prices
   double myMovingAverageArray20[],myMovingAverageArray70[];
   
   //Define the properties of the moving Average1
   int movingAverageDefinition1 = iMA (_Symbol,_Period,20,0,MODE_EMA,PRICE_CLOSE);
   
   //Define the properties of the moving Average2
   int movingAverageDefinition2 = iMA (_Symbol,_Period,70,0,MODE_SMA,PRICE_CLOSE);
   
   // sort the price array 1 from the current candle downwards
   ArraySetAsSeries(myMovingAverageArray20,true);
   
   // sort the price array 2 from the current candle downwards
   ArraySetAsSeries(myMovingAverageArray70,true);
   
   //Defined MA1, one line,current candle, 3 candles, store result
   CopyBuffer(movingAverageDefinition1,0,0,3,myMovingAverageArray20);
   
   //Defined MA1, one line,current candle, 3 candles, store result
   CopyBuffer(movingAverageDefinition2,0,0,3,myMovingAverageArray70);
   
   if (  //Check if the 20 candle EA is above the 50 candle EA
         (myMovingAverageArray20[0]>myMovingAverageArray70[0])
      && (myMovingAverageArray20[1]<myMovingAverageArray70[1])
      )
         (
         entry="buy"
         );
         
   if (  //Check if the 20 candle EA is above the 50 candle EA
         (myMovingAverageArray20[0]<myMovingAverageArray70[0])
      && (myMovingAverageArray20[1]>myMovingAverageArray70[1])
      )
         (
         entry="sell"
         );
         
         
   // sell 0.001 lot
   if (entry =="Sell" && PositionsTotal()<1)
   trade.sell (0.001,NULL,Bid,0,(Bid-150 * _Point),NULL);
   
   
   // Buy 0.001 Lot
   if (entry =="Buy" && PositionsTotal()<1)
   trade.buy (0.001,NULL,Ask,0,(Ask-150 * _Point),NULL);
   
   
  }



 
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  2.  void OnTick()
      {
       ⋮
       int movingAverageDefinition1 = iMA (_Symbol,_Period,20,0,MODE_EMA,PRICE_CLOSE); 

    Perhaps you should read the manual, especially the examples.
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
              MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
              How to call indicators in MQL5 - MQL5 Articles (2010)

  3.    )
             (
             entry="buy"   
             ); 
    Brackets are not the same as parentheses. You terminate a statement with a semi-colon; not brackets.
 

Some simple fixes:

1. You need to declare the object "trade" after including the mqh file

#include<Trade\Trade.mqh>
CTrade trade;


2. The buy and sell methods need to spelt correctly (capitalised 1st letters)

// sell 0.001 lot
   if(entry == "Sell" && PositionsTotal() < 1)
      trade.Sell(0.001,NULL,Bid,0,(Bid - 150 * _Point),NULL);


// Buy 0.001 Lot
   if(entry == "Buy" && PositionsTotal() < 1)
      trade.Buy(0.001,NULL,Ask,0,(Ask - 150 * _Point),NULL);
  }


Now it compiles for me - give that a try.

Also when pasting code use "Alt+S" or click the "Code" button on the toolbar

 
omg thanks guys this helped a lot, also thankyou for directing me on examples and references. <3
 
R4tna C #: Now it compiles for me - give that a try.
      trade.Sell(0.001,NULL,Bid,0,(Bid - 150 * _Point),NULL);
⋮
      trade.Buy(0.001,NULL,Ask,0,(Ask - 150 * _Point),NULL);

You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

  2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
              MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

  3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
    Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

 
William Roeder #:

You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

  2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
              MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

  3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
    Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

Yes, well observed - I was just aiming to make it compile rather than look deeper into it, but this will help the OP for sure

 

hi im new in coding please help with this code it gives errors 


double maFastPrev =iMA(_Symbol, _Period, FastMA, 0, MODE_EMA, PRICE_CLOSE, 1);

double maFast =iMA(_Symbol, _Period, FastMA, 0, MODE_EMA, PRICE_CLOSE, 0);

double maSlowPrev =iMA(_Symbol, _Period, SlowMA, 0, MODE_EMA, PRICE_CLOSE, 1);

double maSlow =iMA(_Symbol, _Period, SlowMA, 0, MODE_EMA, PRICE_CLOSE, 0);

 
071180 #:

hi im new in coding please help with this code it gives errors 


double maFastPrev =iMA(_Symbol, _Period, FastMA, 0, MODE_EMA, PRICE_CLOSE, 1);

double maFast =iMA(_Symbol, _Period, FastMA, 0, MODE_EMA, PRICE_CLOSE, 0);

double maSlowPrev =iMA(_Symbol, _Period, SlowMA, 0, MODE_EMA, PRICE_CLOSE, 1);

double maSlow =iMA(_Symbol, _Period, SlowMA, 0, MODE_EMA, PRICE_CLOSE, 0);

please go read the thread about how to post a question on the forum. It will include how to post your code so that it is easier to read with the button </>.

Post your full code, not just the lines with the errors.

 
071180 #:

hi im new in coding please help with this code it gives errors 


double maFastPrev =iMA(_Symbol, _Period, FastMA, 0, MODE_EMA, PRICE_CLOSE, 1);

double maFast =iMA(_Symbol, _Period, FastMA, 0, MODE_EMA, PRICE_CLOSE, 0);

double maSlowPrev =iMA(_Symbol, _Period, SlowMA, 0, MODE_EMA, PRICE_CLOSE, 1);

double maSlow =iMA(_Symbol, _Period, SlowMA, 0, MODE_EMA, PRICE_CLOSE, 0);

new to coding means you should go to the reference manual, this is MQL4 code and not MQL5