need trailling stop
need trailling stop
17 Feb 2015, 00:33
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 Bot4_Feuer : Robot
{
// Paraméterek
[Parameter("Label", DefaultValue = "Feuer")]
public string Label { get; set; }
[Parameter("Initial volume", DefaultValue = 1000, MinValue = 1000)]
public int Init_Volume { get; set; }
[Parameter("Absolute volume limit", DefaultValue = 32000)]
public int Absolute_Volume { get; set; }
[Parameter("Stop loss", DefaultValue = 5)]
public int StopLoss { get; set; }
[Parameter("Take profit", DefaultValue = 7)]
public int TakeProfit { get; set; }
[Parameter(DefaultValue = 0)]
public int TrailingStop { get; set; }
[Parameter(DefaultValue = 0)]
public int TriggerTrailing { get; set; }
// Segédváltozók
bool buy_closed = true;
bool sell_closed = true;
// private Random random = new Random();
//int action_count = 0;
//TradeType last_action;
// Start
protected override void OnStart()
{
if (Symbol.Code != position.SymbolCode) continue;
//set initial sotploss
if (position.TradeType == TradeType.Sell && position.StopLoss == null)
Trade.ModifyPosition(position, position.EntryPrice + init_StopLoss * Symbol.PipSize, null);
if (position.TradeType == TradeType.Buy && position.StopLoss == null)
Trade.ModifyPosition(position, position.EntryPrice - init_StopLoss * Symbol.PipSize, null);
{
Print("Starting bot");
// EventHandler hozzáadása
Positions.Closed += OnPositionsClosed;
// Nyitás két irányba
Open_TwoWays();
}
private void Open_TwoWays()
{
Print("Opening two ways");
ExecuteAction(Init_Volume, TradeType.Buy);
ExecuteAction(Init_Volume, TradeType.Sell);
}
// Művelet végrehajtása
private void ExecuteAction(long vol, TradeType type)
{
switch (type)
{
case TradeType.Sell:
sell_closed = false;
break;
case TradeType.Buy:
buy_closed = false;
break;
}
// Abszolút limit ellenőrzése
if (vol > Absolute_Volume)
{
vol = Init_Volume;
}
Print("Executing action: {0}, Volume: {1}", type, vol);
//last_action = type;
var e = ExecuteMarketOrder(type, Symbol, vol, Label, StopLoss, TakeProfit);
if (e.Error == ErrorCode.NoMoney)
{
Print("No more money");
// Ha elfogyott a pénzünk..
Stop();
}
}
// Zárás event
private void OnPositionsClosed(PositionClosedEventArgs args)
{
var p = args.Position;
// Nézzük meg hogy ez a mienk-e
if (p.Label != Label || p.SymbolCode != Symbol.Code)
{
// Nem érdekel minket
return;
}
switch (p.TradeType)
{
case TradeType.Sell:
sell_closed = true;
break;
case TradeType.Buy:
buy_closed = true;
break;
}
Print("Position closed [Action: {0}, Profit: {1}]", p.TradeType, p.GrossProfit);
Print("buy: {0}, sell: {1}", buy_closed, sell_closed);
// Újranyitás, ha már minden zárt
if (buy_closed && sell_closed)
{
Open_TwoWays();
}
}
// Fő program
protected override void OnTick()
{
// Összes tétel listázása
foreach (var openedPosition in Positions.FindAll(Label))
{
Position Position = openedPosition;
bool profitable = (Position.GrossProfit > 0);
switch (Position.TradeType)
{
case TradeType.Sell:
if (profitable)
{
Print("Sell -> Profit [Action: Trailing stop]");
double profit1 = Position.GrossProfit + Position.Commissions - StopLoss * 10 * Position.Volume / 100000.0;
if (profit1 > 0.0 && Position.TradeType == TradeType.Sell)
{
double? stopLossPrice = Symbol.Bid + StopLoss * Symbol.PipSize;
if (StopLoss != 0 && stopLossPrice < Position.StopLoss && stopLossPrice < Position.EntryPrice)
{
if (stopLossPrice - Symbol.Bid > 0)
{
ModifyPosition(Position, stopLossPrice, Position.TakeProfit);
}
}
}
}
else
{
if (Position.GrossProfit < -1 * StopLoss)
{
Print("Sell -> No profit -> Loss over limit [Action: Closing position]");
ClosePosition(Position);
}
else
{
Print("Sell -> No profit -> Loss under limit [Action: No action]");
}
}
break;
case TradeType.Buy:
if (profitable)
{
Print("Buy -> Profit [Action: Trailing stop]");
double profit2 = Position.GrossProfit + Position.Commissions - StopLoss * 10 * Position.Volume / 100000.0;
if (profit2 > 0.0 && Position.TradeType == TradeType.Buy)
{
double? stopLossPrice = Symbol.Ask - StopLoss * Symbol.PipSize;
if (StopLoss != 0 && stopLossPrice > Position.StopLoss && stopLossPrice > Position.EntryPrice)
if (stopLossPrice - Symbol.Ask < 0)
ModifyPosition(Position, stopLossPrice, Position.TakeProfit);
}
}
else
{
if (Position.GrossProfit < -1 * StopLoss)
{
Print("Buy -> No profit -> Loss over limit [Action: Closing position]");
ClosePosition(Position);
}
else
{
Print("Buy -> No profit -> Loss under limit [Action: No action]");
}
}
break;
}
}
}
}
}
can u put trailling stop in this robot?
Replies
kricka
21 Feb 2015, 04:05
Hi,
trailing stop for positions can be very profitable. How about a trailing stop for the trading account itself? We can have many positions opened at the same time. The idea is to have a trialing stop for the entire account regardless of how many positions are opened at the same time. This make sense to have the double protection in case a major draw down is on its way.
@kricka
Spotware
20 Feb 2015, 17:27
We can recommend you to contact one of our Partners or post a job in Development Jobs section.
@Spotware