bollinger Band and Supertrend
bollinger Band and Supertrend
29 Jul 2024, 11:09
Hello
I have created the following program with ChatGPT.
It should work on a strategy from TradingView.
Can someone help me and tell me if the program works for the cTrader Algo?
using cAlgo.API; using cAlgo.API.Indicators; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class BollingerSupertrendAlgo : Robot { [Parameter("Bollinger Bands Length", DefaultValue = 20)] public int BBLength { get; set; } [Parameter("Bollinger Bands Multiplier", DefaultValue = 2.0)] public double BBMultiplier { get; set; } [Parameter("Supertrend ATR Length", DefaultValue = 8)] public int ATRLength { get; set; } [Parameter("Supertrend Factor", DefaultValue = 3.0)] public double SupertrendFactor { get; set; } [Parameter("Stop Loss Percent", DefaultValue = 0.009)] public double StopLossPercent { get; set; } [Parameter("Take Profit Percent", DefaultValue = 0.00095)] public double TakeProfitPercent { get; set; } private BollingerBands _bollingerBands; private SuperTrend _supertrend; protected override void OnStart() { _bollingerBands = Indicators.BollingerBands(MarketSeries.Close, BBLength, BBMultiplier, MovingAverageType.Simple); _supertrend = Indicators.SuperTrend(ATRLength, SupertrendFactor); } protected override void OnBar() { var lowerBB = _bollingerBands.Lower.LastValue; var upperBB = _bollingerBands.Upper.LastValue; var close = MarketSeries.Close.LastValue; var direction = _supertrend.UpSeries.LastValue > 0 ? 1 : -1; // Handelslogik if (close > lowerBB && direction > 0) { double stopLossPrice = close * (1 - StopLossPercent); double takeProfitPrice = close * (1 + TakeProfitPercent); double volume = Symbol.VolumeInUnitsMin; // Long Position ExecuteMarketOrder(TradeType.Buy, SymbolName, volume, "Long", stopLossPrice, takeProfitPrice); } else if (close < upperBB && direction < 0) { double stopLossPrice = close * (1 + StopLossPercent); double takeProfitPrice = close * (1 - TakeProfitPercent); double volume = Symbol.VolumeInUnitsMin; // Short Position ExecuteMarketOrder(TradeType.Sell, SymbolName, volume, "Short", stopLossPrice, takeProfitPrice); } } } }
Replies
YesOrNot2
02 Aug 2024, 13:35
Hi, you should know that Chat GPT is really not good for Calgo. It can help you sketch a draft, but you constantly have to go over it afterward.
So, I recommend that after your GPT draft, you do the following:
- Go to the algo section
- Click on your code (new or existing code)
- Once on this page, press Ctrl + E
- In the window that opens, you can write the names of your indicators, and typical examples are displayed directly. For example, they would have helped you understand the errors in your code.
Here is your code :
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class BollingerSupertrendAlgo : Robot
{
[Parameter("Bollinger Bands Length", DefaultValue = 20)]
public int BBLength { get; set; }
[Parameter("Bollinger Bands Multiplier", DefaultValue = 2.0)]
public double BBMultiplier { get; set; }
[Parameter("Supertrend ATR Length", DefaultValue = 8)]
public int ATRLength { get; set; }
[Parameter("Supertrend Factor", DefaultValue = 3.0)]
public double SupertrendFactor { get; set; }
[Parameter("Stop Loss Percent", DefaultValue = 0.009)]
public double StopLossPercent { get; set; }
[Parameter("Take Profit Percent", DefaultValue = 0.00095)]
public double TakeProfitPercent { get; set; }
private BollingerBands _bollingerBands;
private SuperTrend _supertrend;
protected override void OnStart()
{
_bollingerBands = Indicators.BollingerBands(MarketSeries.Close, BBLength, BBMultiplier, MovingAverageType.Simple); _supertrend = Indicators.SuperTrend(ATRLength, SupertrendFactor);
}
protected override void OnBar()
{
var lowerBB = _bollingerBands.Lower.LastValue;
var upperBB = _bollingerBands.Upper.LastValue;
var close = MarketSeries.Close.LastValue;
var direction = _supertrend.UpSeries.LastValue > 0 ? 1 : -1;
//Handelslogik
if (close > lowerBB && direction > 0)
{
double stopLossPrice = close * (1 - StopLossPercent);
double takeProfitPrice = close * (1 + TakeProfitPercent);
double volume = Symbol.VolumeInUnitsMin;
// Long Position
ExecuteMarketOrder(TradeType.Buy, SymbolName, volume, "Long", stopLossPrice, takeProfitPrice);
}
else if (close < upperBB && direction < 0)
{
double stopLossPrice = close * (1 + StopLossPercent);
double takeProfitPrice = close * (1 - TakeProfitPercent);
double volume = Symbol.VolumeInUnitsMin;
// Short Position
ExecuteMarketOrder(TradeType.Sell, SymbolName, volume, "Short", stopLossPrice, takeProfitPrice);
}
}
}
}
- SuperTrend is underlined, so it’s an error.
- _bollingerBands.Lower.LastValue, so it’s an error.
- _bollingerBands.Lower.Upper, so it’s an error.
Here is the perfect road to debug your simple GPT programme:
- Automate section
- click on your code or new button
- ctrl + e
- In the window that opens in the search section, type Supertrend and click on it.
Here is what we can find :
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
// This sample cBot shows how to use the Supertrend indicator
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class SupertrendSample : Robot
{
private double _volumeInUnits;
private Supertrend _supertrend;
[Parameter("Volume (Lots)", DefaultValue = 0.01)]
public double VolumeInLots { get; set; }
[Parameter("Stop Loss (Pips)", DefaultValue = 10)]
public double StopLossInPips { get; set; }
[Parameter("Take Profit (Pips)", DefaultValue = 10)]
public double TakeProfitInPips { get; set; }
[Parameter("Label", DefaultValue = "Sample")]
public string Label { get; set; }
public Position[] BotPositions
{
get
{
return Positions.FindAll(Label);
}
}
protected override void OnStart()
{
_volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
_supertrend = Indicators.Supertrend(10, 3);
}
protected override void OnBar()
{
if (_supertrend.UpTrend.Last(1) < Bars.LowPrices.Last(1) && _supertrend.DownTrend.Last(2) > Bars.HighPrices.Last(2))
{
ClosePositions(TradeType.Sell);
ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
}
else if (_supertrend.DownTrend.Last(1) > Bars.HighPrices.Last(1) && _supertrend.UpTrend.Last(2) < Bars.LowPrices.Last(2))
{
ClosePositions(TradeType.Buy);
ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
}
}
private void ClosePositions(TradeType tradeType)
{
foreach (var position in BotPositions)
{
if (position.TradeType != tradeType) continue;
ClosePosition(position);
}
}
}
}
- SUPERTREND: You wrote "SuperTrend". Calgo needs it to be written as "Supertrend".
5. In the window that opens in the search section, type Bollinger Bands and click on it..
Here is what we can find :
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
///
/// This sample cBot shows how to use the Bollinger Bands indicator
///
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class BollingerBandsSample : Robot
{
private double _volumeInUnits;
private BollingerBands _bollingerBands;
[Parameter("Volume (Lots)", DefaultValue = 0.01)]
public double VolumeInLots { get; set; }
[Parameter("Label", DefaultValue = "Sample")]
public string Label { get; set; }
[Parameter("Source")]
public DataSeries Source { get; set; }
public Position[] BotPositions
{
get
{
return Positions.FindAll(Label);
}
}
protected override void OnStart()
{
_volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
_bollingerBands = Indicators.BollingerBands(Source, 14, 2, MovingAverageType.Exponential);
}
protected override void OnBar()
{
if (Bars.LowPrices.Last(1) <= _bollingerBands.Bottom.Last(1) && Bars.LowPrices.Last(2) > _bollingerBands.Bottom.Last(2))
{
ClosePositions(TradeType.Sell);
ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label);
}
else if (Bars.HighPrices.Last(1) >= _bollingerBands.Top.Last(1) && Bars.HighPrices.Last(2) < _bollingerBands.Top.Last(2))
{
ClosePositions(TradeType.Buy);
ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, Label);
}
}
private void ClosePositions(TradeType tradeType)
{
foreach (var position in BotPositions)
{
if (position.TradeType != tradeType) continue;
ClosePosition(position);
}
}
}
}
BOLLINGER BANDS:
You wrote "bollingerBands.Lower". Calgo needs it to be written as "bollingerBands.Bottom".
You wrote "bollingerBands.Upper". Calgo needs it to be written as "bollingerBands.Top".
Now that you have the method to debug very simple code of GPT for next time.
Here is your wanted code :
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class BollingerSupertrendAlgo : Robot
{
[Parameter("Bollinger Bands Length", DefaultValue = 20)]
public int BBLength { get; set; }
[Parameter("Bollinger Bands Multiplier", DefaultValue = 2.0)]
public double BBMultiplier { get; set; }
[Parameter("Supertrend ATR Length", DefaultValue = 8)]
public int ATRLength { get; set; }
[Parameter("Supertrend Factor", DefaultValue = 3.0)]
public double SupertrendFactor { get; set; }
[Parameter("Stop Loss Percent", DefaultValue = 0.009)]
public double StopLossPercent { get; set; }
[Parameter("Take Profit Percent", DefaultValue = 0.00095)]
public double TakeProfitPercent { get; set; }
private BollingerBands _bollingerBands;
private Supertrend _supertrend;
protected override void OnStart()
{
_bollingerBands = Indicators.BollingerBands(MarketSeries.Close, BBLength, BBMultiplier, MovingAverageType.Simple);
_supertrend = Indicators.Supertrend(ATRLength, SupertrendFactor);
}
protected override void OnBar()
{
var lowerBB = _bollingerBands.Bottom.LastValue;
var upperBB = _bollingerBands.Top.LastValue;
var close = MarketSeries.Close.LastValue;
var direction = _supertrend.UpTrend.LastValue > 0 ? 1 : -1;
//Handelslogik
if (close > lowerBB && direction > 0)
{
double stopLossPrice = close * (1 - StopLossPercent);
double takeProfitPrice = close * (1 + TakeProfitPercent);
double volume = Symbol.VolumeInUnitsMin;
// Long Position
ExecuteMarketOrder(TradeType.Buy, SymbolName, volume, "Long", stopLossPrice, takeProfitPrice);
}
else if (close < upperBB && direction < 0)
{
double stopLossPrice = close * (1 + StopLossPercent);
double takeProfitPrice = close * (1 - TakeProfitPercent);
double volume = Symbol.VolumeInUnitsMin;
// Short Position
ExecuteMarketOrder(TradeType.Sell, SymbolName, volume, "Short", stopLossPrice, takeProfitPrice);
}
}
}
}
You welcome. =)
@YesOrNot2
PanagiotisCharalampous
30 Jul 2024, 05:50
Hi there,
Did you try it yourself? Does it not work? What do yo expect it to do?
Best regards,
Panagiotis
@PanagiotisCharalampous