just posting a simple EA in Hope for some Code Review, Tips and Advices ;-)
Feel free to test or use parts of the Code, just some playing around for learning purposes anyways
//+------------------------------------------------------------------+//| minion1.mq4 |//| This Minion trades Sell and Buy Positions based on RSI |//| Feel Free to test, use and develop |//+------------------------------------------------------------------+#property copyright"GNU/GPL"#property link"https://www.mql5.com/en/users/digitalalchemist"#property version"1.00"#property strictinputint magicno=331177; //no explanation inputint RSIPeriod=14; //Period of RSI indicatorinputint RSILow=30; //trigger for opening sell Positionsinputint RSIHigh=70; // trigger for opening buy Positionsinputint maxOpenBuyOrders=1; // specifies open buy orders at the same timeinputint maxOpenSellOrders=1; //specifies open sell orders at the same timeinputdouble lotsize=0.1; // size of opened Positions//all following values are factors of ASK/BID or OrderStopLos(Adjust values)inputdouble sellSL=1.005; //initial SL when opening a sell Positioninputdouble sellTP=0.997; //TP for a sell Positioninputdouble buySL=0.9975; //initial SL when opening a buy Positioninputdouble buyTP=1.01; //TP for a buy Positioninputdouble adjustSellSL=0.9995; //adjusts sl when the OrderProfit turned positiveinputdouble adjustBuySL=1.0005; //adjusts sl when the OrderProfit turned positiveintOnInit(){
Print("Minion 1 has joined the battlefield on "+Symbol());
return(INIT_SUCCEEDED);
}
voidOnDeinit(constint reason){
Print("Minion 1 has left the battlefield on "+Symbol());
}
voidOnTick(){
adjustSL();
int signal=calculateRSI();
if(signal==0 && countOpenSellOrders()<maxOpenSellOrders){
executeShort();
}
elseif(signal==2 && countOpenBuyOrders()<maxOpenBuyOrders){
executeLong();
}
}
//+------------------------------------------------------------------+//returns an int 0 means sell 1 means do nothing 2 means buyint calculateRSI(){
int signal=1;
double rsi0=iRSI(NULL,0,RSIPeriod,PRICE_TYPICAL,0);
double rsi3=iRSI(NULL,0,RSIPeriod,PRICE_TYPICAL,3);
double rsi6=iRSI(NULL,0,RSIPeriod,PRICE_TYPICAL,6);
double rsi9=iRSI(NULL,0,RSIPeriod,PRICE_TYPICAL,9);
if(rsi0>rsi9+7 && rsi0>rsi6+5 && rsi0>rsi3+3 && rsi0>RSIHigh){
signal=2;
}
elseif(rsi0<rsi9-7 && rsi0<rsi6-5 && rsi0<rsi3-3 && rsi0<RSILow){
signal=0;
}
return signal;
}
//opens a short positionint executeShort(){
int ticket=OrderSend(Symbol(),OP_SELL,lotsize,Bid,9999,Bid*sellSL,Bid*sellTP,"execute sell order",magicno,0,clrRed);
if(ticket<0){
Print("Sell Order Exception: "+GetLastError());
}
else{
Print("Sell Order opened at :"+Bid);
}
return ticket;
}
//opens a long positionint executeLong(){
int ticket=OrderSend(Symbol(),OP_BUY,lotsize,Ask,9999,Ask*buySL,Ask*buyTP,"execute buy order",magicno,0,clrGreen);
if(ticket<0){
Print("Sell Order Exception: "+GetLastError());
}
else{
Print("Sell Order opened at :"+Bid);
}
return ticket;
}
//counts the actual opened buy positionsint countOpenBuyOrders(){
int counter=0;
for(int i=0;i<OrdersTotal();i++){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if(OrderType()==OP_BUY && OrderMagicNumber()==magicno){
counter++;
}
}
}
return counter;
}
//counts the actual opened sell positionsint countOpenSellOrders(){
int counter=0;
for(int i=0;i<OrdersTotal();i++){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if(OrderType()==OP_SELL && OrderMagicNumber()==magicno){
counter++;
}
}
}
return counter;
}
//iterates through open Position and adjust the sl every tickvoid adjustSL(){
for(int i=0;i<OrdersTotal();i++){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if(OrderType()==OP_SELL && OrderMagicNumber()==magicno){
if(OrderProfit()>15 && OrderStopLoss()>OrderOpenPrice()){
bool modorder=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,clrAqua);
if(!modorder){
Print("OrderModify Exception: "+GetLastError());
}
else{
Print("Order Modified successfully");
}
}
elseif(OrderStopLoss()<=OrderOpenPrice()){
bool modorder=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()*adjustSellSL,OrderTakeProfit(),0,clrAqua);
if(!modorder){
Print("OrderModify Exception: "+GetLastError());
}
else{
Print("Order Modified successfully");
}
}
}
elseif(OrderType()==OP_BUY && OrderMagicNumber()==magicno){
if(OrderProfit()>15 && OrderStopLoss()<OrderOpenPrice()){
bool modorder=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,clrAqua);
if(!modorder){
Print("OrderModify Exception: "+GetLastError());
}
else{
Print("Order Modified successfully");
}
}
elseif(OrderStopLoss()>=OrderOpenPrice()){
bool modorder=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()*adjustBuySL,OrderTakeProfit(),0,clrAqua);
if(!modorder){
Print("OrderModify Exception: "+GetLastError());
}
else{
Print("Order Modified successfully");
}
}
}
}
}
}
Hey there Folks,
just posting a simple EA in Hope for some Code Review, Tips and Advices ;-)
Feel free to test or use parts of the Code, just some playing around for learning purposes anyways