cBot Development for Trading

Job ID: 38753082

Budget: £10 – £20 GBP

### Key Features of the cBot:

#### **Trade Direction**:
- You can choose whether to allow **Buy**, **Sell**, or **Both** trade directions using the `TradeDirection` parameter.

#### **Position Management**:
- The bot opens a new position **only if no position is currently open** (to avoid multiple simultaneous positions).
- You can choose which configuration, or combination of configurations, the bot uses to trigger an **exit signal**, and the position is closed when the **opposite condition** that triggered the entry occurs, **on candle close**. For example, if a buy trade was opened because the **Chikou Span** closed above the cloud, it will be closed as soon as the **Chikou Span** closes below the cloud. This ensures that trades are exited only after full confirmation **on candle close**.

#### **Entry Configurations**:
- You get to choose which of the following conditions, or combinations of them, the bot will use to trigger **entries and exits**:

1. **Chikou Span vs. Price**:
- The bot checks whether the **Chikou Span** closes **above** or **below** the price (from 26 periods ago). A trade is only taken **after the candle closes**, confirming whether the **Chikou Span** is above or below the price.
- **Exit occurs** when the **Chikou Span** crosses the price in the opposite direction **on candle close**.

2. **Price vs. Cloud**:
- The bot checks whether the **current price** closes **above** or **below** the Ichimoku cloud. The decision is only made **after the candle closes**, confirming whether the price is above or below the cloud.
- **Exit occurs** when the **current price** crosses the cloud boundary in the opposite direction **on candle close**.

3. **Chikou Span vs. Cloud**:
- The bot checks whether the **Chikou Span** closes **above** or **below** the cloud before entering or exiting trades. This is done **only after the candle closes**, confirming whether the **Chikou Span** is above or below the cloud.
- **Exit occurs** when the **Chikou Span** crosses the cloud in the opposite direction **on candle close**.

#### **Position Size and Stop Loss**:

1. **Risk-based Position Sizing**:
- The position size is calculated based on a **fixed losing amount** or a **percentage of equity**. The bot determines how much you are willing to lose on a trade and calculates position size accordingly.
- This ensures that even if the trade hits its stop loss, your loss is kept within the predefined risk.

2. **Stop Loss Based on ATR**:
- The stop loss is determined by the **Average True Range (ATR)**, which measures market volatility.
- The stop loss is placed as a multiple of the ATR value, ensuring it adjusts according to market conditions.

---

### **Summary**:
- All decisions are based on **candle closes**, ensuring that entry and exit conditions are fully confirmed before trades are taken or exited. You can choose which of the three entry configurations — **Chikou Span vs. Price**, **Price vs. Cloud**, and **Chikou Span vs. Cloud** — to apply or combine for your trading strategy. Similarly, you have full control over which configuration or combination of configurations to use as **exit signals**. Trades are exited when the **opposite condition** occurs, ensuring clear exit signals **on candle close**. The bot also implements strong risk management with position sizing based on a predefined loss and stop loss based on ATR.

-------

I ha ve tried to generate the code with chat gpt but when running backtests, the trades open and close completely irrelevant to the rules of the strategy above. Here is the code I get:

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

namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class IchimokuChikouSpanBot : Robot
{
[Parameter("Trade Direction (Buy = 1, Sell = 2, Both = 3)", DefaultValue = 3)]
public int TradeDirection { get; set; }

[Parameter("Risk Type (Fixed = 1, Percentage = 2)", DefaultValue = 1)]
public int RiskType { get; set; }

[Parameter("Risk Amount", DefaultValue = 100)]
public double RiskAmount { get; set; }

[Parameter("Risk Percentage of Equity", DefaultValue = 1, MinValue = 0.1)]
public double RiskPercentage { get; set; }

[Parameter("ATR Period", DefaultValue = 14)]
public int AtrPeriod { get; set; }

[Parameter("ATR Multiplier for Stop Loss", DefaultValue = 2)]
public double AtrMultiplier { get; set; }

[Parameter("Use Chikou Span vs Price for Entry?", DefaultValue = true)]
public bool UseChikouVsPriceEntry { get; set; }

[Parameter("Use Price vs Cloud for Entry?", DefaultValue = true)]
public bool UsePriceVsCloudEntry { get; set; }

[Parameter("Use Chikou Span vs Cloud for Entry?", DefaultValue = true)]
public bool UseChikouVsCloudEntry { get; set; }

[Parameter("Use Chikou Span vs Price for Exit?", DefaultValue = true)]
public bool UseChikouVsPriceExit { get; set; }

[Parameter("Use Price vs Cloud for Exit?", DefaultValue = true)]
public bool UsePriceVsCloudExit { get; set; }

[Parameter("Use Chikou Span vs Cloud for Exit?", DefaultValue = true)]
public bool UseChikouVsCloudExit { get; set; }

private IchimokuKinkoHyo ichimoku;
private AverageTrueRange atr;

protected override void OnStart()
{
ichimoku = Indicators.IchimokuKinkoHyo(9, 26, 52);
atr = Indicators.AverageTrueRange(AtrPeriod, MovingAverageType.Simple);
}

protected override void OnBar()
{
var currentClose = MarketSeries.Close.LastValue;
var chikouIndex = MarketSeries.Close.Count - 26;

if (chikouIndex < 0)
return;

var chikouSpan = MarketSeries.Close[chikouIndex];
var senkouSpanA = ichimoku.SenkouSpanA.Last(0);
var senkouSpanB = ichimoku.SenkouSpanB.Last(0);
var pastPrice = MarketSeries.Close[chikouIndex];

var isChikouAboveCloud = chikouSpan > Math.Max(senkouSpanA, senkouSpanB);
var isChikouBelowCloud = chikouSpan < Math.Min(senkouSpanA, senkouSpanB);

var isPriceAboveCloud = currentClose > Math.Max(senkouSpanA, senkouSpanB);
var isPriceBelowCloud = currentClose < Math.Min(senkouSpanA, senkouSpanB);

var isChikouAbovePrice = chikouSpan > pastPrice;
var isChikouBelowPrice = chikouSpan < pastPrice;

var position = Positions.Find("IchimokuChikouSpanBot");

if (position == null)
{
if (ShouldEnterTrade(true, isChikouAbovePrice, isChikouAboveCloud, isPriceAboveCloud))
{
if (TradeDirection == 1 || TradeDirection == 3)
EnterTrade(TradeType.Buy);
}
else if (ShouldEnterTrade(false, isChikouBelowPrice, isChikouBelowCloud, isPriceBelowCloud))
{
if (TradeDirection == 2 || TradeDirection == 3)
EnterTrade(TradeType.Sell);
}
}
else
{
if (position.TradeType == TradeType.Buy && ShouldExitTrade(true, isChikouBelowPrice, isChikouBelowCloud, isPriceBelowCloud))
{
ClosePosition(position);
}
else if (position.TradeType == TradeType.Sell && ShouldExitTrade(false, isChikouAbovePrice, isChikouAboveCloud, isPriceAboveCloud))
{
ClosePosition(position);
}
}
}

private bool ShouldEnterTrade(bool isBuy, bool chikouVsPrice, bool chikouVsCloud, bool priceVsCloud)
{
return (!UseChikouVsPriceEntry || (isBuy ? chikouVsPrice : !chikouVsPrice)) &&
(!UseChikouVsCloudEntry || (isBuy ? chikouVsCloud : !chikouVsCloud)) &&
(!UsePriceVsCloudEntry || (isBuy ? priceVsCloud : !priceVsCloud));
}

private bool ShouldExitTrade(bool isBuy, bool chikouVsPrice, bool chikouVsCloud, bool priceVsCloud)
{
return (UseChikouVsPriceExit && !(isBuy ? chikouVsPrice : !chikouVsPrice)) ||
(UseChikouVsCloudExit && !(isBuy ? chikouVsCloud : !chikouVsCloud)) ||
(UsePriceVsCloudExit && !(isBuy ? priceVsCloud : !priceVsCloud));
}

private void EnterTrade(TradeType tradeType)
{
var atrValue = atr.Result.LastValue;
var stopLossPips = atrValue * AtrMultiplier;
var riskAmount = RiskType == 1 ? RiskAmount : (Account.Balance * (RiskPercentage / 100));
var volume = CalculateVolume(riskAmount, stopLossPips);

ExecuteMarketOrder(tradeType, SymbolName, volume, "IchimokuChikouSpanBot", stopLossPips, null);
}

private double CalculateVolume(double riskAmount, double stopLossPips)
{
var pipValue = Symbol.PipValue;
var riskPerUnit = stopLossPips * pipValue;
return Symbol.QuantityToVolumeInUnits(riskAmount / riskPerUnit);
}
}
}


Please fix code and ill pay tips as soon as possible :)