one trade per day...

 

Hello

I want to know CODE about one trade per day..

I tried this as below

int i,onedayorder, totalorders = OrdersHistoryTotal();

for(i=0;i<totalorders; i++)

{

if( OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true)

{ datetime A = OrderOpenTime();

if( (TimeYear(A)==Year()) && (TimeMonth(A)==Month())&& (TimeDay(A)==Day()) )

{

onedayorder = 1;

}

else

{onedayorder =2;

}

}

}

if this code is added, error I keep getting an error message. but without this code, no error...

How should i do? Please help me.

I am not familiar with English. Please understand that.

Files:
gp.mq4  3 kb
 

Hi kjwoong001,

Try Google http://www.google.com/search?q=site:forum.mql4.com/ one per day and Google Translate

There is incomplete 'if statement' inside your init().

:D

 
onewithzachy:

Hi kjwoong001,

Try Google http://www.google.com/search?q=site:forum.mql4.com/ one per day and Google Translate

There is incomplete 'if statement' inside your init().

:D

Hello,onewithzachy

Thank you for reply. I will try,, :D

Your answer was a big help.

Again,, Thanks a lot . ^^

 

  1. Also you're code won't work with multiple closed orders. If you only want to trade once per day, not once per 24 hours try
    Better from
    onedayorder =2;
    datetime now = TimeCurrent(),
             bod = now - now%86400;// Beginning of day
    for(i=0;i<totalorders; i++){
      if( OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)){
      // Always filter by magic number and pair also.
        datetime tot = OrderOpenTime(), // Time of Trade
                 dot = tot - tot%86400; // Day of Trade.
        if(dot == bot) onedayorder = 1;
      }
    }
    for(i=0;i<totalorders; i++)
    {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true)
      { datetime A = OrderOpenTime();
         if( (TimeYear(A)==Year())
         && (TimeMonth(A)==Month())
         && (TimeDay(A)==Day()) )
         {
            onedayorder = 1;
         }
         else
         {onedayorder =2;
         }
    }
 
WHRoeder:

  1. 또한 코드가 여러 개 폐쇄 명령과 함께 작동하지 않습니다에요. 당신은 단지 시도하지 번만 24시간 회, 하루에 한 번씩 교환하려는 경우
    더 나은 부터



Hello,

Thank you for your help

In my country, foreign exchange trading is the toddler level...

I'm sorry if I violated the courtesy and form,,

 
kjwoong001:

Hello

I want to know CODE about one trade per day..

I tried this as below

...

How should i do? Please help me.

I am not familiar with English. Please understand that.

Hi. In my EA I use the same conception, one trade per one bar (this may be PERIOD_D1 as well). Program is looking for an opened position with opened time more then the time when bar was opened. Try this:

int MagNum=555;
int TimeFrame=PERIOD_D1; // may be other period
int Closed_bar;
//--- 
if (OrdersTotal()>0)
{  for (i=OrdersTotal()-1; i>=0; i--)
   {  if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {  if (OrderSymbol()==Symbol())
         {  if (OrderMagicNumber()==MagNum)
            {  Closed_bar=OrderOpenTime();
               if (Closed_bar>=iTime(NULL,TimeFrame,0))
               {  Comment ("At the current bar position was opened.");
                  return(0);
         }  }  }
}  }  }
//--- 

As you see there are some filters, symbol and magic number.

 

I am not quite sure if I understand the problem definition.

Are you trying to trade only once per day bar or real day as defined by your local timezone?

Does an orderclose/stop out on the next bar count as a trade on that bar?

Anyway, to open a new trade only once per bar (1h, 4h, 1d etc):

datetime LastTraded=0;



void Start() {
   if (LastTraded!=Time[0] && ...) {   // check last traded time and my EA conditions to trade
      OrderSend(...)
      LastTraded=Time[0]
   }
}

The above logic does not prevent a trade if there was a stop out or order close on some later bar. The code is pseudo code - you will need to adapt to your own needs.

 
Due to the answer from rocketman99, I need to precise my conception. The code I posted above allows to open one position per bar, but if this position was closed on the same bar and new bar was not created yet, the second position may be opened again (on the current bar). So, more then one position may be opened, but only one at the same time. So the code from rocketman99 is good for case, when you need no more then one trade per bar.
 
paladin80:

Hi. In my EA I use the same conception, one trade per one bar (this may be PERIOD_D1 as well). Program is looking for an opened position with opened time more then the time when bar was opened. Try this:

As you see there are some filters, symbol and magic number.



Hello, paladin80,,

Thank you for your reply! I will try it,,^^

 
rocketman99:

I am not quite sure if I understand the problem definition.

Are you trying to trade only once per day bar or real day as defined by your local timezone?

Does an orderclose/stop out on the next bar count as a trade on that bar?

Anyway, to open a new trade only once per bar (1h, 4h, 1d etc):

The above logic does not prevent a trade if there was a stop out or order close on some later bar. The code is pseudo code - you will need to adapt to your own needs.


I don't have tried that,, only once per day bar or real day as defined by ~~~~

In fact,, I don't understand concept which is that real day as defined by ~~~~

In my country, foreign exchange trading is the toddler level. I knew this forumsite a couple of weeks ago.

Can you describe more about the above code ?

 
Simplified
Original
for (i=OrdersTotal()-1; i>=0; i--) if(
   OrderSelect(i,SELECT_BY_POS)
&& OrderMagicNumber()==MagNum
&& OrderSymbol()==Symbol()
){  
    Closed_bar=OrderOpenTime();
    if (Closed_bar>=iTime(NULL,TimeFrame,0))
    {  Comment ("At the current bar pos...";
       return(0);
    }
} 
if (OrdersTotal()>0)
{  for (i=OrdersTotal()-1; i>=0; i--)
   {  if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {  if (OrderSymbol()==Symbol())
         {  if (OrderMagicNumber()==MagNum)
            {  Closed_bar=OrderOpenTime();
               if (Closed_bar>=iTime(NULL,TimeFrame,0))
               {  Comment ("At the current bar ...";
                  return(0);
         }  }  }
}  }  }
The code I posted above allows to open one position per bar,

No it doesn't. iTime is the start of the TimeFrame bar. The test checks if the highest opened order was opened this bar. If it opened the previous bar, or once it closes the loop will not find ANY order and you could open another on the same bar.

Has nothing to do this checking for the last closed order was the same day. There you must fine the last closed order. See here