help with code. Beginner.,

 

Hi,

 

I am trying to learn to code an EA starting with the basics using icustom and opening a position.

 

I am trying to simply open a position when the indicator signals long, if there is no position open. If there is to ignore.

 

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start() { 
  double ArrowUp;
  static int iBuyTicket=-1;
  static bool Tradeopen = false;
  
  
  // Get Supersignal state
  ArrowUp = iCustom(NULL,0,"Ultimate Arrows",2,5);
 
  
  // Open Buy order if previous bar has arrow up signal
  if  (ArrowUp!=EMPTY_VALUE && Tradeopen!=false) {
    iBuyTicket=OrderSend(Symbol(),OP_BUY,Lots, Ask, Slippage, 0, 0, 
                          "Trend", MagicNr, NULL, LimeGreen);    
  }
  
  
  
  // Check SL and TP for BUY ticket
  if (OrderSelect( iBuyTicket, SELECT_BY_TICKET)==True) {
    // Close position if SL is Hit
    if (dPips(Bid,OrderOpenPrice())<=-StopLoss)
      if ( OrderClose(iBuyTicket,Lots,Bid,Slippage)==True)
        iBuyTicket=-1; 
    // Close position if TP is Hit     
    if (dPips(Bid,OrderOpenPrice())>=TakeProfit)
      if ( OrderClose(iBuyTicket,Lots,Bid,Slippage)==True)
        iBuyTicket=-1;       
    //Print("Buy. Current Pips=",dPips(OrderOpenPrice(),Bid));  
  }
  
  
  return(0);
}


//------------------------------------------------------------
// function: dPips()
// Description: Convert a price difference to pips.
//-------------------------------------------------------------
double dPips(double dPrice1,double dPrice2,bool bAbs=false) {
  double dPipValue=(dPrice1-dPrice2)/Point; 
  if (bAbs) return( MathAbs(dPipValue));
  else return(dPipValue);
}  

 

 I know this is very basic but if someone could help me with the basic construct it would be great. Thanks you.

 
  static int iBuyTicket=-1;
  static bool Tradeopen = false;
EA's must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV+flush/file) of ticket numbers required.
  double dPipValue=(dPrice1-dPrice2)/Point; 

There is Tick, PIP, and Point. They are all different in general. A tick is the smallest change of price. A Point is the least significant digit quoted. In currencies a pip is defined as 0.0001 (or for JPY 0.01)

On a 4 digit broker a point (0.0001) = pip (0.0001). [JPY 0.01 == 0.01] On a 5 digit broker a point (0.00001) = 1/10 pip (0.00010/10). Just because you quote an extra digit doesn't change the value of a pip. (0.0001 == 0.00010) EA's must adjust pips to points (for mq4.) In currencies a tick is a point. Price can change by least significant digit (1.23456 -> 1.23457)

In metals a Tick is still the smallest change but is larger than a point. If price can change from 123.25 to 123.50, you have a TickSize of 0.25 and a point of 0.01. Pip has no meaning.

This is why you don't use TickValue by itself. Only as a ratio with TickSize. See DeltaValuePerLot()

if (OrderSelect( iBuyTicket, SELECT_BY_TICKET)==True) {
if ( OrderClose(iBuyTicket,Lots,Bid,Slippage)==True)
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). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
 
Thanks for your help but I am still struggling here
 
WHRoeder:
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). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.

hello,

somehow OT but I have a different opinion here; by using NOT i am almost sure you are basically make compilation slower. The compiler will make such a code that it gets the return value from the called function and compares against true (or false) in order to decide if the "if" will be executed or not. What you are proposing is that the compiler, sometimes, will first fetch the return value, then will NOT it and after all that it will compare; so you are puttin one more task in the compiler

 
if(aBool == true)
That results in a second compare. compare the variable to the constant and generate a boolean. Then the if compares the boolean.
 
Mccallum28: I am trying to simply open a position when the indicator signals long, if there is no position open.
  static bool Tradeopen = false;
  :
  if  (ArrowUp!=EMPTY_VALUE && Tradeopen!=false) {
    iBuyTicket=OrderSend(Symbol(),OP_BUY,Lots, Ask, Slippage, 0, 0, 
Your if is never true.
Reason: