can't figure out why does not open trades
can't figure out why does not open trades
07 Feb 2021, 01:04
cant figure out why does not open trades, the bot is still a workn in progress , need also to make pairs selectables
using System;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class MultisymbolBacktesting : Robot
{
[Parameter("EURUSD", DefaultValue = true)]
public bool EURUSD { get; set; }
[Parameter("GBPUSD", DefaultValue = true)]
public bool GBPUSD { get; set; }
[Parameter("USDJPY", DefaultValue = true)]
public bool USDJPY { get; set; }
[Parameter("USDCHF", DefaultValue = true)]
public bool USDCHF { get; set; }
[Parameter("SSL Periods", DefaultValue = 20)]
public int SSLPer { get; set; }
[Parameter("SSL MA", DefaultValue = MovingAverageType.Simple)]
public MovingAverageType SSLMa { get; set; }
[Parameter("ATR multiply", DefaultValue = 1.5)]
public double ATRM { get; set; }
[Parameter("Risk ", DefaultValue = 1)]
public double Risk { get; set; }
[Parameter("SMA period ", DefaultValue = 50)]
public int maPeriod { get; set; }
[Parameter(DefaultValue = 0.0)]
public double Parameter { get; set; }
public Symbol[] MySymbols;
private SSLChannel ssl;
private AverageTrueRange atr;
private SimpleMovingAverage sma;
protected override void OnStart()
{
Risk = Risk / 100;
// We get a symbol array for the following for symbols
MySymbols = Symbols.GetSymbols("EURUSD", "GBPUSD", "USDJPY", "USDCHF");
// We subscribe to the tick event for each symbol
foreach (var symbol in MySymbols)
{
symbol.Tick += Symbol_Tick;
}
// We subscribe to the bar event for each symbol for the current timeframe
foreach (var symbol in MySymbols)
{
var bars = MarketData.GetBars(TimeFrame, symbol.Name);
bars.BarOpened += Bars_BarOpened;
}
}
protected override void OnTick()
{
ssl = Indicators.GetIndicator<SSLChannel>(SSLPer, SSLMa);
//stoc = Indicators.StochasticOscillator(K, KS, D, StocMa);
atr = Indicators.AverageTrueRange(20, MovingAverageType.Exponential);
sma = Indicators.SimpleMovingAverage(Bars.ClosePrices, maPeriod);
}
private void Bars_BarOpened(BarOpenedEventArgs obj)
{
var up3 = Bars.ClosePrices.Last(0) > Bars.ClosePrices.Last(1) && Bars.ClosePrices.Last(1) > Bars.ClosePrices.Last(2);
//&& Bars.ClosePrices.Last(2) > Bars.ClosePrices.Last(3);
var down3 = Bars.ClosePrices.Last(0) < Bars.ClosePrices.Last(1) && Bars.ClosePrices.Last(1) < Bars.ClosePrices.Last(2);
// && Bars.ClosePrices.Last(2) < Bars.ClosePrices.Last(3);
var smaLast = sma.Result.Last(0);
var position = Positions.Find("");
var price = Bars.ClosePrices.Last(0);
var stopPips = Math.Round(atr.Result.Last(0) / Symbol.PipSize);
var TakeProfitPips = (2 * stopPips);
var tradeAmount = (Account.Equity * Risk) / (ATRM * stopPips * Symbol.PipValue);
tradeAmount = Symbol.NormalizeVolumeInUnits(tradeAmount, RoundingMode.Down);
var sslUp = ssl._sslUp;
var sslDown = ssl._sslDown;
//------------------------------------buy
//When a bar opens we check the previous bar. If the bar is bullish, we send a buy order
if ((sslUp.HasCrossedAbove(sslDown, 1)) & up3)
{
ExecuteMarketOrder(TradeType.Buy, obj.Bars.SymbolName, tradeAmount, "", TakeProfitPips, stopPips, null, true);
// When a bar opens we check the previous X bar for anoter buy
if (up3)
{
ExecuteMarketOrder(TradeType.Buy, obj.Bars.SymbolName, tradeAmount, "", TakeProfitPips, stopPips, null, true);
}
}
//------------------------------------sell
//When a bar opens we check the previous bar. If the bar is bearish, we send a sell order
if (sslDown.HasCrossedAbove(sslUp, 1) & down3)
{
ExecuteMarketOrder(TradeType.Sell, obj.Bars.SymbolName, tradeAmount, "", TakeProfitPips, stopPips, null, true);
// When a bar opens we check the previous X bar for another sell
if (down3)
{
ExecuteMarketOrder(TradeType.Sell, obj.Bars.SymbolName, tradeAmount, "", TakeProfitPips, stopPips, null, true);
}
}
}
private void Symbol_Tick(SymbolTickEventArgs obj)
{
// On each symbol tick, we print the symbol's bid/ask prices on the chart
var sb = new StringBuilder();
foreach (var symbol in MySymbols)
{
var textLine = string.Format("{0} {1} {2}", symbol.Name, symbol.Bid, symbol.Ask);
sb.AppendLine(textLine);
}
Chart.DrawStaticText("symbols", sb.ToString(), VerticalAlignment.Top, HorizontalAlignment.Left, Chart.ColorSettings.ForegroundColor);
}
private void PositionsOnClosed(PositionClosedEventArgs args)
{
if (args.Reason == PositionCloseReason.TakeProfit)
{
var position = Positions.Find("");
ModifyPosition(position, position.EntryPrice, null);
}
}
/* protected override void OnTick()
{
// Put your core logic here
}*/
protected override void OnBar()
{
}
/* var smaLast = sma.Result.Last(0);
if (position != null)
{
if (position.TradeType == TradeType.Buy & price > smaLast)
ModifyPosition(position, sma, position.TakeProfit, true);
else if (position.TradeType == TradeType.Sell & price < smaLast)
ModifyPosition(position, sma, position.TakeProfit, true);
}
*/
protected override void OnStop()
{
// Put your deinitialization logic here
}
}
}
firemyst
09 Feb 2021, 04:10
In your OnTick or SymbolTick methods, it doesn't look like you're getting any values for the indicators.
In OnTick, with every tick, you're getting the actual indicator itself over and over and over again (waste of CPU and memory resources), but not the values.
The reason' it's done as it is above is because cTrader does "lazy" loading of indicator values. That is, it doesn't load the values unless explicitly called. Hence why the safe way to get the values is to follow the pattern as shown above.
@firemyst