Unable to move stoploss by a fixed percentage behind current Ask

Created at 23 Jul 2019, 14:01
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!
TH

therealnakedtrader

Joined 23.07.2019

Unable to move stoploss by a fixed percentage behind current Ask
23 Jul 2019, 14:01


Hi,

New to cAlgo so I have a bit of a problem.

Unable to find a solution for this so, I thought it might be a good idea to try here. 

Here's what I am trying to do: 

After a gain of certain pips, say 10 pips, I would like to move stop loss to just 10% above current Ask for short trades. 

This percentage will change as the pips gained increases. 

Problem code: 

==========================================================================

double distance = (position.EntryPrice - Symbol.Ask);

.....

var  newStopLossPrice =  Symbol.Ask + ((double)10 / 100 * distance));

===========================================================================

Trouble with above code is I get weird output when I Print, such as: "

"1.117780.0004"

For the above, price was at 1.11778 and the percentage gained was 10% of (((double)10 / 100) * distance) is: 0.000401999999999991

I know this looks a bit confusing but any help would be very much appreciated. :) 

Thank you! 

 


@therealnakedtrader
Replies

PanagiotisCharalampous
23 Jul 2019, 14:07

Hi therealnakedtrader,

Thanks for posting in our forum. Can you please post the complete cBot code including the statement used to print? It seems that your print function is just concatenating the two values as strings

Best Regards,

Panagiotis


@PanagiotisCharalampous

therealnakedtrader
23 Jul 2019, 14:25

RE:

Panagiotis Charalampous said:

Hi therealnakedtrader,

Thanks for posting in our forum. Can you please post the complete cBot code including the statement used to print? It seems that your print function is just concatenating the two values as strings

Best Regards,

Panagiotis

Thank for offering to help out, Panagiotis. 

I am afraid the code is very basic and full of gobbledygook.. but here it is: 

=================================================================================================================

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class StandardTrailing : Robot
    {
        // This bot executes standard trailing with a default 2RR trailing; triggers after pips gained is greater than 2 times Stoploss.
        [Parameter(DefaultValue = "Enter PID")]
        public string sPID { get; set; }
        [Parameter(DefaultValue = 2)]
        public int Reward { get; set; }
        [Parameter(DefaultValue = 10)]
        public int TrailByPercent { get; set; }
        public int[] intPID;
        public string stringPositionList;
        protected override void OnStart()
        {
            string[] tempPID = sPID.Split(',');
            intPID = Array.ConvertAll<string, int>(tempPID, int.Parse);
            foreach (var position in Positions)
            {
                if (position.SymbolName == Chart.SymbolName)
                {
                    stringPositionList = stringPositionList + position.Id + ", ";
                }
            }
            Print("Positions: " + stringPositionList);
        }
        protected override void OnTick()
        {
            foreach (var position in Positions)
            {
                bool exists = intPID.Contains(position.Id);
                //if position ID is not in exclusion list, then trail standard
                // for shorts
                if (!exists && position.TradeType == TradeType.Sell && (position.SymbolName == Chart.SymbolName))
                {
                    double distance = (position.EntryPrice - Symbol.Ask);

                    if (distance >= ((position.StopLoss - position.EntryPrice) * Reward))
                    {
                        var newStopLossPrice = Symbol.Ask + ((double)10 / 100 * distance));
                        Print("newStopLossPrice = Symbol.Ask + ((double)10 / 100 * distance));
                        if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
                        {
                             ModifyPosition(position, newStopLossPrice, position.TakeProfit);
                        }
                    }
                }
                //else if ( /* else if PID matches and position is a long */position.Id == cPID && position.TradeType == TradeType.Buy)
                else if (!exists && position.TradeType == TradeType.Buy)       

/// please ignore code for this 'else if'  -- not 
                {
                    var distance = (Symbol.Bid - position.EntryPrice);
                    //   Print("Distance for " + position.Id + " is: " + distance * Symbol.PipSize);
                    // not coded yet 
                }
            }
        }
        protected override void OnStop()
        {
        }
    }
}

==================================================================================================================


@therealnakedtrader

therealnakedtrader
25 Jul 2019, 03:25

BTW, found a solution to this issue. I just separated the print statement from the calculations and it worked just fine. :) Thanks for pointing the issue out, Panagiotis. 

 


@therealnakedtrader