Sell Stop order position closed with stop loss higher than intended
Sell Stop order position closed with stop loss higher than intended
19 Mar 2024, 10:54
I've set the Stop Loss at 40 pips but for some reason there are times where it closes a very high amount to the extend its double the pips I've placed in. Placing the code and an image of what's been happening on backtesting
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Collections.Generic;
namespace cAlgo.indicators
{
[Robot(TimeZone = TimeZones.CentralPacificStandardTime, AccessRights = AccessRights.None)]
public class Chasm : Robot
{
string version_number = "1.0";
// Declarations
private int currenthour;
private int currentminute;
private string stringdate;
private bool is_position_open;
private string scenario;
private double today_open;
private double today_high;
private double today_low;
private double bid_price;
private double ask_price;
private double yesterday_close;
private double yesterday_high;
private double yesterday_low;
private int kindex;
[Parameter("N. Contracts", DefaultValue = 1, MinValue = 0)]
public int ncontracts { get; set; }
// Additional parameters
[Parameter("Order Time (HH:mm)", DefaultValue = "01:28")]
public string orderTime { get; set; }
[Parameter("Order Expiry (Minutes)", DefaultValue = 2, MinValue = 1)]
public double orderTimer { get; set; }
[Parameter("Stop Loss Distance (Pips)", DefaultValue = 15, MinValue = 0, Group = "Buy Order Settings")]
public double BuyStopLossAfterPips { get; set; }
[Parameter("Take Profit (Pips)", DefaultValue = 40, MinValue = 0, Group = "Buy Order Settings")]
public double BuyTakeProfitAfterPips { get; set; }
[Parameter("Stop Loss Distance (Pips)", DefaultValue = 15, MinValue = 0, Group = "Sell Order Settings")]
public double SellStopLossAfterPips { get; set; }
[Parameter("Take Profit (Pips)", DefaultValue = 40, MinValue = 0, Group = "Sell Order Settings")]
public double SellTakeProfitAfterPips { get; set; }
protected override void OnStart()
{
is_position_open = false;
kindex = 0;
Positions.Closed += PositionsOnClosed;
Print("Chasm {0} Started", version_number);
Print("Server time is {0}", Server.Time.AddHours(0));
Timer.Start(60);
}
protected override void OnBar()
{
kindex = Bars.ClosePrices.Count - 1;
today_open = Bars.OpenPrices[kindex];
yesterday_close = Bars.ClosePrices[kindex - 1];
yesterday_high = Bars.HighPrices[kindex - 1];
yesterday_low = Bars.LowPrices[kindex - 1];
if (today_open > yesterday_close) scenario = "GAPUP";
else if (today_open < yesterday_close) scenario = "GAPDOWN";
else scenario = null;
}
protected override void OnTimer()
{
today_high = Bars.HighPrices[kindex];
today_low = Bars.LowPrices[kindex];
// Time Vars
stringdate = Server.Time.ToString("HH:mm");
currenthour = int.Parse(stringdate.Substring(0, 2));
currenthour = Convert.ToInt32(stringdate.Substring(0, 2));
currentminute = int.Parse(stringdate.Substring(3, 2));
currentminute = Convert.ToInt32(stringdate.Substring(3, 2));
bid_price = Symbol.Bid;
ask_price = Symbol.Ask;
if (scenario != null)
{
if (stringdate.Equals(orderTime)) ExecuteOrder();
}
}
private void PositionsOnClosed(PositionClosedEventArgs args)
{
var pos = args.Position;
Print("Position closed with €{0} profit", pos.GrossProfit);
is_position_open = false;
}
private void ExecuteOrder()
{
if (is_position_open) return;
double lastLowPrice = Bars.LowPrices.Last(1);
double lastHighPrice = Bars.HighPrices.Last(1);
DateTime expiry = Server.Time.AddMinutes(orderTimer);
var entryPrice = lastLowPrice;
TradeResult sellResult = PlaceStopOrder(TradeType.Sell, SymbolName, ncontracts, entryPrice, "", SellStopLossAfterPips * Symbol.PipSize,
SellTakeProfitAfterPips * Symbol.PipSize, expiry);
if (sellResult.IsSuccessful) Print("Sell Stop Order placed at: " + entryPrice);
else Print("Failed to place Sell Stop Order: " + sellResult.Error);
entryPrice = lastHighPrice;
TradeResult buyResult = PlaceStopOrder(TradeType.Buy, SymbolName, ncontracts, entryPrice, "", BuyStopLossAfterPips * Symbol.PipSize,
BuyTakeProfitAfterPips * Symbol.PipSize, expiry);
if (buyResult.IsSuccessful) Print("Buy Stop Order placed at: " + entryPrice);
else Print("Failed to place Buy Stop Order: " + buyResult.Error);
is_position_open = true;
}
}
}
Replies
nubfight
25 Mar 2024, 06:15
( Updated at: 25 Mar 2024, 06:43 )
RE: Sell Stop order position closed with stop loss higher than intended
PanagiotisCharalampous said:
Hi there,
You seem to set the stop loss and take profit in relative prices, while they should be set in pips e.g.
PlaceStopOrder(TradeType.Sell, SymbolName, ncontracts, entryPrice, "", SellStopLossAfterPips, SellTakeProfitAfterPips, expiry);
Best regards,
Panagiotis
Hi I forgot to reply to this but I tried using this but it's giving the same results. I also have been setting it in pips rather than relative prices before already
@nubfight
PanagiotisCharalampous
25 Mar 2024, 07:38
RE: RE: Sell Stop order position closed with stop loss higher than intended
nubfight said:
PanagiotisCharalampous said:
Hi there,
You seem to set the stop loss and take profit in relative prices, while they should be set in pips e.g.
PlaceStopOrder(TradeType.Sell, SymbolName, ncontracts, entryPrice, "", SellStopLossAfterPips, SellTakeProfitAfterPips, expiry);
Best regards,
Panagiotis
Hi I forgot to reply to this but I tried using this but it's giving the same results. I also have been setting it in pips rather than relative prices before already
Well what you have provided above is evidently wrong. If you fix the code, try it again and you still have issues, feel free to repost it and we can have a look
@PanagiotisCharalampous
nubfight
25 Mar 2024, 11:46
RE: RE: RE: Sell Stop order position closed with stop loss higher than intended
PanagiotisCharalampous said:
nubfight said:
PanagiotisCharalampous said:
Hi there,
You seem to set the stop loss and take profit in relative prices, while they should be set in pips e.g.
PlaceStopOrder(TradeType.Sell, SymbolName, ncontracts, entryPrice, "", SellStopLossAfterPips, SellTakeProfitAfterPips, expiry);
Best regards,
Panagiotis
Hi I forgot to reply to this but I tried using this but it's giving the same results. I also have been setting it in pips rather than relative prices before already
Well what you have provided above is evidently wrong. If you fix the code, try it again and you still have issues, feel free to repost it and we can have a look
Hi there! I double checked my code and parameters and I don't think I am placing it in their relative prices but pips instead, I'll leave the code below along with the parameters I placed
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Collections.Generic;
namespace cAlgo.indicators
{
[Robot(TimeZone = TimeZones.CentralPacificStandardTime, AccessRights = AccessRights.None)]
public class Chasm : Robot
{
string version_number = "1.0";
// Declarations
private int currenthour;
private int currentminute;
private string stringdate;
private bool is_position_open;
private string scenario;
private double today_open;
private double today_high;
private double today_low;
private double bid_price;
private double ask_price;
private double yesterday_close;
private double yesterday_high;
private double yesterday_low;
private int kindex;
[Parameter("N. Contracts", DefaultValue = 1, MinValue = 0)]
public int ncontracts { get; set; }
// Additional parameters
[Parameter("Order Time (HH:mm)", DefaultValue = "01:28")]
public string orderTime { get; set; }
[Parameter("Order Expiry (Minutes)", DefaultValue = 2, MinValue = 1)]
public double orderTimer { get; set; }
[Parameter("Stop Loss Distance (Pips)", DefaultValue = 15, MinValue = 0, Group = "Buy Order Settings")]
public double BuyStopLossAfterPips { get; set; }
[Parameter("Take Profit (Pips)", DefaultValue = 40, MinValue = 0, Group = "Buy Order Settings")]
public double BuyTakeProfitAfterPips { get; set; }
[Parameter("Stop Loss Distance (Pips)", DefaultValue = 15, MinValue = 0, Group = "Sell Order Settings")]
public double SellStopLossAfterPips { get; set; }
[Parameter("Take Profit (Pips)", DefaultValue = 40, MinValue = 0, Group = "Sell Order Settings")]
public double SellTakeProfitAfterPips { get; set; }
protected override void OnStart()
{
is_position_open = false;
kindex = 0;
Positions.Closed += PositionsOnClosed;
Print("Chasm {0} Started", version_number);
Print("Server time is {0}", Server.Time.AddHours(0));
Timer.Start(60);
}
protected override void OnBar()
{
kindex = Bars.ClosePrices.Count - 1;
today_open = Bars.OpenPrices[kindex];
yesterday_close = Bars.ClosePrices[kindex - 1];
yesterday_high = Bars.HighPrices[kindex - 1];
yesterday_low = Bars.LowPrices[kindex - 1];
if (today_open > yesterday_close) scenario = "GAPUP";
else if (today_open < yesterday_close) scenario = "GAPDOWN";
else scenario = null;
}
protected override void OnTimer()
{
today_high = Bars.HighPrices[kindex];
today_low = Bars.LowPrices[kindex];
// Time Vars
stringdate = Server.Time.ToString("HH:mm");
currenthour = int.Parse(stringdate.Substring(0, 2));
currenthour = Convert.ToInt32(stringdate.Substring(0, 2));
currentminute = int.Parse(stringdate.Substring(3, 2));
currentminute = Convert.ToInt32(stringdate.Substring(3, 2));
bid_price = Symbol.Bid;
ask_price = Symbol.Ask;
if (scenario != null)
{
if (stringdate.Equals(orderTime)) ExecuteOrder();
}
}
protected override void OnStop()
{
Print("Chasm Stopped");
}
private void PositionsOnClosed(PositionClosedEventArgs args)
{
var pos = args.Position;
Print("Position closed with €{0} profit", pos.GrossProfit);
is_position_open = false;
}
private void ExecuteOrder()
{
if (is_position_open) return;
double lastLowPrice = Bars.LowPrices.Last(1);
double lastHighPrice = Bars.HighPrices.Last(1);
DateTime expiry = Server.Time.AddMinutes(orderTimer);
var entryPrice = lastLowPrice;
TradeResult sellResult = PlaceStopOrder(TradeType.Sell, SymbolName, ncontracts, entryPrice, "", SellStopLossAfterPips,
SellTakeProfitAfterPips, expiry);
if (sellResult.IsSuccessful) Print("Sell Stop Order placed at: " + entryPrice);
else Print("Failed to place Sell Stop Order: " + sellResult.Error);
entryPrice = lastHighPrice;
TradeResult buyResult = PlaceStopOrder(TradeType.Buy, SymbolName, ncontracts, entryPrice, "", BuyStopLossAfterPips,
BuyTakeProfitAfterPips, expiry);
if (buyResult.IsSuccessful) Print("Buy Stop Order placed at: " + entryPrice);
else Print("Failed to place Buy Stop Order: " + buyResult.Error);
is_position_open = true;
}
}
}
@nubfight
PanagiotisCharalampous
26 Mar 2024, 06:42
RE: RE: RE: RE: Sell Stop order position closed with stop loss higher than intended
nubfight said:
PanagiotisCharalampous said:
nubfight said:
PanagiotisCharalampous said:
Hi there,
You seem to set the stop loss and take profit in relative prices, while they should be set in pips e.g.
PlaceStopOrder(TradeType.Sell, SymbolName, ncontracts, entryPrice, "", SellStopLossAfterPips, SellTakeProfitAfterPips, expiry);
Best regards,
Panagiotis
Hi I forgot to reply to this but I tried using this but it's giving the same results. I also have been setting it in pips rather than relative prices before already
Well what you have provided above is evidently wrong. If you fix the code, try it again and you still have issues, feel free to repost it and we can have a look
Hi there! I double checked my code and parameters and I don't think I am placing it in their relative prices but pips instead, I'll leave the code below along with the parameters I placed
using System;using System.Linq;using cAlgo.API;using cAlgo.API.Indicators;using cAlgo.API.Internals;using cAlgo.Indicators;using System.Collections.Generic;namespace cAlgo.indicators{ [Robot(TimeZone = TimeZones.CentralPacificStandardTime, AccessRights = AccessRights.None)] public class Chasm : Robot { string version_number = "1.0"; // Declarations private int currenthour; private int currentminute; private string stringdate; private bool is_position_open; private string scenario; private double today_open; private double today_high; private double today_low; private double bid_price; private double ask_price; private double yesterday_close; private double yesterday_high; private double yesterday_low; private int kindex; [Parameter("N. Contracts", DefaultValue = 1, MinValue = 0)] public int ncontracts { get; set; } // Additional parameters [Parameter("Order Time (HH:mm)", DefaultValue = "01:28")] public string orderTime { get; set; } [Parameter("Order Expiry (Minutes)", DefaultValue = 2, MinValue = 1)] public double orderTimer { get; set; } [Parameter("Stop Loss Distance (Pips)", DefaultValue = 15, MinValue = 0, Group = "Buy Order Settings")] public double BuyStopLossAfterPips { get; set; } [Parameter("Take Profit (Pips)", DefaultValue = 40, MinValue = 0, Group = "Buy Order Settings")] public double BuyTakeProfitAfterPips { get; set; } [Parameter("Stop Loss Distance (Pips)", DefaultValue = 15, MinValue = 0, Group = "Sell Order Settings")] public double SellStopLossAfterPips { get; set; } [Parameter("Take Profit (Pips)", DefaultValue = 40, MinValue = 0, Group = "Sell Order Settings")] public double SellTakeProfitAfterPips { get; set; } protected override void OnStart() { is_position_open = false; kindex = 0; Positions.Closed += PositionsOnClosed; Print("Chasm {0} Started", version_number); Print("Server time is {0}", Server.Time.AddHours(0)); Timer.Start(60); } protected override void OnBar() { kindex = Bars.ClosePrices.Count - 1; today_open = Bars.OpenPrices[kindex]; yesterday_close = Bars.ClosePrices[kindex - 1]; yesterday_high = Bars.HighPrices[kindex - 1]; yesterday_low = Bars.LowPrices[kindex - 1]; if (today_open > yesterday_close) scenario = "GAPUP"; else if (today_open < yesterday_close) scenario = "GAPDOWN"; else scenario = null; } protected override void OnTimer() { today_high = Bars.HighPrices[kindex]; today_low = Bars.LowPrices[kindex]; // Time Vars stringdate = Server.Time.ToString("HH:mm"); currenthour = int.Parse(stringdate.Substring(0, 2)); currenthour = Convert.ToInt32(stringdate.Substring(0, 2)); currentminute = int.Parse(stringdate.Substring(3, 2)); currentminute = Convert.ToInt32(stringdate.Substring(3, 2)); bid_price = Symbol.Bid; ask_price = Symbol.Ask; if (scenario != null) { if (stringdate.Equals(orderTime)) ExecuteOrder(); } } protected override void OnStop() { Print("Chasm Stopped"); } private void PositionsOnClosed(PositionClosedEventArgs args) { var pos = args.Position; Print("Position closed with €{0} profit", pos.GrossProfit); is_position_open = false; } private void ExecuteOrder() { if (is_position_open) return; double lastLowPrice = Bars.LowPrices.Last(1); double lastHighPrice = Bars.HighPrices.Last(1); DateTime expiry = Server.Time.AddMinutes(orderTimer); var entryPrice = lastLowPrice; TradeResult sellResult = PlaceStopOrder(TradeType.Sell, SymbolName, ncontracts, entryPrice, "", SellStopLossAfterPips, SellTakeProfitAfterPips, expiry); if (sellResult.IsSuccessful) Print("Sell Stop Order placed at: " + entryPrice); else Print("Failed to place Sell Stop Order: " + sellResult.Error); entryPrice = lastHighPrice; TradeResult buyResult = PlaceStopOrder(TradeType.Buy, SymbolName, ncontracts, entryPrice, "", BuyStopLossAfterPips, BuyTakeProfitAfterPips, expiry); if (buyResult.IsSuccessful) Print("Buy Stop Order placed at: " + entryPrice); else Print("Failed to place Buy Stop Order: " + buyResult.Error); is_position_open = true; } }}
Can you tell us your broker as well?
@PanagiotisCharalampous
nubfight
26 Mar 2024, 06:54
RE: RE: RE: RE: RE: Sell Stop order position closed with stop loss higher than intended
PanagiotisCharalampous said:
nubfight said:
PanagiotisCharalampous said:
nubfight said:
PanagiotisCharalampous said:
Hi there,
You seem to set the stop loss and take profit in relative prices, while they should be set in pips e.g.
PlaceStopOrder(TradeType.Sell, SymbolName, ncontracts, entryPrice, "", SellStopLossAfterPips, SellTakeProfitAfterPips, expiry);
Best regards,
Panagiotis
Hi I forgot to reply to this but I tried using this but it's giving the same results. I also have been setting it in pips rather than relative prices before already
Well what you have provided above is evidently wrong. If you fix the code, try it again and you still have issues, feel free to repost it and we can have a look
Hi there! I double checked my code and parameters and I don't think I am placing it in their relative prices but pips instead, I'll leave the code below along with the parameters I placed
using System;using System.Linq;using cAlgo.API;using cAlgo.API.Indicators;using cAlgo.API.Internals;using cAlgo.Indicators;using System.Collections.Generic;namespace cAlgo.indicators{ [Robot(TimeZone = TimeZones.CentralPacificStandardTime, AccessRights = AccessRights.None)] public class Chasm : Robot { string version_number = "1.0"; // Declarations private int currenthour; private int currentminute; private string stringdate; private bool is_position_open; private string scenario; private double today_open; private double today_high; private double today_low; private double bid_price; private double ask_price; private double yesterday_close; private double yesterday_high; private double yesterday_low; private int kindex; [Parameter("N. Contracts", DefaultValue = 1, MinValue = 0)] public int ncontracts { get; set; } // Additional parameters [Parameter("Order Time (HH:mm)", DefaultValue = "01:28")] public string orderTime { get; set; } [Parameter("Order Expiry (Minutes)", DefaultValue = 2, MinValue = 1)] public double orderTimer { get; set; } [Parameter("Stop Loss Distance (Pips)", DefaultValue = 15, MinValue = 0, Group = "Buy Order Settings")] public double BuyStopLossAfterPips { get; set; } [Parameter("Take Profit (Pips)", DefaultValue = 40, MinValue = 0, Group = "Buy Order Settings")] public double BuyTakeProfitAfterPips { get; set; } [Parameter("Stop Loss Distance (Pips)", DefaultValue = 15, MinValue = 0, Group = "Sell Order Settings")] public double SellStopLossAfterPips { get; set; } [Parameter("Take Profit (Pips)", DefaultValue = 40, MinValue = 0, Group = "Sell Order Settings")] public double SellTakeProfitAfterPips { get; set; } protected override void OnStart() { is_position_open = false; kindex = 0; Positions.Closed += PositionsOnClosed; Print("Chasm {0} Started", version_number); Print("Server time is {0}", Server.Time.AddHours(0)); Timer.Start(60); } protected override void OnBar() { kindex = Bars.ClosePrices.Count - 1; today_open = Bars.OpenPrices[kindex]; yesterday_close = Bars.ClosePrices[kindex - 1]; yesterday_high = Bars.HighPrices[kindex - 1]; yesterday_low = Bars.LowPrices[kindex - 1]; if (today_open > yesterday_close) scenario = "GAPUP"; else if (today_open < yesterday_close) scenario = "GAPDOWN"; else scenario = null; } protected override void OnTimer() { today_high = Bars.HighPrices[kindex]; today_low = Bars.LowPrices[kindex]; // Time Vars stringdate = Server.Time.ToString("HH:mm"); currenthour = int.Parse(stringdate.Substring(0, 2)); currenthour = Convert.ToInt32(stringdate.Substring(0, 2)); currentminute = int.Parse(stringdate.Substring(3, 2)); currentminute = Convert.ToInt32(stringdate.Substring(3, 2)); bid_price = Symbol.Bid; ask_price = Symbol.Ask; if (scenario != null) { if (stringdate.Equals(orderTime)) ExecuteOrder(); } } protected override void OnStop() { Print("Chasm Stopped"); } private void PositionsOnClosed(PositionClosedEventArgs args) { var pos = args.Position; Print("Position closed with €{0} profit", pos.GrossProfit); is_position_open = false; } private void ExecuteOrder() { if (is_position_open) return; double lastLowPrice = Bars.LowPrices.Last(1); double lastHighPrice = Bars.HighPrices.Last(1); DateTime expiry = Server.Time.AddMinutes(orderTimer); var entryPrice = lastLowPrice; TradeResult sellResult = PlaceStopOrder(TradeType.Sell, SymbolName, ncontracts, entryPrice, "", SellStopLossAfterPips, SellTakeProfitAfterPips, expiry); if (sellResult.IsSuccessful) Print("Sell Stop Order placed at: " + entryPrice); else Print("Failed to place Sell Stop Order: " + sellResult.Error); entryPrice = lastHighPrice; TradeResult buyResult = PlaceStopOrder(TradeType.Buy, SymbolName, ncontracts, entryPrice, "", BuyStopLossAfterPips, BuyTakeProfitAfterPips, expiry); if (buyResult.IsSuccessful) Print("Buy Stop Order placed at: " + entryPrice); else Print("Failed to place Buy Stop Order: " + buyResult.Error); is_position_open = true; } }}
Can you tell us your broker as well?
I am currently using Pepperstone at the moment
@nubfight
PanagiotisCharalampous
31 Mar 2024, 09:44
RE: Sell Stop order position closed with stop loss higher than intended
nubfight said:
Don't mean to double post but want to bump this up as I haven't solved the issue yet
It seems you are not using tick data, use tick data for your backtesting instead
@PanagiotisCharalampous
nubfight
01 Apr 2024, 04:50
RE: RE: Sell Stop order position closed with stop loss higher than intended
PanagiotisCharalampous said:
nubfight said:
Don't mean to double post but want to bump this up as I haven't solved the issue yet
It seems you are not using tick data, use tick data for your backtesting instead
Ah okay, does this mean tick data is better for backtesting and for live use we can just go back to using standard data?
@nubfight
PanagiotisCharalampous
01 Apr 2024, 06:36
RE: RE: RE: Sell Stop order position closed with stop loss higher than intended
nubfight said:
PanagiotisCharalampous said:
nubfight said:
Don't mean to double post but want to bump this up as I haven't solved the issue yet
It seems you are not using tick data, use tick data for your backtesting instead
Ah okay, does this mean tick data is better for backtesting and for live use we can just go back to using standard data?
Tick data will always execute your strategy as it would execute on a live environment. Other data sources can only be used when your strategy's logic is executed exclusively in OnBar() method
@PanagiotisCharalampous
PanagiotisCharalampous
20 Mar 2024, 07:01
Hi there,
You seem to set the stop loss and take profit in relative prices, while they should be set in pips e.g.
Best regards,
Panagiotis
@PanagiotisCharalampous