Miami Lakes
Experience: Beginner
Platform: Multicharts
Broker: Interactive Brokers
Trading: Emini ES
Posts: 19 since Oct 2016
Thanks Given: 2
Thanks Received: 3
|
Hi everyone,
I am working on this strategy and need help figuring this out please. The strategy begins with opening up a position either long or short doesn't matter. Set a target profit and a stop loss and send the profit and stop orders right away. If target profit is reached, then exit and wait for another position to be opened (manually). If the stop loss is reached, then it should close the initial position, and then open up twice as many contracts in the opposite direction, and this is where I need help. The stop works fine, but I haven't been able to figure out how to send those twice as many contracts. This is what I have so far.
With this line
stoLongToShort.Send(takeReversePrice,takeReversePrice,orderSize*2);
stoLongToShort is a stop limit order, I am attempting to enter the reverse position, but the order just won't go through. I am sure I am missing something. Please help, thanks in advance.
using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using ATCenterProxy.interop;
namespace PowerLanguage.Strategy {
[IOGMode(IOGMode.Enabled)]
public class ProfitTaker : SignalObject
{
private IOrderMarket bto,sto;
private IOrderPriced stcProfitLong, btcProfitShort;
private IOrderStopLimit stcStopLong, btcStopShort, stoLongToShort, btoShortToLong;
private double takeProfitPrice, takeReversePrice;
int orderSize = 0;
int maxEntriesInPosition = 10;
int maxOrderSize = 1000;
[Input]
public int ProfitTicks { get; set; }
[Input]
public int StopTicks { get; set; }
public ProfitTaker(object _ctx) : base(_ctx) { }
private bool bracketSet;
protected override void Create() {
bto = OrderCreator.MarketThisBar(new SOrderParameters(Contracts.UserSpecified, "Buy", EOrderAction.Buy));
sto = OrderCreator.MarketThisBar(new SOrderParameters(Contracts.UserSpecified, "SellShort", EOrderAction.SellShort));
stcProfitLong = OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "TakeProfitLong", EOrderAction.Sell));
stcStopLong = OrderCreator.StopLimit(new SOrderParameters(Contracts.UserSpecified, "StopLong", EOrderAction.Sell));
stoLongToShort = OrderCreator.StopLimit(new SOrderParameters(Contracts.UserSpecified, "LongToShort", EOrderAction.SellShort));
btcProfitShort = OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "TakeProfitShort", EOrderAction.BuyToCover));
btcStopShort = OrderCreator.StopLimit(new SOrderParameters(Contracts.UserSpecified, "StopReverseShort", EOrderAction.BuyToCover));
btoShortToLong = OrderCreator.StopLimit(new SOrderParameters(Contracts.UserSpecified, "ShortToLong", EOrderAction.Buy));
ProfitTicks = 24;
StopTicks = 8;
}
protected override void StartCalc() {
Output.Clear();
}
protected override void CalcBar() {
if (StrategyInfo.MarketPosition == 0 && StrategyInfo.MarketPositionAtBroker != 0) {
ChangeMarketPosition(StrategyInfo.MarketPositionAtBroker - StrategyInfo.MarketPosition, StrategyInfo.AvgEntryPriceAtBroker);
Output.WriteLine("{0} - Sync with broker", Bars.Time[0].ToString("d-M HH:mm"));
}
if (StrategyInfo.MarketPosition == 0) {
orderSize = 1;
} else {
orderSize = Math.Min(CurrentPosition.OpenLots, maxOrderSize);
}
if (StrategyInfo.MarketPosition > 0) {
int barsInPosition = Bars.CurrentBar - Positions[0].OpenTrades[0].EntryOrder.BarNumber;
if (barsInPosition == 0) {
takeProfitPrice = Positions[0].OpenTrades[0].EntryOrder.Price + (ProfitTicks * (Bars.Info.MinMove / Bars.Info.PriceScale));
takeReversePrice = Positions[0].OpenTrades[0].EntryOrder.Price - (StopTicks * (Bars.Info.MinMove / Bars.Info.PriceScale));
}
stcProfitLong.Send(takeProfitPrice,orderSize);
stcStopLong.Send(takeReversePrice,takeReversePrice,orderSize);
stoLongToShort.Send(takeReversePrice,takeReversePrice,orderSize*2);
}
if (StrategyInfo.MarketPosition < 0) {
int barsInPosition = Bars.CurrentBar - Positions[0].OpenTrades[0].EntryOrder.BarNumber;
if (barsInPosition == 0) {
takeProfitPrice = Positions[0].OpenTrades[0].EntryOrder.Price - (ProfitTicks * (Bars.Info.MinMove / Bars.Info.PriceScale));
takeReversePrice = Positions[0].OpenTrades[0].EntryOrder.Price + (StopTicks * (Bars.Info.MinMove / Bars.Info.PriceScale));
}
btcProfitShort.Send(takeProfitPrice,orderSize);
btcStopShort.Send(takeReversePrice,takeReversePrice,orderSize);
btoShortToLong.Send(takeReversePrice,takeReversePrice,orderSize*2);
}
}
}
}
|