Ask! - page 88

 
 

thanks Ralph, I tried this but then it gave another list of errors - said that all the variables in the includes file are not defined - but they are

I'll have another go at this over the weekend, it's frustrating me a bit at the moment.

@faqcya - I suspect you are right about putting it into a script but I don't know how else to handle calling it unless I have a shell EA to put it into.

Maybe I'll have a look at the templates and see if I can call it from that or even better possibly turn it into an EA, I only want it as it is, not to do anything else.

Thanks for your help,

Kevin

ralph.ronnquist:
@pgtips: change the second line to be only
#define MAGIC 20051120
i.e., no equal sign and no comma.
 

Trying to learn programming

First of all, I have read the entire course by Corder's Guru and found it very interesting. I have come to the conclusion that the only reasonable way to try out a strategy is by writing a program to make sure it is fully accurate. Unfortunately, I am a total newbie when it comes to this.

I have been trying to write a basic program and can't even get that to work. At least I don't get any errors anymore but it doesn't take any orders. I am persistent and will keep trying but would appreciate some help from anybody who sees my mistake

All I am trying to do at this point is to track the lowest price (CurrentLow) and then when it retraces by a certain amount (Ret1), place a sell order with TP of the low point.

Please don't laugh but the following is what I have written so far:

//---- input parameters

extern double TP1=75.0;

extern double Ret1=75.0;

extern double SL1=150.0;

extern double Lots=0.1;

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

//| expert initialization function |

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

int init()

{

//----

//----

return(0);

}

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

//| expert deinitialization function |

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

int deinit()

{

//----

//----

return(0);

}

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

//| expert start function |

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

int start()

{

int ticket=0;

if(Bars<100)

{

Print("bars less than 100");

return(0);

}

double CurrentLow,CurrentHigh;

if(Bid < CurrentLow)

{

CurrentLow = Bid;

if(Bid > CurrentHigh)

{

CurrentHigh = Bid;

if(CurrentHigh >= CurrentLow+(Point*Ret1))

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+(SL1*Point),Bid-(TP1*Point),"Albatross_v1",757575,0,Green);

return (0);

}

return (0);

}

return(0);

}

}

 
Putz:
First of all, I have read the entire course by Corder's Guru and found it very interesting. I have come to the conclusion that the only reasonable way to try out a strategy is by writing a program to make sure it is fully accurate. Unfortunately, I am a total newbie when it comes to this.

I have been trying to write a basic program and can't even get that to work. At least I don't get any errors anymore but it doesn't take any orders. I am persistent and will keep trying but would appreciate some help from anybody who sees my mistake

All I am trying to do at this point is to track the lowest price (CurrentLow) and then when it retraces by a certain amount (Ret1), place a sell order with TP of the low point.

Please don't laugh but the following is what I have written so far:

//---- input parameters

extern double TP1=75.0;

extern double Ret1=75.0;

extern double SL1=150.0;

extern double Lots=0.1;

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

//| expert initialization function |

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

int init()

{

//----

//----

return(0);

}

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

//| expert deinitialization function |

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

int deinit()

{

//----

//----

return(0);

}

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

//| expert start function |

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

int start()

{

int ticket=0;

if(Bars<100)

{

Print("bars less than 100");

return(0);

}

double CurrentLow,CurrentHigh;

if(Bid < CurrentLow)

{

CurrentLow = Bid;

if(Bid > CurrentHigh)

{

CurrentHigh = Bid;

if(CurrentHigh >= CurrentLow+(Point*Ret1))

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+(SL1*Point),Bid-(TP1*Point),"Albatross_v1",757575,0,Green);

return (0);

}

return (0);

}

return(0);

}

}

It seems wrong to check if Bid > CurrentHigh inside the true condition that Bid < CurrentLow; I thing you should have independent tests like this :

double CurrentLow,CurrentHigh;

int start()

{

if(Bid < CurrentLow) CurrentLow = Bid;

if(Bid > CurrentHigh) CurrentHigh = Bid;

if(Bid >= CurrentLow+(Point*Ret1))

OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+(SL1*Point),Bid-(TP1*Point),"Albatross_v1",757575,0,Green);

return(0);

}

Now, three remarks:

- You have to declare CurrentHigh and CurrentLow on a global scoop, ie out of any function (and start() is a function) otherwize those variables will be initialized to 0 at each call of the function.

- You don't need CurrentHigh as it is not used.

- You have to implement some logic to reset/update the value of CurrentLow, and probably to limit the opening of orders at each tick above CurrentLow+(Point*Ret1))

 

Hi Michel,

Thank you very much for your prompt response and help.

I have deleted CurrentHigh and now understand a lot better. I have also defined CurrentLow at the top of the program and now it actually places orders.

I have also added a second order based on a larger retracement and that seems to work. Now I have to figure out how to stop it from creating a new order on every tick but I will work on that on my own for now as I will learn it better that way.

I'm sure I will be back here with more questions.

Thanks again.

Putz

 
Putz:
Hi Michel,

Thank you very much for your prompt response and help.

I have deleted CurrentHigh and now understand a lot better. I have also defined CurrentLow at the top of the program and now it actually places orders.

I have also added a second order based on a larger retracement and that seems to work. Now I have to figure out how to stop it from creating a new order on every tick but I will work on that on my own for now as I will learn it better that way.

I'm sure I will be back here with more questions.

Thanks again.

Putz

Don't forget to initialize CurrentLow with a big value (like 1000); if its' initialized with 0, you will never have a Bid < 0

 

Which programming luaguage shall I learn?

codersguru:
Hi folks,

I've got a lot of private messages asking me for helping with some pieces of code.

Here you can post your questions related to MQL4, and I'll do my best to answer them.

Dear codersguru,

If I wish to learn to write MQL4 codes, which programming language shall I learn? Visual basic or C++? Thank your for your answer.

 

More help please

Hello,

I'm back for more help. I've been reading and looking at many other EA's to try to figure it out but I still can't get it to work properly.

What I am trying to do is place two orders.

1) Sell 0.1 lot once price retraces by Ret1 (75 pips) with TP1 (75) and SL1 (150)

2) Sell 0.2 lot once price retraces by Ret2 (150 pips) with TP1 (75) and SL1 (75)

In the long term, I want to get a buy order like above to be placed based on direction of trend. I haven't yet determined which indicator I will use to determine trend yet but I will get there at some point.

The following is what I currently have. It seems to place the first order properly but the second one never gets triggered. Any help will be highly appreciated.

extern string Expert_Name = "Albatross v1";

extern int MagicNumber = 757575;

extern int Slippage = 3.0;

//---- input parameters

extern double TP1=75.0;

extern double TP2=75.0;

extern double Ret1=75.0;

extern double Ret2=150.0;

extern double SL1=150.0;

extern double SL2=75.0;

extern double Lots=0.1;

double CurrentLow=1000;

//double CurrentHigh=0;

int OpenOrders=0,cnt=0;

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

//| Custom indicator initialization function |

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

//int init()

// {

//---- indicators

//----

// return(0);

// }

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

//| Custom indicator deinitialization function |

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

// int deinit()

// {

//---- TODO: add your code here

//----

// return(0);

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

//| Custom indicator iteration function |

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

// int start()

// {

//---- TODO: add your code here

//----

// return(0);

// }

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

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

//| expert initialization function |

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

int init()

{

//----

//----

return(0);

}

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

//| expert deinitialization function |

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

int deinit()

{

//----

//----

return(0);

}

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

//| Check Open Position Controls |

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

int CheckOpenTrades()

{

int cnt;

int NumTrades; // Number of buy and sell trades in this symbol

NumTrades = 0;

for(cnt=OrdersTotal()-1;cnt>=0;cnt--)

{

OrderSelect (cnt, SELECT_BY_POS, MODE_TRADES);

if ( OrderSymbol() != Symbol()) continue;

if ( OrderMagicNumber() != MagicNumber) continue;

if(OrderType() == OP_BUY ) NumTrades++;

if(OrderType() == OP_SELL ) NumTrades++;

}

return (NumTrades);

}

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

//| expert start function |

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

int start()

{

int cnt, ticket, total;

double TP;

if(Bars<100)

{

Print("bars less than 100");

return(0);

}

total = CheckOpenTrades();

if(total < 1)

{

if(Bid < CurrentLow) CurrentLow = Bid;

if(Bid >= CurrentLow+(Point*Ret1))

OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+(SL1*Point),Bid-(TP1*Point),"Albatross_v1",MagicNumber,0,Green);

}

if(total < 2)

{

if(Bid >= CurrentLow+(Point*Ret2))

OrderSend(Symbol(),OP_SELL,Lots*2,Bid+(Point*Ret2),3,Bid+(SL2*Point),Bid-(TP2*Point),"Albatross_v1",MagicNumber,0,Green);

return(0);

}

}

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

 

Sorry to Bother You With This Question

How do you create an object that is filled with a color??

Dave

<<<

 

Hello All, I am a coder and been coding for long time in C++ but my knowledege of FX and MQL4 is very basic. I am going thorugh an EA that someone wrote and trying to understand the code...

can you please explain me in simple english that what is SHIFT parameter in iMA method?

double iMA(string symbol, int timeframe, int period, int ma_shift, int ma_method, int applied_price, int shift)

according to MT4 help, its "Index of the value taken from the indicator buffer (shift relative to the current bar the given amount of periods ago)." - what is indicator buffer???

he's also doing the following...

int RealTime = 0;

if( UseCompletedBars )

{

if(timeprev==Time[0]){return(0);} timeprev = Time[0];

RealTime = 1;

}

double MA11 = iMA(NULL,0,MA1Periods,0,MA1MethodSelected, MA1ArraySelected,0+RealTime);

double MA12 = iMA(NULL,0,MA1Periods,0,MA1MethodSelected, MA1ArraySelected,1+RealTime);

double MA21 = iMA(NULL,0,MA2Periods,0,MA2MethodSelected, MA2ArraySelected,0+RealTime);

double MA22 = iMA(NULL,0,MA2Periods,0,MA2MethodSelected, MA2ArraySelected,1+RealTime);

if( MA11 > MA21 && MA12 < MA22 ) { EnterLong = True; }

if( MA11 MA22 ) { EnterShort = True; }

what would that return? since i dont know the shift, i think thats why i cant get it ...

your answer will be highly appritiated.

please help.

Reason: