After attain profit target, calculate the loss of closed position and open position profit....

Questions about MultiCharts and user contributed studies.
exboy
Posts: 6
Joined: 31 May 2023
Has thanked: 2 times

After attain profit target, calculate the loss of closed position and open position profit....

Postby exboy » 11 Jun 2023

Hello, I want to code a filter function
That is:
After attain profit target, calculate the loss of closed position(after attain the target) and open position profit.
Then use the average value per contract as the filter.
The code can run correctly on TS, but it can't on MC, and I try several days but it still can't work...

Code: Select all

inputs: Per_PL_Min_TARG( numericsimple ); variables: Closedpositionprofit( 0 ),//only consider when currentcontracts != 0 Closedpositionprofit_pre( 0 ),//only consider when currentcontracts != 0 Closedbarloss( 0 ),//only consider when currentcontracts != 0 value_currentcontract( 0 ), Cl_grossloss_AfterTARG( 0 ),//only consider when currentcontracts != 0 Closedbarlosscontracts( 0 ), Cl_losscontracts_AfterTARG( 0 ),//only consider when currentcontracts != 0 ProfitTargetAttain( false ); Closedpositionprofit = positionprofit - openpositionprofit; Closedpositionprofit_pre = positionprofit[1] - openpositionprofit[1]; value_currentcontract = currentcontracts; If Closedpositionprofit - Closedpositionprofit_pre > 0.1 Then ProfitTargetAttain = true; If currentcontracts = 0 Then Begin ProfitTargetAttain = False; Closedbarloss = 0; Closedbarlosscontracts = 0; Cl_grossloss_AfterTARG = 0; Cl_losscontracts_AfterTARG = 0; end; If Closedpositionprofit[ 0 ] - Closedpositionprofit[ 1 ] < -0.1 Then Begin Closedbarloss = Closedpositionprofit[ 0 ] - Closedpositionprofit[ 1 ]; Closedbarlosscontracts = currentcontracts[ 1 ] - currentcontracts[ 0 ]; //This is true if profit rarget is greater than or equal to stop loss and # of max. contracts hold is 3. End Else Begin Closedbarloss = 0; Closedbarlosscontracts = 0; End; If ProfitTargetAttain = True Then Begin Cl_grossloss_AfterTARG = Cl_grossloss_AfterTARG + Closedbarloss; Cl_losscontracts_AfterTARG = Cl_losscontracts_AfterTARG + Closedbarlosscontracts; {if Cl_losscontracts_AfterTARG + Currentcontracts > 0 then} _Filter_Per_PL_TARG = IFFLogic( ( {Cl_grossloss_AfterTARG +} Openpositionprofit ) / ( {Cl_losscontracts_AfterTARG +} Currentcontracts ) > Per_PL_Min_TARG, True, False ); End Else _Filter_Per_PL_TARG = True;
1.Is positionprofit - openpositionprofit the closed position profit?
I think it is but it can't work correctly. It seems ProfitTargetAttain = true will lag for several bars...
2.Is Closedpositionprofit[0]-Closedpositionprofit[1] the closed position profit on the last bar?
it always implies ProfitTargetAttain = true
I also try positionprofit[0] - openpositionprofit[0] - ( positionprofit[1] - openpositionprofit[1] ),
it always implies ProfitTargetAttain = false

Does any one know how to calculate closed position profit/closed position profit on the last bar on MC?

the codes work correctly on TS is as following:

Code: Select all

inputs: Per_PL_Min_TARG( numericsimple ); variables: Closedpositionprofit( 0 ),//only consider when currentcontracts != 0 Closedbarloss( 0 ),//only consider when currentcontracts != 0 Closedpositiongrossloss_AfterTARG( 0 ),//only consider when currentcontracts != 0 value_currentcontracts( 0 ), Closedbarlosscontracts( 0 ), Closedpositionlosscontracts_AfterTARG( 0 ),//only consider when currentcontracts != 0 ProfitTargetAttain( false ); value_currentcontracts = Currentcontracts; Closedpositionprofit = Positionprofit - Openpositionprofit; If Closedpositionprofit[ 0 ] - Closedpositionprofit[ 1 ] > 0.1 Then ProfitTargetAttain = True; If currentcontracts = 0 Then Begin ProfitTargetAttain = False; Closedpositiongrossloss_AfterTARG = 0; Closedpositionlosscontracts_AfterTARG = 0; end; If Closedpositionprofit[ 0 ] - Closedpositionprofit[ 1 ] < -0.1 Then Begin Closedbarloss = Closedpositionprofit[ 0 ] - Closedpositionprofit[ 1 ]; Closedbarlosscontracts = value_currentcontracts[ 1 ] - value_currentcontracts[ 0 ]; //This is true if profit rarget is greater than stop loss and # of max. contracts hold is 3. End Else Begin Closedbarloss = 0; Closedbarlosscontracts = 0; End; If ProfitTargetAttain = True Then Begin Closedpositiongrossloss_AfterTARG = Closedpositiongrossloss_AfterTARG + Closedbarloss; Closedpositionlosscontracts_AfterTARG = Closedpositionlosscontracts_AfterTARG + Closedbarlosscontracts; _Filter_Per_PL_TARG = IFFLogic( ( Closedpositiongrossloss_AfterTARG + Openpositionprofit ) / ( Closedpositionlosscontracts_AfterTARG + Currentcontracts ) > Per_PL_Min_TARG, True, False ); End Else _Filter_Per_PL_TARG = True;

User avatar
rrams
Posts: 128
Joined: 10 Feb 2011
Location: USA
Has thanked: 7 times
Been thanked: 70 times
Contact:

Re: After attain profit target, calculate the loss of closed position and open position profit....

Postby rrams » 13 Jun 2023

Hello exboy,

I am thinking you want to record the change in profit per contract starting from a fixed price level target on a multi-contract position, but I don't completely understand your objective.

Your use of Closedpositionprofit as a variable name confuses me because the profit on the last closed position is just given by the keyword PositionProfit(1) for any bar on the chart. OpenPositionProfit keyword is the profit or loss on the currently open position. PositionProfit(0) is always the same as OpenPositionProfit. Historically referenced keywords using brackets "[1]" will return the value that many bars back, not positions back.

Perhaps you could clarify the issue with a print statement that doesn't give the result you want and a chart to show what the correct value should be. Or maybe someone with TS can test the code and recognize what is different. Sorry.

User avatar
Vlada MultiCharts
Posts: 293
Joined: 22 Apr 2020
Has thanked: 8 times
Been thanked: 76 times

Re: After attain profit target, calculate the loss of closed position and open position profit....

Postby Vlada MultiCharts » 14 Jun 2023

Hello exboy,

The logic of PositionProfit in MultiCharts and TS is different. In MC, PositionProfit(0) and PositionProfit with unspecified parameters return realized profit for the closed part of the current position.

For example, one opens a position for 10 contracts. It is partially closed for 5 contracts with realized profit of $3. On one of the calculations, the Open P/L for the position is -$2. In MC, OpenPositionProfit will return -2 and PositionProfit will return 3. In TS, OpenPositionProfit will return -2, and PositionProfit will return 1. As I have stated above, in MC PositionProfit only returns realized profit.

For more info, please refer to the wiki article. Please note that PLE Help for PositionProfit is not relevant and we will edit it as soon as possible.

exboy
Posts: 6
Joined: 31 May 2023
Has thanked: 2 times

Re: After attain profit target, calculate the loss of closed position and open position profit....

Postby exboy » 15 Jun 2023

Thank you for your replies, both are very helpful. I have changed Closedpositionprofit to PositionProfit and optimized, and I will see the backtest results tomorrow to see if it is OK.

But I still have a question, what is the definition of Contractprofit? Is it
the total profit or loss of all the closed and open positions of the current position /
the total number of all the closed and open positions of the current position?

User avatar
Vlada MultiCharts
Posts: 293
Joined: 22 Apr 2020
Has thanked: 8 times
Been thanked: 76 times

Re: After attain profit target, calculate the loss of closed position and open position profit....

Postby Vlada MultiCharts » 16 Jun 2023

But I still have a question, what is the definition of Contractprofit? Is it
the total profit or loss of all the closed and open positions of the current position /
the total number of all the closed and open positions of the current position?
exboy,

ContractProfit returns a numerical value indicating the current profit or loss per each contract or share of a multi-share or multi-contract position.
More info: https://www.multicharts.com/trading-sof ... ractProfit

exboy
Posts: 6
Joined: 31 May 2023
Has thanked: 2 times

Re: After attain profit target, calculate the loss of closed position and open position profit....

Postby exboy » 16 Jun 2023

it is including all the closed and open positions of the current position?

User avatar
Vlada MultiCharts
Posts: 293
Joined: 22 Apr 2020
Has thanked: 8 times
Been thanked: 76 times

Re: After attain profit target, calculate the loss of closed position and open position profit....

Postby Vlada MultiCharts » 21 Jun 2023

it is including all the closed and open positions of the current position?
exboy,

ContractProfit = OpenPositionProfit / CurrentContracts. It indicates the current unrealized profit and does not include profit for the part of the position that was closed.


Return to “MultiCharts”