• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • User/Email: Password:
  • 12:33am
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 12:33am
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

Candle close cross MA alert.mq4 Not working Please Help 11 replies

Help getting this indicator working correctly please.. 0 replies

E-Mail Alert Not Working! Please Help! 20 replies

iMAOnArray not working on timeframe change: help please 2 replies

MT4 Demo platform not working with EAs... Please help 6 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
  • 6
Attachments: Please help me to get my Profit estimator working
Exit Attachments

Please help me to get my Profit estimator working

  • Post #1
  • Quote
  • First Post: Apr 17, 2020 2:45pm Apr 17, 2020 2:45pm
  •  enoeraew
  • | Joined Dec 2014 | Status: Member | 47 Posts
Hi,
what I want is a quick way to estimate how much profit/loss I will have if I close all orders at a given level.
I just can't seem to find the right math. I would appreciate any and all help with it. Thank you.
Attached File
File Type: mq4 estimated Gain.mq4   4 KB | 52 downloads
  • Post #2
  • Quote
  • Edited at 4:46pm Apr 17, 2020 4:12pm | Edited at 4:46pm
  •  Beerrun
  • Joined Jan 2016 | Status: Good Coder | Bad Trader | 461 Posts
Quoting enoeraew
Disliked
Hi, what I want is a quick way to estimate how much profit/loss I will have if I close all orders at a given level. I just can't seem to find the right math. I would appreciate any and all help with it. Thank you. {file}
Ignored
Something like this:
Inserted Code
double profit(){
   double x=0;
   for(int i=OrdersTotal()-1;i>=0;i—)
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)x+=OrderProfit();
   return x;}

edit:sorry just noticed you wanted for an arbitrary level. Something similar to this then:
Inserted Code
double profit(double price){
   double x=0;
   for(int i=OrdersTotal()-1;i>=0;i—)
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES){
         if(OrderType())x+=OrderOpenPrice()-price;
         if(OrderType()==0)x+=price-OrderOpenPrice();
         }
   return x;}

and combined with default value:
Inserted Code
double profit(double price=NULL){
   double x=0;
   for(int i=OrdersTotal()-1;i>=0;i—)
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES){
         if(price==NULL){x+=OrderProfit();continue;}
         if(OrderType())x+=OrderOpenPrice()-price;
         if(OrderType()==0)x+=price-OrderOpenPrice();
      }
   return x;}
profit(); for all orders at current price
profit(your_price); for all orders at your price
 
1
  • Post #3
  • Quote
  • Apr 17, 2020 5:09pm Apr 17, 2020 5:09pm
  •  enoeraew
  • | Joined Dec 2014 | Status: Member | 47 Posts
Hi, thanks for your time. I'm a nub here, Could you just slap that code into my indicator. It will take me 3 hrs of trial and error to insert your solution myself. I really appreciate the lesson. Just so you know how incompetent I am i've been at this indi for 3 days copying and pasting the thing together from code i find online.. I am quite proud of what i did so far but also exhausted and frustrated, I really just need someone else to finish it for me at this point. thanks
 
1
  • Post #4
  • Quote
  • Apr 17, 2020 5:19pm Apr 17, 2020 5:19pm
  •  Beerrun
  • Joined Jan 2016 | Status: Good Coder | Bad Trader | 461 Posts
Quoting enoeraew
Disliked
Hi, thanks for your time. I'm a nub here, Could you just slap that code into my indicator. It will take me 3 hrs of trial and error to insert your solution myself. I really appreciate the lesson. Just so you know how incompetent I am i've been at this indi for 3 days copying and pasting the thing together from code i find online.. I am quite proud of what i did so far but also exhausted and frustrated, I really just need someone else to finish it for me at this point. thanks
Ignored
I am glad you are learning and proud of it too You're doing great, take a break! Can't be incompetent since you've made it this far and learned a lot.

You can copy the whole function and paste into the global space of the page, so just not inside any {}. Then you can call it and assign it to a variable to use in the code or call it directly.
For example:
Inserted Code
#property indicator_chart_window
#property strict
 int start(){
 
   double currProfit=profit();
   Comment("My current profit is: "+(string)currProfit);
   //or
   Comment("My current profit is: "+(string)profit());
   return 0;}
 
double profit(double price=NULL){
   double x=0;
   for(int i=OrdersTotal()-1;i>=0;i—)
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES){
         if(price==NULL){x+=OrderProfit();continue;}
         if(OrderType())x+=OrderOpenPrice()-price;
         if(OrderType()==0)x+=price-OrderOpenPrice();
         }
   return x;}
 
1
  • Post #5
  • Quote
  • Apr 17, 2020 5:40pm Apr 17, 2020 5:40pm
  •  enoeraew
  • | Joined Dec 2014 | Status: Member | 47 Posts
here is what is in my code so far:


#property copyright ""
#property link ""
#property version "1.00"
#property strict
#property indicator_chart_window
double cprice, OpenPrice, pips, Size, Distance;
static double EstimateLoss, EstimateGain ;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
double ticksize = MarketInfo(Symbol(),MODE_TICKSIZE);
if (ticksize == 0.00001 || ticksize == 0.001)
pips = ticksize*10 ;
else pips = ticksize;
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
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[])
{
//---
EstimateGain = 0;
EstimateLoss = 0;

Distance = Ask + (20*pips);
ObjectCreate("TotalCloseLine",OBJ_HLINE,0,0,Distance);
ObjectSet("TotalCloseLine",OBJPROP_COLOR,Gold);
ObjectSet("TotalCloseLine",OBJPROP_STYLE,STYLE_SOLID);
ObjectSet("TotalCloseLine",OBJPROP_WIDTH,2);


if (ObjectGet("TotalCloseLine", OBJPROP_COLOR) == Gold)
{
cprice = ObjectGet("TotalCloseLine", OBJPROP_PRICE1);



for(int s=OrdersTotal()-1; s >= 0; s--)
{
if (OrderSelect(s,SELECT_BY_POS,MODE_TRADES)==true)
{

if (OrderSymbol() == Symbol())
{

OpenPrice = OrderOpenPrice();
//Size = OrderLots();

if(OrderType()== OP_SELL)
{
if (cprice > OpenPrice ) EstimateLoss = EstimateLoss + ((cprice - OpenPrice) * OrderLots());


if (cprice < OpenPrice ) EstimateGain = EstimateGain + ((OpenPrice - cprice )* OrderLots());
}

if(OrderType()==OP_BUY)
{

if (cprice < OpenPrice ) EstimateLoss = EstimateLoss + ((cprice - OpenPrice) * OrderLots());

if (cprice > OpenPrice ) EstimateGain = EstimateGain + ((OpenPrice - cprice )* OrderLots());
}

}

}

}

}

double EstimateClose = (EstimateGain - EstimateLoss) / pips;
color EstimateClosecolor;
if(EstimateClose>=0)EstimateClosecolor=DarkGreen;
else EstimateClosecolor=Red;
ObjectCreate("EstimateClose", OBJ_LABEL, 0,0,0);
ObjectSetText("EstimateClose","Estimated Profit $ "+DoubleToStr(EstimateClose,2),14,"Arial",EstimateClosecolor);
ObjectSet("EstimateClose",OBJPROP_CORNER,CORNER_RIGHT_LOWER);
ObjectSet("EstimateClose",OBJPROP_XDISTANCE,300);
ObjectSet("EstimateClose",OBJPROP_YDISTANCE,20);

//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
 
1
  • Post #6
  • Quote
  • Apr 17, 2020 5:45pm Apr 17, 2020 5:45pm
  •  enoeraew
  • | Joined Dec 2014 | Status: Member | 47 Posts
I don't want to waste your time teaching me the basics, I just don't get it. I will get back to it after some sleep. Just don't know how to marry your changes with the code I have already. cheers
 
1
  • Post #7
  • Quote
  • Edited at 8:25pm Apr 17, 2020 6:12pm | Edited at 8:25pm
  •  Beerrun
  • Joined Jan 2016 | Status: Good Coder | Bad Trader | 461 Posts
Quoting enoeraew
Disliked
here is what is in my code so far: #property copyright "" #property link "" #property version "1.00" #property strict #property indicator_chart_window double cprice, OpenPrice, pips, Size, Distance; static double EstimateLoss, EstimateGain ; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping double ticksize = MarketInfo(Symbol(),MODE_TICKSIZE); if (ticksize == 0.00001...
Ignored
Something like this. I don't have open orders to test but it should work okay. You can move the line and get different profit values. The horizontal line and label should change colours based on profit at that level. Use input width=zero for current profit values without the arbitrary price line.
Inserted Code
#property strict
#property indicator_chart_window
#define closeLine "TotalCloseLine"
#define closeLabel "EstimateClose"
 
extern color p1=clrGreen;//Positive Profit Colour
extern color p2=clrRed;//Negative Profit Colour
extern color p3=clrGold;//Zero Profit Colour
enum _width{Zero,One,Two,Three,Four,Five};
extern _width width=2;//Line Width
 
void OnInit(){
   if(width>0)
      if(ObjectCreate(closeLine,OBJ_HLINE,0,0,Ask+(_Point*200))){
         ObjectSet(closeLine,OBJPROP_STYLE,STYLE_SOLID);
         ObjectSet(closeLine,OBJPROP_WIDTH,width);
         }
   if(ObjectCreate(closeLabel,OBJ_LABEL, 0,0,0)){
      ObjectSet(closeLabel,OBJPROP_CORNER,CORNER_RIGHT_LOWER);
      ObjectSet(closeLabel,OBJPROP_XDISTANCE,300);
      ObjectSet(closeLabel,OBJPROP_YDISTANCE,20);
      }
   EventSetMillisecondTimer(1);
   }
 
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[]){main();return(rates_total);}
void OnTimer(){main();}
 
void OnDeinit(const int re){
   ObjectDelete(0,closeLine);
   ObjectDelete(0,closeLabel);
   }
 
void main(){
   long chart=ObjectFind(closeLine);
   double myProfit=chart>=0?profit(ObjectGetDouble(chart,closeLine,OBJPROP_PRICE)):profit();
   color colour=myProfit>0?p1:myProfit<0?p2:p3;
   ObjectSetText(closeLabel,"Estimated Profit $ "+DoubleToStr(myProfit,2),14,"Arial",colour);
   ObjectSet(closeLine,OBJPROP_COLOR,colour);
   }
 
double profit(double price=NULL){
  double x=0;
  for(int i=OrdersTotal()-1;i>=0;i--)
     if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      if(OrderSymbol()==Symbol()){
        if(price==NULL){x+=OrderProfit();continue;}
        if(OrderType())x+=OrderOpenPrice()-price;
        if(OrderType()==0)x+=price-OrderOpenPrice();
        }
  return x;}
Attached File
File Type: mq4 OrdersProfitLine.mq4   4 KB | 46 downloads
 
 
  • Post #8
  • Quote
  • Apr 17, 2020 6:20pm Apr 17, 2020 6:20pm
  •  enoeraew
  • | Joined Dec 2014 | Status: Member | 47 Posts
BEERRUN! Thanks. I will sleep well now. Can't wait for the ticks to come in. I will have fun figuring out why it works.
 
1
  • Post #9
  • Quote
  • Edited at 7:43pm Apr 17, 2020 6:27pm | Edited at 7:43pm
  •  Beerrun
  • Joined Jan 2016 | Status: Good Coder | Bad Trader | 461 Posts
Quoting enoeraew
Disliked
BEERRUN! Thanks. I will sleep well now. Can't wait for the ticks to come in. I will have fun figuring out why it works.
Ignored
It shouldn't be too difficult for you since you already know some things. But the ternary operators ("? :") might be confusing: they are like "if(){} else if(){} else{}" statements but when assigning a value.

If you want me to explain anything step by step let me know.
 
 
  • Post #10
  • Quote
  • Apr 19, 2020 9:29pm Apr 19, 2020 9:29pm
  •  enoeraew
  • | Joined Dec 2014 | Status: Member | 47 Posts
Hi Beerrun,

it looks to me that what you did calculated total pips. I wanted to get a dollar value so I made this change:

if(OrderType()==1 && ( OrderOpenPrice() > price )) x+= ((OrderOpenPrice() -price) * OrderLots() * 10);
if(OrderType()==1 && ( OrderOpenPrice() < price )) x-= ((price-OrderOpenPrice()) * OrderLots() * 10) ;

if(OrderType()==0 && ( OrderOpenPrice() > price )) x-= ((OrderOpenPrice() -price) * OrderLots() * 10) ;
if(OrderType()==0 && ( OrderOpenPrice() < price )) x+= ((price-OrderOpenPrice()) * OrderLots() * 10) ;

That seemed to do what i wanted. The difference i noticed could be attributed to Swap. When I tried to add OrderSwap() to the equation it all went terribly wrong. Any thoughts on swap. I.ve attached my work to help you follow my thinking.
Attached File
File Type: mq4 OrdersProfitLine Beerrun.mq4   7 KB | 55 downloads
 
1
  • Post #11
  • Quote
  • Edited Apr 20, 2020 3:06am Apr 19, 2020 11:06pm | Edited Apr 20, 2020 3:06am
  •  Beerrun
  • Joined Jan 2016 | Status: Good Coder | Bad Trader | 461 Posts
Quoting enoeraew
Disliked
Hi Beerrun, it looks to me that what you did calculated total pips. I wanted to get a dollar value so I made this change: if(OrderType()==1 && ( OrderOpenPrice() > price )) x+= ((OrderOpenPrice() -price) * OrderLots() * 10); if(OrderType()==1 && ( OrderOpenPrice() < price )) x-= ((price-OrderOpenPrice()) * OrderLots() * 10) ; if(OrderType()==0 && ( OrderOpenPrice() > price )) x-= ((OrderOpenPrice() -price) * OrderLots() * 10) ; if(OrderType()==0 && ( OrderOpenPrice() < price )) x+= ((price-OrderOpenPrice()) * OrderLots() * 10) ; That seemed to do...
Ignored
Yessir my apologies I forgot about the currency conversion

Most likely its because in this (and the alternative OrderType()==0):
Inserted Code
if(OrderType()==1&&(OrderOpenPrice()>price))x+=((OrderOpenPrice()-price)*OrderLots()*10);
if(OrderType()==1&&(OrderOpenPrice()<price))x-=((price-OrderOpenPrice())*OrderLots()*10);
you are just moving the negative sign to the other side of the equation,
and the same thing as:
Inserted Code
if(OrderType())x+=(OrderOpenPrice()-price)*OrderLots()*10;
However the second line with the OrderSwap() included:
Inserted Code
if(...)x-=(...)+OrderSwap();
has the unintended effect of potentially subtracting positive swaps from the profit sum.

If you would rather do it your way then add the OrderSwap() after both if() statements, and maybe add variables so that functions are not called more than once if not needed:
Inserted Code
double openPrice=OrderOpenPrice();
int type=OrderType();
if(type==1&&(openPrice>price))x+=(openPrice-price)*OrderLots()*10;
if(type==1&&(openPrice<price))x-=(price-openPrice)*OrderLots()*10;
x+=OrderSwap();
Or more simply:
Inserted Code
if(OrderType()==1)x+=((OrderOpenPrice()-price)*OrderLots()*10)+OrderSwap();
which keeps the sign on the righthand side of the equation. If the order's profit is negative, it is already calculated in at: OrderOpenPrice()-price.

The overall function:
Inserted Code
double profit(double _price=NULL,int _pips=10){
  double x=0;
  for(int i=OrdersTotal()-1;i>=0;i--)
     if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      if(OrderSymbol()==Symbol()){
        if(_price!=NULL){
         int type=OrderType();
         if(type==1)x+=((OrderOpenPrice()-_price)*OrderLots()*_pips);
         if(type==0)x+=((_price-OrderOpenPrice())*OrderLots()*_pips);
         }
        else x+=OrderProfit();
        x+=OrderSwap();
        }
  return x;}

I hope this helps (unless I'm an idiot and missed something )
Attached File
File Type: mq4 OrdersProfitLine.mq4   5 KB | 58 downloads
 
1
  • Post #12
  • Quote
  • Apr 20, 2020 12:04am Apr 20, 2020 12:04am
  •  Ican2020
  • Joined Aug 2014 | Status: Member | 444 Posts
Beerrun, you are not what you posted towards the last part of your last post.
You are an amazing guy, a great friend of the community who is willing to help any time.
So, don't ever use that word again
 
1
  • Post #13
  • Quote
  • Apr 20, 2020 12:10am Apr 20, 2020 12:10am
  •  Beerrun
  • Joined Jan 2016 | Status: Good Coder | Bad Trader | 461 Posts
Quoting Ican2020
Disliked
Beerrun, you are not what you posted towards the last part of your last post. You are an amazing guy, a great friend of the community who is willing to help any time. So, don't ever use that word again
Ignored
Thank you I don't want to sound full of myself or rude, so I add in some self-deprecation to try and balance it out
I'll try my best sir, but if I do then maybe that's confirmation?
 
 
  • Post #14
  • Quote
  • Apr 21, 2020 11:29am Apr 21, 2020 11:29am
  •  enoeraew
  • | Joined Dec 2014 | Status: Member | 47 Posts
Hi Again BeerRun,

Something is still not right with the indicator. Once swaps are introduced the thing goes off the rails. Let me say I don't mind that much, for what I need it for it is fine without including swaps. I just like to know why swaps seem to kill it.

I made a little video so you can get an idea of what I mean. https://streamable.com/mxs9up

Thank you
 
1
  • Post #15
  • Quote
  • Edited at 3:13pm Apr 21, 2020 2:55pm | Edited at 3:13pm
  •  Beerrun
  • Joined Jan 2016 | Status: Good Coder | Bad Trader | 461 Posts
Quoting enoeraew
Disliked
Hi Again BeerRun, Something is still not right with the indicator. Once swaps are introduced the thing goes off the rails. Let me say I don't mind that much, for what I need it for it is fine without including swaps. I just like to know why swaps seem to kill it. I made a little video so you can get an idea of what I mean. https://streamable.com/mxs9up Thank you
Ignored
Print statements are your friend. Lets try adding a temporary one right before OrderSwap gets added in to see what it really is:
Inserted Code
Print(“X: “+(string)x+” /OrderSwap: “+(string)OrderSwap());
x+=OrderSwap();
Then in the experts tab of the terminal, or the log, we can see the print.
Or can use Comment.
 
 
  • Post #16
  • Quote
  • Apr 21, 2020 4:33pm Apr 21, 2020 4:33pm
  •  enoeraew
  • | Joined Dec 2014 | Status: Member | 47 Posts
Hi Beerrun,

So I think my monkey programming got it working. I moved the " /ticksize" part to the Profit() from the main()

changed this

ObjectSetText(closeLabel,"Estimated Profit $ "+DoubleToStr(myProfit/ticksize,2),14,"Arial",colour);

to this

ObjectSetText(closeLabel,"Estimated Profit $ "+DoubleToStr(myProfit,2),14,"Arial",colour);

and the added this to the profit()

if(type)x+=((OrderOpenPrice()-_price)*OrderLots()*_pips / ticksize);
if(type==0)x+=((_price-OrderOpenPrice())*OrderLots()*_pips / ticksize);



My brain can't comprehend the reasons why it made a difference but it did. Thanks for sticking with it for me. Time for a BeerRun
 
1
  • Post #17
  • Quote
  • Apr 21, 2020 6:09pm Apr 21, 2020 6:09pm
  •  Beerrun
  • Joined Jan 2016 | Status: Good Coder | Bad Trader | 461 Posts
Quoting enoeraew
Disliked
Hi Beerrun, So I think my monkey programming got it working. I moved the " /ticksize" part to the Profit() from the main() changed this ObjectSetText(closeLabel,"Estimated Profit $ "+DoubleToStr(myProfit/ticksize,2),14,"Arial",colour); to this ObjectSetText(closeLabel,"Estimated Profit $ "+DoubleToStr(myProfit,2),14,"Arial",colour); and the added this to the profit() if(type)x+=((OrderOpenPrice()-_price)*OrderLots()*_pips / ticksize); if(type==0)x+=((_price-OrderOpenPrice())*OrderLots()*_pips / ticksize); My brain can't comprehend the reasons why...
Ignored
I’m glad you figured out something that works for you
And you figured out the reason behind the name haha, just trying to make enough pips for a beer run
 
 
  • Post #18
  • Quote
  • Last Post: Edited at 12:07pm Apr 27, 2020 8:43am | Edited at 12:07pm
  •  enoeraew
  • | Joined Dec 2014 | Status: Member | 47 Posts
Hi Beerrun or Nicholishen or other awesomely generous coder,

My AVA account adds a weird Sunday opening . The indicator sees it as a day when I think it should be just be part of the next day.

Attached Image (click to enlarge)
Click to Enlarge

Name: Screenshot1.png
Size: 39 KB



Is that a fixable thing?

Attached File
File Type: mq4 ZEBs Daily quick Trend.mq4   3 KB | 30 downloads
 
 
  • Platform Tech
  • /
  • Please help me to get my Profit estimator working
  • Reply to Thread
0 traders viewing now
Top of Page
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2022