Manual backtesting codes

Hi;
I am already able to capture the number of long trades and the number of short trades generated for the current date for my strategy.
Now my next targets are to capture:
TOTAL NUMBER OF LOSS TRADES FOR THE DAY
TOTAL NUMBER OF PROFIT TRADES FOR THE DAY
TOTAL LOSS/PROFIT FOR THE DAY
HIGHEST AND LOWEST PROFIT / LOSS IN ANY TRADE ON CURRENT DATE

Please give me some hint codes and I will work on it and do it.
I am a new bee and a non tech guy, please help.

REGARDS

SANTOSH PADHY

1 Like

What have you tried so far?

Hello. @santy
Many time written here that sometimes users they doNot give enough information for what they exactly need. And for that reason that threads never have an answer.
So. by making another Duplicate thread 6 days later… will not help me at all.


Take your time please, and try to think what you need and tell us.
For example for me Manual back testing means, to see various results inside the graphic chart.
But for you, maybe you not need that.

I have tried the below, but its not giving desired result:
isStartOfDay = Day() != Ref(Day(),-1);
numLOSSTRADES = SumSince(isStartOfDay, (SellPrice - BuyPrice) < 0 OR (ShortPrice - CoverPrice) < 0);
numPROFITTRADES = SumSince(isStartOfDay, (SellPrice - BuyPrice) > 0 OR (ShortPrice - CoverPrice) > 0);
GfxSetTextColor ( colorWhite );
GfxTextOut(" , LOSS TRADES = " + numLOSSTRADES , 250,y-60);
GfxTextOut(" , PROFIT TRADES = " + numPROFITTRADES , 335,y-60);
GfxTextOut(" , P/L = " + 0 , 430,y-60);

You need to match up each individual exit price with its corresponding entry price. If you write a very simple exploration which outputs SellPrice-BuyPrice you will see why your current logic is not working.

Perhaps instead you could try something like this untested snippet:

longProfit = IIf(Sell, SellPrice - ValueWhen(Buy, BuyPrice), 0);
shortProfit = IIf(Cover, ValueWhen(Short, ShortPrice) - CoverPrice, 0);

In other words, we will only calculate profit on the bars where an exit occurred, and that profit will be the difference between the exit price on that bar and the entry price on the most recent entry signal bar. Depending on your entry and exit logic, you may need to use ExRem() to get rid of extra signals.

Of course you will still need your SumSince() logic to tally up the number of winning and losing trades.

Matt

Thanks @ Matt. its working correctly now.
Now I am just 3 steps away from completing this project. they are as below:

  1. Capturing the total loss / profit for the day due to all the generated trades.
  2. The highest loss making trade.
  3. The highest profit making trade.

Please give me a starting point code and I will work on them and proceed like the previous times.
Needless to mention that with out your help and guidance I could have successfully reached this stage.
I lost all my savings in the market as like many others I traded with out any proper knowledge and strategy. I suffered a lot both financially and emotionally. However , I have now started learning things first. That is why currently I can not afford paid codes.

Please help.

Thanks @ Matt. its working correctly now.
Now I am just 3 steps away from completing this project. they are as below:

Capturing the total loss / profit for the day due to all the generated trades.
The highest loss making trade.
The highest profit making trade.

Please give me a starting point code and I will work on them and proceed like the previous times.
Needless to mention that with out your help and guidance I could have successfully reached this stage.
I lost all my savings in the market as like many others I traded with out any proper knowledge and strategy. I suffered a lot both financially and emotionally. However , I have now started learning things first. That is why currently I can not afford paid codes.

Please help.

You already have the longProfit and shortProfit values for each trade. By combining that with your isStartOfDay variable, you should be able to use SumSince(), HighestSince, and LowestSince() to find the total, max, and min values for each day. Give that a try, and come back to the forum if you can’t figure it out.

Matt

Yes, I could successfully coded for the total p/l, max profit and max loss for the day.

But , I am stuck at coding for Min profit trade and Min loss trade for the day.

Any hint please…

Hi

How are you?

Greetings of the Day!!!

Hope all the best with you.

Of late , in connection to our old discussion (if you could remember that you helped me in coding some manual backtesting codes) i notice some problems as below and request for your help / guide in this regard:

numLOSSTRADES = SumSince(isStartOfDay, longProfit < 0 OR shortProfit < 0 );

numLOSSTRADES = SumSince(isStartOfDay, longProfit < 3 OR shortProfit < 3 );

The first line of code successfully counts the loss making trades. Then to be more accurate , i thought we should consider the brokerage and other charges also when we want to decide if a trade is loss or profit. So, i amended the lie of code as the second line of code ad considered the 3 more points.

But UUUUUUUUUUUUUUUUGGGGGGGGGGGGGHHHHH!!! its not workig and gives some fancy results!!!

Please guide me to know where the logic went wrong or what error I did. Also please help how to achieve it.

numLOSSTRADES = SumSince(isStartOfDay, (Sell AND longProfit < 3) OR (Cover AND shortProfit < 3) );
1 Like

WOW!!!

Great soul!!!

God Bless you!!!

I spent almost 3 months in anxiety, but could not resolve it on my own.

Hats off to you!!!

TOTALPL = SumSince(isStartOfDay, longProfit + shortProfit) ;

The above code gives the total profit / loss for the day as the out put successfully (I think, but in doubt as the below does not work).

Next , I want to get the total loss incurred by the total number of loss making trades altogether and also the same for profit making trades.

please can you guide me with the coding to get the above output.

TOTALLOSS =  SumSince(isStartOfDay, (longProfit < 3) + ( shortProfit < 3)) ;
TOTALPROFIT =  SumSince(isStartOfDay, (longProfit > 3) + (shortProfit > 3)) ;

MINPROFITTRADE = HighestSince (isStartOfDay, longProfit + shortProfit)>3;
MINLOSSTRADE = LowestSince (isStartOfDay, longProfit + shortProfit)<3;

I TRIED THE ABOVE CODE, BUT TILL NOW NOT SUCCESSFUL. PLEASE, HELP ME SOMEBODY!!!

please help any one.

@santy, if your code is not working, then may we assume that you've used an Exploration or another method to determine how it's behaving? Tell us about the incorrect output, and how it needs to change.

TOTALLOSS =  SumSince(isStartOfDay, (longProfit < 3) + ( shortProfit < 3)) ;
TOTALPROFIT =  SumSince(isStartOfDay, (longProfit > 3) + (shortProfit > 3)) ;

The above gives total number of loss & profit trades respectively, where as they should give (I want it should work for that) the out put as the aggregate total points due to all loss making / profit making trades respectively.

Next, the below code I want them to produce the out put as the minimum loss / minimum profit making trade just after the deciding points(in my case I fixed +3 points as the cut off line i.e trades giving less than 3 points as profit are loss trades and trades giving more than 3 points are profit trades). That means I want the trade result which result is just after 3 points and just below 3 points

MINPROFITTRADE = HighestSince (isStartOfDay, longProfit + shortProfit)>3;
MINLOSSTRADE = LowestSince (isStartOfDay, longProfit + shortProfit)<3;

The same logic for catching the max profit / max loss trade, I successfully coded as below, AND ITS WORKING FINE. I just became nuts for catching the MIn loss / Min profit as its not working and all my R & D fails.

MAXPROFITTRADE = HighestSince (isStartOfDay, longProfit + shortProfit);
MAXLOSSTRADE = LowestSince (isStartOfDay, longProfit + shortProfit);

Please help.

TOTALLOSS = SumSince(isStartOfDay, (longProfit < 3) + ( shortProfit < 3)) ;
TOTALPROFIT = SumSince(isStartOfDay, (longProfit > 3) + (shortProfit > 3)) ;

The above gives total number of loss & profit trades respectively, where as they should give (I want it should work for that) the out put as the aggregate total points due to all loss making / profit making trades respectively.

Next, the below code I want them to produce the out put as the minimum loss / minimum profit making trade just after the deciding points(in my case I fixed +3 points as the cut off line i.e trades giving less than 3 points as profit are loss trades and trades giving more than 3 points are profit trades). That means I want the trade result which result is just after 3 points and just below 3 points
MINPROFITTRADE = HighestSince (isStartOfDay, longProfit + shortProfit)>3;
MINLOSSTRADE = LowestSince (isStartOfDay, longProfit + shortProfit)<3;

The same logic for catching the max profit / max loss trade, I successfully coded as below, AND ITS WORKING FINE. I just became nuts for catching the MIn loss / Min profit as its not working and all my R & D fails.
MAXPROFITTRADE = HighestSince (isStartOfDay, longProfit + shortProfit);
MAXLOSSTRADE = LowestSince (isStartOfDay, longProfit + shortProfit);

Please help.

santy
Try debuging your code. Read here How do I debug my formula?
Debuging your code is the best way to learn and find out whats wrong.

For example

The line of code above, do you expect MINPROFITTRADE to be a number ?
But the way you wrote, the return will be 0 (false) or 1 (true)

Yes, it gives out put as 0/1 only. please give me a start to get the desired result.