Trade Profit Info

Custom MT4 expert advisors published here.

Moderator: admin

Re: Trade Profit Info

Postby BTS0301 » Thu Jul 09, 2020 3:17 am

Solved that problem. But the profit lags by .20c for some unknown reason
BTS0301
 
Posts: 16
Joined: Fri Jun 26, 2020 9:17 pm

Re: Trade Profit Info

Postby BTS0301 » Sun Jul 12, 2020 9:40 pm

A portion of the code I have now...
Can't figure out why the calculations for profit for a sell entry is different for a buy... Please help

Code: Select all
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   datetime last = 0;
   double price = 0;
   double lots = 0;
   bool isBuy = true;
   bool isSell = true;
   OrdersIterator it();
   it.WhenSymbol(_Symbol).WhenTrade();
   while (it.Next())
   {
      datetime dt = it.GetOpenTime();
      if (last < dt)
      {
         price = it.GetOpenPrice();
         lots = OrderLots();
         isBuy = it.IsBuy();
         isSell = it.IsSell();
      }
   }
   if (last == 0 && OrderType() == OP_BUY)
   {
      double point = MarketInfo(_Symbol, MODE_POINT);
      int digits = (int)MarketInfo(_Symbol, MODE_DIGITS);
      int mult = digits == 3 || digits == 5 ? 10 : 1;
      double pipSize = point * mult;
      double unitCost = MarketInfo(_Symbol, MODE_TICKVALUE);
      double pips = isBuy ? ((close[0] - price) / pipSize)*10 : ((price - close[0]) / pipSize)*10;
      double profit = ((lots * pips * unitCost)- (7 * lots));
      Comment("Pips: " + DoubleToString(pips,0) + "  $: " + DoubleToString(profit, 2));
   }
   else
   if (last == 0 && OrderType() == OP_SELL)
   {
      double point = MarketInfo(_Symbol, MODE_POINT);
      int digits = (int)MarketInfo(_Symbol, MODE_DIGITS);
      int mult = digits == 3 || digits == 5 ? 10 : 1;
      double pipSize = point * mult;
      double unitCost = MarketInfo(_Symbol, MODE_TICKVALUE);
      double pips = isSell ? ((price - close[0]) / pipSize)*10 : ((close[0] - price) / pipSize)*10;
      double profit = ((lots * pips / unitCost)- (7 * lots));
      Comment("Pips: " + DoubleToString(pips,0) + "  $: " + DoubleToString(profit, 2));
   }
   
   return 0;
}
BTS0301
 
Posts: 16
Joined: Fri Jun 26, 2020 9:17 pm

Re: Trade Profit Info

Postby Apprentice » Mon Jul 13, 2020 8:15 am

Your request is added to the development list.
Development reference 1689.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36476
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Trade Profit Info

Postby Apprentice » Tue Jul 14, 2020 3:51 am

Because the price needs to move into a different direction to get the profit. The current price is higher than the open price means profit for buy and loss for sell.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36476
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Trade Profit Info

Postby BTS0301 » Thu Jul 23, 2020 9:29 am

But my point is how do I show a winning sell order as a profit... not a negative?
BTS0301
 
Posts: 16
Joined: Fri Jun 26, 2020 9:17 pm

Re: Trade Profit Info

Postby Apprentice » Tue Jul 28, 2020 3:46 pm

The current formula does show exactly that.

double pips = isBuy ? ((close[0] - price) / pipSize) : ((price - close[0]) / pipSize)
positive for buy when current price > open price
positive for sell when current price < open price;
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36476
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Previous

Return to MT4 Expert Advisors

Who is online

Users browsing this forum: No registered users and 40 guests