Download MetaTrader 5
To add comments, please log in or register
How to use MQL5.community messages editor? Find out in the article!
wcol
11
wcol 2016.09.30 18:31 
Hello All, 

I am trying to call the value of the Stop Loss from my previous trade and then have a condition whereby the stop loss of the next trade can not have a stop loss of the same value.

I thought I would add a variable PreviousStopLoss and set it equal to the Stop Loss of that trade, like this;

OrderSend(Symbol(),OP_SELL,CurrentLotSize,Bid,Slippagepips,Sell_CurrentStopLossPrice,NULL,NULL,Sell_MagicNumber,0,Red);
                          {              
                            PreviousStopLoss=Sell_CurrentStopLossPrice;
                          }                  

Then have this condition before the OrderSend Line. It appears to do as I intended, but also affects other trades that shouldn't be. I would really appreciate if anybody has any advice or insight. Many Thanks, 
if (Sell_CurrentStopLossPrice==PreviousStopLoss) return (0);
Keith Watford
Moderator
7993
Keith Watford 2016.09.30 21:04  
PreviousStopLoss=Sell_CurrentStopLossPrice;

once you have set this

if (Sell_CurrentStopLossPrice==PreviousStopLoss) return (0);

no code after this will be executed unless something changes the value of CurrentStopLossPrice or PreviousStopLoss

wcol
11
wcol 2016.09.30 21:27  
Hi GumRai, 

The value of CurrentStopLossPrice is taken from an indicator, so when that changes, that is when it is acceptable for a new trade to be placed. 

I have also tried OrderSelect options to no avail. 

So I thought that after the order sent it would update PreviousStopLoss and then the If line would be the check before a new order is sent in the future. 
       if (Sell_CurrentStopLossPrice==PreviousStopLoss) return (0);
                        OrderSend(Symbol(),OP_SELL,CurrentLotSize,Bid,Slippagepips,Sell_CurrentStopLossPrice,NULL,NULL,Sell_MagicNumber,0,Red);
                          {              
                            PreviouStopLoss=Sell_CurrentStopLossPrice;
Fernando Carreiro
1529
Fernando Carreiro 2016.10.01 07:41  
wcol: I am trying to call the value of the Stop Loss from my previous trade and then have a condition whereby the stop loss of the next trade can not have a stop loss of the same value. I thought I would add a variable PreviousStopLoss and set it equal to the Stop Loss of that trade, like this; Then have this condition before the OrderSend Line. It appears to do as I intended, but also affects other trades that shouldn't be. I would really appreciate if anybody has any advice or insight. Many Thanks, 

The code you have shown is useless if we don't know the rest of the context. If you are serious about the help you are requesting, then you will have to be more forthcoming and show a much larger portion of your code.

Don't just copy/paste snippets. Show code that can properly compile. In does not have to be your complete code, as it can just be a skeletal part as a proof of concept, but make it possible to compile and test in order to get a proper and informed response from us.

wcol
11
wcol 2016.10.01 15:39  
Hi FMIC, 

I agree, I should have posted more. Here is the Order Send portion for a sell trade. Before this is I have code that ensures no more than one trade can be open at a time and the conditions to open the trade. Though I'm happy to post anything else you might need to help fix my problem. 
CurrentOrder=OP_SELL;
          CurrentEquityAtRisk=(Sell_MaxPercentEquityAtRisk/100.)*5000;
            CurrentLotSize=LotSize(CurrentEquityAtRisk,Sell_CurrentStopLossPrice,CurrentOrder,CurrentSymbol,PairForCross);
                CurrentLotSize=NormalizeLotSize(CurrentLotSize);
                  if(CurrentLotSize<MarketInfo(CurrentSymbol,MODE_MINLOT)) CurrentLotSize=MarketInfo(CurrentSymbol,MODE_MINLOT);
                  if(CurrentLotSize>MarketInfo(CurrentSymbol,MODE_MAXLOT)) CurrentLotSize=MarketInfo(CurrentSymbol,MODE_MAXLOT);
                    CurrentEquityAtRisk=EquityAtRisk(CurrentLotSize,Sell_CurrentStopLossPrice,CurrentOrder,CurrentSymbol,PairForCross);
                      Print("Current EquityAtRisk = $",DoubleToStr(CurrentEquityAtRisk,2)," and Current Lotsize = ",CurrentLotSize);
                        OrderSend(Symbol(),OP_SELL,CurrentLotSize,Bid,Slippagepips,Sell_CurrentStopLossPrice,NULL,NULL,Sell_MagicNumber,0,Red);

I have taken out the code posted earlier in forum because it was not working as intended. To re-state the problem - I need a portion of code that prevents my EA from placing a trade which has the same stop loss as the previous trade. A push in the right direction would be very appreciated. Thank you, 

whroeder1
13644
whroeder1 2016.10.01 18:23  
wcol I need a portion of code that prevents my EA from placing a trade which has the same stop loss as the previous trade. A push in the right direction would be very appreciated.
  1. Scan history (it's not necessarily sorted.) Find the "previous trade"
  2. Get the previous SL.
  3. Compare to what you are about to do.
  4. Do or do not, there is no try.
/
To add comments, please log in or register