Opinion - very successfull EA - $3000 account to $6300 in two weeks (could have been $9000) - page 5

 

1 fast thing before i go to work, there are boolean variables which can be true or false. you dont have to use a string for that.

Hardly to believe that you are following the book

 
zzuegg:

1 fast thing before i go to work, there are boolean variables which can be true or false. you dont have to use a string for that.

Hardly to believe that you are following the book

I am trying. I am learning to trade + program + MQL at the same time.

Do you learn to drive a car by reading the book alone ?

but thankyou

 
MickGlancy:

I am getting the error that OpenBuyOrder and OpenSellOrder function returns no result

what am I doing wrong ?

you are getting this error because the functions OpenBuyOrder() & OpenSellOrder() actually dont return a result.. you have defined them as void, meaning they do not return any argument, yet you are trying to return an integer (0).

here's the code with some comments:

void OpenBuyOrder( double StopLoss, double TakeProfit )
{
         int Ticket;			
         double SL,TP,SLP,TPP,OPP;
         
         if(HideSL==false && StopLoss>0){SL=Ask-StopLoss*Point;}
         else {SL=0;}
         
         if(SL>0 && SL>(Bid-MarketInfo(Symbol(),MODE_STOPLEVEL)*Point)){SL=Bid-MarketInfo(Symbol(),MODE_STOPLEVEL)*Point;}
         
         if(HideTP==false && TakeProfit>0){TP=Ask+TakeProfit*Point;}
         else {TP=0;}
         
         Ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,SL,TP,EAName,Magic,0,Blue);	//-- this stores the value for Ticket, but it never gets used.. 
                                                                                        //-- you can simply call OrderSend() without storing the result 

   return;    //--- void means the function returns no argument.. thus, no parentheses for return
}

void OpenSellOrder( double StopLoss, double TakeProfit)
{
         int Ticket;
         double SL,TP,SLP,TPP,OPP;
         
         if(HideSL==false && StopLoss>0){SL=Bid+StopLoss*Point;}
         else {SL=0;}
         
         if(SL>0 && SL<(Ask+MarketInfo(Symbol(),MODE_STOPLEVEL)*Point)){SL=Ask+MarketInfo(Symbol(),MODE_STOPLEVEL)*Point;}
         
         if(HideTP==false && TakeProfit>0){TP=Bid-TakeProfit*Point;}
         else {TP=0;/*TPP=0;*/}
         
         Ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,SL,TP,EAName,Magic,0,Red);
         
    return; 
}

based on your GetSignal function, it seems like you want to use OpenBuy/SellOrder() to just open an order, rather than actually return some argument .. I cleaned up your GetSignal() function a bit with some comments

void GetSignal(int MaxBuyOrders, double StopLoss, double TakeProfit) 		//--- Why make this function a bool??
{
   int Op_Buy,Op_Sell,Op_BuyStop,Op_SellStop,Op_BuyLimit,Op_SellLimit;
        
   int total = OrdersTotal();
  
   for(int x=total-1; x>=0; x--)
   {
      OrderSelect(x, SELECT_BY_POS, MODE_TRADES);
      int type   = OrderType();
      bool result = false;    //-- not used anywhere??
      
      if (type==OP_BUY)       Op_Buy++;
      if (type==OP_SELL)      Op_Sell++;
      if (type==OP_BUYSTOP)   Op_BuyStop++;
      if (type==OP_SELLSTOP)  Op_SellStop++;         
   }
  
   int limit=1;		        //--- why define limit to 1 
   for(int i=1;i<=limit;i++)    //--- your for-loop will always run once, in which case why even have a for-loop?
   {
      double MA1=iMA(NULL,0,100,0,1,0,0);
      double MA2=iMA(NULL,0,100,0,1,0,1);
      double MA3=iMA(NULL,0,40,0,1,0,0);
      double MA4=iMA(NULL,0,40,0,1,0,1);
      
      bool BUY=false;   //-- as one guy mentioned better to go with booleans here
      bool SELL=false;

      if(MA1 < MA3 && MA2 > MA4) BUY=true;  
      if(MA1 > MA3 && MA2 < MA4) SELL=true;
      // missed out  && total == 0 for now
      bool SignalBUY=false;
      bool SignalSELL=false;
      
      if(BUY) //-- dont need to write == for bool's ..BUY is same as BUY==true, !BUY same as BUY==false
      if(ReverseSystem) SignalSELL=true;
      else SignalBUY=true;
      
      if(SELL)
      if(ReverseSystem) SignalBUY=true;
      else SignalSELL=true;
      
      if (SignalBUY && Op_Buy < MaxBuyOrders )   OpenBuyOrder(StopLoss,TakeProfit); 	//--- no need to return the return of OpenBuyOrder().. simply calling it will run the code
      if (SignalSELL && Op_Sell < MaxSellOrders) OpenSellOrder(StopLoss,TakeProfit);	//-- same
   }
   return;
}
 
supertrade:

you are getting this error because the functions OpenBuyOrder() & OpenSellOrder() actually dont return a result.. you have defined them as void, meaning they do not return any argument, yet you are trying to return an integer (0).

here's the code with some comments:

based on your GetSignal function, it seems like you want to use OpenBuy/SellOrder() to just open an order, rather than actually return some argument .. I cleaned up your GetSignal() function a bit with some comments

That explains tonnes, thankyou very much, I am trying it now

my own code is a bit messed up because I am basically copying from other EAs and trying to make it work.

 
no problem.. feel free to ask if you run into other issues
 
void MoveTrailingStop()
{
   int cnt,total=OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
   {
      OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      if(OrderType()<=OP_SELL  &&  OrderSymbol()==Symbol() ) //&&  OrderMagicNumber()==Magic
      {
         if(OrderType()==OP_BUY)
         {
            if(TrailingStop>0)  
            {                 
               if((NormalizeDouble(OrderStopLoss(),Digits)<NormalizeDouble(Bid-Point*(TrailingStop+TrailingStep),Digits)) || (OrderStopLoss()==0))
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TrailingStop,Digits),OrderTakeProfit(),0,Blue);
                  return(0);
               }
            }
         }
         else 
         {
            if(TrailingStop>0)  
            {                 
               if((NormalizeDouble(OrderStopLoss(),Digits)>(NormalizeDouble(Ask+Point*(TrailingStop+TrailingStep),Digits)))||(OrderStopLoss()==0))
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+Point*TrailingStop,Digits),OrderTakeProfit(),0,Red);
                  return(0);
               }
            }
         }
      }
   }
}
supertrade
:

no problem.. feel free to ask if you run into other issues

ive collected lots of examples of trailing stops, but I need one which starts to trail immediately through the -ve values to 0, and then stop like a break even.

so if the market entry price is taken and the stop loss is 100, id the price moves to +25, the trailing stop moves to -75, and then once the price reaches 100, the trailing stop stops at 0 and doesnt move any further.

could anyone help me with this ? I have spent all day trying to get it to work and I just cant.

I thought I had done it but I haven't, my trailing stop follows through 0.

 

Pseudo Code:

OrderSelect()

if (Buyorder and stoploss < openrice) : we need to trail
if (Sellorder and stoploss > openprice): we need to trail
if (we need to trail):
Modify Stoploss to Currentprice (+ or -) original stoploss
 

zzuegg:

if (Buyorder and stoploss < openrice) : we need to trail

wouldnt stop loss always be lower than openprice, and vice versa ?

I am intending eventually to use a MaxLoss function, so I wont put in a stop loss with the OrderSend.

if Op_Buy

if bid <= OpenOrderPrice()+(Trailing stop value) : trail -- once bid > TSV it shouldn't continue to move ?

and

if Op_Sell

if ask >= OpenOrderPrice()-(Trailing stop value): trail ?

Then at a value higher than Trailingstop, Breakeven can finish the job ?

 OrderSelect(SELECT_BY_POS,MODE_TRADES);
   if(TrailingStop>0)
      {
      if(OrderType()==OP_BUY)
            {
            if ( Bid <= (OrderOpenPrice()+Point*TrailingStop)  )
               {
                  MoveTrailingStop();
               }
            }
      if(OrderType()==OP_SELL)
            {   
            if ( Ask >= (OrderOpenPrice()-Point*TrailingStop) )
               {
                  MoveTrailingStop();
               }
            }
      }
BTW thanks zzuegg, thats another order management milestone achieved. What can happen with a little help. I am gratefull.
 
MickGlancy:

zzuegg:

if (Buyorder and stoploss < openrice) : we need to trail

wouldnt stop loss always be lower than openprice, and vice versa ? No, only before breakeven

I don't think your code works, seems you try to do the opposite...
 
zzuegg:
I don't think your code works, seems you try to do the opposite...

no, it is working perfectly. Let me check it is the same now as in that response.

it is as close as I can get to a compromise of closing the loss gap behind a moving trade, but still giving it space to breath. Before this, the trade had to reach 60 points before BE happened so there were a lot of max stopped out trades, which caused my drawdown to be high. hopefully this will alter that.

OrderSelect(SELECT_BY_POS,MODE_TRADES);
   if(TrailingStop>0)
      {
      if(OrderType()==OP_BUY)
            {
            if ( Bid <= (OrderOpenPrice()+Point*TrailingStop)  )
               {
                  MoveTrailingStop();
               }
            }
      if(OrderType()==OP_SELL)
            {   
            if ( Ask >= (OrderOpenPrice()-Point*TrailingStop) )
               {
                  MoveTrailingStop();
               }
            }
      }
Reason: