Download MetaTrader 5
To add comments, please log in or register
Articles can help you to solidify your knowledge. Come and read them!
rwatts50
26
rwatts50 2014.05.01 16:23 

Hi

I am having a lot of pain with an Error 130. From what I have read everything points to a NormalizeDouble issue but I can't seem to solve it. The ea takes the low of the day and places a buy order, I have written similar code using 'Ask' as a price reference with no problem. The broker is Alpari and it's designed to work on the UK100 chart. Here are my code snippets, any help would be most appreciated.

Credit to WHRoeder for the functions.

extern double BuyDistanceFromLow =2;

extern double StopLossBUY   =10;  

double AcctBalance = AccountBalance();

double PercentageRisk  = 0.03;

double AvailiableRisk = AcctBalance * PercentageRisk;

double LotSize;

LotSize = (AvailiableRisk / StopLossBUY

double low = MarketInfo(Symbol(),MODE_LOW); 

PriceBuy=(low+BuyDistanceFromLow);

SLBuy=(PriceBuy - StopLossBUY);

TPBuy=(PriceBuy + TakeProfit);

PriceBuy = NormalizePrice(PriceBuy);

SLBuy = NormalizePrice(SLBuy);

TPBuy = NormalizePrice(TPBuy); 

TicketBuy=OrderSend(Symbol(),OP_BUYSTOP,LotSize,PriceBuy,Slip,SLBuy,TPBuy,"FTSEDoublesBuy",BuyMagicNumber,0,Blue); 

double NormalizePrice(double p){
    double ts = MarketInfo(Symbol(), MODE_TICKSIZE);
    return( MathRound(p/ts)*ts );
}

double NormalizeLots(double p){
    double ls = MarketInfo(Symbol(), MODE_LOTSTEP);
    return( MathRound(p/ls)*ls );
}
Keith Watford
Moderator
7984
Keith Watford 2014.05.01 19:43  
extern double BuyDistanceFromLow =2;

extern double StopLossBUY   =10;  
double low = MarketInfo(Symbol(),MODE_LOW); 

PriceBuy=(low+BuyDistanceFromLow);

SLBuy=(PriceBuy - StopLossBUY);

TPBuy=(PriceBuy + TakeProfit);

Using a day low of say 1.500

PriceBuy=1.5+2, which is 3.5

SLBuy =3.5-10, which is -6.5

rwatts50
26
rwatts50 2014.05.02 13:55  

Hi GumRai

Thanks for your help :-)

I don't think this is the issue though but please correct me if I'm wrong - I am trading on the UK 100 where the price is currently around 6783.0. If I run print statements from the above code for order price, stop loss, and take profit I get the following output:

Price StopLoss Take Profit Print Statement

I really hope this is me being stupid because they look like valid prices to me?? PLEASE HELP! :-(

Keith Watford
Moderator
7984
Keith Watford 2014.05.02 14:37  

Sorry, I didn't realise you were trading on the FTSE.

Check the stop level, probably you are trying to place a buy stop order too close to or even above the current price .

Bear in mind that the day low is bid, but a buy is placed at ask.

whroeder1
13641
whroeder1 2014.05.02 14:50  
rwatts50: price is currently around 6783.0. If I run print statements from the above code for order price, I get 6760.5
  1. No need for NormalizeDouble EVER
  2. You need to NormalizePrice for pending orders.
  3. Are you returning from start/OnInit between server calls? Otherwise RefreshRates
  4. Did you NormalizeLots and verify min/max lot?
  5. // LotSize = (AvailiableRisk / StopLossBUY
       LotSize =  AvailiableRisk / StopLossBUY * DeltaValuePerLot());
  6. AccountBalance * percent = Risk = ( OrderOpenPrice - OrderStopLoss )*DIR * DeltaPerLot * lotsize. Solve for lotsize and normalize properly.
    PriceBuy=(low+BuyDistanceFromLow);
    SLBuy=(PriceBuy - StopLossBUY);
    Does those two consider the spread? Are you adjusting differently for each direction?

  7. When I tried pending orders (years ago) I couldn't get them to work as the TP/SL was compared to current price not pending price. A) Open without TP/SL and then set them, or B) Wait for market to reach the trigger and open. Pending orders are necessary for humans because they can't watch every second - EAs can.
Keith Watford
Moderator
7984
Keith Watford 2014.05.02 21:29  
rwatts50:

Hi GumRai

Thanks for your help :-)

I don't think this is the issue though but please correct me if I'm wrong - I am trading on the UK 100 where the price is currently around 6783.0. If I run print statements from the above code for order price, stop loss, and take profit I get the following output:

I really hope this is me being stupid because they look like valid prices to me?? PLEASE HELP! :-(


Sorry, I seem to be unable to read your posts properly, so missed this before.

IF the day low is 6758.5, then +2.0 is 6760.5

If current price is 6783.0, then you Cannot place a buy stop order at 6760.5

You can place a buy limit though.

rwatts50
26
rwatts50 2014.05.06 08:37  

GumRai you little beauty, this was the problem, how I overlooked it I have no idea but hours of frustration are now at an end. THANK YOU!

WHRoeder Many thanks for your input. I wasn't doing everything you suggested, great checklist, I will amend my code accordingly.

:-D :-D :-D :-D :-D :-D :-D

/
To add comments, please log in or register