simple EA needs stop-loss/take-profit and money management

 

Hello,

Since I chose programming a market robot in school as a subject (a decision I highly regret) I assumed we would get taught all the basics- mistakenly and since our teacher won´t teach us anything about how to program an ea but still wants to see results from us, I have to train it myself, poorly I cannot find a clear red line to begin and have a hard time programming this even though I owned the book from Andrew Young.


This is what I managed to do myself, poorly the stoploss/takeprofit is still not working, I would be very grateful if anyone could get this to work and if he likes even maybe program it a bit further.


The include file simply makes up a timer that makes the ea stop at night which apparently even works, I am still mystified how I got this to work but glad it works :-)


I would be unbelievably grateful if someone would help me,

thanks a lot in advance,

sincerely yours Andre Leonhardt

btw sorry for my bad English, I come from Germany

Files:
 
  1.        bool modified = OrderModify(gBuyTicket,0,stopLoss,TakeProfit,0);   
    
    Code fails because you can't change the OrderOpenPrice to zero. You would know why, if you Check your return codes for errors, and report them including GLE/LE. Don't look at it unless you have an error. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

  2.      //open buy order
         gBuyTicket = OrderSend(_Symbol,OP_BUY,lotSize,Ask,Slippage,0,0,"buy Order",MagicNumber,0,clrGreen);
    
    You have open code inside your if(tradeEnabled == true). So why do you have an addition one outside that will open one order per tick?

  3.        if(StopLoss > 0) stopLoss = OrderOpenPrice() - (StopLoss * _Point);
           if(TakeProfit > 0) takeProfit = OrderOpenPrice() + (TakeProfit * _Point);
    
    You buy at the Ask and sell at the Bid. So for buy orders you pay the spread on open. For sell orders you pay the spread on close.
    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid reaches it. Not the Ask.
    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3
    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.)

    Your stops have StopLoss Points minus spread for your buy SL and takeProfit Points plus spread for your TP. Don't you want the same amount for either direction? Make them relative to the OrderClosePrice.

  4. You have no code to set any for Sell orders.
  5. No reason to open an order and then set the stops.


 
caulerpa:

Hello,

Since I chose programming a market robot in school as a subject (a decision I highly regret) I assumed we would get taught all the basics- mistakenly and since our teacher won´t teach us anything about how to program an ea but still wants to see results from us, I have to train it myself, poorly I cannot find a clear red line to begin and have a hard time programming this even though I owned the book from Andrew Young.


This is what I managed to do myself, poorly the stoploss/takeprofit is still not working, I would be very grateful if anyone could get this to work and if he likes even maybe program it a bit further.


The include file simply makes up a timer that makes the ea stop at night which apparently even works, I am still mystified how I got this to work but glad it works :-)


I would be unbelievably grateful if someone would help me,

thanks a lot in advance,

sincerely yours Andre Leonhardt

btw sorry for my bad English, I come from Germany

//#include "Timer.mqh"
//No need for timer.mgh

//input bool UseTimer = true;
input int StartHour=0;
input int StartMinute=0;
input int EndHour=0;
input int EndMinute=0;
//input bool UseLocalTime=false;

/*-----------------------------------------------------------------------------------------------
input int StopLoss=0;
input int TakeProfit=0;//set TakePrfit to 0
int MagicNumber=1850;
input int lotSize=10;
/*-----------------------------------------------------------------------------------------------*/
double StopLoss=0;//<----changed int to double no longer input
double TakeProfit=0;//<----changed int to double no longer input
int MagicNumber=1850;
input double lotSize=10;//<----changed int to double
input int Slippage=5;
//int gBuyTicket = 1;
//CTimer Timer;
int gBuyTicket=0;
//added gSellTicket
int gSellTicket=0;
bool TradeisAllowed=false;//<----changed to bool

int gClose_Ticket=0;//<----close orders
int i=0;//<----for closing of orders
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
/*-----------------------------------------------------------------------------------------------
//not needed
      if(UseTimer==true)
        {
         tradeEnabled=Timer.DailyTimer(StartHour,StartMinute,EndHour,EndMinute,UseLocalTime);
        }

      if(tradeEnabled==true)
        {

         Comment("Copyright Andre Leonhardt");
/*-----------------------------------------------------------------------------------------------*/
   if((Hour()>=StartHour) && (Minute()>=StartMinute)){TradeisAllowed=true;}
   if((Hour()<=EndHour) && (Minute()<=EndMinute)){TradeisAllowed=false;}
/*-----------------------------------------------------------------------------------------------*/
   if(TradeisAllowed==true)
     {
      string signal="";
      double LowerBB=iBands(_Symbol,_Period,20,2,0,PRICE_CLOSE,MODE_LOWER,1);
      double UpperBB=iBands(_Symbol,_Period,20,2,0,PRICE_CLOSE,MODE_UPPER,1);

      double PrevLowerBB=iBands(_Symbol,_Period,20,2,0,PRICE_CLOSE,MODE_LOWER,2);
      double PrevUpperBB=iBands(_Symbol,_Period,20,2,0,PRICE_CLOSE,MODE_UPPER,2);

      if((Close[2]<PrevLowerBB) && (Close[1]>LowerBB))
        {
         signal="buy";
         StopLoss=Bid-(150*_Point);
         TakeProfit=Ask+(150*_Point);
        }
      if((Close[2]>PrevUpperBB) && (Close[1]<UpperBB))
        {
         signal="sell";
         StopLoss=Ask+(150*_Point);
         TakeProfit=Bid-(150*_Point);
        }
      if(OrdersTotal()==0)
        {
         if(signal=="buy")
           {
            gBuyTicket=OrderSend(_Symbol,OP_BUY,0.10,Ask,3,StopLoss,TakeProfit,NULL,0,0,Green);
           }
         if(signal=="sell")
           {
            gBuyTicket=OrderSend(_Symbol,OP_SELL,0.10,Bid,3,StopLoss,TakeProfit,0,0,Red);
           }
         Comment("signal: ",signal);
        }
      Alert("GetLastError()    "+GetLastError());//alway check for errors
     }
/*-----------------------------------------------------------------------------------------------*/
//close trades
   if(TradeisAllowed==false)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol())
           {
            if(OrderType()==OP_BUY)
              {
               gClose_Ticket=OrderClose(OrderTicket(),OrderLots(),Bid,0,clrNONE);
              }
            if(OrderType()==OP_SELL)
              {
               gClose_Ticket=OrderClose(OrderTicket(),OrderLots(),Ask,0,clrNONE);
              }
           }
      Alert("GetLastError()    "+GetLastError());//alway check for errors
     }
  }
//+------------------------------------------------------------------+

simple and untested...

 
Donald Gibson:

simple and untested...

Hello,

That really helps me lot, thank you very much ^^

I tested the ea and it only seems to make make loss, but this is at least a beginning for me.


Thanks a lot for your help,

Sincerely yours,

Andre Leonhardt


 

This will not work correctly:

   if((Hour()>=StartHour) && (Minute()>=StartMinute)){TradeisAllowed=true;}
   if((Hour()<=EndHour) && (Minute()<=EndMinute)){TradeisAllowed=false;}

It should be replaced with

   TradeisAllowed=( Hour()> StartHour && Hour()  < EndHour     ) ||
                  ( Hour()==StartHour && Minute()>=StartMinute ) ||
                  ( Hour()==EndHour   && Minute()< EndMinute   );
 
lippmaje: It should be replaced with
   TradeisAllowed=( Hour()> StartHour && Hour()  < EndHour     ) ||
                  ( Hour()==StartHour && Minute()>=StartMinute ) ||
                  ( Hour()==EndHour   && Minute()< EndMinute   );

Or simplified.

SECONDS start = (StartHour()*60+StartMinute)*60,
        end   = (  EndHour()*60+  EndMinute)*60;
SECONDS TOD   = time();
TradeisAllowed= start <= TOD && TOD < end;
          Find bar of the same time one day ago - Simple Trading Strategies - MQL4 programming forum
Reason: