0
Completed

Orders Strategy Inconvinient

Natalia da Fontoura 1 year ago updated by Андрей Ринас2 (ATAS developer) 1 year ago 5

I developed this so simple Strategy. When the candle(bar -1) is Up (green) place 2 Orders Limit Long, if the candle(bar -1) is not Up place 2 Orders Limit Sell.

Then problem I am having is the strategy is putting a lot of Orders, not only 2. 

In the code there is a part that is checking if there are active orders. But there is a delay between the Orders are placed and the strategy can see them:

private bool ActiveOrders()
{
foreach (var order in Orders.Where(t => t.State == OrderStates.Active))
{
return true;
}
return false;
}


This is the full strategy:

using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using System.IO;

using ATAS.Strategies.Chart;
using ATAS.Indicators;
using ATAS.DataFeedsCore;

using Utils.Common;
using Utils.Common.Logging;

namespace StrategyATAS
{
public class PruebaStrategy : ChartStrategy
{
#region Private fields
public bool isOpenTrade = false;
public bool isEntryOrder;
public bool isStopOrder;
public bool isTrailStopOrder;
public bool isTargetOrder;
public decimal enterPrice;
public decimal stopPrice;
public decimal targetPrice;
public bool isLong;
#endregion

#region PropiedadesStrategy
#endregion

#region Overrides of BaseIndicator

protected override void OnCalculate(int bar, decimal value)
{
var candle = GetCandle(bar);
if (bar > 0)
{
candle = GetCandle(bar - 1);
}

if (isOpenTrade)
{
VerifyTradeStatus(candle, bar);
}
if (IsUp(candle))
{
OpenTrade(candle, "Long", "LONG TENDENCIA", bar);
}
else
{
OpenTrade(candle, "Short", "SHORT CONTRATENDENCIA", bar);
}
}
#endregion

#region Private methods

private void OpenPosition(OrderDirections direction)
{
if (isEntryOrder)
{
var order = new Order
{
Portfolio = Portfolio,
Security = Security,
Direction = direction,
Type = OrderTypes.Limit,
Price = enterPrice,
QuantityToFill = 2,
Comment = "Entrada"
};
OpenOrder(order);
isEntryOrder = false;
}
if (isStopOrder)
{
var order = new Order
{
Portfolio = Portfolio,
Security = Security,
Direction = direction,
Type = OrderTypes.Limit,
Price = enterPrice,
QuantityToFill = 2,
Comment = "Stop"
};
OpenOrder(order);
isStopOrder = false;
}
if (isTargetOrder)
{
var order = new Order
{
Portfolio = Portfolio,
Security = Security,
Direction = direction,
Type = OrderTypes.Limit,
Price = targetPrice,
QuantityToFill = 2,
Comment = "Target"
};
isTargetOrder = false;
}
}

#endregion


#region MetodosAuxiliares

public bool IsUp(IndicatorCandle candle)
{
if (candle.High >= candle.Close && candle.Close > candle.Open && candle.Open >= candle.Low) return true;

if (candle.Close < candle.Open && candle.Close < candle.High) return false;

return false;
}

private bool ActiveOrders()
{
foreach (var order in Orders.Where(t => t.State == OrderStates.Active))
{
return true;
}
return false;
}

public void VerifyTradeStatus(IndicatorCandle candleParam, int bar)
{
var candle = GetCandle(bar);
if (Math.Abs(CurrentPosition) == 2)
{
isStopOrder = true;
if (isLong)
{
OpenPosition(OrderDirections.Sell);
}
else
{
OpenPosition(OrderDirections.Buy);
}
}
else
{
if (!ActiveOrders())
{
isOpenTrade = false;
}
}
}

public void OpenTrade(IndicatorCandle candle, string tipoTrade, string tipotradeAux, int bar)
{
if (!isOpenTrade && CurrentPosition == 0 && !ActiveOrders())
{
isOpenTrade = true;
if (tipoTrade.Equals("Long"))
{
isEntryOrder = true;
enterPrice = candle.Open;
stopPrice = enterPrice - 4;
targetPrice = enterPrice + 4;
OpenPosition(OrderDirections.Buy);
}
else
{
enterPrice = candle.Open;
stopPrice = enterPrice + 4;
targetPrice = enterPrice - 4;
isEntryOrder = true;
OpenPosition(OrderDirections.Sell);
}
}
}

#endregion


}
}


Can you help me please?

Regards,

Natalia

Under review

Hello

When you send an order you need to wait until it is placed or rejected.

Order doesn't get Active state immediately after sending. Only when ATAS receive Active response from the exchange, state of order is changed.

Hello

In my code I have this part that open an Stop Order:

if (isStopOrder)
{
var order = new Order
{
Portfolio = Portfolio,
Security = Security,
Direction = direction,
Type = OrderTypes.Stop,
Price = enterPrice,
QuantityToFill = 2,
Comment = "Stop"
};
OpenOrder(order);
isStopOrder = false;

But I have this error

The stopPrice is correct, I was being debugging and is OK.

Can you help me?

Regards,

Natalia

Hello

In my code I have this part that open an Stop Order:

if (isStopOrder)
{
var order = new Order
{
Portfolio = Portfolio,
Security = Security,
Direction = direction,
Type = OrderTypes.Stop,
Price = enterPrice,
QuantityToFill = 2,
Comment = "Stop"
};
OpenOrder(order);
isStopOrder = false;

But I have this error

The stopPrice is correct, I was being debugging and is OK.

Can you help me?

Regards,

Natalia

Hello.

For stop orders you need to specify TriggerPrice.