HO
Information
Username: | Host |
Member since: | 04 Dec 2022 |
Last login: | 15 May 2023 |
Status: | Active |
Activity
Where | Created | Comments |
---|---|---|
Algorithms | 0 | 3 |
Forum Topics | 4 | 3 |
Jobs | 0 | 0 |
Last Algorithm Comments
HO
I am also learning. I started a few days ago, but I'm very focused.
If you don't mind, I made some changes, like the organization in the code structure, which is something important, and the option to decide how many operations can be performed in a row.
But, answering your question, I believe that what you did wrong is having mentioned Default==Sell and Default==Buy. In this case, it is only necessary to mention only the variant, which, if true, will follow the code.
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class MACDMarketTimerV2 : Robot
{
[Parameter("Label", Group = "Label", DefaultValue = " MACDMarketTimerV2,RSI,MACD")]
public string Label { get; set; }
[Parameter("Sentiment: Buy", Group = "Basic Setup", DefaultValue = true)]
public bool Buy { get; set; }
[Parameter("Sentiment: Sell", Group = "Basic Setup", DefaultValue = true)]
public bool Sell { get; set; }
[Parameter("Max Trades", Group = "Basic Setup", DefaultValue = 1, MinValue = 1)]
public int TradeCount { get; set; }
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }
[Parameter("Stop Loss ", Group = "Protection", DefaultValue = 100)]
public int StopLoss { get; set; }
[Parameter("Take Profit", Group = "Protection", DefaultValue = 100)]
public int TakeProfit { get; set; }
[Parameter("Start Hour", Group = "Market Time", DefaultValue = 10.0)]
public double StartTime { get; set; }
[Parameter("Stop Hour", Group = "Market Time", DefaultValue = 12.0)]
public double StopTime { get; set; }
[Parameter("MME Slow", Group = "MA", DefaultValue = 16)]
public int MmeSlow { get; set; }
[Parameter("MME Fast", Group = "MA", DefaultValue = 12)]
public int MmeFast { get; set; }
[Parameter("Source", Group = "RSI")]
public DataSeries Source { get; set; }
[Parameter("Periods", Group = "RSI", DefaultValue = 19)]
public int Periods { get; set; }
[Parameter(" Period", Group="MACD",DefaultValue = 9)]
public int Period { get; set; }
[Parameter(" Long Cycle",Group="MACD", DefaultValue = 26)]
public int LongCycle { get; set; }
[Parameter(" Short Cycle",Group="MACD", DefaultValue = 12)]
public int ShortCycle { get; set; }
private MovingAverage i_MA_slow;
private MovingAverage i_MA_fast;
private RelativeStrengthIndex rsi;
private DateTime Starttime;
private DateTime Stoptime;
private MacdCrossOver macd;
private double volumeInUnits;
protected override void OnStart()
{
i_MA_slow = Indicators.MovingAverage(Bars.ClosePrices, MmeSlow, MovingAverageType.Exponential);
i_MA_fast = Indicators.MovingAverage(Bars.ClosePrices, MmeFast, MovingAverageType.Exponential);
rsi = Indicators.RelativeStrengthIndex(Source, Periods);
macd=Indicators.MacdCrossOver(LongCycle, ShortCycle, Period);
volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
Starttime = Server.Time.Date.AddHours(StartTime);
Stoptime = Server.Time.Date.AddHours(StopTime);
}
protected override void OnBar()
{
if (!MarketTime())
return;
if (Positions.FindAll(Label, SymbolName).Length <= TradeCount)
{
SendTrade();
}
}
private void SendTrade()
{
var MACDLine = macd.MACD.Last(1);
var PrevMACDLine = macd.MACD.Last(2);
var Signal = macd.Signal.Last(1);
var PrevSignal = macd.Signal.Last(2);
if (rsi.Result.LastValue > 25 && rsi.Result.LastValue < 70)
{
if (MACDLine > Signal && PrevMACDLine < PrevSignal && Buy)
{
if (i_MA_fast.Result.LastValue > i_MA_slow.Result.LastValue)
{
ExecuteMarketOrder(TradeType.Buy, SymbolName, volumeInUnits, Label, StopLoss, TakeProfit);
}
}
else if (MACDLine < Signal && PrevMACDLine > PrevSignal && Sell)
{
if (i_MA_fast.Result.LastValue < i_MA_slow.Result.LastValue)
{
ExecuteMarketOrder(TradeType.Sell, SymbolName, volumeInUnits, Label, StopLoss, TakeProfit);
}
}
}
}
private bool MarketTime()
{
var CorrentHour = Server.Time.TimeOfDay.TotalHours;
if (CorrentHour > StartTime && CorrentHour < StopTime)
return true;
else
return false;
}
protected override void OnError(Error result)
{
if (result.Code == ErrorCode.NoMoney)
Stop();
}
}
}