|
|||||||
| Register in Forex TSD! | |
|
Trading Systems Leaders in this forum (automated trading systems) are winning more than 3000 pips in a month (30000$ investing one lot every time). Click here to register and get more information |
|
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
Templates to create EAs and Indicators
Coders' Guru,
I want to ask: May we have some templates to create Custom indicators and EAs? It will be easy for us to start. Later on we will have our own practics to create it and our own experience. But the templates will help us from the beginning. For example I found in internet some template of EA. May be it is not right template and everything is possible to program in easier way ... May be I do not know. But it is the template I found: Code:
#define MAGIC 10653
#include
#include
extern double lStopLoss = 50; //Stop Loss for BUY
extern double sStopLoss = 50; //Stop Loss for SELL
extern double lTakeProfit = 100; //Take Profit for BUY
extern double sTakeProfit = 100; //Take Profit SELL
extern double lTrailingStop = 15; //Trailing Stop for BUY
extern double sTrailingStop = 15; //Trailing Stop for SELL
extern color clOpenBuy = Blue; //Color for BUY order
extern color clCloseBuy = Aqua; //Color for BUY exit
extern color clModiBuy = Blue; //Modofication Color BUY
extern color clOpenSell = Red; //Color to open SELL
extern color clCloseSell = Violet; //Color SELL exit
extern color clModiSell = Red; //Color to open SELL
extern string Name_Expert =
..;
extern int Slippage = 3; //
..
extern bool ProfitTrailing = True; // Profit Trailing
extern int TrailingStop = 8; //
extern int TrailingStep = 2; //
extern bool UseSound = True; //
extern string NameFileSound = "alert.wav"; //
extern double Lots = 0.1;
extern bool UseHourTrade = True; //
extern int FromHourTrade = 0; //
extern int ToHourTrade = 23; //
extern double mm = -1;
extern double Risk = 10;
int prevCountBars;
void deinit() {
Comment("");
}
//+-----------------------------------------------------------+
//| |
//+-----------------------------------------------------------+
int start(){
for (int i=0; i
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
TrailingPositions();
}
}
if (UseHourTrade){
if (!(Hour()>=FromHourTrade && Hour()<=ToHourTrade)) {
Comment(Non-Trading hours!);
return(0);
}
}
if(Bars<100){
Print("bars less than 100");
return(0);
}
if(lStopLoss<10){
Print("StopLoss less than 10");
return(0);
}
if(lTakeProfit<10){
Print("TakeProfit less than 10");
return(0);
}
if(sStopLoss<10){
Print("StopLoss less than 10");
return(0);
}
if(sTakeProfit<10){
Print("TakeProfit less than 10");
return(0);
}
double diOpen0=iOpen(NULL,0,1);
double diHigh0=iHigh(NULL,0,1);
double diLow0=iLow(NULL,0,1);
double diClose0=iClose(NULL,0,1);
double diOpen1=iOpen(NULL,0,2);
double diHigh1=iHigh(NULL,0,2);
double diLow1=iLow(NULL,0,2);
double diClose1=iClose(NULL,0,2);
double Median=((iHigh(NULL,0,1)+iLow(NULL,0,1))/2);
double Typical=((iHigh(NULL,0,0)+iLow(NULL,0,0)+iClose(NULL,0,0))/3);
double Weighted=((iHigh(NULL,0,0)+iLow(NULL,0,0)+iClose(NULL,0,0)+iClose(NULL,0,0))/4);
double diVolume=iVolume(NULL,0,0);
if (prevCountBars != Bars && prevCountBars !=0 && !ExistPositions()) {
if(AccountFreeMargin()<(1000*Lots)){
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
if ((.........)){
OpenBuy();
return(0);
}
if ((.........)){
OpenSell();
return(0);
}
}
if (prevCountBars != Bars && prevCountBars !=0 && ExistPositions()) {
if(OrderType()==OP_BUY){
if ((valueh<0 && valuel1>0)) {
CloseBuy();
return(0);
}
}
if(OrderType()==OP_SELL){
if ((valuel>0 && valueh1<0)) {
CloseSell();
return(0);
}
}
}
TrailingPositions();
prevCountBars = Bars;
return (0);
}
bool ExistPositions() {
for (int i=0; i
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC) {
return(True);
}
}
}
return(false);
}
void TrailingPositions() {
double pBid, pAsk, pp;
pp = MarketInfo(OrderSymbol(), MODE_POINT);
if (OrderType()==OP_BUY) {
pBid = MarketInfo(OrderSymbol(), MODE_BID);
if (!ProfitTrailing || (pBid-OrderOpenPrice())>TrailingStop*pp) {
if (OrderStopLoss()
ModifyStopLoss(pBid-TrailingStop*pp);
return;
}
}
}
if (OrderType()==OP_SELL) {
pAsk = MarketInfo(OrderSymbol(), MODE_ASK);
if (!ProfitTrailing || OrderOpenPrice()-pAsk>TrailingStop*pp) {
if (OrderStopLoss()>pAsk+(TrailingStop+TrailingStep-1)*pp || OrderStopLoss()==0) {
ModifyStopLoss(pAsk+TrailingStop*pp);
return;
}
}
}
}
//+------------------------------------------------------------------+
//| StopLoss Modifying|
//| Parameters: |
//| ldStopLoss StopLoss level |
//+------------------------------------------------------------------+
void ModifyStopLoss(double ldStopLoss) {
bool fm;
fm=OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE);
if (fm && UseSound) PlaySound(NameFileSound);
}
//+------------------------------------------------------------------+
void CloseBuy() {
bool fc;
fc=OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, clCloseBuy);
if (fc && UseSound) PlaySound(NameFileSound);
}
void CloseSell() {
bool fc;
fc=OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, clCloseSell);
if (fc && UseSound) PlaySound(NameFileSound);
}
void OpenBuy() {
double ldLot, ldStop, ldTake;
string lsComm;
double b=0;
double balance=0;
double Ilo=0;
balance=AccountBalance();
if (mm < 0) {
Ilo = MathCeil(balance*Risk/10000)/10;
if (Ilo > 100) {
Ilo = 100;
}
} else {
Ilo = Lots;
}
if (mm > 0)
{
Ilo = MathCeil(balance*Risk/10000)/10;
if (Ilo > 1)
{
Ilo = MathCeil(Ilo);
}
if (Ilo < 1)
{
Ilo = 1;
}
if (Ilo > 100)
{
Ilo = 100;
}
}
ldLot = GetSizeLot();
ldStop = GetStopLossBuy();
ldTake = GetTakeProfitBuy();
lsComm = GetCommentForOrder();
//OrderSend(Symbol(),OP_BUY,ldLot,Ask,Slippage,0,0,lsComm,MAGIC,0,clOpenBuy);
//OrderSend(Symbol(),OP_BUY,Ilo,Ask,Slippage,ldStop,ldTake,lsComm,MAGIC,0,clOpenBuy);
OrderSend(Symbol(),OP_BUY,ldLot,Ask,Slippage,ldStop,ldTake,lsComm,MAGIC,0,clOpenBuy);
if (UseSound) PlaySound(NameFileSound);
}
void OpenSell() {
double ldLot, ldStop, ldTake;
string lsComm;
double b=0;
double balance=0;
double Ilo=0;
balance=AccountBalance();
if (mm < 0) {
Ilo = MathCeil(balance*Risk/10000)/10;
if (Ilo > 100) {
Ilo = 100;
}
} else {
Ilo = Lots;
}
if (mm > 0)
{
Ilo = MathCeil(balance*Risk/10000)/10;
if (Ilo > 1)
{
Ilo = MathCeil(Ilo);
}
if (Ilo < 1)
{
Ilo = 1;
}
if (Ilo > 100)
{
Ilo = 100;
}
}
ldLot = GetSizeLot();
ldStop = GetStopLossSell();
ldTake = GetTakeProfitSell();
lsComm = GetCommentForOrder();
//OrderSend(Symbol(),OP_SELL,ldLot,Bid,Slippage,0,0,lsComm,MAGIC,0,clOpenSell);
//OrderSend(Symbol(),OP_SELL,Ilo,Bid,Slippage,ldStop,ldTake,lsComm,MAGIC,0,clOpenSell); OrderSend(Symbol(),OP_SELL,ldLot,Bid,Slippage,ldStop,ldTake,lsComm,MAGIC,0,clOpenSell);
if (UseSound) PlaySound(NameFileSound);
}
string GetCommentForOrder() { return(Name_Expert); }
double GetSizeLot() { return(Lots); }
double GetStopLossBuy() { return (Bid-lStopLoss*Point);}
double GetStopLossSell() { return(Ask+sStopLoss*Point); }
double GetTakeProfitBuy() { return(Ask+lTakeProfit*Point); }
double GetTakeProfitSell() { return(Bid-sTakeProfit*Point); }
|
|
|||
|
More useful pieces of the code
And an other suggestion.
May you create some thread in the end of the main part of our lessons or any time: most useful pieces of the code? Some people started to ask the question about some pieces of the code already Basic questions ... |
|
||||
|
Quote:
I've just created the new thread. Ask! |
|
|||
|
Actually, templates are a part of the MQL4 language. If you properly construct a template file, you can use the New EA Wizard to create an EA using that template. I haven't actually done it yet, but it's been rolling around my head for a while. Maybe it could be another lesson?
-lcg |
|
|||
|
When I asked about template I was thinking that we should have the code of EA and indicator but codes only, with everything and all that we need just delete unnecessary lines from the code. Template is not right word for it may be.
Let's finish the indicator's lessons first. And EA's lessons. And then we may create the templates for ourself. |
|
||||
|
Quote:
![]() Last edited by codersguru : 11-09-2005 at 03:53 AM. |
|
|||
|
Expert templates (templates to create EA) which I have inside my computer:
- Expert Template for MT 4.mq4 (comments in Russian) and Expert Template english.mq4 (with comments in English; - Expert Template add-on.mq4 (almost everything). I am keeping it in samples/ExpertSample folder and open sometimes in MetaEditor of I need to create something. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| EA Templates | xxDavidxSxx | Expert Advisors - Metatrader 4 | 15 | 01-29-2008 12:38 PM |
| Lesson 18 - Working with templates | codersguru | Metatrader 4 mql 4 - Development course | 11 | 09-30-2007 08:57 AM |
| Profitunity Templates....... | Qbert006 | Indicators - Metatrader 4 | 2 | 03-06-2006 07:33 PM |