Calculate profit monthly

Hi,
Pls help me to fix my code afl! I want Calculate Profit in every month in year. I try to write code but profit show in my chart not exactly. My code here:

Open_Long = Flip( Buy, Sell ); 
Open_Short = Flip( Sell, Buy ); 
Buy_Price = ValueWhen( Buy, BuyPrice, 1 ); 
Short_Price = ValueWhen( Short, ShortPrice, 1 ); 
Sell_Price = IIf( Sell, ValueWhen( Sell, SellPrice, 1 ), Short_Price ); 
Cover_Price = IIf( Cover, ValueWhen( Cover, CoverPrice, 1 ), Buy_Price ); 
Profit = IIf( Open_Long, ( Close - Buy_Price ), IIf( Open_Short, ( Short_Price - Close ), 0 ) ); 
Last_Profit = IIf( Ref( Open_Long, -1 ) == 1 AND Open_Long != 1, ( Sell_Price - Buy_Price ), IIf( Ref( Open_Short, -1 ) == 1 AND Open_Short != 1, ( Short_Price - Cover_Price ), 0 ) ); 
Cum_Profit[0] = Last_Profit[0]; 
LastprofitMoValue[0]=Cum_Profit[0];
PreviousProfit = ValueWhen(Last_Profit!=0,Last_Profit,1); 
mo= Month();
yr=Year();
NumberM[0] = 0; 
Month1[0] = 0; 
Month2[0] = 0; 
Month3[0] = 0; 
Month4[0] = 0; 
Month5[0] = 0; 
Month6[0] = 0; 
for ( i = 1;i < BarCount;i++ ) 
{ 
if(yr[i]==2021) {
 Cum_Profit[i] = Cum_Profit[i-1] + Last_Profit[i]; 
    if(mo[i]==1) {
  Cum_Profit[i] = Cum_Profit[i-1] + Last_Profit[i]; 
  Month1=Cum_Profit[i];
  }
    if(mo[i]==2) {
  Cum_Profit[i] = Cum_Profit[i-1] + Last_Profit[i]; 
  Month2=Cum_Profit[i];
  Month2=Month2-Month1;
  }
    if(mo[i]==3) {
  Cum_Profit[i] = Cum_Profit[i-1] + Last_Profit[i]; 
  Month3=Cum_Profit[i];
  Month3=Month3-Month2;
  }
    if(mo[i]==4) {
  Cum_Profit[i] = Cum_Profit[i-1] + Last_Profit[i]; 
  Month4=Cum_Profit[i];
  Month4=Month4-Month3;
  }
    if(mo[i]==5) {
  Cum_Profit[i] = Cum_Profit[i-1] + Last_Profit[i]; 
  Month5=Cum_Profit[i];
  Month5=Month5-Month4;
  }
    if(mo[i]==6) {
  Cum_Profit[i] = Cum_Profit[i-1] + Last_Profit[i]; 
  Month6=Cum_Profit[i];
  Month6=Month6-Month5;
  }
 }
 }

TR2 = ParamToggle( "Profit Monthly", "Show|Hide", 1 ); 
 
if ( TR2 == 1 ) 
{ 
 GfxSelectFont( "Verdana", 10, 100 ); 
 GfxSetBkMode( 1 ); 
 GfxSetTextColor( colorWhite ); 
 y = Status( "pxchartheight" );
  GfxTextOut(  "Last 5 trades results"  , 13, y - 275 );
 GfxTextOut( ( "Month1: " + WriteVal( Month1, 3.0 ) ), 13, y - 250 );
 GfxTextOut( ( "Month2: " + WriteVal( Month2, 3.0 ) ), 13, y - 225 ); 
 GfxTextOut( ( "Month3: " + WriteVal( Month3, 3.0 ) ), 13, y - 200 ); 
 GfxTextOut( ( "Month4: " + WriteVal( Month4, 3.0 ) ), 13, y - 175 ); 
 GfxTextOut( ( "Month5: " + WriteVal( Month5, 3.0 ) ), 13, y - 150 ); 
 GfxTextOut( ( "Month6: " + WriteVal( Month6, 3.0 ) ), 13, y - 125 ); 
} 

Tks All

You do not need BarCount loop.

Instead of this one

mo = Month();
yr = Year();

Month1[0] = 0; 
Month2[0] = 0; 
Month3[0] = 0; 
Month4[0] = 0; 
Month5[0] = 0; 
Month6[0] = 0; 

for( i = 1; i < BarCount; i++ )
{
    if( yr[i] == 2021 )
    {
        Cum_Profit[i] = Cum_Profit[i - 1] + Last_Profit[i];

        if( mo[i] == 1 )
        {
            Cum_Profit[i] = Cum_Profit[i - 1] + Last_Profit[i];
            Month1 = Cum_Profit[i];
        }

        if( mo[i] == 2 )
        {
            Cum_Profit[i] = Cum_Profit[i - 1] + Last_Profit[i];
            Month2 = Cum_Profit[i];
            Month2 = Month2 - Month1;
        }

        if( mo[i] == 3 )
        {
            Cum_Profit[i] = Cum_Profit[i - 1] + Last_Profit[i];
            Month3 = Cum_Profit[i];
            Month3 = Month3 - Month2;
        }

        if( mo[i] == 4 )
        {
            Cum_Profit[i] = Cum_Profit[i - 1] + Last_Profit[i];
            Month4 = Cum_Profit[i];
            Month4 = Month4 - Month3;
        }

        if( mo[i] == 5 )
        {
            Cum_Profit[i] = Cum_Profit[i - 1] + Last_Profit[i];
            Month5 = Cum_Profit[i];
            Month5 = Month5 - Month4;
        }

        if( mo[i] == 6 )
        {
            Cum_Profit[i] = Cum_Profit[i - 1] + Last_Profit[i];
            Month6 = Cum_Profit[i];
            Month6 = Month6 - Month5;
        }
    }
}

You just need very few lines:

mo = Month();
yr = Year();

is2021 = yr == 2021;
for ( i = 1;i <= 6;i++ ) {
	cs = Cum(IIf(mo == i AND is2021,Last_Profit,0));
	VarSet("Month"+i, SelectedValue(cs)); 
}	

As for the rest of your posted code...

It is incomplete.

Also no one knows what following one means:

What is shown and what do you expect to be shown and why?


Upper one is not "open short". It is "not in long trade".
"In short trade" would be this one

Open_Short = Flip( Short, Cover ); 
3 Likes

Tks @fxshrat ! I try to your code. It's perfect for me!

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.