Hi everyone
I'm trying to get the percentage value of openpositionprofit, and I coded it in this way : vDeltaProfitPerc=(c-entryprice)/entryprice*100;
But I get an error message from MC that says that I can't divide from 0. Do you have any idea hove come this happen and how can I fix it?
Thanks
OpenPositionProfit percentage value [SOLVED]
OpenPositionProfit percentage value
- Attachments
-
- WhatsApp Image 2024-03-15 at 09.49.13_dcce45cf.jpg
- (14.91 KiB) Not downloaded yet
Re: OpenPositionProfit percentage value
Are you checking this for on the same bar your placing the trade with? Maybe your getting the warning on the first bar-- and you think the warning is coming from another bar? Though its really just staying there from checking on the bar the trade was placed...
entryprice -close price=0, so would = 0/price*100 = 0... Warning comes up and then stays..
Another way to do what your trying to would be
I brain farted and just excluded you getting the %.. But who knows if something here will invite a fresh thought in your head... so ill leave it even though it doesnt really pertain to what you originally were asking about..
if trade taking function = true then begin
CurrentNetProfit = Netprofit;
Place trade..
End;
NetTrdeProfit = NetProfit -CurrentNetProfit ;
// if you want to view your NetTradeProfit then just print it :
Print ("NetTradeProfit =",NetTradeProfit);
if NetTradeProfit >= TPGoal then Closetrade;
entryprice -close price=0, so would = 0/price*100 = 0... Warning comes up and then stays..
Another way to do what your trying to would be
I brain farted and just excluded you getting the %.. But who knows if something here will invite a fresh thought in your head... so ill leave it even though it doesnt really pertain to what you originally were asking about..
if trade taking function = true then begin
CurrentNetProfit = Netprofit;
Place trade..
End;
NetTrdeProfit = NetProfit -CurrentNetProfit ;
// if you want to view your NetTradeProfit then just print it :
Print ("NetTradeProfit =",NetTradeProfit);
if NetTradeProfit >= TPGoal then Closetrade;
- ABC
- Posts: 721
- Joined: 16 Dec 2006
- Location: www.abctradinggroup.com
- Has thanked: 125 times
- Been thanked: 409 times
- Contact:
Re: OpenPositionProfit percentage value
Mik72,
as a rule of thumb it is good practice to check that a denominator is not zero before you perform a division in PowerLanguage. Otherwise, it is very likely to run into an error sooner or later. Not knowing if you evaluate your expression conditionally, one can only guess, but you should keep in mind that entryprice equals zero before you have taken any trade and in turn you might try to divide by zero on the very first calculation.
Regards,
ABC
as a rule of thumb it is good practice to check that a denominator is not zero before you perform a division in PowerLanguage. Otherwise, it is very likely to run into an error sooner or later. Not knowing if you evaluate your expression conditionally, one can only guess, but you should keep in mind that entryprice equals zero before you have taken any trade and in turn you might try to divide by zero on the very first calculation.
Regards,
ABC
Hi everyone
I'm trying to get the percentage value of openpositionprofit, and I coded it in this way : vDeltaProfitPerc=(c-entryprice)/entryprice*100;
But I get an error message from MC that says that I can't divide from 0. Do you have any idea hove come this happen and how can I fix it?
Thanks
Re: OpenPositionProfit percentage value [SOLVED]
Thank you to help me , the problem was on the first bar where obviusly I don't have an open position an then I can't have an openpositionprofit.
Re: OpenPositionProfit percentage value
HI mIk.. im a bit confused.. When you say "the problem was on the first bar" do you mean the first bar of the day? Or First bar of a setup (before the trade?) .. I coded a function to find start of day values-- current global net profit ect..
Lets say your question is referring to before a trade is opened...
1. Id get my current NetProfit before the start of the trade;
StartNewProfit = NetProfit;
2. Id then use that StartNewProfit var against my current net profit.
This gets me to where i know the current net account value including open positions.. As you can see i then compare that against my goal target amount and if its reached i close the trade..
Of course you can take the result of netprofit-StartNewProfit against your TPStopAmount to get the % the trade has moved in or out of your direction..
Not sure if this helps or if i totally misunderstood your question.. But if it helps-- great.. If not- then maybe it will help someone else..
Lets say your question is referring to before a trade is opened...
1. Id get my current NetProfit before the start of the trade;
StartNewProfit = NetProfit;
2. Id then use that StartNewProfit var against my current net profit.
Code: Select all
if ((netprofit-StartNewProfit) >= TPStopAmount) then begin
//do something;
End;//if
Of course you can take the result of netprofit-StartNewProfit against your TPStopAmount to get the % the trade has moved in or out of your direction..
Not sure if this helps or if i totally misunderstood your question.. But if it helps-- great.. If not- then maybe it will help someone else..