Topics
Replies
alpha_austin
02 Aug 2017, 13:53
Is that able to add random trade?
I wanna ask, if not using RSI and if I wanna open a random market order,
and at the smae time I wanna place the mentioned limit orders OF THE SAME DIRECTION OF THE MARKET ORDER,
then once either trade TP (or gross profit > 0), return to the initial position of opening a new random market order.
What coding do I need to add?
Many thanks!
using System;
using System.Linq;
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 Random_with_LimitOrders : Robot
{
[Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
public int InitialVolume { get; set; }
[Parameter("Stop Loss", DefaultValue = 10)]
public int StopLoss { get; set; }
[Parameter("Take Profit", DefaultValue = 10)]
public int TakeProfit { get; set; }
private Random random = new Random();
protected override void OnStart()
{
Positions.Closed += OnPositionsClosed;
ExecuteOrder(InitialVolume, GetRandomTradeType());
}
private void ExecuteOrder(long volume, TradeType tradeType)
{
var result = ExecuteMarketOrderAsync(tradeType, Symbol, volume, "Random_Trade", StopLoss, TakeProfit);
////////////////////here, how to place limit order the same trade type of the above market order 10pips away from it?////////////////////
////////like this//////// PlaceLimitOrder(TradeType, Symbol, 10000, Symbol.Bid + 10 * Symbol.PipSize, "Sell + 10", 30, 10);
PlaceLimitOrder(TradeType.Sell, Symbol, 20000, Symbol.Bid + 20 * Symbol.PipSize, "Sell + 20", 20, 10);
PlaceLimitOrder(TradeType.Sell, Symbol, 40000, Symbol.Bid + 30 * Symbol.PipSize, "Sell + 30", 10, 10);
}
if (result.Error == ErrorCode.NoMoney)
Stop();
}
private void OnPositionsClosed(PositionClosedEventArgs args)
{
Print("Closed");
var position = args.Position;
if (position.Label != "Random_Trade" || position.SymbolCode != Symbol.Code)
return;
////////////////////////////////how to write this part////////////////////////////////
if either position TP, close all trades including all pending orders and return to opening a new random market order
/////////////////////////////////////////////////////////////////////////////////////////////////
}
private TradeType GetRandomTradeType()
{
return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell;
}
}
}
@alpha_austin
alpha_austin
02 Aug 2017, 10:38
Adding more on the codes
Thanks Spotware Support, the codes show Built successful in cAlgo.
Moreover, I wanna know how to add coding if I wanna close all trades once either position hit TP?
And then return to open a new trade immediately after that?
Thanks again for the help.
@alpha_austin
alpha_austin
01 Aug 2017, 11:38
Can Spotware Team or someone correct my coding, many thanks.
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class SampleRSIcBot : Robot
{
[Parameter("Source")]
public DataSeries Source { get; set; }
[Parameter("Periods", DefaultValue = 14)]
public int Periods { get; set; }
[Parameter("Volume", DefaultValue = 10000)]
public double Quantity { get; set; }
private RelativeStrengthIndex rsi;
protected override void OnStart()
{
rsi = Indicators.RelativeStrengthIndex(Source, Periods);
}
protected override void OnTick()
{
if (rsi.Result.LastValue > 70)
{
TradeOperation operation = ExecuteMarketOrderAsync(TradeType.Sell, Symbol, 10000, 40, 10);
PlaceLimitOrderAsync(TradeType.Sell, Symbol, 10000, Symbol.Bid + 10 * Symbol.PipSize, 30, 10);
PlaceLimitOrderAsync(TradeType.Sell, Symbol, 20000, Symbol.Bid + 20 * Symbol.PipSize, 20, 10);
PlaceLimitOrderAsync(TradeType.Sell, Symbol, 40000, Symbol.Bid + 30 * Symbol.PipSize, 10, 10);
}
else if (rsi.Result.LastValue < 30)
{
TradeOperation operation = ExecuteMarketOrderAsync(TradeType.Buy, Symbol, 10000, 40, 10);
PlaceLimitOrderAsync(TradeType.Buy, Symbol, 10000, Symbol.Ask + 10 * Symbol.PipSize, 30, 10);
PlaceLimitOrderAsync(TradeType.Buy, Symbol, 20000, Symbol.Ask + 20 * Symbol.PipSize, 20, 10);
PlaceLimitOrderAsync(TradeType.Buy, Symbol, 40000, Symbol.Ask + 30 * Symbol.PipSize, 10, 10);
}
}
return
@alpha_austin
alpha_austin
02 Aug 2017, 16:57
Can coding it out?
Dear Support Team,
In fact, I don't know how to code the mentioned concept out, can you help?
Thanks a lot ^^
@alpha_austin