It will be easy for us to start.
Later on we will have our own practics to create it and our own experience.
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.
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); }