Search code examples
mql4metatrader4

consider spreads and comission


i want to code an EA that when open a Buy/Sell position with 60 point profit. if the position goes to -20 point profit then open a position In the opposite direction of first position with 60 point profit

the code is hear:

if(orderType==OP_BUY)
        ticket = OrderSend(Symbol(),orderType,lots,openPrice,5,stopLossPrice,takeProfitPrice,"AFKARIAUS ",MagicBuy,0,clrGreen);
if(orderType==OP_SELL)
        ticket = OrderSend(Symbol(),orderType,lots,openPrice,5,stopLossPrice,takeProfitPrice,"AFKARIAUS ",MagicSell,0,clrGreen);

for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicBuy && OrderType()==OP_BUY)
                BuyTicket=OrderSend(Symbol(),OP_SELLSTOP,lots,OrderOpenPrice()-20*Point,5,OrderOpenPrice()+60*Point,OrderOpenPrice()-20*Point-60*Point,"AFKARIAUS",MagicBuy+1,0,clrBlue);

         if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicSell && OrderType()==OP_SELL)
                  SellTicket=OrderSend(Symbol(),OP_BUYSTOP,lots,OrderOpenPrice()+20*Point,5,OrderOpenPrice()-60*Point,OrderOpenPrice()+20*Point+60*Point,"AFKARIAUS",MagicSell+1,0,clrBlue);
     }

every thing is ok

but when test with real money there is Comission,Spreads and Swap.and i dont know what should i do

please help me

thanks


Solution

  • A smart solution, that includes swap and commission is to do with that order:

    1. OrderSelect

    2. OrderSwap & OrderCommission (sum those)

       if (OrderSelect(ticket_number,SELECT_BY_TICKET,MODE_TRADES)==true)
       {
          current_trade_absolute_profit = OrderSwap() + OrderProfit() + OrderCommission();
       }
      

    You can also find and store pip value like that:

    double tick_value = MarketInfo(Symbol(),MODE_TICKVALUE);
    

    Then calculate current pip prices and after that you can simply replace a double's value each time you earn more pips like that

       if (current_pip_number > highest_pip_number)
       {
           highest_pip_number = current_pip_number;
       }
       if (highest_pip_number < current_pip_number + 20)
       {
           OrderClose(ticket_number,lotsize,iClose(NULL,0,0),slippage,clrViolet);
       }