OpenPositionProfit percentage value  [SOLVED]

Questions about MultiCharts and user contributed studies.
Mik72
Posts: 20
Joined: 13 May 2022
Has thanked: 5 times

OpenPositionProfit percentage value

Postby Mik72 » 15 Mar 2024

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
Attachments
WhatsApp Image 2024-03-15 at 09.49.13_dcce45cf.jpg
(14.91 KiB) Not downloaded yet

User avatar
BB123
Posts: 68
Joined: 24 Nov 2023
Has thanked: 22 times
Been thanked: 10 times

Re: OpenPositionProfit percentage value

Postby BB123 » 15 Mar 2024

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;

User avatar
ABC
Posts: 721
Joined: 16 Dec 2006
Location: www.abctradinggroup.com
Has thanked: 125 times
Been thanked: 409 times
Contact:

Re: OpenPositionProfit percentage value

Postby ABC » 18 Mar 2024

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
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

Mik72
Posts: 20
Joined: 13 May 2022
Has thanked: 5 times

Re: OpenPositionProfit percentage value  [SOLVED]

Postby Mik72 » 26 Mar 2024

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.

User avatar
BB123
Posts: 68
Joined: 24 Nov 2023
Has thanked: 22 times
Been thanked: 10 times

Re: OpenPositionProfit percentage value

Postby BB123 » 31 Mar 2024

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.

Code: Select all

if ((netprofit-StartNewProfit) >= TPStopAmount) then begin //do something; End;//if
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..


Return to “MultiCharts”