Code request HELP

 

Hi there,


I wonder if someone has some code or can develop a code with the following conditions: (trend following)


Moving Average:

- Period: 90

- Shift: 0

- Mode: Simple


The code must test the candlestick bar status for 2 different timeframes: 1H and 4H


Rules:

NO ORDER: Wait for the candlestick cross the MA on 1H time frame

BUY: When there is a candlestick totally above the MA on 1H time frame (lower price of candlestick of the closed bar above the MA) it must check on 4H timeframe if the candlestick is also totally above the MA (lower price of candlestick of the closed bar above the MA).

SELL: When the candlestick totally bellow the MA on both 1H and 4H time frames.


So, I need:

- A function that returns a value if the NO ORDER condition is found;

- A function that, when NO ORDER condition is false, returns another value when the candlestick is totally above MA on both 1H and on 4H

- A function that, when NO ORDER condition is false, returns another value when the candlestick is totally bellow MA on both 1H and on 4H


Can anyone help me?

Regards

 

see if this is ok:



int orders_total(string _symbol, int _type)
{
int total=0;
for(int pos = 0; pos < OrdersTotal(); pos++)
if(OrderSelect(pos, SELECT_BY_POS))
if( (OrderSymbol()==_symbol) && (OrderType()==_type) )
total++;
return(total);
}




bool check_order_condition(string _symbol, int _type,int _period)

{
double ma_h1_1=iMA(_symbol, PERIOD_H1,_period, 0,MODE_SMA,MODE_CLOSE,1);
double ma_h1_2=iMA(_symbol, PERIOD_H1,_period, 0,MODE_SMA,MODE_CLOSE,2);

double ma_h4_1=iMA(_symbol, PERIOD_H4,_period, 0,MODE_SMA,MODE_CLOSE,1);
double ma_h4_2=iMA(_symbol, PERIOD_H4,_period, 0,MODE_SMA,MODE_CLOSE,2);

double lowest_h1 = iLow(_symbol, PERIOD_H1,1);
double lowest_h4 = iLow(_symbol, PERIOD_H4,1);

double highest_h1 = iLow(_symbol, PERIOD_H1,1);
double highest_h4 = iLow(_symbol, PERIOD_H4,1);

if(_type == OP_BUY)
if( (lowest_h1 > ma_h1_1) && (lowest_h4>ma_h4_1) )
return(true);
if(_type == OP_SELL)
if( (highest_h1 < ma_h1_1) && (highest_h4<ma_h4_1) )
return(true);

}

bool dont_order(int period)
{
bool ok_to_buy = check_order_condition(Symbol(), OP_BUY,period);
bool ok_to_sell = check_order_condition(Symbol(), OP_SELL,period);

bool do_order = ok_to_buy || ok_to_sell;
return( !do_order ); // return negation of 'do_order'
}

/**************EXAMPLE HOW TO USE***********************/

int start()
{
int longs = orders_total(Symbol(), OP_BUY);
int shorts = orders_total(Symbol(), OP_SELL);

bool ok_to_buy = check_order_condition(Symbol(), OP_BUY,period);
bool ok_to_sell = check_order_condition(Symbol(), OP_SELL,period);

if((longs==0) && ok_to_buy)
OrderSend(Symbol(),OP_BUY,0.1,Ask,3,0,0,"skdjfsnkf",3478,0,White);
if((shorts==0) && ok_to_sell)
OrderSend(Symbol(),OP_SELL,0.1,Ask,3,0,0,"skdjfssedfnkf",3478,0,Red);

// HERE SHOULD BE ORDER MANAGEMENT..

//...

}

 
abstract_mind:

see if this is ok:



int orders_total(string _symbol, int _type)
{
int total=0;
for(int pos = 0; pos < OrdersTotal(); pos++)
if(OrderSelect(pos, SELECT_BY_POS))
if( (OrderSymbol()==_symbol) && (OrderType()==_type) )
total++;
return(total);
}




bool check_order_condition(string _symbol, int _type,int _period)

{
double ma_h1_1=iMA(_symbol, PERIOD_H1,_period, 0,MODE_SMA,MODE_CLOSE,1);
double ma_h1_2=iMA(_symbol, PERIOD_H1,_period, 0,MODE_SMA,MODE_CLOSE,2);

double ma_h4_1=iMA(_symbol, PERIOD_H4,_period, 0,MODE_SMA,MODE_CLOSE,1);
double ma_h4_2=iMA(_symbol, PERIOD_H4,_period, 0,MODE_SMA,MODE_CLOSE,2);

double lowest_h1 = iLow(_symbol, PERIOD_H1,1);
double lowest_h4 = iLow(_symbol, PERIOD_H4,1);

double highest_h1 = iLow(_symbol, PERIOD_H1,1);
double highest_h4 = iLow(_symbol, PERIOD_H4,1);

if(_type == OP_BUY)
if( (lowest_h1 > ma_h1_1) && (lowest_h4>ma_h4_1) )
return(true);
if(_type == OP_SELL)
if( (highest_h1 < ma_h1_1) && (highest_h4<ma_h4_1) )
return(true);

}

bool dont_order(int period)
{
bool ok_to_buy = check_order_condition(Symbol(), OP_BUY,period);
bool ok_to_sell = check_order_condition(Symbol(), OP_SELL,period);

bool do_order = ok_to_buy || ok_to_sell;
return( !do_order ); // return negation of 'do_order'
}

/**************EXAMPLE HOW TO USE***********************/

int start()
{
int longs = orders_total(Symbol(), OP_BUY);
int shorts = orders_total(Symbol(), OP_SELL);

bool ok_to_buy = check_order_condition(Symbol(), OP_BUY,period);
bool ok_to_sell = check_order_condition(Symbol(), OP_SELL,period);

if((longs==0) && ok_to_buy)
OrderSend(Symbol(),OP_BUY,0.1,Ask,3,0,0,"skdjfsnkf",3478,0,White);
if((shorts==0) && ok_to_sell)
OrderSend(Symbol(),OP_SELL,0.1,Ask,3,0,0,"skdjfssedfnkf",3478,0,Red);

// HERE SHOULD BE ORDER MANAGEMENT..

//...

}




Dear Sir,


Thank you very much

I have one additional question and I believe you can help me. How can I detect when a new bar has open?


Thank you once again


Regards


Paulo

 

here is approach:

int timeCandle = 0;

int start()
{
// ..... read indicators and so on...

// ....


// check for new candle
if(timeCandle!= iTime(Symbol(),PERIOD_H4,0)) {

// if I got inside here, then it is a new candle
//update flag for next new candle
time_candle = iTime(Symbol(),PERIOD_H4,0);

// ...go trading......
go_trading();

// ...check each order for modify/close
do_order_management();
//... do something else here
}
}

with this approach you have to make sure that any order to be open at a new candle, is in fact opened, otherwise opportunity will be missed. Sometimes, server don't open orders (because of several things, including busy contexts). The problem is that flag time_candle is updated, and EA will only enter into go_trading section again at the next new candle. A solution is to perform update of the time_candle flag only after eventual orders have been opened. So, you might need to adjust this logic to your EA's logic.

Reason: