Topics
Replies
Uche
31 Oct 2012, 18:51
RE:
Hello,
We are testing your code but have been unsuccessful thus far in regenerating neither a technical error nor multiple positions.
In any case we are sending you a modified version of your code which might be a little more clear to read which includes reseting the pendingorder field to ensure that this robot creates exactly one position/pending order each time.
private Position position;
private PendingOrder pendingOrder;
private HistoricalVolatility _Hvol;
private WilliamsPctR _Wp;
// Initialize Indicators
protected override void OnStart()
{
_Hvol = Indicators.HistoricalVolatility(Source, HVPeriod, HVBarhistory, HVD);
_Wp = Indicators.WilliamsPctR(WprPeriod);
}
protected override void OnTick()
{
if (Trade.IsExecuting) return;
ResetPendingOrder();
double Hvol = _Hvol.Result.LastValue;
double Wp = _Wp.Result.LastValue;
double Time = Server.Time.Hour;
bool isTradingTime = Time >= 2 && Time <= 23;
bool isHighVolatility = Hvol >= 0.003 && Hvol <= 0.020;
if (isTradingTime && isHighVolatility && position == null && pendingOrder == null)
{
double? stopLoss;
DateTime? expiration = Server.Time.AddSeconds(20);
if (Wp < -80)
{
stopLoss = Symbol.Bid - Symbol.PipSize*StopLoss;
Trade.CreateBuyStopOrder(Symbol, Volume, Symbol.Bid, stopLoss, null, expiration);
}
else if (Wp > -20)
{
stopLoss = Symbol.Ask + Symbol.PipSize*StopLoss;
Trade.CreateSellStopOrder(Symbol, Volume, Symbol.Ask, stopLoss, null, expiration);
}
}
if (position == null) return;
if (position.TradeType == TradeType.Sell)
{
double distance = position.EntryPrice - Symbol.Ask;
if (distance > Trigger*Symbol.PipSize)
{
double newStopLossPrice = Symbol.Ask + TrailingStop*Symbol.PipSize;
if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
{
Trade.ModifyPosition(position, newStopLossPrice, position.TakeProfit);
}
}
}
else
{
double distance = Symbol.Bid - position.EntryPrice;
if (distance > Trigger*Symbol.PipSize)
{
double newStopLossPrice = Symbol.Bid - TrailingStop*Symbol.PipSize;
if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
{
Trade.ModifyPosition(position, newStopLossPrice, position.TakeProfit);
}
}
}
}
private void ResetPendingOrder()
{
bool exists = false;
foreach (var accountPendingOrder in Account.PendingOrders)
{
if (pendingOrder.Equals(accountPendingOrder))
{
exists = true;
break;
}
}
if (!exists)
pendingOrder = null;
}
protected override void OnPendingOrderCreated(PendingOrder newOrder)
{
pendingOrder = newOrder;
bool isLargeSpread = Symbol.Spread > 2;
bool isLargeBidDiff = (pendingOrder.TargetPrice - Symbol.Bid)*Symbol.PipSize > 2;
bool isLargeAskDiff = (Symbol.Ask - pendingOrder.TargetPrice)*Symbol.PipSize > 2;
if (position != null || isLargeSpread || isLargeBidDiff || isLargeAskDiff)
{
Trade.DeletePendingOrder(pendingOrder);
pendingOrder = null;
}
}
protected override void OnPositionOpened(Position openedPosition)
{
position = openedPosition;
// Pending Order Filled
pendingOrder = null;
if (position.GrossProfit < MaxLoss)
{
Trade.Close(position);
}
}
protected override void OnPositionClosed(Position closedPosition)
{
position = null;
}
}
}
Looks good on tester.Lets see how it goes on real time.
Thanks for prompt response.
@Uche
Uche
31 Oct 2012, 10:41
Hello Admin,
Please look closely at the code and performance to accertain why multiple orders are sent to the server.
Five orders were placed on one currency pair at same time while trading the 1min/2min charts;this is the highest number of orders I have seen so far.
I am trying to code my Mt4 strategy in Calgo,I love the speed and easy coding language but its starting to disappoint.
Kind regards
@Uche
Uche
26 Oct 2012, 13:34
Hello,
I modified the code to:
using System.Collections.Generic;
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
[Robot]
public class Uchechukwu : Robot
{
[Parameter]
public DataSeries Source { get; set; }
[Parameter(DefaultValue = 100)]
public int WprPeriod { get; set; }
[Parameter("HV Period", DefaultValue = 14)]
public int HVPeriod { get; set; }
[Parameter("HV Barhistory", DefaultValue = 400)]
public int HVBarhistory { get; set; }
[Parameter(DefaultValue = 2)]
public int HVD { get; set; }
[Parameter(DefaultValue = 100000)]
public int Volume { get; set; }
[Parameter("Stop Loss (pips)", DefaultValue = 30)]
public int StopLoss { get; set; }
[Parameter("Trigger (pips)", DefaultValue = 30)]
public int Trigger { get; set; }
[Parameter("Trailing Stop (pips)", DefaultValue = 30)]
public int TrailingStop { get; set; }
[Parameter("MaxLoss", DefaultValue = -500.0)]
public double MaxLoss { get; set; }
private Position position;
private PendingOrder pendingorder;
private HistoricalVolatility _Hvol;
private WilliamsPctR _Wp;
///
/// Initialize Indicators
///
protected override void OnStart()
{
_Hvol = Indicators.HistoricalVolatility(Source,HVPeriod,HVBarhistory,HVD);
_Wp = Indicators.WilliamsPctR(WprPeriod);
}
protected override void OnTick()
{
if (Trade.IsExecuting) return;
double Hvol = _Hvol.Result.LastValue;
double Wp = _Wp.Result.LastValue;
double Time = Server.Time.Hour;
if (Time>=2 && Time<=23 && Hvol>=0.003 && Hvol<=0.020 && Wp<-80 && position == null)
{
if (pendingorder == null)
{
Trade.CreateBuyStopOrder(Symbol,Volume,Symbol.Bid,Symbol.Bid - Symbol.PipSize * StopLoss,null,Server.Time.AddSeconds(20));
}
}
if (Time>=2 && Time<=23 && Hvol>=0.003 && Hvol<=0.020 && Wp>-20 && position == null)
{
if (pendingorder == null)
{
Trade.CreateSellStopOrder(Symbol,Volume,Symbol.Ask,Symbol.Ask + Symbol.PipSize * StopLoss,null,Server.Time.AddSeconds(20));
}
}
if (position == null) return;
{
if (position != null && position.TradeType == TradeType.Sell)
{
double distance = position.EntryPrice - Symbol.Ask;
if (distance > Trigger * Symbol.PipSize)
{
double newStopLossPrice = Symbol.Ask + TrailingStop * Symbol.PipSize;
if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
{
Trade.ModifyPosition(position, newStopLossPrice, position.TakeProfit);
}
}
}
else
{
double distance = Symbol.Bid - position.EntryPrice;
if (distance > Trigger * Symbol.PipSize)
{
double newStopLossPrice = Symbol.Bid - TrailingStop * Symbol.PipSize;
if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
{
Trade.ModifyPosition(position, newStopLossPrice, position.TakeProfit);
}
}
}
}
}
protected override void OnPendingOrderCreated(PendingOrder newOrder)
{
pendingorder = newOrder;
if (pendingorder != null && pendingorder.SymbolCode == Symbol.Code)
{
if (position != null && position.SymbolCode == Symbol.Code || Symbol.Spread>2 || (pendingorder.TargetPrice-Symbol.Bid)*Symbol.PipSize>2 || (Symbol.Ask-pendingorder.TargetPrice)*Symbol.PipSize>2)
{
Trade.DeletePendingOrder(pendingorder);
}
}
}
protected override void OnPositionOpened(Position openedPosition)
{
position = openedPosition;
if (position != null && position.GrossProfit < MaxLoss)
{
Trade.Close(position);
}
}
}
}
@Uche
Uche
26 Oct 2012, 10:43
This is my code:
using System.Collections.Generic;
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
[Robot]
public class Uchechukwu : Robot
{
[Parameter]
public DataSeries Source { get; set; }
[Parameter(DefaultValue = 100)]
public int WprPeriod { get; set; }
[Parameter("HV Period", DefaultValue = 14)]
public int HVPeriod { get; set; }
[Parameter("HV Barhistory", DefaultValue = 400)]
public int HVBarhistory { get; set; }
[Parameter(DefaultValue = 2)]
public int HVD { get; set; }
[Parameter(DefaultValue = 100000)]
public int Volume { get; set; }
[Parameter("Stop Loss (pips)", DefaultValue = 30)]
public int StopLoss { get; set; }
[Parameter("Trigger (pips)", DefaultValue = 30)]
public int Trigger { get; set; }
[Parameter("Trailing Stop (pips)", DefaultValue = 30)]
public int TrailingStop { get; set; }
[Parameter("MaxLoss", DefaultValue = -500.0)]
public double MaxLoss { get; set; }
private Position position;
private PendingOrder pendingorder;
private HistoricalVolatility _Hvol;
private WilliamsPctR _Wp;
///
/// Initialize Indicators
///
protected override void OnStart()
{
_Hvol = Indicators.HistoricalVolatility(Source,HVPeriod,HVBarhistory,HVD);
_Wp = Indicators.WilliamsPctR(WprPeriod);
}
protected override void OnTick()
{
if (Trade.IsExecuting) return;
double Hvol = _Hvol.Result.LastValue;
double Wp = _Wp.Result.LastValue;
double Time = Server.Time.Hour;
if (Time>=2 && Time<=23 && Hvol>=0.003 && Hvol<=0.020 && Wp<-80 && position == null)
{
if (pendingorder == null)
{
Trade.CreateBuyStopOrder(Symbol,Volume,Symbol.Bid,Symbol.Bid - Symbol.PipSize * StopLoss,null,Server.Time.AddSeconds(20));
}
}
if (Time>=2 && Time<=23 && Hvol>=0.003 && Hvol<=0.020 && Wp>-20 && position == null)
{
if (pendingorder == null)
{
Trade.CreateSellStopOrder(Symbol,Volume,Symbol.Ask,Symbol.Ask + Symbol.PipSize * StopLoss,null,Server.Time.AddSeconds(20));
}
}
if (pendingorder != null && pendingorder.SymbolCode == Symbol.Code)
{
if (position != null && position.SymbolCode == Symbol.Code || Symbol.Spread>2 || (pendingorder.TargetPrice-Symbol.Bid)*Symbol.PipSize>2 || (Symbol.Ask-pendingorder.TargetPrice)*Symbol.PipSize>2)
{
Trade.DeletePendingOrder(pendingorder);
}
}
if (position == null) return;
{
if (position != null && position.GrossProfit < MaxLoss)
{
Trade.Close(position);
}
if (position != null && position.TradeType == TradeType.Sell)
{
double distance = position.EntryPrice - Symbol.Ask;
if (distance > Trigger * Symbol.PipSize)
{
double newStopLossPrice = Symbol.Ask + TrailingStop * Symbol.PipSize;
if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
{
Trade.ModifyPosition(position, newStopLossPrice, position.TakeProfit);
}
}
}
else
{
double distance = Symbol.Bid - position.EntryPrice;
if (distance > Trigger * Symbol.PipSize)
{
double newStopLossPrice = Symbol.Bid - TrailingStop * Symbol.PipSize;
if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
{
Trade.ModifyPosition(position, newStopLossPrice, position.TakeProfit);
}
}
}
}
}
private void OpenPosition(TradeType command)
{
if (position != null)
{
Trade.Close(position);
position = null;
}
Trade.CreateMarketOrder(command, Symbol, Volume);
}
protected override void OnPositionOpened(Position openedPosition)
{
position = openedPosition;
Trade.ModifyPosition(openedPosition, GetAbsoluteStopLoss(openedPosition, StopLoss), null);
}
private double GetAbsoluteStopLoss(Position position, int stopLossInPips)
{
return position.TradeType == TradeType.Buy
? position.EntryPrice - Symbol.PipSize * stopLossInPips
: position.EntryPrice + Symbol.PipSize * stopLossInPips;
}
private void ClosePosition(Position position)
{
if (position == null) return;
Trade.Close(position);
}
}
}
@Uche
Uche
07 Nov 2012, 11:47
Testing Symbol and currency.
AUDUSD,GBP Account.
@Uche