Hi Letharion,
I noticed from a lot of your topics and replies that you are very familiar with TS/EL. I am actually trying to covert my EL logic into C# and am very hard time...
Following are the EL logic for money mamagement fucntions. All the examples in openquant are percent stop exits.
If I want to set my stop exit at $1000 for StopLoss,$5000 for ProfitTarget and $3000 of Floor Amt with 5% trailing Stop for Percent Trailing, how would I write logic in C# ?
----EasyLanguage ----
Inputs: SL(1000), PT(5000), FA(3000), PTrail(5);
SetStopLoss(SL);
SetProfitTarget(PT);
SetPercentTrailing(FA,PTrail);
------------------------------
I attached my C# logic.. I am confusing on treating my Target Price too. Thanks !
Code:
public class MyStrategy:Strategy
{
[Parameter ("Order quantity ")]
double Qty = 10;
[Parameter (" Long Momentum Length")]
int Length_L = 3;
[Parameter (" Short Momentum Length")]
int Length_S = 4;
[Parameter ("Profit Target Long")]
double pTarge_L = 5000;
[Parameter ("Profit Traget Short")]
double pTarget_S = 4000;
[Parameter ("Stop Type", "Stop")]
StopType StopType = StopType.Trailing;
[Parameter ("Stop Mode", "Stop")]
StopMode StopMode = StopMode.Percent;
[Parameter ("Stop Loss Long")]
double StopLoss_L = 1000;
[Parameter ("Stop Loss Short")]
double StopLoss_S = 3000;
// Indicators
MOM mLong;
MOM mShort;
Order sellOrder, buyOrder, stopOrder, limitOrder;
public override void OnStrategyStart()
{
mLong = new MOM(Bars, Length_L);
mShort = new MOM(Bars, Length_S);
}
public override void OnBar(Bar bar)
{
if(Bars.Count < 10) return;
double pivotval_L = Bars.Ago(1).Close + mLong[bar];
double pivotval_S = Bars.Ago(1).Close + mShort[bar];
if (!HasPosition)
{
if(Bar.Close > pivotval_L)
{
Buy(Qty,"Buy Long");
}
}
else
{ if (!HasPosition)
{
if(Bar.Close < pivotval_S)
{
Sell(Qty, "Sell Short");
}
}
}
}
// Long Stop Exit
public override void OnPositionOpened()
{
SetStop(Position,StopLoss_L,StopType, StopMode);
}
// Short Stop Exit
public override void OnPositionOpened()
{
SetStop(Position,StopLoss_S,StopType, StopMode);
}
public override void OnStopExecuted(Stop stop)
{
if(Position.Side == PositionSide.Long)
{
Order marketOrder = MarketOrder (Instrument, OrderSide.Sell, Qty, "LongStopLoss");
marketOrder.Send();
}
if (Position.Side == PositionSide.Short)
{
Order marketOrder = MarketOrder(Instrument, OrderSide.Buy, Qty, "ShortStopLoss");
marketOrder.Send();
}
}
}