here is the code : private bool ManageExit()
{
double currentSpread = CalculateSpread();
double spreadMean = spreadMeanIndicator[0];
double spreadStdDev = spreadStdDevIndicator[0];
double currentCorrelation = CalculateCurrentCorrelation();
Print("Checking Exit Conditions:");
Print("Current Spread: " + currentSpread);
Print("Spread Mean: " + spreadMean);
Print("Spread StdDev: " + spreadStdDev);
Print("Spread Deviation from Mean for Exit: " + Math.Abs(currentSpread - spreadMean));
Print("Required Deviation (StdDev) for Exit: " + spreadStdDev);
// Calculate the ATR-based profit target with multiplier
double atrProfitTargetPrimary = atrIndicatorPrimary[0] * atrMultiplier;
double atrProfitTargetSecondary = atrIndicatorSecondary[0] * atrMultiplier;
Print("Checking Exit Conditions based on ATR targets:");
Print("ATR Profit Target Primary: " + atrProfitTargetPrimary);
Print("ATR Profit Target Secondary: " + atrProfitTargetSecondary);
// Check if both positions are in profit and meet ATR target
bool primaryProfitTargetMet = Positions[0].MarketPosition == MarketPosition.Long && Close[0] - Positions[0].AveragePrice >= atrProfitTargetPrimary ||
Positions[0].MarketPosition == MarketPosition.Short && Positions[0].AveragePrice - Close[0] >= atrProfitTargetPrimary;
bool secondaryProfitTargetMet = Positions[1].MarketPosition == MarketPosition.Long && Closes[1][0] - Positions[1].AveragePrice >= atrProfitTargetSecondary ||
Positions[1].MarketPosition == MarketPosition.Short && Positions[1].AveragePrice - Closes[1][0] >= atrProfitTargetSecondary;
// Apply the exit logic for each instrument based on the profit target met condition
if (primaryProfitTargetMet)
{
Print("Exiting primary position based on ATR target.");
if (Position.MarketPosition == MarketPosition.Long) ExitLong("ExitATRLongNQ", "NQ_F_Long");
else if (Position.MarketPosition == MarketPosition.Short) ExitShort("ExitATRShortNQ", "NQ_F_Short");
}
if (secondaryProfitTargetMet)
{
Print("Exiting secondary position based on ATR target.");
if (Position.MarketPosition == MarketPosition.Long) ExitLong("ExitATRLongES", "ES_F_Long");
else if (Position.MarketPosition == MarketPosition.Short) ExitShort("ExitATRShortES", "ES_F_Short");
}
// Check if the spread-based exit condition is met
if (Math.Abs(CalculateSpread() - spreadMeanIndicator[0]) <= spreadStdDevIndicator[0])
{
Print("Exiting all positions based on spread condition.");
ExitLong("ExitSPRDLongNQ", "NQ_F_Long");
ExitShort("ExitSPRDShortNQ", "NQ_F_Short");
ExitLong("ExitSPRDLongES", "ES_F_Long");
ExitShort("ExitSPRDShortES", "ES_F_Short");
}
return primaryProfitTargetMet || secondaryProfitTargetMet; // Return true if any exit was executed
}
please help , i cant seem to figure this one out
Comment