Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
1.6 kB
2
Indexable
Never


```csharp
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace MovingAverageCrossoverBot
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class MovingAverageCrossoverBot : Robot
    {
        [Parameter("Fast MA Periods", DefaultValue = 10)]
        public int FastMAPeriods { get; set; }

        [Parameter("Slow MA Periods", DefaultValue = 20)]
        public int SlowMAPeriods { get; set; }

        private MovingAverage fastMA;
        private MovingAverage slowMA;

        protected override void OnStart()
        {
            fastMA = Indicators.MovingAverage(MarketSeries.Close, FastMAPeriods, MovingAverageType.Exponential);
            slowMA = Indicators.MovingAverage(MarketSeries.Close, SlowMAPeriods, MovingAverageType.Exponential);
        }

        protected override void OnBar()
        {
            if (fastMA.Result.Last(1) > slowMA.Result.Last(1) && fastMA.Result.Last(2) <= slowMA.Result.Last(2))
            {
                // Cross-over detected, place a buy trade
                ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, "Buy Trade", 0, 0, 0, "Buy Trade");
            }
            else if (fastMA.Result.Last(1) < slowMA.Result.Last(1) && fastMA.Result.Last(2) >= slowMA.Result.Last(2))
            {
                // Cross-under detected, place a sell trade
                ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, "Sell Trade", 0, 0, 0, "Sell Trade");
            }
        }
    }
}
```
Leave a Comment