Hi there...
I have developed my own trading strategies and written them into ctrader with relative success. The area I am struggling with is to make my bot get indicator info on multiple symbols to then trade that symbol. Rather than having one cbot per symbol.
I thoughts Id start with an example bot to understand how to pull data from 4 SMA's as per the example below. However, "_sma = Indicators.SimpleMovingAverage(MarketData.GetSymbol("GBPUSD"), 9);" does not do as I thought it might?
Can anyone help me work out how to code this correctly please?
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using System.Linq;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class NewcBot : Robot
{
private MovingAverage _sma;
protected override void OnStart()
{
_sma = Indicators.SimpleMovingAverage(MarketData.GetSymbol("GBPUSD"), 9);
_sma = Indicators.SimpleMovingAverage(MarketData.GetSymbol("EURJPY"), 9);
_sma = Indicators.SimpleMovingAverage(MarketData.GetSymbol("AUDCHF"), 9);
_sma = Indicators.SimpleMovingAverage(MarketData.GetSymbol("NZDCAD"), 9);
}
protected override void OnBar()
{
foreach (var position in Positions)
{
if (position.SymbolName == "GBPUSD" || position.SymbolName == "EURJPY" || position.SymbolName == "AUDCHF" || position.SymbolName == "NZDCAD")
{
if (position.TradeType == TradeType.Buy && _sma.Result.Last(0) < position.EntryPrice)
{
ClosePosition(position);
}
else if (position.TradeType == TradeType.Sell && _sma.Result.Last(0) > position.EntryPrice)
{
ClosePosition(position);
}
}
}
if (_sma.Result.Last(0) > _sma.Result.Last(1))
{
foreach (var symbol in MarketData.GetSymbols())
{
if (symbol.Name == "GBPUSD" || symbol.Name == "EURJPY" || symbol.Name == "AUDCHF" || symbol.Name == "NZDCAD")
{
if (!Positions.Any(p => p.SymbolName == symbol.Name))
{
ExecuteMarketOrder(TradeType.Buy, symbol.Name, 10000, "New Position", null, null);
}
}
}
}
else if (_sma.Result.Last(0) < _sma.Result.Last(1))
{
foreach (var symbol in MarketData.GetSymbols())
{
if (symbol.Name == "GBPUSD" || symbol.Name == "EURJPY" || symbol.Name == "AUDCHF" || symbol.Name == "NZDCAD")
{
if (!Positions.Any(p => p.SymbolName == symbol.Name))
{
ExecuteMarketOrder(TradeType.Sell, symbol.Name, 10000, "New Position", null, null);
}
}
}
}
}
}
}