I have taken 3 udemy courses and I am still having a problem with coding the following:
Maybe someone can recommend a good course ?
Here is what I want to do:
if 0 bar or tick is above ma by (X) number of points and bar number 2 is below ma by (X) number of points then
()send order
pause (x number of min)
repeat.
here is the code i got
//+------------------------------------------------------------------+
//| freshsystem.mq4 |
//| freshpl |
//| |
//+------------------------------------------------------------------+
#property copyright "freshpl"
#property link "freshpl@gmail.com"
#property version "1.00"
#property strict
/*
*/
extern int StartHour = 9;
extern int MinPipLimit = 0;
extern int TakeProfit = 40;
extern int StopLoss = 40;
extern double Lots = 1;
extern bool UseMAFilter = false;
extern int MA_Period = 100;
extern int Magic = 59789101;
extern bool ECNExecution = false;
extern bool UseTrailingSL = false;
extern int TrailValue = 40;
extern bool AutoAdjustTo5Digits = false;
double MyPoint;
void OnInit()
{
SetMyPoint();
}
void OnTick()
{
static bool IsFirstTick = true;
static int ticket = 0;
string comment;
double ma = iMA(Symbol(), Period(), MA_Period, 0, 0, 0, 1);
if(Hour() == StartHour)
{
comment = GetDateAndTime();
if(IsFirstTick == true)
{
IsFirstTick = false;
//FindTicket makes sure that the EA will pick up its orders
//even if it is relaunched
ticket = FindTicket(Magic);
bool res;
res = OrderSelect(ticket, SELECT_BY_TICKET);
if(res == true)
{
if(OrderCloseTime() == 0)
{
bool res2;
res2 = OrderClose(ticket, Lots, OrderClosePrice(), 10);
if(res2 == false)
{
Alert("Error Closing Order #", ticket);
}
}
}
if(Open[0] < Open[StartHour] - MinPipLimit*MyPoint) //v3.0
{
//check ma
if(Close[1] < ma || UseMAFilter == false) //v3.0
{
ticket = MarketOrderSend(Symbol(), OP_BUY, Lots, ND(Ask), 10*int(MyPoint/Point()), ND(Bid-StopLoss*MyPoint), ND(Bid+TakeProfit*MyPoint), "Set by SimpleSystem", Magic);
if(ticket < 0)
{
comment = comment + " " + "Error Sending BUY Order";
}
else
{
comment = comment + " " + "BUY Order Executed Succesfuly";
}
}
else
{
comment = comment + " " + "Reason Order Not Opened: MA Filter Not Passed";
}
}
else if(Open[0] > Open[StartHour] + MinPipLimit*MyPoint) //v3.0
{
//check ma
if(Close[1] > ma || UseMAFilter == false) //v3.0
{
ticket = MarketOrderSend(Symbol(), OP_SELL, Lots, ND(Bid), 10*int(MyPoint/Point()), ND(Ask+StopLoss*MyPoint), ND(Ask-TakeProfit*MyPoint), "Set by SimpleSystem", Magic);
if(ticket < 0)
{
comment = comment + " " + "Error Sending SELL Order";
}
else
{
comment = comment + " " + "SELL Order Executed Succesfuly";
}
}
else
{
comment = comment + " " + "Reason Order Not Opened: MA Filter Not Passed";
}
}
else
{
comment = comment + " " + "Reason Order Not Opened: MinPipLimit Filter Not Passed";
}
Comment(comment);
Print(comment);
}
}
else
{
IsFirstTick = true;
if(UseTrailingSL == true) //execute trailing stop - this has to be done on every tick
{
//FindTicket makes sure that the EA will pick up its orders
//even if it is relaunched
int ticket_trailing = FindTicket(Magic);
if(ticket_trailing > 0)
{
TrailingStop(ticket_trailing);
}
}
}
}
/*
-------------------------------
Auxiliary Functions
-------------------------------
*/
int FindTicket(int M)
{
int ret = 0;
for(int i = OrdersTotal()-1; i >= 0; i--)
{
bool res;
res = OrderSelect(i, SELECT_BY_POS);
if(res == true)
{
if(OrderMagicNumber() == M)
{
ret = OrderTicket();
break;
}
}
}
return(ret);
}
string GetDateAndTime()
{
return( string(Year())+"-"+StringFormat("%02d",Month())+"-"+StringFormat("%02d",Day())+" "+StringFormat("%02d",Hour())+":"+StringFormat("%02d",Minute()));
}
int MarketOrderSend(string symbol, int cmd, double volume, double price, int slippage, double stoploss, double takeprofit, string comment, int magic)
{
int ticket;
if(ECNExecution == false)
{
ticket = OrderSend(symbol, cmd, volume, price, slippage, stoploss, takeprofit, comment, magic);
if(ticket <= 0) Alert("OrderSend Error: ", GetLastError());
return(ticket);
}
else
{
ticket = OrderSend(symbol, cmd, volume, price, slippage, 0, 0, comment, magic);
if(ticket <= 0) Alert("OrderSend Error: ", GetLastError());
else
{
bool res = OrderModify(ticket, 0, stoploss, takeprofit, 0);
if(!res) { Alert("OrderModify Error: ", GetLastError());
Alert("!!! ORDER #", ticket, " HAS NO STOPLOSS AND TAKEPROFIT --- ORDER MODIFY ERROR: GetLastError()");}
}
return(ticket);
}
}
void TrailingStop(int ticket)
{
bool res;
res = OrderSelect(ticket, SELECT_BY_TICKET);
if(res == true)
{
if(OrderType() == OP_BUY)
{
if(Bid - OrderStopLoss() > TrailValue*MyPoint) //adjust stop loss if it is too far
{
res = OrderModify(OrderTicket(), 0, ND(Bid - TrailValue*MyPoint), OrderTakeProfit(), 0);
if(!res)
{
Alert("TrailingStop OrderModify Error: ", GetLastError());
}
}
}
if(OrderType() == OP_SELL)
{
if(OrderStopLoss() - Ask > TrailValue*MyPoint) //adjust stop loss if it is too far
{
res = OrderModify(OrderTicket(), 0, ND(Ask + TrailValue*MyPoint), OrderTakeProfit(), 0);
if(!res)
{
Alert("TrailingStop OrderModify Error: ", GetLastError());
}
}
}
}
}
void SetMyPoint()
{
MyPoint = Point();
if(AutoAdjustTo5Digits == true && (Digits() == 3 || Digits() == 5))
{
Alert("Digits=", Digits(), " Broker quotes given in 5-digit mode. Old values of SL, TP and slippage will be multiplied by 10");
MyPoint = Point()*10;
}
}
double ND(double val)
{
return(NormalizeDouble(val, Digits()));
- www.forexboat.com
I have taken 3 udemy courses and I am still having a problem with coding the following:
Maybe someone can recommend a good course ?
Here is what I want to do:
if 0 bar or tick is above ma by (X) number of points and bar number 2 is below ma by (X) number of points then
()send order
pause (x number of min)
repeat.
here is the code i got
...Forum on trading, automated trading systems and testing trading strategies
How to Start with Metatrader 5
Simon Gniadkowski, 2013.07.24 10:18
How to post code on this forum . . .
I have taken 3 udemy courses and I am still having a problem with coding the following:
Maybe someone can recommend a good course ?
//| freshsystem.mq4 |
//| freshpl |
//| |
//+------------------------------------------------------------------+
#property copyright "freshpl"
#property link "freshpl@gmail.com"
#property version "1.00"
#property strict
/*
*/
extern int StartHour = 9;
extern int MinPipLimit = 0;
extern int TakeProfit = 40;
extern int StopLoss = 40;
extern double Lots = 1;
extern bool UseMAFilter = false;
extern int MA_Period = 100;
extern int Magic = 59789101;
extern bool ECNExecution = false;
extern bool UseTrailingSL = false;
extern int TrailValue = 40;
extern bool AutoAdjustTo5Digits = false;
double MyPoint;
void OnInit()
{
SetMyPoint();
}
void OnTick()
{
static bool IsFirstTick = true;
static int ticket = 0;
string comment;
double ma = iMA(Symbol(), Period(), MA_Period, 0, 0, 0, 1);
if(Hour() == StartHour)
{
comment = GetDateAndTime();
if(IsFirstTick == true)
{
IsFirstTick = false;
//FindTicket makes sure that the EA will pick up its orders
//even if it is relaunched
ticket = FindTicket(Magic);
bool res;
res = OrderSelect(ticket, SELECT_BY_TICKET);
if(res == true)
{
if(OrderCloseTime() == 0)
{
bool res2;
res2 = OrderClose(ticket, Lots, OrderClosePrice(), 10);
if(res2 == false)
{
Alert("Error Closing Order #", ticket);
}
}
}
if(Open[0] < Open[StartHour] - MinPipLimit*MyPoint) //v3.0
{
//check ma
if(Close[1] < ma || UseMAFilter == false) //v3.0
{
ticket = MarketOrderSend(Symbol(), OP_BUY, Lots, ND(Ask), 10*int(MyPoint/Point()), ND(Bid-StopLoss*MyPoint), ND(Bid+TakeProfit*MyPoint), "Set by SimpleSystem", Magic);
if(ticket < 0)
{
comment = comment + " " + "Error Sending BUY Order";
}
else
{
comment = comment + " " + "BUY Order Executed Succesfuly";
}
}
else
{
comment = comment + " " + "Reason Order Not Opened: MA Filter Not Passed";
}
}
else if(Open[0] > Open[StartHour] + MinPipLimit*MyPoint) //v3.0
{
//check ma
if(Close[1] > ma || UseMAFilter == false) //v3.0
{
ticket = MarketOrderSend(Symbol(), OP_SELL, Lots, ND(Bid), 10*int(MyPoint/Point()), ND(Ask+StopLoss*MyPoint), ND(Ask-TakeProfit*MyPoint), "Set by SimpleSystem", Magic);
if(ticket < 0)
{
comment = comment + " " + "Error Sending SELL Order";
}
else
{
comment = comment + " " + "SELL Order Executed Succesfuly";
}
}
else
{
comment = comment + " " + "Reason Order Not Opened: MA Filter Not Passed";
}
}
else
{
comment = comment + " " + "Reason Order Not Opened: MinPipLimit Filter Not Passed";
}
Comment(comment);
Print(comment);
}
}
else
{
IsFirstTick = true;
if(UseTrailingSL == true) //execute trailing stop - this has to be done on every tick
{
//FindTicket makes sure that the EA will pick up its orders
//even if it is relaunched
int ticket_trailing = FindTicket(Magic);
if(ticket_trailing > 0)
{
TrailingStop(ticket_trailing);
}
}
}
}
/*
-------------------------------
Auxiliary Functions
-------------------------------
*/
int FindTicket(int M)
{
int ret = 0;
for(int i = OrdersTotal()-1; i >= 0; i--)
{
bool res;
res = OrderSelect(i, SELECT_BY_POS);
if(res == true)
{
if(OrderMagicNumber() == M)
{
ret = OrderTicket();
break;
}
}
}
return(ret);
}
string GetDateAndTime()
{
return( string(Year())+"-"+StringFormat("%02d",Month())+"-"+StringFormat("%02d",Day())+" "+StringFormat("%02d",Hour())+":"+StringFormat("%02d",Minute()));
}
int MarketOrderSend(string symbol, int cmd, double volume, double price, int slippage, double stoploss, double takeprofit, string comment, int magic)
{
int ticket;
if(ECNExecution == false)
{
ticket = OrderSend(symbol, cmd, volume, price, slippage, stoploss, takeprofit, comment, magic);
if(ticket <= 0) Alert("OrderSend Error: ", GetLastError());
return(ticket);
}
else
{
ticket = OrderSend(symbol, cmd, volume, price, slippage, 0, 0, comment, magic);
if(ticket <= 0) Alert("OrderSend Error: ", GetLastError());
else
{
bool res = OrderModify(ticket, 0, stoploss, takeprofit, 0);
if(!res) { Alert("OrderModify Error: ", GetLastError());
Alert("!!! ORDER #", ticket, " HAS NO STOPLOSS AND TAKEPROFIT --- ORDER MODIFY ERROR: GetLastError()");}
}
return(ticket);
}
}
void TrailingStop(int ticket)
{
bool res;
res = OrderSelect(ticket, SELECT_BY_TICKET);
if(res == true)
{
if(OrderType() == OP_BUY)
{
if(Bid - OrderStopLoss() > TrailValue*MyPoint) //adjust stop loss if it is too far
{
res = OrderModify(OrderTicket(), 0, ND(Bid - TrailValue*MyPoint), OrderTakeProfit(), 0);
if(!res)
{
Alert("TrailingStop OrderModify Error: ", GetLastError());
}
}
}
if(OrderType() == OP_SELL)
{
if(OrderStopLoss() - Ask > TrailValue*MyPoint) //adjust stop loss if it is too far
{
res = OrderModify(OrderTicket(), 0, ND(Ask + TrailValue*MyPoint), OrderTakeProfit(), 0);
if(!res)
{
Alert("TrailingStop OrderModify Error: ", GetLastError());
}
}
}
}
}
void SetMyPoint()
{
MyPoint = Point();
if(AutoAdjustTo5Digits == true && (Digits() == 3 || Digits() == 5))
{
Alert("Digits=", Digits(), " Broker quotes given in 5-digit mode. Old values of SL, TP and slippage will be multiplied by 10");
MyPoint = Point()*10;
}
}
double ND(double val)
{
return(NormalizeDouble(val, Digits()));
Here is what I want to do:
If current bar is above ma by 4 points
and
bar 2 was below ma by 4 points then send order.
Hi, I have 2 issues to raise here,
1) .After I copy my custom indicators to MT4 folder and paste on the indicator folder, restarted my MT4, there is still no custom indicator folder shown on the navigator panel.
2) And my MT4 doesn't seem to be running on live demo account even after I have logged in.
Please advice.
Hi, I have 2 issues to raise here,
1) .After I copy my custom indicators to MT4 folder and paste on the indicator folder, restarted my MT4, there is still no custom indicator folder shown on the navigator panel.
2) And my MT4 doesn't seem to be running on live demo account even after I have logged in.
Please advice.
1) There is no "custom indicator folder", just "indicators"
2) It is the weekend, so no new ticks
Hello again!
I got my EA its perfect (for me anyways)
Can someone help me out I can't add an alert (alert for everytime EA opens or closes a position)
Source attached.
//| Three moving averages EA |
//| Copyright © 2008, TradingSytemForex |
//| http://www.tradingsystemforex.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, TradingSytemForex"
#property link "http://www.tradingsystemforex.com"
#define OrSt "Three moving averages EA"
extern string IS="---------------- Indicator Settings";
extern double Short_Period=5;
extern double Middle_Period=15;
extern double Long_Period=30;
extern double Mode_MA=0; //0=simple, 1=exponential, 2=smoothed, 3=linea weighted
extern string LM="---------------- Lot Management";
extern double Lots=0.1;
extern bool MM=false; //money management
extern double Risk=10; //risk in percentage
extern string TSTB="---------------- TP SL TS BE";
bool RealSL_Enabled=false;
int RealSL=35; //stop loss under 15 pîps
bool RealTP_Enabled=false;
int RealTP=10; //take profit under 10 pîps
extern int SL=0; //stop loss
extern int TP=0; //take profit
extern int TS=0; //trailing stop
extern int TS_Step=20; //trailing stop step
extern int BE=0; //breakeven
extern string EXT="---------------- Extras";
extern bool Reverse=false;
extern bool Add_Positions=true; //positions cumulated
extern int MaxOrders=100; //maximum number of orders
extern int Magic=0;
int Slip=3;static int TL=0;double MML=0;
// expert start function
int start(){int j=0,limit=1;double BV=0,SV=0;BV=0;SV=0;double SMA1a,SMA1b,SMA2a,SMA2b,SMA3a,SMA3b;
if(CntO(OP_BUY,Magic)>0) TL=1;if(CntO(OP_SELL,Magic)>0) TL=-1;for(int i=1;i<=limit;i++){
SMA1a=iMA(Symbol(),0,Short_Period,0,Mode_MA,PRICE_CLOSE,i+1);
SMA1b=iMA(Symbol(),0,Short_Period,0,Mode_MA,PRICE_CLOSE,i);
SMA2a=iMA(Symbol(),0,Middle_Period,0,Mode_MA,PRICE_CLOSE,i+1);
SMA2b=iMA(Symbol(),0,Middle_Period,0,Mode_MA,PRICE_CLOSE,i);
SMA3a=iMA(Symbol(),0,Long_Period,0,Mode_MA,PRICE_CLOSE,i+1);
SMA3b=iMA(Symbol(),0,Long_Period,0,Mode_MA,PRICE_CLOSE,i);
if((SMA1a<SMA3a && SMA1b>SMA3b)||(SMA1a<SMA2a && SMA1b>SMA2b && SMA2b>SMA3b)){if(Reverse) SV=1; else BV=1; break;}
if((SMA1a>SMA3a && SMA1b<SMA3b)||(SMA1a>SMA2a && SMA1b<SMA2b && SMA2b<SMA3b)){if(Reverse) BV=1; else SV=1; break;}}
// expert money management
if(MM){if(Risk<0.1 || Risk>100) {Comment("Invalid Risk Value."); return(0);}
else {MML=MathFloor((AccountFreeMargin() *AccountLeverage()*Risk*Point*100)/(Ask*MarketInfo(Symbol(),MODE_LOTSIZE)*MarketInfo(Symbol(),MODE_MINLOT )))*MarketInfo(Symbol(),MODE_MINLOT );}}
if(MM==false){MML=Lots;}
// expert init positions
int cnt=0,OP=0,OS=0,OB=0,CS=0,CB=0;OP=0;for(cnt=0; cnt<OrdersTotal();cnt++) {OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if((OrderType()==OP_SELL || OrderType()==OP_BUY) && OrderSymbol()==Symbol() && ((OrderMagicNumber()==Magic) || Magic==0)) OP=OP+1;}
if(OP>=1){OS=0; OB=0;}OB=0;OS=0;CB=0;CS=0;
// expert conditions to open position
if(SV>0){OS=1;OB=0;}if(BV>0){OB=1;OS=0;}
// expert conditions to close position
if((SV>0) || (RealSL_Enabled && (OrderOpenPrice()-Bid)/Point>=RealSL)||(RealTP_Enabled && (Ask-OrderOpenPrice())/Point>=RealTP)){CB=1;}
if((BV>0) || (RealSL_Enabled && (Ask-OrderOpenPrice())/Point>=RealSL)||(RealTP_Enabled && (OrderOpenPrice()-Bid)/Point>=RealTP)){CS=1;}
for(cnt=0;cnt<OrdersTotal();cnt++){OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && ((OrderMagicNumber()==Magic) || Magic==0)){if (CB==1){OrderClose(OrderTicket(),OrderLots(),Bid,Slip,Red); return(0);}}
if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && ((OrderMagicNumber()==Magic) || Magic==0)){
if(CS==1){OrderClose(OrderTicket(),OrderLots(),Ask,Slip,Red);return(0);}}}double SLI=0,TPI=0;int TK=0;
// expert open position value
if((AddP() && Add_Positions && OP<=MaxOrders) || (OP==0 && !Add_Positions)) {
if(OS==1){if (TP==0) TPI=0; else TPI=Bid-TP*Point;if (SL==0) SLI=0; else SLI=Bid+SL*Point;TK=OrderSend(Symbol(),OP_SELL,MML,Bid,Slip,SLI,TPI,OrSt,Magic,0,Red);OS=0;return(0);}
if(OB==1){if(TP==0) TPI=0; else TPI=Ask+TP*Point;if(SL==0) SLI=0; else SLI=Ask-SL*Point;TK=OrderSend(Symbol(),OP_BUY,MML,Ask,Slip,SLI,TPI,OrSt,Magic,0,Lime);OB=0; return(0);}}
for(j=0;j<OrdersTotal();j++){if(OrderSelect(j,SELECT_BY_POS, MODE_TRADES)){if (OrderSymbol()==Symbol() && ((OrderMagicNumber()==Magic) || Magic==0)) {TrP();}}}return(0);}
// expert number of orders
int CntO(int Type,int Magic){int _CntO;_CntO=0;
for(int j=0;j<OrdersTotal();j++){OrderSelect(j, SELECT_BY_POS, MODE_TRADES);if(OrderSymbol()==Symbol()) {if((OrderType()==Type && (OrderMagicNumber()==Magic) || Magic==0)) _CntO++;}}return(_CntO);}
// expert trailing stop
void TrP(){double pb,pa,pp;pp=MarketInfo(OrderSymbol(),MODE_POINT);if (OrderType()==OP_BUY){pb=MarketInfo(OrderSymbol(),MODE_BID);
//expert breakeven
if(BE>0){if((pb-OrderOpenPrice())>BE*pp){if((OrderStopLoss()-OrderOpenPrice())<0){ModSL(OrderOpenPrice()+0*pp);}}}
if(TS>0){if((pb-OrderOpenPrice())>TS*pp){if(OrderStopLoss()<pb-(TS+TS_Step-1)*pp){ModSL(pb-TS*pp);return;}}}}
if(OrderType()==OP_SELL){pa=MarketInfo(OrderSymbol(),MODE_ASK);if(BE>0){if((OrderOpenPrice()-pa)>BE*pp){if((OrderOpenPrice()-OrderStopLoss())<0){ModSL(OrderOpenPrice()-0*pp);}}}
if (TS>0){if (OrderOpenPrice()-pa>TS*pp){if (OrderStopLoss()>pa+(TS+TS_Step-1)*pp || OrderStopLoss()==0){ModSL(pa+TS*pp);return;}}}}}
//expert stoploss
void ModSL(double ldSL){bool fm;fm=OrderModify(OrderTicket(),OrderOpenPrice(),ldSL,OrderTakeProfit(),0,CLR_NONE);}
//expert add positions function
bool AddP(){int _num=0; int _ot=0;
for (int j=0;j<OrdersTotal();j++){if(OrderSelect(j,SELECT_BY_POS)==true && OrderSymbol()==Symbol() && OrderType()<3 && ((OrderMagicNumber()==Magic) || Magic==0)) {
_num++;if(OrderOpenTime()>_ot) _ot=OrderOpenTime();}}
if(_num==0) return(true);if(_num>0 && ((Time[0]-_ot))>0) return(true);else return(false);}
if (TK>0)
{
//Code to send alerts
}
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I need help with adding some code
if(Close[1] < ma)
How would I add that the ask is higher by (x) number of points above ma.