How to create a new order when trade lose (sample when short sl is hit to create double short)
How to create a new order when trade lose (sample when short sl is hit to create double short)
10 Jun 2015, 03:42
I am looking to add the next to this bot
When trade is loss trade double in the direction of the loss and when trade is closed with profit start from the first step in the direction of the profit
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class SampleMartingalecBot : Robot
{
[Parameter("Initial Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double InitialQuantity { get; set; }
[Parameter("Stop Loss", DefaultValue = 40)]
public int StopLoss { get; set; }
[Parameter("Take Profit", DefaultValue = 40)]
public int TakeProfit { get; set; }
private Random random = new Random();
protected override void OnStart()
{
Positions.Closed += OnPositionsClosed;
ExecuteOrder(InitialQuantity, position.TradeType());
}
private void ExecuteOrder(double quantity, TradeType tradeType)
{
var volumeInUnits = Symbol.QuantityToVolume(quantity);
var result = ExecuteMarketOrder(tradeType, Symbol, volumeInUnits, "Martingale", StopLoss, TakeProfit);
if (result.Error == ErrorCode.NoMoney)
Stop();
}
private void OnPositionsClosed(PositionClosedEventArgs args)
{
Print("Closed");
var position = args.Position;
if (position.Label != "Martingale" || position.SymbolCode != Symbol.Code)
return;
if (position.GrossProfit > 0)
{
ExecuteOrder(InitialQuantity, position.TradeType());
}
else
{
ExecuteOrder(position.Quantity * 2, position.TradeType);
}
}
private TradeType position.TradeType()
{
return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell;
}
Spotware
11 Jun 2015, 14:33
Dear Trader,
We do not provide coding assistance services. We more than glad to assist you with specific questions about cAlgo.API. You also can contact one of our Partners or post a job in Development Jobs section for further coding assistance.
@Spotware