Advertisement
jbrava

case #04360364

Apr 12th, 2024 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.38 KB | None | 0 0
  1. ///Initial method call is up here, followed by the rest of this code -
  2.                 bool profitChanged = false;
  3.                 double currentProfit = 0;
  4.                 double price = Close[0];
  5.                 if(currentEntry.IsLong){
  6.                     currentProfit = price - currentEntry.AverageFillPrice; 
  7.                 }else if (currentEntry.IsShort){
  8.                     currentProfit = currentEntry.AverageFillPrice - price;
  9.                 }
  10.                            
  11.  
  12.  
  13.                 if (currentProfit >= highestProfitSinceEntry)
  14.                 {
  15.                     highestProfitSinceEntry = currentProfit;
  16.                     profitChanged = true;
  17.                 }
  18.  
  19.                 if (!endTimePrint && !firstEntryFlat)
  20.                 {
  21.                     ///will only run when order has been placed and is still working
  22.  
  23.                     if (bEntryBreakeven != 0 && !firstBreakevenHit)
  24.                     {
  25.                         if (currentEntry.IsLong && currentProfit >= bEntryBreakeven)
  26.                         {
  27.                             if (!aaaaCompanionMode) Print(currentEntry.Name + ": Breakeven target achieved, moving stop loss to " + (currentEntry.AverageFillPrice + bEntryBreakevenSize).ToString("C"));
  28.                             SetStopLoss(currentEntry.Name, CalculationMode.Price, currentEntry.AverageFillPrice + bEntryBreakevenSize, false);
  29.                             firstBreakevenHit = true;
  30.                             firstBreakevenPrice = firstEntry.AverageFillPrice + bEntryBreakevenSize;
  31.                         }
  32.                         else if (currentEntry.IsShort && currentProfit >= bEntryBreakeven)
  33.                         {
  34.                             if (!aaaaCompanionMode) Print(currentEntry.Name + ": Breakeven target achieved, moving stop loss to " + (currentEntry.AverageFillPrice - bEntryBreakevenSize).ToString("C"));
  35.                             SetStopLoss(firstEntryName, CalculationMode.Price, currentEntry.AverageFillPrice - bEntryBreakevenSize, false);
  36.                             firstBreakevenHit = true;
  37.                             firstBreakevenPrice = currentEntry.AverageFillPrice - bEntryBreakevenSize;
  38.                         }
  39.  
  40.                     }
  41.  
  42.                     if (cEntryProfit > 0 && !firstProfitHit)
  43.                     {
  44.                         if (cEntryTrailingStopSize > 0)
  45.                         {
  46.                             if (firstPreviousStopPrice == 0 && currentEntry.IsLong && currentProfit >= cEntryProfit)
  47.                             {
  48.                                 Print(currentEntry.Name + ": Profit target achieved, initiating trailing stop loss.");
  49.                                 firstPreviousStopPrice = price - cEntryTrailingStopSize;
  50.                                 SetStopLoss(currentEntry.Name, CalculationMode.Price, firstPreviousStopPrice, false);
  51.                                 firstProfitHit = true;
  52.                             }
  53.                             else if (firstPreviousStopPrice == 0 && currentEntry.IsShort && currentProfit >= cEntryProfit)
  54.                             {
  55.                                 Print(currentEntry.Name + ": Profit target achieved, initiating trailing stop loss.");
  56.                                 firstPreviousStopPrice = price + cEntryTrailingStopSize;
  57.                                 SetStopLoss(currentEntry.Name, CalculationMode.Price, firstPreviousStopPrice, false);
  58.                                 firstProfitHit = true;
  59.                             }
  60.  
  61.                         }
  62.                         else if (cEntryTrailingStopSize == 0 && !firstProfitSet && (firstBreakevenHit || bEntryBreakeven == 0))
  63.                         {
  64.                             if (currentEntry.IsLong && currentProfit >= cEntryProfit)
  65.                             {
  66.                                 ExitLong("Profit Target", currentEntry.Name);
  67.                                 firstProfitSet = true;
  68.                             }
  69.  
  70.                             else if (currentEntry.IsShort && currentProfit >= cEntryProfit)
  71.                             {
  72.                                 ExitShort("Profit Target", currentEntry.Name);
  73.                                 firstProfitSet = true;
  74.                             }
  75.  
  76.                            
  77.                         }
  78.                     }
  79.  
  80. ///This is the specific code that is adjusting the stop loss after our initial profit target has been hit -
  81.                     if (cEntryTrailingStopSize > 0 && firstProfitHit && profitChanged) //if we hit our profit target, enable trailing stop
  82.                     {
  83.                        
  84.                         if (currentEntry.IsLong && price > firstPreviousStopPrice && firstPreviousStopPrice < price - cEntryTrailingStopSize)
  85.                         {
  86.                             firstPreviousStopPrice = price - cEntryTrailingStopSize;
  87.                             SetStopLoss(currentEntry.Name, CalculationMode.Price, firstPreviousStopPrice, false);
  88.                         }
  89.                         else if (currentEntry.IsShort && price < firstPreviousStopPrice && firstPreviousStopPrice > price + cEntryTrailingStopSize)
  90.                         {
  91.                             firstPreviousStopPrice = price + cEntryTrailingStopSize;
  92.                             SetStopLoss(currentEntry.Name, CalculationMode.Price, firstPreviousStopPrice, false);
  93.                         }
  94.                     }
  95.  
  96.                 }
  97.                
  98.                 if (firstEntryFlat)
  99.                 { ///only runs when an entry reports back as completely filled, meaning we have exited our position
  100.                     if (cEntryTrailingStopSize == 0 && !firstTrimPrint && firstProfitHit)
  101.                     {
  102.                         if (!aaaaCompanionMode)
  103.                         {
  104.                             Print("We hit our profit target - Closing trim!");
  105.                         }
  106.  
  107.                         firstTrimPrint = true;
  108.  
  109.  
  110.                     }
  111.  
  112.                     PrintStrategyCompletion(currentEntry.Name);
  113.                 }
  114.  
  115.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement