Why is each iteration accumulating the values here?

Joined
Aug 10, 2023
Messages
1
Reaction score
0
The intention of my code below should give values to ExitShortLimit() for each iteration of EntriePerTrade and them Short TP 1, Short TP 2, etc.
The expected result is:

Short TP 1 = 40 ticks from price
Short TP 2 = 60 ticks from price
Short TP 3 = 80 ticks from price
Short TP 4 = 100 ticks from price

But for some reason each iteration is accumulating the previous value giving this result:

Short TP 1 = 40 ticks from price
Short TP 2 = 100 ticks from price
Short TP 3 = 180 ticks from price
Short TP 4 = 280 ticks from price

Is there a problem with the logic? I am using the print functions to debug, all the calculations appear to be correct from the print functions.

C#:
int EntriesPerTrade = 4
int[] TakeProfitLevels = new int[EntriesPerTrade];
TakeProfitLevels[0] = 40;
TakeProfitLevels[1] = 60;
TakeProfitLevels[2] = 80;
TakeProfitLevels[3] = 100;

if (execution.Order.OrderState == OrderState.Filled)
{
                double takeProfitShortLevels;
                double takeProfitLongLevels;

                for (int i = 0; i < EntriesPerTrade; i++)
                {
                    PrintTo = PrintTo.OutputTab2;
                    takeProfitShortLevels = execution.Order.AverageFillPrice - TakeProfitLevels[i] * TickSize;
                    Print(string.Format("{0} AverageFillPrice: {1} - TakeProfitLevels[i]: {2} * TickSize: {3} takeProfitShortLevels: {4}" + " " + i, Time[0], execution.Order.AverageFillPrice, TakeProfitLevels[i], TickSize, takeProfitShortLevels));
                    takeProfitLongLevels = execution.Order.AverageFillPrice + TakeProfitLevels[i] * TickSize;
                    Print(string.Format("{0} AverageFillPrice: {1} + TakeProfitLevels[i]: {2} * TickSize: {3} takeProfitLongLevels: {4}" + " " + i, Time[0], execution.Order.AverageFillPrice, TakeProfitLevels[i], TickSize, takeProfitLongLevels));

                    // Handle short position TP and SL
                    ExitShortLimit(0, true, execution.Order.Filled, takeProfitShortLevels, "Short TP " + i, "Short " + i);
                    Print(Time[0] + "ExitShortLimit at limit price: " + takeProfitShortLevels + " " + i);
                    ExitShortStopLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + InitialSLAmount, execution.Order.AverageFillPrice, "Initial Short SL " + i, "Short " + i);
                   
                    // Handle long position TP and SL
                    ExitLongLimit(0, true, execution.Order.Filled, takeProfitLongLevels, "Long TP " + i, "Long " + i);
                    Print(Time[0] + "ExitLongLimit at limit price: " + takeProfitLongLevels + " " + i);
                    ExitLongStopLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - InitialSLAmount, execution.Order.AverageFillPrice, "Initial Long SL " + i, "Long " + i);

                    Print(Time[0] + " " + execution.Order.AverageFillPrice + " short levels: " + i + " " + takeProfitShortLevels);
                    Print(Time[0] + " " + execution.Order.AverageFillPrice + " short levels: " + i + " " + takeProfitLongLevels);
                }
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top