Half Trend EA
help me for buy and sell condition based on half trend indicator
canbuy and cansell bool condition send multiple orders
you need to try buff nr. 2 and 3 getting buy and sell signals.
#define _htCall(_buff,_ind) iCustom(_Symbol,_Period,"half-trend nrp",inpHTPeriod,_buff,_ind) double _ht_2 = _htCall(2,inpHtBarTotest); double _ht_2_1 = _htCall(2,inpHtBarTotest+1); double _ht_3 = _htCall(3,inpHtBarTotest); double _ht_3_1 = _htCall(3,inpHtBarTotest+1); bool _canBuy = (_ht_2_1=_ht_3_1 && _ht_3==EMPTY_VALUE); //bool _canSell = (_htCurr!=_htPrev && _htCurr==-1); if(_canBuy) { // BuyOrder(); int buyticket = OrderSend(_Symbol,OP_BUY,0.01,Ask,10,0,0,"TEST",110,0,clrWhite); }also send multiple orders

Bar 2, Bar 1 and Bar 0
#define _htCall(_buff,_ind) iCustom(_Symbol,_Period,"half-trend nrp",inpHTPeriod,_buff,_ind) double _ht_2 = _htCall(5,inpHtBarTotest); double _ht_2_1 = _htCall(5,inpHtBarTotest+1); double _ht_3 = _htCall(6,inpHtBarTotest); double _ht_3_1 = _htCall(6,inpHtBarTotest+1); bool _canBuy = (_ht_2_1==EMPTY_VALUE && _ht_2!=EMPTY_VALUE);
not tested but you should try this way...
//+------------------------------------------------------------------+ //| Test_Own.mq4 | //| Copyright 2021, RNHPL | //| XXXXXXXXXXX | //+------------------------------------------------------------------+ #property copyright "Copyright 2021, RNHPL" #property link "" #property version "1.00" #property strict enum enTestedBar { test_opened=0, // Test still opened bar test_closed=1 // Test first closed bar }; input int inpHTPeriod = 2; // Fast EMA period input enTestedBar inpHtBarTotest = test_opened; // Bar to test input bool alertsOn = false; input bool alertsOnCurrent = false; input bool alertsMessage = true; input bool alertsSound = false; input bool alertsEmail = false; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- newBar(); SearchSignal(); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ bool newBar() { static datetime TimeBar=0; bool flag=false; if(TimeBar!=Time[0]) { TimeBar=Time[0]; flag=true; } // return true if you are in new bar. return (flag); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void SearchSignal() { #define _htCall(_buff,_ind) iCustom(_Symbol,_Period,"HalfTrend 1 & alerts",inpHTPeriod,_buff,_ind) double _ht_0_current = _htCall(0,inpHtBarTotest); double _ht_0_previous = _htCall(0,inpHtBarTotest+1); double _ht_1_current = _htCall(1,inpHtBarTotest); double _ht_1_previous = _htCall(1,inpHtBarTotest+1); bool _can_buy = (_ht_0_previous==_ht_1_previous && _ht_0_current!=_ht_1_current); if(_can_buy) { int Order = OrderSend(_Symbol,OP_BUY,1,Ask,100,0,0,"BUY",0,0,clrWhite); } } //+------------------------------------------------------------------+
Can help me to order once per bar?
hi
put this command before your buy order line
if(iVolume(Symbol(),0,0)<=1 && Position_Chek()==0)
.
.
/------------------------------------------------------/
int Position_Chek()
{
int num=0;
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderMagicNumber()==1010 || OrderMagicNumber()==2020)
num++;
}
}
return(num);
}
if(iVolume(Symbol(),0,0)<=1
For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
MT4: New candle - MQL4 programming forum #3 (2014)
MT5: Accessing variables - MQL4 programming forum #3 (2022)
I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
Running EA once at the start of each bar - MQL4 programming forum (2011)
help me for buy and sell condition based on half trend indicator
canbuy and cansell bool condition send multiple orders
<ex4 file deleted>