here is the code, don't worry about the trading technique, the matter is the code of course...
//+------------------------------------------------------------------+ //| MACD Sample.mq4 | //| CopyLeft Kfree | //| http://localhost/ | //+------------------------------------------------------------------+ extern double TakeProfit = 30; extern double Lots = 0.1; extern double TrailingStop = 30; //extern double StopLoss = 30; extern int Period_MA = 15; // Calculated MA period extern int Period_MA2 = 30; extern int Period_MA3 = 156; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int start() { int cnt, ticket, total; double MA; // MA value on 0 bar double MA2; double MA3; // initial data checks // it is important to make sure that the expert works with a normal // chart and the user did not make any mistakes setting external // variables (Lots, StopLoss, TakeProfit, // TrailingStop) in our case, we check TakeProfit // on a chart of less than 100 bars if(Bars<100) { Print("bars less than 100"); return(0); } if(TakeProfit<10) { Print("TakeProfit less than 10"); return(0); // check TakeProfit } // to simplify the coding and speed up access // data are put into internal variables MA=iMA(NULL,0,15,0,MODE_LWMA,PRICE_CLOSE,0); MA2=iMA(NULL,0,30,0,MODE_LWMA,PRICE_CLOSE,1); MA3=iMA(NULL,0,156,0,MODE_LWMA,PRICE_CLOSE,2); total=OrdersTotal(); if(total<1) { // no opened orders identified if(AccountFreeMargin()<(1000*Lots)) { Print("We have no money. Free Margin = ", AccountFreeMargin()); return(0); } // check for long position (BUY) possibility if (MA-MA2 > 0 && MA2-MA3 > 0 ) { ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"macd sample",16384,0,Green); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice()); } else Print("Error opening BUY order : ",GetLastError()); return(0); } // check for short position (SELL) possibility if (MA2-MA < 0 && MA3-MA2 < 0 ) { ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"macd sample",16384,0,Red); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice()); } else Print("Error opening SELL order : ",GetLastError()); return(0); } return(0); } // EXIT RULES ----------------------------------------------------- for(cnt=0;cnt<total;cnt++) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if(OrderType()<=OP_SELL && // check for opened position OrderSymbol()==Symbol()) // check for symbol { if(OrderType()==OP_BUY) // long position is opened { // should it be closed? if (ticket > 0 ) { OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position return(0); // exit } // check for trailing stop if(TrailingStop>0) { if(Bid-OrderOpenPrice()>Point*TrailingStop) { if(OrderStopLoss()<Bid-Point*TrailingStop) { OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green); return(0); } } } } else // go to short position { // should it be closed? if (ticket > 0) { OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position return(0); // exit } // check for trailing stop if(TrailingStop>0) { if((OrderOpenPrice()-Ask)>(Point*TrailingStop)) { if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0)) { OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red); return(0); } } } } } } return(0); } // the end.
I repeat i'm not coder, i guess this code will make you laughing out loud
at the end, i would like an EA who check if one order is opened before open a new order, and if one order is opened then close it first and open the new one.
I repeat i'm not coder, i guess this code will make you laughing out loud
For a non-coder, that is very disciplined formatting/indentation. That is a good start. Rather than laughing, I'm impressed ;)
here is the code, don't worry about the trading technique, the matter is the code of course...
I repeat i'm not coder, i guess this code will make you laughing out loud
at the end, i would like an EA who check if one order is opened before open a new order, and if one order is opened then close it first and open the new one.
I'd look at two things... first it's taking profit at 3, when it's set to 30. That's going to be because you are using a broker with 5 digits and the EA was only designed to handle 4 digits. Lots of people have posted fixes for that scenario... the quick/cheating way would be to change your trailingStop & take profit to 300 & 300, which will really be 30 & 30 on a 5 digit broker. Look up the correct way for a long term solution, though.
The second is that it's not setting a stopLoss when it opens the order. And, has some interesting "exit" logic. Unless I'm reading that incorrectly, the first thing it looks for is a ticket, and automatically closes it, which wouldn't make much sense, but luckily it will never hit that block anyway. The ticket variable is only created when it opens an order, and then it exits that tick (return 0 halts the logic of that tick and you're waiting for the next tick to start again) so ticket will never exist when it reaches the exit section on the following ticks. Then, the trailing stop is only set if you are in profit, so if your position moves at a loss, you're running around with no stop loss and your one loss at the end kills all your small profits. If you put a stoploss in when you open it, you'll be a lot closer to your goal, and then you can see if that trailingstop logic works.
I'd look at two things... first it's taking profit at 3, when it's set to 30. That's going to be because you are using a broker with 5 digits and the EA was only designed to handle 4 digits. Lots of people have posted fixes for that scenario... the quick/cheating way would be to change your trailingStop & take profit to 300 & 300, which will really be 30 & 30 on a 5 digit broker. Look up the correct way for a long term solution, though.
The second is that it's not setting a stopLoss when it opens the order. And, has some interesting "exit" logic. Unless I'm reading that incorrectly, the first thing it looks for is a ticket, and automatically closes it, which wouldn't make much sense, but luckily it will never hit that block anyway. The ticket variable is only created when it opens an order, and then it exits that tick (return 0 halts the logic of that tick and you're waiting for the next tick to start again) so ticket will never exist when it reaches the exit section on the following ticks. Then, the trailing stop is only set if you are in profit, so if your position moves at a loss, you're running around with no stop loss and your one loss at the end kills all your small profits. If you put a stoploss in when you open it, you'll be a lot closer to your goal, and then you can see if that trailingstop logic works.
I didn't test it but I've been there many times. What is pissing me off with this and similar cases is that code with errors gives us better results that it will probably give after fixing them.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
I'm not coder and i tried to code an indicator with success but i would like to use it as EA.
The wining ratio is over 95 % (on the report...)
trailing stop 30
take profit 30
and i'm loosing, please help me to figure out this:
i can post the code if you want, it is based on the macd sample but with MA instead.
Thanx