Candle Trading EA : Based on Days of week and Last week Candle color ( needs editing )

 

Good day, I have been programming this EA to Trade Candlistics Based on candle sequences, So far I reached Dayly weekly Candles trading but the problem is The programming is all Good and it did compile but something seems missing :

I added a day of week on the conditional and i think its the source of the problem :

here is the Code :



//+------------------------------------------------------------------+
//| dumdumcandle trader.mq4 |
//| Copyright 2011, dumdumlol. |
//|
//+------------------------------------------------------------------+
#property copyright "Copyright 2011, dumdumlol"
#property link "http://dumdum.lol"



//+------------------------------------------------------------------+
//| Universal Constants |
//+------------------------------------------------------------------+
#define TRADEPEND -1
#define TRADEALL 0
#define TRADETRADE 1
#define TRADEBUY 1
#define TRADESELL -1
#define Green 1
#define Red -1
#define Non 0
//+------------------------------------------------------------------+
//| User input variables |
//+------------------------------------------------------------------+
extern string ExpertName = "DumDumEa"; // For Logging
extern bool ECNBroker = false; // Whether using ECN broker
extern bool AllowBuy = true; // Whether allow to trade buy
extern bool AllowSell = true; // Whether allow to trade sell
extern int Magic = 222222; // For identify trades belong to this EA
extern int Slippage = 3; // Allow slippage
extern double Lots = 0.01; // Fixed trade lots without using MoneyManagement
extern bool MoneyManagement = false; // Whether using Money Management
extern double MM_Risk = 5; // Risk as equity, in percent
extern double TakeProfit = 0; // Take Profit, Change to 0 if you don't want to use
extern double StopLoss = 0; // Stop Loss, Change to 0 if you don't want to use
extern bool UseBreakEven = false; // Whether using BreakEven
extern double BreakEvenPoints = 200; // BreakEven Points
int sellTicket = -1;
int buyTicket = -1;
int PointMultiplier = 1;
//+------------------------------------------------------------------+
//| Universal variables |
//+------------------------------------------------------------------+
bool trade_first=false;
datetime time_first;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
time_first = Time[0];
return(0);
}

//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}

//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
double bar0close = iClose(NULL,PERIOD_M1,1);
double bar0open = iOpen(NULL,PERIOD_M1,1);
double barclose = iClose(NULL,PERIOD_W1,1);
double baropen = iOpen(NULL,PERIOD_W1,1);
double bar3close = iClose(NULL,PERIOD_D1,1);
double bar3open = iOpen(NULL,PERIOD_D1,1);
double bar2close = iClose(NULL,PERIOD_H4,1);
double bar2open = iOpen(NULL,PERIOD_H4,1);
double bar4close = iClose(NULL,PERIOD_H1,1);
double bar4open = iOpen(NULL,PERIOD_H1,1);
double bar22close = iClose(NULL,PERIOD_H4,2);
double bar22open = iOpen(NULL,PERIOD_H4,2);
double bar23close = iClose(NULL,PERIOD_H4,3);
double bar23open = iOpen(NULL,PERIOD_H4,3);
int Dow;
double DOW = TimeDayOfWeek(Time[1]);

int Candle0=Non;
if(bar0close>bar0open) {
Candle0 = Green;
}
else if(bar0close<bar0open) {
Candle0 = Red;
}
int Candle=Non;
if(barclose>baropen) {
Candle = Green;
}
else if(barclose<baropen) {
Candle = Red;
}
int Candle2=Non;
if(bar2close>bar2open) {
Candle2 = Green;
}
else if(bar2close<bar2open) {
Candle2 = Red;
}
int Candle3=Non;
if(bar3close>bar3open) {
Candle3 = Green;
}
else if(bar3close<bar3open) {
Candle3 = Red;
}
int Candle4=Non;
if(bar4close>bar4open) {
Candle4 = Green;
}
else if(bar4close<bar4open) {
Candle4 = Red;
}
int Candle22=Non;
if(bar22close>bar22open) {
Candle22 = Green;
}
else if(bar22close<bar22open) {
Candle22 = Red;
}
int Candle23=Non;
if(bar23close>bar23open) {
Candle23 = Green;
}
else if(bar23close<bar23open) {
Candle23 = Red;
}


if(scanTrades(TRADETRADE,TRADESELL)>0 && Candle3==Green) {
closeTradesSell();
}
if(scanTrades(TRADETRADE,TRADEBUY)>0 && Candle3==Red) {
closeTradesBuy();
}


if(!trade_first && Time[0]>time_first) {
if ((Candle == Red && (Dow == 1||Dow == 2) && Candle3 == Green )||(Candle ==Green&& (Dow==3||Dow==4||Dow==2) && Candle3==Green)){
tradeBuy();
trade_first = true;
}
if ((Candle == Green && (Dow == 1||Dow == 2) && Candle3 == Red )||(Candle ==Red&& (Dow==3||Dow==4||Dow==2) && Candle3==Red)){
tradeSell();
trade_first = true;
}
}
if(!tradedInBar() && AllowBuy&& scanTrades(TRADETRADE,TRADEBUY)<=0&& scanTrades(TRADETRADE,TRADESELL)<=0) {
if ((Candle == Red && (Dow == 1||Dow == 2) && Candle3 == Green )||(Candle ==Green&& (Dow==3||Dow==4||Dow==2) && Candle3==Green))
tradeBuy();
}
if(!tradedInBar() && AllowSell&& scanTrades(TRADETRADE,TRADESELL)<=0&& scanTrades(TRADETRADE,TRADEBUY)<=0) {
if ((Candle == Green && (Dow == 1||Dow == 2) && Candle3 == Red )||(Candle ==Red&& (Dow==3||Dow==4||Dow==2) && Candle3==Red))
tradeSell();
}
// BreakEven && TrailStop
if(scanTrades(TRADETRADE)>0) {
if(UseBreakEven) {
breakEven();
}}


return(0);
}

//+------------------------------------------------------------------+
//| Trade Buy @http://kolier.li @v1.0 |
//| @import |
//| bool ECNBroker |
//+------------------------------------------------------------------+
void tradeBuy()
{
double bar2low = iLow(NULL,PERIOD_H4,1);
double price_now, price_profit, price_stop, lots,low;
int ticket;
price_now = Ask;
if(TakeProfit > 0) {
low = iLow(NULL,PERIOD_H4,1);
price_profit = price_now + TakeProfit*Point;
}
else {
price_profit = 0;
}
if(StopLoss > 0) {
price_stop = low- StopLoss*Point;
}
else {
price_stop = 0;
}
if(MoneyManagement) {
lots = mmLots(StopLoss);
}
else {
lots = Lots;
}

if(!ECNBroker) {
ticket = OrderSend(Symbol(), OP_BUY, lots, price_now, Slippage, price_stop, price_profit, NULL, Magic, 0, Lime);
}
else {
ticket = OrderSend(Symbol(), OP_BUY, lots, price_now, Slippage, 0, 0, NULL, Magic, 0, Lime);
}

if(ticket < 0) {
//tradeBuy();
Print(ExpertName, "- tradeBuy() error #", GetLastError());
Print("Price Buy: ", price_now, " TP: ", price_profit, " SL: ", price_stop, " Lots: ", lots);
}
else {
if(ECNBroker) {
OrderSelect(ticket, SELECT_BY_TICKET);
OrderModify(OrderTicket(), OrderOpenPrice(), price_stop, price_profit, 0, CLR_NONE);
}
}
}

//+------------------------------------------------------------------+
//| Trade Sell @http://kolier.li @v1.0 |
//| @import |
//| bool ECNBroker |
//+------------------------------------------------------------------+
void tradeSell()
{
double price_now, price_profit, price_stop, lots,high;
high = iHigh(NULL,PERIOD_H4,1);
int ticket;
price_now = Bid;
if(TakeProfit > 0) {
price_profit = price_now - TakeProfit*Point;
}
else {
price_profit = 0;
}
if(StopLoss > 0) {
price_stop = high+ StopLoss*Point;
}
else {
price_stop = 0;
}
if(MoneyManagement) {
lots = mmLots(StopLoss);
}
else {
lots = Lots;
}

if(!ECNBroker) {
ticket = OrderSend(Symbol(), OP_SELL, lots, price_now, Slippage, price_stop, price_profit, NULL, Magic, 0, Red);
}
else {
ticket = OrderSend(Symbol(), OP_SELL, lots, price_now, Slippage, 0, 0, NULL, Magic, 0, Red);
}

if(ticket < 0) {
//tradeBuy();
Print(ExpertName, "- tradeSell() error #", GetLastError());
Print("Price Sell: ", price_now, " TP: ", price_profit, " SL: ", price_stop, " Lots: ", lots);
}
else {
if(ECNBroker) {
OrderSelect(ticket, SELECT_BY_TICKET);
OrderModify(OrderTicket(), OrderOpenPrice(), price_stop, price_profit, 0, CLR_NONE);
}
}
}

//+------------------------------------------------------------------+
//| Scan Trades @http://kolier.li @v1.0 |
//| @params |
//| string order_type TRADEALL|TRADETRADE|TRADEPEND |
//| string order_dir TRADEALL|TRADEBUY|TRADESELL |
//| @return |
//| int orders_number |
//+------------------------------------------------------------------+
int scanTrades(int order_type=TRADEALL, int order_dir=TRADEALL)
{
int orders_total = OrdersTotal();
int orders_number = 0;
for(int i=0; i<orders_total; i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber() == Magic && OrderSymbol() == Symbol())
{
switch(order_type)
{
case TRADEALL:
orders_number++;
break;
case TRADETRADE:
if(OrderType()<=OP_SELL)
{
orders_number = _scanTradesDir(order_dir, orders_number);
}
break;
case TRADEPEND:
if(OrderType()>OP_SELL)
{
orders_number = _scanTradesDir(order_dir, orders_number);
}
break;
}// switch
}
}
return(orders_number);
}

int _scanTradesDir(int order_dir, int orders_number)
{
switch(order_dir)
{
case TRADEALL:
orders_number++;
break;
case TRADEBUY:
if(OrderType()==OP_BUY)
{
orders_number++;
}
break;
case TRADESELL:
if(OrderType()==OP_SELL)
{
orders_number++;
}
break;
}// switch
return(orders_number);
}

//+------------------------------------------------------------------+
//| Whether traded in that bar, default to current bar |
//| @http://kolier.li |
//+------------------------------------------------------------------+
bool tradedInBar(int shift=0)
{
datetime time_begin = Time[shift];
datetime time_end = time_begin + Period();
int i;
for(i=0; i<OrdersTotal(); i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber()==Magic && OrderSymbol()==Symbol()
&& OrderOpenTime()>=time_begin && OrderOpenPrice()<=time_end) {
return(true);
}
}
for(i=0; i<OrdersHistoryTotal(); i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
if(OrderMagicNumber()==Magic && OrderSymbol()==Symbol()
&& OrderOpenTime()>=time_begin && OrderOpenPrice()<=time_end) {
return(true);
}
}

return(false);
}

//+------------------------------------------------------------------+
//| MM Lots @http://kolier.li |
//| Open lots base on the percent of equity and stoploss |
//+------------------------------------------------------------------+
double mmLots(double stoploss)
{
// Use the fixed Lots
if(stoploss==0) {
return(Lots);
}

double stoploss_4;
if(Close[0]<10)
{
stoploss_4 = stoploss/MathPow(10, Digits-4);
}
else if(Close[0]>10)
{
stoploss_4 = stoploss/MathPow(10, Digits-2);
}

double lots_equity = AccountEquity() * MM_Risk/100;
// In 4 Digits platform, 1 point in 1 lots means $10
double lots = NormalizeDouble(lots_equity/(stoploss_4*10), 1);
return(lots);
}

//-----------------------------------------------------------------------------------

//+------------------------------------------------------------------+
//| Break Even @http://kolier.li |
//+------------------------------------------------------------------+
void breakEven()
{
int orders_total = OrdersTotal();
for(int i=0; i<orders_total; i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderProfit()>0)
{
if(OrderStopLoss()<OrderOpenPrice() && OrderType()==OP_BUY && Bid-OrderOpenPrice()>=BreakEvenPoints*Point)
{
OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), 0, Magenta);
}
else if(OrderStopLoss()>OrderOpenPrice() && OrderType()==OP_SELL && OrderOpenPrice()-Ask>=BreakEvenPoints*Point)
{
OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), 0, Green);
}
}
}
}
//--------------------------------------------------
//+------------------------------------------------------------------+
//| Close Buy Trades @http://kolier.li |
//+------------------------------------------------------------------+
void closeTradesBuy()
{
int trades_total = OrdersTotal();
for(int i=0; i<trades_total; i++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && OrderMagicNumber()==Magic) {
OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, White);
}
}
}

//+------------------------------------------------------------------+
//| Close Trades Sell @http://kolier.li |
//+------------------------------------------------------------------+
void closeTradesSell()
{
int trades_total = OrdersTotal();
for(int i=0; i<trades_total; i++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && OrderMagicNumber()==Magic) {
OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, White);
}
}
}
 

can u describe what u mean by "something seems missing"

& for large code please Attach the file

 
qjol:

can u describe what u mean by "something seems missing"

& for large code please Attach the file


as you see there is a conditional, simple one really,

in Monday and Tuesday last weekly candle is red and daily is Green Go buy and close condition is red candle on daily, now the logicseemed good to me, but i think the problem is in the dayofweek function, seemingly it doesn't give a returning value hence not giving order commencing function,

it is the only thing i added that stopped it, and i was wondering how i can correct it, i looked up in the forum and i found the way i did it,

in the trading log it just commences the EA and ends it no errors or anything

 
Mrsaya:

as you see there is a conditional, simple one really,

in Monday and Tuesday last weekly candle is red and daily is Green Go buy and close condition is red candle on daily, now the logicseemed good to me, but i think the problem is in the dayofweek function, seemingly it doesn't give a returning value hence not giving order commencing function,

it is the only thing i added that stopped it, and i was wondering how i can correct it, i looked up in the forum and i found the way i did it,

in the trading log it just commences the EA and ends it no errors or anything



Dow should give a value of 0 1 2 3 4 ... as dayz of week as i read, but when i did return value it gives it but it does not commence it still,


if(!trade_first && Time[0]>time_first) {
if ((Candle == Red && (Dow == 1||Dow == 2) && Candle3 == Green )||(Candle ==Green&& (Dow==3||Dow==4||Dow==2) && Candle3==Green)){
tradeBuy();
trade_first = true;
}
if ((Candle == Green && (Dow == 1||Dow == 2) && Candle3 == Red )||(Candle ==Red&& (Dow==3||Dow==4||Dow==2) && Candle3==Red)){
tradeSell();
trade_first = true;
}
}
if(!tradedInBar() && AllowBuy&& scanTrades(TRADETRADE,TRADEBUY)<=0&& scanTrades(TRADETRADE,TRADESELL)<=0) {
if ((Candle == Red && (Dow == 1||Dow == 2) && Candle3 == Green )||(Candle ==Green&& (Dow==3||Dow==4||Dow==2) && Candle3==Green))
tradeBuy();
}
if(!tradedInBar() && AllowSell&& scanTrades(TRADETRADE,TRADESELL)<=0&& scanTrades(TRADETRADE,TRADEBUY)<=0) {
if ((Candle == Green && (Dow == 1||Dow == 2) && Candle3 == Red )||(Candle ==Red&& (Dow==3||Dow==4||Dow==2) && Candle3==Red))
tradeSell();
}
 

in Monday and Tuesday [...] daily is Green

u mean the current candle ?

 
qjol:

in Monday and Tuesday [...] daily is Green

u mean the current candle ?

Well I set it to last candle, but both are valid I set shift to 1 or at 0 both return no value :S
 

try to use iTime() instead of Time[]

double DOW = TimeDayOfWeek(iTime(NULL,PERIOD_D1,0));
 

Thank You So much found the solution

Turned out the function was always returning 0 so I tried DayOfweek() function and it gave a return all good now at least for now :) have a briliant day

int Dow ;
Dow = DayOfWeek();

 

that's what i thought

 
  1. Use SRC
  2. void closeTradesBuy()
    {
    int trades_total = OrdersTotal();
    for(int i=0; i<trades_total; i++) {
    OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
    if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && OrderMagicNumber()==Magic) {
    OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, White);
    }
    In the presence of multiple order (multiple charts) you MUST count down. Also Test the return values and Alert if the OrderClose Fails.
    void closeTradesBuy(){
        for(int pos=OrdersTotal()-1; pos>=0; pos--) if(
           OrderSelect(pos, SELECT_BY_POS, MODE_TRADES)
        && OrderSymbol()     ==Symbol() 
        && OrderType()       ==OP_BUY 
        && OrderMagicNumber()==Magic) {
           If(!OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, White)) Alert("OC failed: ",GetLastError();
    }

  3. if(Close[0]<10)
    This will either usually be always true or always false (JPY) but you must adjust TP, SL, and slippage
    //++++ These are adjusted for 5 digit brokers.
    double  pips2points,    // slippage  3 pips    3=points    30=points
            pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    

  4. OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), 0, Green);
    This does nothing.
  5. I added a day of week on the conditional and i think its the source of the problem :
    if ((Candle == Red && (Dow == 1||Dow == 2) && Candle3 == Green )||(Candle ==Green&& (Dow==3||Dow==4||Dow==2) && Candle3==Green)){
    tradeBuy();
    trade_first = true;
    }
    This is un-readable and therefor you can't understand it. simplify and self document and it should become clear.
    bool dnLastWeek.now.Mon_Tue = Candle == Red && (Dow == 1||Dow == 2),
         upYesterday            = Candle3 == Green,
         upLastWeek.now.Tue_Thu = Candle ==Green&& (Dow==3||Dow==4||Dow==2),
         buy1             = dnLastWeek.now.Mon_Tue && upYesterday,
         buy2             = upLastWeek.now.Tue_Thu && upYesterday;
    if ( buy1 || buy2 ){
      tradeBuy();
      trade_first = true;
    }

  6. int Dow;
    double DOW = TimeDayOfWeek(Time[1]);
    You NEVER set Dow, you set DOW (which should be an int) is never used. To control which days my EA can open I use:
        int         DOW = TimeDayOfWeek(now),   /* https://www.mql5.com/en/forum/127483
        // reports DayOfWeek() always returns 5 in the tester. No refresh?*/
                    DayMask = 1 << DOW;
        //                      #define DAYS_MAX    0x3F// 1<<6-1=63. (S-F)
        //extern int      Days.Mask               =  55;      // Not Wed
        if (Days.Mask & DayMask == 0){  EA.status="Day="+DOW;           return; }
    

Reason: