technical error
Created at 07 May 2024, 18:18
technical error
07 May 2024, 18:18
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class BestTradingBot : Robot
{
private RelativeStrengthIndex rsi;
private BollingerBands bollingerBands;
private MovingAverage ma;
[Parameter("Risk Percentage", DefaultValue = 2)]
public double RiskPercentage { get; set; }
[Parameter("Stop Loss (pips)", DefaultValue = 50)]
public double StopLoss { get; set; }
[Parameter("Take Profit (pips)", DefaultValue = 100)]
public double TakeProfit { get; set; }
[Parameter("Fixed Lot Size", DefaultValue = 0.1)]
public double FixedLotSize { get; set; }
[Parameter("RSI Periods", DefaultValue = 14)]
public int RsiPeriods { get; set; }
[Parameter("Bollinger Bands Periods", DefaultValue = 20)]
public int BollingerBandsPeriods { get; set; }
[Parameter("Bollinger Bands Deviations", DefaultValue = 2)]
public double BollingerBandsDeviations { get; set; }
[Parameter("MA Periods", DefaultValue = 100)]
public int MaPeriods { get; set; }
private double VolumeInUnits;
protected override void OnStart()
{
rsi = Indicators.RelativeStrengthIndex(MarketSeries.Close, RsiPeriods);
bollingerBands = Indicators.BollingerBands(MarketSeries.Close, BollingerBandsPeriods, BollingerBandsDeviations, MovingAverageType.Simple);
ma = Indicators.MovingAverage(MarketSeries.Close, MaPeriods, MovingAverageType.Simple);
CalculateVolume();
}
private void CalculateVolume()
{
if (FixedLotSize > 0)
{
VolumeInUnits = FixedLotSize;
}
else
{
double riskAmount = Account.Balance * RiskPercentage / 100;
double onePipValue = Symbol.PipValue / Symbol.PipSize;
double stopLossAmount = StopLoss * onePipValue;
double takeProfitAmount = TakeProfit * onePipValue;
VolumeInUnits = riskAmount / stopLossAmount;
// Consider additional adjustments if calculated volume is not valid
}
}
protected override void OnBar()
{
double buyScore = CalculateBuyScore();
double sellScore = CalculateSellScore();
if (buyScore > sellScore)
{
ClosePosition(TradeType.Sell);
ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, "Buy Trade", StopLoss, TakeProfit);
}
else if (sellScore > buyScore)
{
ClosePosition(TradeType.Buy);
ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, "Sell Trade", StopLoss, TakeProfit);
}
}
private void ClosePosition(TradeType tradeType)
{
foreach (var position in Positions)
{
if (position.TradeType == tradeType)
{
var positionLabel = position.Label;
var takeProfit = position.TakeProfit ?? 0;
ClosePosition(position);
Print($"Closed {tradeType} position with take profit: {takeProfit}");
}
}
}
private double CalculateBuyScore()
{
double rsiScore = rsi.Result.LastValue;
double bbScore = MarketSeries.Close.Last(0) - bollingerBands.Bottom.Last(0);
double maScore = MarketSeries.Close.Last(0) - ma.Result.Last(0);
// Normalize scores (optional)
rsiScore /= 100;
bbScore /= Symbol.PipSize;
maScore /= Symbol.PipSize;
// Define weights
double rsiWeight = 0.4;
double bbWeight = 0.3;
double maWeight = 0.3;
// Calculate total score
return (rsiScore * rsiWeight) + (bbScore * bbWeight) + (maScore * maWeight);
}
private double CalculateSellScore()
{
// Similar to CalculateBuyScore(), but for sell positions
double rsiScore = 100 - rsi.Result.LastValue;
double bbScore = bollingerBands.Top.Last(0) - MarketSeries.Close.Last(0);
double maScore = ma.Result.Last(0) - MarketSeries.Close.Last(0);
// Normalize scores (optional)
rsiScore /= 100;
bbScore /= Symbol.PipSize;
maScore /= Symbol.PipSize;
// Define weights
double rsiWeight = 0.4;
double bbWeight = 0.3;
double maWeight = 0.3;
// Calculate total score
return (rsiScore * rsiWeight) + (bbScore * bbWeight) + (maScore * maWeight);
}
}
}
firemyst
18 Jul 2024, 02:47 ( Updated at: 18 Jul 2024, 05:55 )
See this thread:
https://ctrader.com/forum/cbot-support/43666/
@firemyst