Bo Jin: I would like to open a buy trade when RSI passes 50, and I would like to use the
last ATR value when the trade is opened as my stoploss and takeprofit,
how should I do it?
- Why did you post your MT4 question in the Root / MT5 EA
section instead of the MQL4 section, (bottom of the Root page?)
General rules and best pratices of the Forum. - General - MQL5 programming forum
Next time post in the correct place. The moderators will likely move this thread there soon. - Just do it. Get the ATR value, and and use it to compute your
SL/TP in your
OrderSend functions. Show us your attempt (using the CODE button)
and
state the nature of your problem.
No free help
urgent help.
William Roeder:
- Why did you post your MT4 question in the Root / MT5 EA
section instead of the MQL4 section, (bottom of the Root page?)
General rules and best pratices of the Forum. - General - MQL5 programming forum
Next time post in the correct place. The moderators will likely move this thread there soon. - Just do it. Get the ATR value, and and use it to compute your
SL/TP in your
OrderSend functions. Show us your attempt (using the CODE button)
and
state the nature of your problem.
No free help
urgent help.
//+------------------------------------------------------------------+ #property copyright "Copyright 2019, X" #property link "" #property version "1.0" #property strict input bool OpenBUY=True; input bool OpenSELL=True; input bool CloseBySignal=True; input double StopLoss=200; input double TakeProfit=500; input double TrailingStop=0; input int RSIperiod=14; input double BuyLevel=30; input double SellLevel=70; input bool AutoLot=True; input double Risk=1; input double ManualLots=0.1; input int MagicNumber=123; input string Koment="RSIea"; input int Slippage=10; //---- buffers //--- int OrderBuy,OrderSell; int ticket; int LotDigits; int BarsCount = 0; double Trail,iTrailingStop; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int init() { return(0); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int deinit() { return(0); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int start() { double stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL); OrderBuy=0; OrderSell=0; for(int cnt=0; cnt<OrdersTotal(); cnt++) { if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)) if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderComment()==Koment) { if(OrderType()==OP_BUY) OrderBuy++; if(OrderType()==OP_SELL) OrderSell++; if(TrailingStop>0) { iTrailingStop=TrailingStop; if(TrailingStop<stoplevel) iTrailingStop=stoplevel; Trail=iTrailingStop*Point; //?? how should I set up here to make the Trail at 2*the ATR value when the trade was opened? Thanks! double tsbuy=NormalizeDouble(Bid-Trail,Digits); double tssell=NormalizeDouble(Ask+(Trail*Point),Digits); if(OrderType()==OP_BUY && Bid-OrderOpenPrice()>Trail && Bid-OrderStopLoss()>Trail) { ticket=OrderModify(OrderTicket(),OrderOpenPrice(),tsbuy,OrderTakeProfit(),0,Blue); } if(OrderType()==OP_SELL && OrderOpenPrice()-Ask>(Trail*Point) && (OrderStopLoss()-Ask>(Trail*Point) || OrderStopLoss()==0)) { ticket=OrderModify(OrderTicket(),OrderOpenPrice(),tssell,OrderTakeProfit(),0,Blue); } } } } double rsim60=iRSI(Symbol(),60,RSIperiod,PRICE_CLOSE,1); double rsim60R=iRSI(Symbol(),60,RSIperiod,PRICE_CLOSE,2); //--- open position if(Bars > BarsCount) { if(OpenBUY && OrderBuy<1 && rsim60R<50 && rsim60>50 )OPBUY(); if((OpenSELL && OrderSell<1 && rsim60R>50 && rsim60<50 ) )OPSELL(); // Dec 16 a vicious loop K around 38 BarsCount = Bars; } //--- close position by signal if(CloseBySignal) { if(OrderSell>0 && (rsim60>50) ) CloseSell(); if(OrderBuy>0 && (rsim60<50) ) CloseBuy(); } //--- return(0); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OPBUY() { double StopLossLevel; double TakeProfitLevel; double LATR=iATR(NULL,0,14,1); if(StopLoss>0) StopLossLevel=Bid-LATR*Point; else StopLossLevel=0.0; if(TakeProfit>0) TakeProfitLevel=Ask+3*LATR*Point; else TakeProfitLevel=0.0; ticket=OrderSend(Symbol(),OP_BUY,LOT(),Ask,Slippage,StopLossLevel,TakeProfitLevel,Koment,MagicNumber,0,DodgerBlue); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OPSELL() { double StopLossLevel; double TakeProfitLevel; double SATR=iATR(NULL,0,14,1); if(SStopLoss>0) StopLossLevel=Ask+SATR*Point; else StopLossLevel=0.0; if(TakeProfit>0) TakeProfitLevel=Bid-3*SATR*Point; else TakeProfitLevel=0.0; //--- ticket=OrderSend(Symbol(),OP_SELL,LOT(),Bid,Slippage,StopLossLevel,TakeProfitLevel,Koment,MagicNumber,0,DeepPink); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ void CloseSell() { int total=OrdersTotal(); for(int y=OrdersTotal()-1; y>=0; y--) { if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES)) if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && OrderMagicNumber()==MagicNumber) { ticket=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Black); } } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CloseBuy() { int total=OrdersTotal(); for(int y=OrdersTotal()-1; y>=0; y--) { if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES)) if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && OrderMagicNumber()==MagicNumber) { ticket=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Black); } } } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ double LOT() { double lotsi; double ilot_max =MarketInfo(Symbol(),MODE_MAXLOT); double ilot_min =MarketInfo(Symbol(),MODE_MINLOT); double tick=MarketInfo(Symbol(),MODE_TICKVALUE); //--- double myAccount=AccountBalance(); //--- if(ilot_min==0.01) LotDigits=2; if(ilot_min==0.1) LotDigits=1; if(ilot_min==1) LotDigits=0; //--- if(AutoLot) { lotsi=NormalizeDouble((myAccount*Risk)/10000,LotDigits); } else { lotsi=ManualLots; } //--- if(lotsi>=ilot_max) { lotsi=ilot_max; } //--- return(lotsi); } //+------------------------------------------------------------------+
Well, sir, thanks for the reply. I did make the attempt but something really puzzles me and I don't know how to approach this issue. I computed the ATR value in my OrderSend functions as you suggested, but I am unsure if it is correct. Moreover, I would like the EA can initiate the trailing stop setting the trail at 2*ATR in which the ATR should be the ATR value when the trade was opened instead of the ATR of the nearest bar. I am looking for a way to anchor the ATR value at the specified bar so that the trailing stop can be initiated referencing it. I really have no idea how it can be done, any help or suggestion would be appreciated, thanks.
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
Hi everyone, I know this should have been posted before, but unfortunately I failed to find any reference.
Basically this is the problem I am facing.
Suppose I would like to open a buy trade when RSI passes 50, and I would like to use the last ATR value when the trade is opened as my stoploss and takeprofit, how should I do it?
For example,
when the last RSI just passes 50, last ATR1==a, I would like my order to automatically set my stoploss to a and take profit to 2a, what should I do to set this up? Thanks! I have been working on this but couldn't fix it.
Below is my code and as you can see I am using fixed levels for stoploss and takeprofit. I would really like to know how I can set up them based on ATR to make it more dynamic, thanks very much!