Stop Loss Calculation
Stop Loss Calculation
16 Oct 2024, 19:30
Hello,
I have broken this down as I working on a bigger strategy but I can't seem to get the Stop Loss right so it should be 10 pips above upper band and 10 pips below lower band. Any ideas what I'm doing wrong?
Thanks
Ross
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using System;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class BollingerBandBot : Robot
{
private BollingerBands _bollingerBands;
private double _pipSize;
[Parameter("Period", DefaultValue = 20)]
public int Period { get; set; }
[Parameter("Deviation", DefaultValue = 2.0)]
public double Deviation { get; set; }
[Parameter("Volume", DefaultValue = 10000)]
public int Volume { get; set; }
[Parameter("Stop Loss Pips", DefaultValue = 10)]
public int StopLossPips { get; set; }
protected override void OnStart()
{
// Initialize Bollinger Bands using Bars.ClosePrices instead of MarketSeries.Close
_bollingerBands = Indicators.BollingerBands(Bars.ClosePrices, Period, Deviation, MovingAverageType.Simple);
_pipSize = Symbol.PipSize;
}
protected override void OnBar()
{
// Access the Bollinger Band values
double lowerBand = _bollingerBands.Bottom.LastValue;
double middleBand = _bollingerBands.Main.LastValue;
double upperBand = _bollingerBands.Top.LastValue;
double lastPrice = Bars.ClosePrices.LastValue;
// Buy logic: Price hits lower band, TP at middle band
if (lastPrice <= lowerBand && Positions.Find("BollingerBuy") == null)
{
// Correct stop loss for buy: 10 pips below lower band
double stopLoss = lowerBand - (StopLossPips * Symbol.PipSize);
double takeProfit = middleBand;
// Updated ExecuteMarketOrder with string symbol name
ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, "BollingerBuy", stopLoss, takeProfit);
Print("Buy Order Opened. Lower Band: {0}, Middle Band: {1}, Upper Band: {2}", lowerBand, middleBand, upperBand);
}
// Sell logic: Price hits upper band, TP at middle band
if (lastPrice >= upperBand && Positions.Find("BollingerSell") == null)
{
// Correct stop loss for sell: 10 pips above upper band
double stopLoss = upperBand + (StopLossPips * Symbol.PipSize);
double takeProfit = middleBand;
// Updated ExecuteMarketOrder with string symbol name
ExecuteMarketOrder(TradeType.Sell, SymbolName, Volume, "BollingerSell", stopLoss, takeProfit);
Print("Sell Order Opened. Upper Band: {0}, Middle Band: {1}, Lower Band: {2}", upperBand, middleBand, lowerBand);
}
}
protected override void OnStop()
{
// Handle any cleanup when stopping the bot
}
}
}
Replies
rosscortb
18 Oct 2024, 09:55
RE: Stop Loss Calculation
firemyst said:
Depends on what you want.
You're getting the LastValue, which is value as of the moment it's taken in the current bar. As you know, values for the current bar can change depending on what the price does.
So at the beginning of the bar for example, the top band could be one value, but then if price really skyrockets and the band expands, the top band could end up being another value by the close of the bar.
If that's what you want, fine.
Perhaps what you want is the value of the previous bar when price closed? With Bollinger Bands, that shouldn't change. So instead of getting the .LastValue, you need to get .Last(1).
@firemyst
Thanks for your reply.
This never really worked me, been struggling with this for a while. When back testing I would expect to see a stop loss line or Take profit line above or below the bands. No matter what pip level I set the variable to just stays the same.
@rosscortb
PanagiotisCharalampous
18 Oct 2024, 10:41
RE: RE: Stop Loss Calculation
rosscortb said:
firemyst said:
Depends on what you want.
You're getting the LastValue, which is value as of the moment it's taken in the current bar. As you know, values for the current bar can change depending on what the price does.
So at the beginning of the bar for example, the top band could be one value, but then if price really skyrockets and the band expands, the top band could end up being another value by the close of the bar.
If that's what you want, fine.
Perhaps what you want is the value of the previous bar when price closed? With Bollinger Bands, that shouldn't change. So instead of getting the .LastValue, you need to get .Last(1).
@firemyst
Thanks for your reply.This never really worked me, been struggling with this for a while. When back testing I would expect to see a stop loss line or Take profit line above or below the bands. No matter what pip level I set the variable to just stays the same.
It seems that you are using absolute prices as stop losses. You need to convert them to pips.
@PanagiotisCharalampous
firemyst
17 Oct 2024, 01:40 ( Updated at: 17 Oct 2024, 04:57 )
Depends on what you want.
You're getting the LastValue, which is value as of the moment it's taken in the current bar. As you know, values for the current bar can change depending on what the price does.
So at the beginning of the bar for example, the top band could be one value, but then if price really skyrockets and the band expands, the top band could end up being another value by the close of the bar.
If that's what you want, fine.
Perhaps what you want is the value of the previous bar when price closed? With Bollinger Bands, that shouldn't change. So instead of getting the .LastValue, you need to get .Last(1).
@firemyst