Modify position isn't working

Created at 07 Oct 2024, 07:50
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
SA

samyelzallat

Joined 03.10.2024

Modify position isn't working
07 Oct 2024, 07:50


Hello everyone, 
I will add the code snippets that might affect these modify position signature to not work, please clarify the problem to be, Thanks.

the following three (modifyposition)s don't work

 

    private double GlobalAtrThreshold;
    private double  GlobalAtrPips;
    private bool isTradeActive = false;
    private bool IsItLong = false;
    private bool IsItShort = false;

protected override void OnStart() {

 isTradeActive = false;
      IsItLong = false;
      IsItShort = false;}

 

 protected override void OnBarClosed() {
    
    // Ensure we are not trading when a trade is still active
    if (isTradeActive) {
        return; // Skip this cycle if a trade is already open
    }

 ExecuteMarketOrder(TradeType.Buy, SymbolName, TradeVolume, "Buy Signal",  StopLossAtr, null);
      
        IsItLong = true;
        isTradeActive = true; // Set the flag to true when trade is opened
        
         GlobalAtrThreshold = atr.Result.Last(1) ;
         GlobalAtrPips = Math.Round(atr.Result.Last(1)/Symbol.PipSize,4) ;

 

The execute market order for selling makes IsLtLong boolean ture.

 

  protected override void OnTick() {
      
     // Reset positions if they are closed
     if (Positions.Count == 0) {
        isTradeActive = false;
        IsItLong = false;
        IsItShort = false;
   }
  
   
   
   

     {
    
    
     foreach (var position in Positions) {
    
      if (IsItLong != false && position.Quantity > 0) {
     

        // break even
        if (Symbol.Ask >= position.EntryPrice + (1.0 * GlobalAtrThreshold)) {
          
          position.ModifyStopLossPips(GlobalAtrPips);
        }
        // Close part of the buy position
if (Symbol.Ask >= position.EntryPrice + (1.0 * GlobalAtrThreshold) && position.Quantity > VolumeToClose)
{
    position.ModifyVolume(VolumeToClose);
   
 
}
        // Check for trailing stop loss condition
        if (Symbol.Ask >= position.EntryPrice + (1.5 * GlobalAtrThreshold)) {
          // Set trailing stop loss
          position.ModifyTrailingStop(true);
    
        }
      }

      if (IsItShort != false && position.Quantity > 0) {
        //double atrValueInPips = atr.Result.Last(1) * Symbol.PipSize;

        // Break even
        if (Symbol.Bid <= position.EntryPrice - (1.0 * GlobalAtrThreshold)) {
     
         position.ModifyStopLossPips(GlobalAtrPips);
        }
         // Close part of the buy position
        if (Symbol.Bid <= position.EntryPrice - (1.0 * GlobalAtrThreshold) && position.Quantity > VolumeToClose)
{
    
    position.ModifyVolume(VolumeToClose);
        // trailing stop loss condition
            if (Symbol.Bid <= position.EntryPrice - (1.5 * GlobalAtrThreshold)) {
          // Set trailing stop loss
      position.ModifyTrailingStop(true);
     
        }
      }
    }
    }
      

    }}

 


@samyelzallat
Replies

... Deleted by UFO ...

samyelzallat
08 Oct 2024, 09:19 ( Updated at: 08 Oct 2024, 10:44 )

RE: Modify position isn't working

jeffrey597doss said: 

Hello!

It looks like the modify position functions aren’t working as expected. To address this, ensure that GlobalAtrThreshold and GlobalAtrPips are correctly assigned and verify their values through debugging. Check that isTradeActive, IsItLong, and IsItShort flags are set and reset correctly, reflecting the true state of your positions. In the OnTick method, TheDisneyHub ensure the loop iterates correctly and that all conditions for modifying positions are met. Reviewing these variables and conditions should help pinpoint the issue. Does this help clarify things?

Your reply gave so much insight it actually helped,The problem was the way globalatrthreshold is compared to entry pricce, i fixed it so current price for selling eg. - position.Entryprice> threshold, written like that directly in code seemed to work, thanks a lot and have great day


@samyelzallat