How to code? - page 206

 

Using External *.set files

I have tried and tried but can not figure this out so hopefully someone can see the tree through the forest here.

I want to program an EA to use either it's programmed settings or else use an external *.set file.

So if:

extern bool UseExternSet = true;

it will use the settings in the external set file rather than the programmed settings.

Know it can be done but simple lost here.

Any ea example will do

Bernard

 

Compiler problem or code problem ?

extern int ma1period=5;

extern int ma1method=1;

extern int ma1tf=0;

extern int ma2period=5;

extern int ma2method=1;

extern int ma2tf=10080;

extern double lots=1;

int init(){}

int deinit(){}

double CMA (int tf,int period, int shift, int method)

{

double MA=iMA(NULL,tf,period,0,method,0,shift)

return(MA)

}

int start()

{

int BarsCount=0;

if (Bars> BarsCount)

{

BarsCount=Bars;

double ma1.1=CMA(ma1tf,ma1period,1,ma1method);

double ma1.2=CMA(ma1tf,ma1period,2,ma1method);

double ma2.1=CMA(ma2tf,ma2period,1,ma2method);

double ma2.2=CMA(ma2tf,ma2period,2,ma2method);

bool ma1upsignal =ma1.2<ma1.1;

bool ma1downsignal =ma1.2>ma1.1;

bool ma2upsignal =ma2.2<ma2.1;

bool ma2downsignal =ma2.2>ma2.1;

if (ma1upsignal&&ma2upsignal)

{

OrderSend(Symbol(),OP_BUY,lots,Ask,3,NULL,NULL,NULL,NULL,0,Green);

}

if (ma1downsignal&&ma2downsignal)

{

OrderSend(Symbol(),OP_SELL,lots,Bid,3,NULL,NULL,NULL,NULL,0,Green);

}

if ((ma1upsignal&&ma2downsignal)||(ma1downsignal&&ma2upsignal))

{

int total=OrdersTotal(); //Script to close all open orders.

for(int i=total-1;i>=0;i--)

{

OrderSelect(i, SELECT_BY_POS);

int type = OrderType();

switch(type)

{

//Close opened long positions

case OP_BUY : OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );

break;

//Close opened short positions

case OP_SELL : OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );

}

}

}

}

This is the piece of code i'm working on. The compiler says "the variable BarsCount is not defined". What do i do? As far as I can see, the variable has been initialized and given a value.

Is it my compiler problem or code problem ?

Files:
capture2_1.png  12 kb
 

[CODE]

double CMA (int tf,int period, int shift, int method)

{

double MA=iMA(NULL,tf,period,0,method,0,shift);

return(MA);

}

 
username1:
[CODE]

double CMA (int tf,int period, int shift, int method)

{

double MA=iMA(NULL,tf,period,0,method,0,shift);

return(MA);

}

Michel,

Thanks. That solved the problem. :-)

 

Counting consecutive bars

Anyone got some code on how to count consecutive buy/sell bars in a row? I want to count how many times on a chart (enter number of bars to check) there are 1 sell bars in a row, 32sell bars in a row, 3 sell bars in a row, 4 sell bars in a row (same w/ buy) etc.

I know I need to loop it, but I can't quite figure out how.

Results can go in a Comment().

thanks.

 

Why does not my prog not work?

Wrote a simple prog.

It is supposed to enter only one order at a time,& exit w/ a take profit or a stop loss.

It is set up for M30 on GBPUSD.

Ie: some of the constants sed in the prog are found by "eyeball" estimates. This should not greatly effect the prog... it also does not work w/ different constants. (At least, when tested w/ the Strategy Tester, set on fast or medium: not "tick by tick". (too slow...)

Anyhow the prog, when tested does not enter an order AND does not ever! check in the "up" direction. (This, via the "print"/trace)

(it is supposed to check: enter w/ a buy or enter w/ a sell)

Incidental logic: check 2 consecutive bars. If bar 1 has both: a higher high & a higher low, prog thinks that the next bar will be up.

(Symetrically for down.)

Take profit & stop loss are set accordingly.

The prog also did not work when the "if" statements were nested....

Thanks for looking at this.

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init()

{

//----

//----

return(0);

}

//+------------------------------------------------------------------+

//| expert deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

//----

//----

return(0);

}

//+------------------------------------------------------------------+

//| expert start function |

//+------------------------------------------------------------------+

int start()

{

//----

bool TrendUp1; double TakeProfitUp; double EE = .00500;

bool OrdTot_OK;double lot;double slip;

double SL_Trailing_Up;bool High2Up;bool Low2Up;

int ticketup;int ticketdown;bool DownAndBig; bool UpAndBig;

bool TrendDown; double SL_Trailing_Down;double TakeProfitDown;

bool High2down; bool Low2down;

int ABC = OrdersTotal();

double Size1 = .00630;bool BigUp; double A = High[1];double B= Low[2];//41

double C= High[2]; double D= Low[1];bool BigDown;

//------------------------------------down order start

if (ABC == 0)// no orders out is true

OrdTot_OK = true;

if (ABC == 0)// no orders out is true

Print ("on down order sequence, no orders were out");

if ((C-D) > Size1)//high of 2 less low of 1 > .0063

BigDown = true;

if ((C-D) > Size1)//high of 2 less low of 1 > .0063

Print ("on down order sequence, found a big breakout down");

if (High[1]< High[2])

High2down=true;

if (High[1]< High[2])

Print ("on down order sequence, the consecutive bars have decreasing highs");

if(Low[1]< Low[2])

Low2down=true;

if(Low[1]< Low[2])

Print ("on down order sequence, the consecutive bars have decreasing lows");

if (High2down && Low2down)

TrendDown=true;

if (High2down && Low2down)

Print ("on down order sequence, TrendDown flag is set");

if (TrendDown)

SL_Trailing_Down = High[1];

if (TrendDown)

Print ("on down order sequence, did set the stop_loss");

if (TrendDown)

TakeProfitDown = Bid - (.005);

if (TrendDown)

Print ("on down order sequence, got a takeprofit", TakeProfitDown);

if (TrendDown && BigDown)

DownAndBig = true;

if (DownAndBig && OrdTot_OK)

ticketdown= OrderSend(Symbol(),OP_SELL,lot, Bid,slip,SL_Trailing_Down,TakeProfitDown,Blue);

if (DownAndBig && OrdTot_OK)

Print ("on down order sequence, tried to enter a ticket", ticketdown);

if (ticketdown < 0)

Print("OrderSend_Down failed with error #",GetLastError());

return(0);

//----------------------------------------------down order done

//---------------------------------------------------up order start

if (ABC == 0)

OrdTot_OK = true;

if (ABC == 0)

Print ("on the going up sequence, no orders were out");

if ((A - B) > Size1)

BigUp = true;

if ((A - B) > Size1)

Print ("on the going up sequence, the size test for the two bars was ok");

if (High[1] > High[2])

High2Up = true;//44

if (High[1] > High[2])

Print ("on the going up sequence, the test for increasing highs was ok");

if(Low[1] > Low[2])

Low2Up=true;

if(Low[1] > Low[2])

Print ("on the going up sequence, the test for increasing lows was ok");

if (High2Up && Low2Up)

TrendUp1=true;

if (High2Up && Low2Up)

Print ("on the going up sequence, did set the flag for going up");

if (TrendUp1)

SL_Trailing_Up = Low[1] ;

if (TrendUp1)

Print ("on the going up sequence, established the stoploss");

if (TrendUp1)

TakeProfitUp = Ask + (.005);

if (TrendUp1)

Print ("on the going up sequence, established the takeprofit", TakeProfitUp);

if (TrendUp1 && BigUp)

UpAndBig = true;

if ( UpAndBig && OrdTot_OK)

ticketup = OrderSend(Symbol(), OP_BUY,lot, Ask,slip, SL_Trailing_Up,TakeProfitUp,Red);

if (UpAndBig && OrdTot_OK)

Print ("on the going up sequence, tried to send in an order", ticketup);

if (ticketup < 0)

Print("OrderSend_Up failed with error #",GetLastError());

return(0);

//-------------------------------------------------up order done

//----

return(0);// not sure about this line

//+------------------------------------------------------------------+

//----

return(0);

}

//+------------

 

Minimum bars before/after trade

Hi,

Hi, i'm new to MT4.. Could someone please let me know how i can place a restriction in an EA that requires a minimum of "x" bars to either open a new position or close a previous one?

(without using the sleep command)

Regards.

 

mql coding help

if (iMA(NULL, 0, 24, 0, MODE_LWMA, PRICE_WEIGHTED, 1) > iMA(NULL, 0, 120, 1, MODE_LWMA, PRICE_WEIGHTED, 1)) gi_268 = 2;

---------------------------------------------------------------

what does it mean ??

2 LWMA used ?

LWMA period =24 shift =0 on price weighted (hcll4) cross over LWMA period 120 shift =1 on price weighted (hcll4) then buy or sell ??

 

Problem with closing position

Hi,

I just started with my new EA and opening positions is OK, I setup tp and sl. EA open and close 1 by 1 position and never there are more than 1 order. But I would like to close position if some of indicators alert me before sl or tp limit.

If I try something like this:

for (int i=1; i<=OrdersTotal(); i++) {

if (OrderSelect(i, SELECT_BY_POS, 2)) {

if (OrderSymbol()==Symbol()) {

Ans=OrderClose(OrderTicket(),alLots,Bid,2);// Order closing

}

}

}[/CODE]

Nothing happened. Doesn't find any opened positions and I know there is 1.

If I try something like this:

for (int i=1; i<=OrdersTotal(); i++) {

CloseOrder(OrderValue(i,VAL_TICKET),OrderValue(i,VAL_LOTS),Bid,3);

}

Compiler report error message that VAL_TICKER and VAL_LOTS are not defined and in examples I found they are nowhere declared.

If I try direcly by number to close:

[CODE]

Ans=OrderClose(0,alLots,Bid,2);// Order closing

I get error message 4107 or 4105 that ticker is invalid

Can somebody check this and my ea in attachment and help please?

Thanks a lot

Files:
rsiproba.mq4  6 kb
 

iHighest iLowest Clarification

Assume 3 bars: Bar [2], Bar [1] and Bar [0] (Current).

Is the following "stop_low()" function correct to find the lowest low of bar [2] and [3] ?

double stop_low = iLowest(Symbol(),0,1,2,1);

------------------------------------------------------------

Reference:

int iLowest( string symbol, int timeframe, int type, int count=WHOLE_ARRAY, int start=0)

Returns the shift of the least value over a specific number of periods depending on type.

Parameters:

symbol - Symbol the data of which should be used to calculate indicator. NULL means the current symbol.

timeframe - Timeframe. It can be any of Timeframe enumeration values. 0 means the current chart timeframe.

type - Series array identifier. It can be any of Series array identifier enumeration values.

count - Number of periods (in direction from the start bar to the back one) on which the calculation is carried out.

start - Shift showing the bar, relative to the current bar, that the data should be taken from.

Sample:

// calculating the lowest value on the 10 consequtive bars in the range

// from the 10th to the 19th index inclusive on the current chart

double val=Low;

Reason: