I wrote an EA that does work but not the way I want it to. I would like it to close the buy position when the histogram goes red on the bar close and then the opposite so it would be in the market all the time. I can get it to stay in the market but it just wont sell or buy at the places i want it to. Attached is the indicator. Here is my EA code.
//---- input parametersexternboolAccountIsMini=true;
externboolMoneyManagement=true; //Lots will be determined by TradeSizePercent.externboolBuy=true;
externboolSell=true;
externdoubleLots=0.1;
externdoubleStopLoss=100;
externdoubleTradePercent=10.0;
externdoubleSlippage=3;
externdoubleEquityCutoff=0; //Expert will stop trading if equity level decreases to this level. Leave it as 0 if you want to disable this feature.//internalintbars;
intLastTrend=0;
//+------------------------------------------------------------------+//| expert initialization function |//+------------------------------------------------------------------+intinit(){return(0);
}//+------------------------------------------------------------------+//| expert deinitialization function |//+------------------------------------------------------------------+intdeinit(){return(0);
}//+------------------------------------------------------------------+//| expert start function |//+------------------------------------------------------------------+intstart(){intcnt;
intticket;
intTrend;
doubleb1;
doubleb2;
boolTrendChanged=false;
if(NewBar() == false){return(0);
}b1=iCustom(NULL, 0, "FX_FISH", 0, 1);
b2=iCustom(NULL, 0, "FX_FISH", 1, 1);
if(b1 > 0){Trend=1;
}elseif(b2 < 0){Trend=2;
}else{Trend=0;
}if(Trend == LastTrend){TrendChanged=false;
LastTrend=Trend;
}elseif(LastTrend == 0){TrendChanged=false;
LastTrend=Trend;
}elseif(Trend != LastTrend){TrendChanged=True;
LastTrend=Trend;
}if(TrendChanged == false){return(0);
}cnt=OrdersTotal();
if(Trend == 1)//up trend{if(cnt > 0)//open position{OrderSelect(cnt - 1, SELECT_BY_POS, MODE_TRADES);
if(OrderType() == OP_SELL){OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Green);
Print(b1, ", ", b2);
}else{return(0);
}}if(Buy == true){if(AccountFreeMargin()<(1000*LotSize()) || AccountFreeMargin() < EquityCutoff){Print("Not enough margin for Buy");
return(0);
}Print(b1, ", ", b2);
ticket=OrderSend(Symbol(), OP_BUY, LotSize(), Ask, Slippage, 0, 0, "", 0, Green);
if(ticket<0){Print("OrderSend failed with error #", GetLastError());
return(0);
}}}elseif(Trend == 2)//down trend{if(cnt > 0)//open position{OrderSelect(cnt - 1, SELECT_BY_POS, MODE_TRADES);
if(OrderType() == OP_BUY){OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Red);
Print(b1, ", ", b2);
}else{return(0);
}}if(Sell == true){if(AccountFreeMargin()<(1000*LotSize()) || AccountFreeMargin() < EquityCutoff){Print("Not enough margin for Sell");
return(0);
}Print(b1, ", ", b2);
ticket=OrderSend(Symbol(), OP_SELL, LotSize(), Bid, Slippage, 0, 0, "", 0, Red);
if(ticket<0){Print("OrderSend failed with error #", GetLastError());
return(0);
}}}return(0);
}//+------------------------------------------------------------------+//| the sign of the new bar |//+------------------------------------------------------------------+boolNewBar(){if(bars!=Bars){bars=Bars;
return(true);
}return(false);
}//+------------------------------------------------------------------+//| calculates lot size |//+------------------------------------------------------------------+doubleLotSize(){if(MoneyManagement){Lots = NormalizeDouble(MathFloor(AccountFreeMargin()*TradePercent/10000)/10,1);
if(AccountIsMini){Lots = MathFloor(Lots)/10;
if(Lots<0.1){Lots=0.1;
}}return(Lots);
}else{Lots = Lots;
if(AccountIsMini){if(Lots >= 1.0){Lots = Lots/10;
}if(Lots < 0.1){Lots = 0.1;
}}return(Lots);
}}//+------------------------------------------------------------------+
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
I wrote an EA that does work but not the way I want it to. I would like it to close the buy position when the histogram goes red on the bar close and then the opposite so it would be in the market all the time. I can get it to stay in the market but it just wont sell or buy at the places i want it to. Attached is the indicator. Here is my EA code.