Help for a Newbie

 

Hi guys. I need some help if possible. Ive only been trading for a couple of months. Ive just started using EA and ive managed to butcher the MACDSample to add a stop loss, however I would like to add a close trade after a certain number of bars (I think this is better than a time period because I leave positions open over the weekend) I dont want an order open for more than 3-4 days and Im using M15. This is what ive got so far:-

I dont mind making a contribution to anyone that can help me :)



//+------------------------------------------------------------------+
//| MACD Modified.mq4 |
//+------------------------------------------------------------------+

extern double TakeProfit = 06;
extern double Lots = 6.0;
extern double StopLoss=12;
extern double MACDOpenLevel=3;
extern double MATrendPeriod=26;

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
double MacdCurrent, MacdPrevious, SignalCurrent;
double SignalPrevious, MaCurrent, MaPrevious;
int cnt, ticket, total;

if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<1)
{
Print("TakeProfit less than 1");
return(0); // check TakeProfit
}
// Simplify coding
MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);
MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);

total=OrdersTotal();
if(total<1)
{
// no opened orders identified
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
// check for long position (BUY) possibility
if(MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious<SignalPrevious &&
MathAbs(MacdCurrent)>(MACDOpenLevel*Point) && MaCurrent>MaPrevious)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-StopLoss*Point,Ask+TakeProfit*Point,"-",0,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
// check for short position (SELL) possibility
if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious &&
MacdCurrent>(MACDOpenLevel*Point) && MaCurrent<MaPrevious)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+StopLoss*Point,Bid-TakeProfit*Point,"-",0,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
}

return(0);
}}
// the end.
 

Well you could try using a magic number whose value is the the integer equivalent of parsing current time i.e. parsing datetime to int as seconds.


On the start function or on each new bar you would then loop through your orders and simply compare their magic number (now parsed back to datetime) against the current time. If the difference is 3-4 days, you close it.

 
  1. ssn:

    Well you could try using a magic number whose value is the the integer equivalent of parsing current time i.e. parsing datetime to int as seconds.

    Don't do that, magic number is to be used to filter all trades to just the EAs. That would prevent using the EA on multiple charts.


  2. however I would like to add a close trade after a certain number of bars
        for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
            OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol() ){              // and my pair.
            int duration = iBarShift(NULL,0, OrderOpenTime());
            Print("Order ",OrderTicket()," has been open for ",duration," bars");
        }
 

Why not use?

datetimeTimeLimit= OrderOpenTime()+Days*24*3600 ;

if( (TimeCurrent()-OrderOpenTime())>=TimeLimit ) { //Code to close order }

 

Thanks guys Im going to have a play with your ideas. Meanwhile today I have been trying to add a bit of money management too. I am trying to open an order with the lots based on my free margin. Cant get it to compile though. Any ideas?

I really appreciate the help.



//+------------------------------------------------------------------+
//| MACD Modified.mq4 |
//+------------------------------------------------------------------+

extern double TakeProfit = 06;
extern double StopLoss=12;
extern double MACDOpenLevel=3;
extern double MATrendPeriod=26;
extern double Lots=0.1;//|-----------------------lots size
extern bool RiskMM=false;//|---------------------risk management
extern double RiskPercent=1;//|------------------risk percentage
extern double MinLots=0.01;//|-------------------minlots
extern double MaxLots=100;//|--------------------maxlots
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
double MacdCurrent, MacdPrevious, SignalCurrent;
double SignalPrevious, MaCurrent, MaPrevious;
int cnt, ticket, total;

if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<1)
{
Print("TakeProfit less than 1");
return(0); // check TakeProfit
}
// to simplify the coding and speed up access
// data are put into internal variables
MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);
MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);

total=OrdersTotal();
if(total<1)
{
// no opened orders identified
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
if(RiskMM)CalculateMM();
{
// check for long position (BUY) possibility
if(MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious<SignalPrevious &&
MathAbs(MacdCurrent)>(MACDOpenLevel*Point) && MaCurrent>MaPrevious)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-StopLoss*Point,Ask+TakeProfit*Point,"-",0,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
// check for short position (SELL) possibility
if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious &&
MacdCurrent>(MACDOpenLevel*Point) && MaCurrent<MaPrevious)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+StopLoss*Point,Bid-TakeProfit*Point,"-",0,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
}
void CalculateMM()
{
double MinLots=MarketInfo(Symbol(),MODE_MINLOT);
double MaxLots=MarketInfo(Symbol(),MODE_MAXLOT);
Lots=AccountFreeMargin()/100000*RiskPercent;
Lots=MathMin(MaxLots,MathMax(MinLots,Lots));
if(MinLots<0.1)Lots=NormalizeDouble(Lots,2);
else
{
if(MinLots<1)Lots=NormalizeDouble(Lots,1);
else Lots=NormalizeDouble(Lots,0);
}
if(Lots<MinLots)Lots=MinLots;
if(Lots>MaxLots)Lots=MaxLots;
return(0);
}
{
double MinLots=MarketInfo(Symbol(),MODE_MINLOT);
double MaxLots=MarketInfo(Symbol(),MODE_MAXLOT);
Lots=AccountFreeMargin()/100000*RiskPercent;
Lots=MathMin(MaxLots,MathMax(MinLots,Lots));
if(MinLots<0.1)Lots=NormalizeDouble(Lots,2);
else
{
if(MinLots<1)Lots=NormalizeDouble(Lots,1);
else Lots=NormalizeDouble(Lots,0);
}
if(Lots<MinLots)Lots=MinLots;
if(Lots>MaxLots)Lots=MaxLots;
return(0);
}

}
return(0);
}}
// the end.
 
WHRoeder:
  1. ssn:

    Well you could try using a magic number whose value is the the integer equivalent of parsing current time i.e. parsing datetime to int as seconds.

    Don't do that, magic number is to be used to filter all trades to just the EAs. That would prevent using the EA on multiple charts.


  2. however I would like to add a close trade after a certain number of bars

Thanks for that WHRoeder. There is always the correct way of doing things.

 
Ickyrus:

Why not use?

datetimeTimeLimit= OrderOpenTime()+Days*24*3600 ;

if( (TimeCurrent()-OrderOpenTime())>=TimeLimit ) { //Code to close order }


Just realised had two separate thoughts and they got combined into bad code above - Sorry.

should probably read

datetimeTimeLimit= OrderOpenTime()+Days*24*3600 ;

if( TimeCurrent()>=TimeLimit ) { //Code to close order }

Reason: