I facing Error 130 from this trade routine

To add comments, please log in or register
Chaiya Srisawat
2036
Chaiya Srisawat  
//+------------------------------------------------------------------+
datetime send_ha_order_bartime;
//+------------------------------------------------------------------+
void send_ha_order(ENUM_TIMEFRAMES ha_TF, double lots,int magic)  //
{
int stop_level=(int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL);
int ticket=0;
int slippage=50;
double sell_SL=0,buy_SL=0,buy_TP=0,sell_TP=0;
string order_comment="HA";
if(send_ha_order_bartime!=iTime(_Symbol,ha_TF,0))
{
send_ha_order_bartime=iTime(_Symbol,ha_TF,0);
//
if(signal_HA[TF_inx]==buy&&iClose(Symbol(),ha_TF,0)>iClose(Symbol(),ha_TF,1))
{
if (TAKE_PROFIT>0)
buy_TP=Bid+TAKE_PROFIT*_Point;
else buy_TP=0;
buy_SL=buy_SL_HA[TF_inx];
buy_TP=NormalizeDouble(buy_TP,Digits);
buy_SL=NormalizeDouble(buy_SL,Digits);
if((buy_SL<_Point||(Ask-buy_SL)/_Point>stop_level)&&
   (buy_TP<_Point||(buy_TP-Ask)/_Point>stop_level))
ticket=OrderSend(_Symbol,OP_BUY,lots,Bid,slippage,buy_SL,buy_TP,order_comment,magic,0);
if(ticket>0) signal_HA[TF_inx]=0;
}
if(signal_HA[TF_inx]==sell&&iClose(Symbol(),ha_TF,0)<iClose(Symbol(),ha_TF,1))
{
if (TAKE_PROFIT>0)
sell_TP=Ask-TAKE_PROFIT*_Point;
else sell_TP=0;
sell_SL=sell_SL_HA[TF_inx];
sell_TP=NormalizeDouble(sell_TP,Digits);
sell_SL=NormalizeDouble(sell_SL,Digits);
if((sell_SL<_Point||(sell_SL-Bid)/_Point>stop_level)&&
   (sell_TP<_Point||(Bid-sell_TP)/_Point>stop_level))
ticket=OrderSend(_Symbol,OP_SELL,lots,Ask,slippage,sell_SL,sell_TP,order_comment,magic,0);
if(ticket>0) signal_HA[TF_inx]=0;
}
}
}
//+------------------------------------------------------------------+

Hello, Do some one help me? I facing ERROR 130 from the above trade routine! What wrong?

Chaiya

whroeder1
17927
whroeder1  
  1. buy_TP=NormalizeDouble(buy_TP,Digits);
    buy_SL=NormalizeDouble(buy_SL,Digits);
    Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong

  2. if((buy_SL<_Point||(Ask-buy_SL)/_Point>stop_level)&&
       (buy_TP<_Point||(buy_TP-Ask)/_Point>stop_level))
    ticket=OrderSend(_Symbol,OP_BUY,lots,Bid,slippage,buy_SL,buy_TP,order_comment,magic,0);
    You buy at the Ask and sell at the Bid.
    • Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
    • Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 3
    • The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools -> Options {control-O} -> charts -> Show ask line.)

  3. Check your return codes for errors and report them.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  4. Lots is a constant, therefor your risk isn't. Risk depends on your initial stop loss, lot size, and the value of the pair.
    • You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
    • Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
    • Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency (EUR, in this case).
                MODE_TICKVALUE is not reliable on non-fx instruments with many brokers.
    • You must normalize lots properly and check against min and max.
    • You must also check FreeMargin to avoid stop out
    Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5=0.1 Lots maximum.

  5. int slippage=50;
    
    buy_TP=Bid+TAKE_PROFIT*_Point;
    Using Points means code breaks on 4 digit brokers, exotics (e.g. USDZAR where spread is over 500 points,) and metals. Compute what a PIP is and use it, not points.
              How to manage JPY pairs with parameters? - MQL4 and MetaTrader 4 - MQL4 programming forum

Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
  • book.mql4.com
Tables below show calculation values that limit the conduction of trades when opening, closing, placing, deleting or modifying orders. To get the minimum distance to StopLevel and freezing distance FreezeLevel the MarketInfo() function should be called. Requirements. Correct prices used when performing trade operations. Order Type Open Price...
Chaiya Srisawat
2036
Chaiya Srisawat  
whroeder1:
  1. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong

  2. You buy at the Ask and sell at the Bid.
    • Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
    • Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 3
    • The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools -> Options {control-O} -> charts -> Show ask line.)

  3. Check your return codes for errors and report them.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  4. Lots is a constant, therefor your risk isn't. Risk depends on your initial stop loss, lot size, and the value of the pair.
    • You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
    • Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
    • Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency (EUR, in this case).
                MODE_TICKVALUE is not reliable on non-fx instruments with many brokers.
    • You must normalize lots properly and check against min and max.
    • You must also check FreeMargin to avoid stop out
    Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5=0.1 Lots maximum.

  5. Using Points means code breaks on 4 digit brokers, exotics (e.g. USDZAR where spread is over 500 points,) and metals. Compute what a PIP is and use it, not points.
              How to manage JPY pairs with parameters? - MQL4 and MetaTrader 4 - MQL4 programming forum

Hello,

It still have an error 130 with this data, what wrong!

result from my debug!

      order_comment=StringSubstr(order_comment,0,20);
      int
      ticket=0;
      //+------------------------------------------------------------------+
      if(((SL-OPEN)/pip>=(STOP_level) || SL==0) &&
         ((OPEN-TP)/pip>=(STOP_level) || TP==0) &&
         (Bid-OPEN)/pip>=(STOP_level) &&
         state.EA_switch_on
         )
        {
         OPEN=NormalizePrice(OPEN);
         SL=NormalizePrice(SL);
         TP=NormalizePrice(TP);
           {
            if(max_allowed_orders>totals_pending_orders || 
               max_allowed_orders==0
               )
               //
               // A3
               //
               ticket=OrderSend(_Symbol,OP_SELLSTOP,LOT,OPEN,slippage,SL,TP,order_comment,magic,0,clrGreen);
               if(ticket<0)
         {

Please help I test it many many time and has same results..

chaiya


whroeder1
17927
whroeder1  
  1. Your last image shows: PIP=0.00001000 Point=0.00001000 Bogus PIP.

  2. if(((SL-OPEN)/pip>=(STOP_level
    Pip is bogus. Your original post showed STOP_level is in points. Bogus comparison. Also on some brokers stop level returns zero, and code then breaks; I use minimum of 3 pips always or there is no reason for a pending. Humans can't watch the screen 24/7 so they use pending orders; EAs can, so no need for pending orders, have it wait until the market reaches the trigger price and open an order.

  3. You last image shows: SellStop=1.12789 Bid-Open/pip=1.00000 > StopLevel. Don't assume.

  4. Print your variables, and Bid/Ask. You don't know what's happening, find out. Use the debugger or print out your variables, including _LastError and find out why.
To add comments, please log in or register