Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///Initial method call is up here, followed by the rest of this code -
- bool profitChanged = false;
- double currentProfit = 0;
- double price = Close[0];
- if(currentEntry.IsLong){
- currentProfit = price - currentEntry.AverageFillPrice;
- }else if (currentEntry.IsShort){
- currentProfit = currentEntry.AverageFillPrice - price;
- }
- if (currentProfit >= highestProfitSinceEntry)
- {
- highestProfitSinceEntry = currentProfit;
- profitChanged = true;
- }
- if (!endTimePrint && !firstEntryFlat)
- {
- ///will only run when order has been placed and is still working
- if (bEntryBreakeven != 0 && !firstBreakevenHit)
- {
- if (currentEntry.IsLong && currentProfit >= bEntryBreakeven)
- {
- if (!aaaaCompanionMode) Print(currentEntry.Name + ": Breakeven target achieved, moving stop loss to " + (currentEntry.AverageFillPrice + bEntryBreakevenSize).ToString("C"));
- SetStopLoss(currentEntry.Name, CalculationMode.Price, currentEntry.AverageFillPrice + bEntryBreakevenSize, false);
- firstBreakevenHit = true;
- firstBreakevenPrice = firstEntry.AverageFillPrice + bEntryBreakevenSize;
- }
- else if (currentEntry.IsShort && currentProfit >= bEntryBreakeven)
- {
- if (!aaaaCompanionMode) Print(currentEntry.Name + ": Breakeven target achieved, moving stop loss to " + (currentEntry.AverageFillPrice - bEntryBreakevenSize).ToString("C"));
- SetStopLoss(firstEntryName, CalculationMode.Price, currentEntry.AverageFillPrice - bEntryBreakevenSize, false);
- firstBreakevenHit = true;
- firstBreakevenPrice = currentEntry.AverageFillPrice - bEntryBreakevenSize;
- }
- }
- if (cEntryProfit > 0 && !firstProfitHit)
- {
- if (cEntryTrailingStopSize > 0)
- {
- if (firstPreviousStopPrice == 0 && currentEntry.IsLong && currentProfit >= cEntryProfit)
- {
- Print(currentEntry.Name + ": Profit target achieved, initiating trailing stop loss.");
- firstPreviousStopPrice = price - cEntryTrailingStopSize;
- SetStopLoss(currentEntry.Name, CalculationMode.Price, firstPreviousStopPrice, false);
- firstProfitHit = true;
- }
- else if (firstPreviousStopPrice == 0 && currentEntry.IsShort && currentProfit >= cEntryProfit)
- {
- Print(currentEntry.Name + ": Profit target achieved, initiating trailing stop loss.");
- firstPreviousStopPrice = price + cEntryTrailingStopSize;
- SetStopLoss(currentEntry.Name, CalculationMode.Price, firstPreviousStopPrice, false);
- firstProfitHit = true;
- }
- }
- else if (cEntryTrailingStopSize == 0 && !firstProfitSet && (firstBreakevenHit || bEntryBreakeven == 0))
- {
- if (currentEntry.IsLong && currentProfit >= cEntryProfit)
- {
- ExitLong("Profit Target", currentEntry.Name);
- firstProfitSet = true;
- }
- else if (currentEntry.IsShort && currentProfit >= cEntryProfit)
- {
- ExitShort("Profit Target", currentEntry.Name);
- firstProfitSet = true;
- }
- }
- }
- ///This is the specific code that is adjusting the stop loss after our initial profit target has been hit -
- if (cEntryTrailingStopSize > 0 && firstProfitHit && profitChanged) //if we hit our profit target, enable trailing stop
- {
- if (currentEntry.IsLong && price > firstPreviousStopPrice && firstPreviousStopPrice < price - cEntryTrailingStopSize)
- {
- firstPreviousStopPrice = price - cEntryTrailingStopSize;
- SetStopLoss(currentEntry.Name, CalculationMode.Price, firstPreviousStopPrice, false);
- }
- else if (currentEntry.IsShort && price < firstPreviousStopPrice && firstPreviousStopPrice > price + cEntryTrailingStopSize)
- {
- firstPreviousStopPrice = price + cEntryTrailingStopSize;
- SetStopLoss(currentEntry.Name, CalculationMode.Price, firstPreviousStopPrice, false);
- }
- }
- }
- if (firstEntryFlat)
- { ///only runs when an entry reports back as completely filled, meaning we have exited our position
- if (cEntryTrailingStopSize == 0 && !firstTrimPrint && firstProfitHit)
- {
- if (!aaaaCompanionMode)
- {
- Print("We hit our profit target - Closing trim!");
- }
- firstTrimPrint = true;
- }
- PrintStrategyCompletion(currentEntry.Name);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement