EA based on 3 different timeframe conditions (Code executes properly; however, there is a logical error I can't find it.)
- Experiments ...
- find timeframe?
- indicator work only when change time frame
if(buyConfirmed){ double LotSize_Buy = 0; double Equity_Buy = AccountEquity(); double RiskedAmount_Buy = Equity_Buy*RiskPercent*0.01; double SL_Buy = Low[1]-StopLoss_Pips*pips; double SL_Buy_Loss = Bid - SL_Buy; double TP_Buy = Bid + (SL_Buy_Loss*Reward_Ratio); LotSize_Buy = (RiskedAmount_Buy/(SL_Buy_Loss/pips))/10; if(Lot_Size>0){ LotSize_Buy = Lot_Size; } ticket = OrderSend(Symbol(),OP_BUY,LotSize_Buy,Ask,3,SL_Buy,TP_Buy,NULL,MagicNumber,0,Green); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Comment("BUY order opened : ",OrderOpenPrice()); } else Comment("Error opening BUY order : ",GetLastError()); return(0); } if(sellConfirmed){ double LotSize_Sell = 0; double Equity_Sell = AccountEquity(); double RiskedAmount_Sell = Equity_Sell*RiskPercent*0.01; double SL_Sell = High[1]+StopLoss_Pips*pips; double SL_Sell_Loss = SL_Sell-Ask; double TP_Sell = Ask-(SL_Sell_Loss*Reward_Ratio); LotSize_Sell = (RiskedAmount_Sell/(SL_Sell_Loss/pips))/10; if(Lot_Size>0){ LotSize_Sell = Lot_Size; } ticket = OrderSend(Symbol(),OP_SELL,LotSize_Sell,Bid,3,SL_Sell,TP_Sell,NULL,MagicNumber,0,Green); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Comment("SELL order opened : ",OrderOpenPrice()); } else Comment("Error opening SELL order : ",GetLastError()); return(0); } TrailingStop(); for(int b = OrdersTotal()-1; b>=0; b--) { if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) if(OrderMagicNumber() == MagicNumber) if(OrderSymbol() == Symbol()) if(OrderType() == OP_BUY){ int shift = iBarShift(NULL, PERIOD_H4, OrderOpenTime()); double openingCCI = iCCI(NULL,PERIOD_H4,CCI_Period_H4, PRICE_CLOSE, shift); // target 1 if(openingCCI > 50 && CCI_Current_H4 < 50){ OrderClose(OrderTicket(), OrderLots(), Bid, 3); Comment("Target 1 reached"); Alert("Target 1"); } else { // target 5 ( last 8 cci > 100 & cci < 100) bool notSatisfied = false; for(int i = 1; i <= 8; i++){ double cci = iCCI(NULL,PERIOD_H4,CCI_Period_H4,PRICE_CLOSE,i); if(cci < 100){ notSatisfied = true; } } if(!notSatisfied){ double cci = iCCI(NULL,PERIOD_H4,CCI_Period_H4,PRICE_CLOSE,0); if(cci < 100){ OrderClose(OrderTicket(), OrderLots(), Bid, 3); Comment("Target 5 reached"); Alert("Target 5"); } else{ notSatisfied = true; } } if(notSatisfied){ // target 6 ( rule 2 reversed) if(!BuyRule_H4_EMAvsSMA){ int ruleCount = 0; if(SellRule_H4_MACD || SellRule_H4_RSI || SellRule_H4_CCI || SellRule_H4_DI){ OrderClose(OrderTicket(), OrderLots(), Bid, 3); Comment("Target 6 reached"); Alert("Target 6"); } } } } } else if(OrderType() == OP_SELL){ int shift = iBarShift(NULL, PERIOD_H4, OrderOpenTime()); double openingCCI = iCCI(NULL,PERIOD_H4,CCI_Period_H4, PRICE_CLOSE, shift); // target 1 if(openingCCI < -50 && CCI_Current_H4 > -50){ OrderClose(OrderTicket(), OrderLots(), Ask, 3); Comment("Target 1 reached"); Alert("Target 1"); } else { // target 5 ( last 8 cci > 100 & cci < 100) bool notSatisfied = false; for(int i = 1; i <= 8; i++){ double cci = iCCI(NULL,PERIOD_H4,CCI_Period_H4,PRICE_CLOSE,i); if(cci > -100){ notSatisfied = true; } } if(!notSatisfied){ double cci = iCCI(NULL,PERIOD_H4,CCI_Period_H4,PRICE_CLOSE,0); if(cci > -100){ OrderClose(OrderTicket(), OrderLots(), Ask, 3); Comment("Target 5 reached"); Alert("Target 5"); } else{ notSatisfied = true; } } if(notSatisfied){ // target 6 ( rule 2 reversed) if(!SellRule_H4_EMAvsSMA){ int ruleCount = 0; if(BuyRule_H4_MACD || BuyRule_H4_RSI || BuyRule_H4_CCI || BuyRule_H4_DI){ OrderClose(OrderTicket(), OrderLots(), Ask, 3); Comment("Target 6 reached"); Alert("Target 6"); } } } } } } } return(0); } // Start() bool isCrossed(int& status, bool condition){ if(status == 0){ if(!condition){ status = 1; } } else if(status == 1){ if(condition){ status = 2; } } return condition && status >= 2; } void resetDailyStatus(){ BuyRule_D_MACD_status= 0; BuyRule_D_RSI_status= 0; BuyRule_D_DI_status= 0; BuyRule_D_CCI_status= 0; BuyRule_D_Stoch_status = 0; } void resetHourlyStatus(){ BuyRule_H4_MACD_status = 0; BuyRule_H4_RSI_status = 0; BuyRule_H4_DI_status = 0; BuyRule_H4_CCI_status = 0; } void resetSellDailyStatus(){ SellRule_D_MACD_status= 0; SellRule_D_RSI_status= 0; SellRule_D_DI_status= 0; SellRule_D_CCI_status= 0; SellRule_D_Stoch_status = 0; } void resetSellHourlyStatus(){ SellRule_H4_MACD_status = 0; SellRule_H4_RSI_status = 0; SellRule_H4_DI_status = 0; SellRule_H4_CCI_status = 0; } bool IsNewCandle() { static int BarsOnChart = 0; if(Bars == BarsOnChart) return(false); BarsOnChart = Bars; return(true); } void TrailingStop() { if(!UseCandleTrailingStop){ return; } int BuyStopCandle = iLowest(NULL,0,MODE_LOW,CandlesBack,0); int SellStopCandle = iHighest(NULL,0,MODE_HIGH,CandlesBack,0); for(int b = OrdersTotal()-1; b>=0; b--) { if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) if(OrderMagicNumber() == MagicNumber) if(OrderSymbol() == Symbol()) if(OrderType() == OP_BUY) if(OrderStopLoss()<Low[BuyStopCandle]-PipAmountsForTrailing*pips) OrderModify(OrderTicket(),OrderOpenPrice(), Low[BuyStopCandle]-PipAmountsForTrailing*pips,OrderTakeProfit(),0,CLR_NONE); } for(int s = OrdersTotal()-1; s>=0; s--) { if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES)) if(OrderMagicNumber()== MagicNumber) if(OrderSymbol() == Symbol()) if(OrderType() == OP_SELL) if(OrderStopLoss()>High[SellStopCandle]+PipAmountsForTrailing*pips) OrderModify(OrderTicket(),OrderOpenPrice(), High[SellStopCandle]+PipAmountsForTrailing*pips,OrderTakeProfit(),0,CLR_NONE); } } // Trailing Stop Closing
NOTE: I should divide the code in two pieces.
Hello,Can anybody help me in this code? I think, indicators don't work properly. I tried to write correctly; however, probably the part related with passing the next rule may be wrong.
I tried to write an EA ruled by conditions attached below.
Rule0: Weekly RSI>50
Rule1: Daily Price >SMA200 and also at least one of them below:
*Daily RSI>50
*Daily DI+>DI-
*MACD crossed upwards
*CCI just crossover -100
*Stochastic crossed upwards and also Stoch D >50
Rule2: 4H EMA22 > 4H SMA20 and also at least two of them should be true:
*4H RSI >50
*4H MACD crossed upwards
*4H DI+>DI-
*4H CCI > -100
*** The main issue in there is for example the second trade cannot open if indicators don't turn to their deault positions. (Like after the first trade, RSI should be down to 50 and then cross upward the 50 level again.)
Target 1: If the position opened above 50, it should be closed when it is below 50.
Target 3: 1.5*StopLoss
Target 5: If 4H CCI is above 100 through 8 candles, it should be closed when it crosses the 100 downward.
Target 6: Opposite Rule 2 conditions (4H EMA22 < 4H SMA 20 and at least two of them give opposite direction signal.
Thank you for your interest,
Cheers,
Baris
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register