SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Sat Dec 14, 2019 3:56 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Mon Aug 11, 2008 5:52 am 
Offline

Joined: Wed Jan 09, 2008 12:19 pm
Posts: 49
Hi,

I am trying to set my stop Loss, Profit Target and Trailing Stop at specific amounts. For Example, I want to set my stop loss at $3000, profit Target at $5000, and Trailing Stop at $3000 with 5% Percent Trailing.

Thanks !


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 11, 2008 11:33 am 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6816
Strategy class:

SetStop (Position, Double, StopType, StopMode) Sets new stop on the specified position with specified parameters

Stop class

OnStopExecuted event handler will be called in the strategy when Stop is triggered. You can override this method to close opened position or take other actions.


So that you set a specific type of stop in a strategy using SetStop method and then write in OnStopExecuted event handler what you want to do (particularly Sell(Position.Qty) or Buy).

You can also search this forum for Stop or SetStop to see code examples.

Regards,
Anton


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 11, 2008 3:00 pm 
Offline

Joined: Wed Jan 09, 2008 12:19 pm
Posts: 49
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(); 
      }
   }         
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 11, 2008 3:29 pm 
Offline

Joined: Thu Mar 20, 2008 4:41 pm
Posts: 273
Location: Sweden
I haven't used trails in OQ, but there's an example in help -> strategy development -> 4.3.2 Breakout with Multiple Exits

Here's "my" code with stop&targets

Code:
using OpenQuant.API;
using OpenQuant.API.Indicators;

public class MyStrategy:Strategy {
   
   
   // 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(Bar.Close < pivotval_S) {
            Sell(Qty, "Sell Short");
         }
      }
   }
   
   public override void OnPositionOpened() {   
      if(HasPosition && Position.Side == PositionSide.Short) SetStop(Position,StopLoss_S,StopType, StopMode);
      if(HasPosition && Position.Side == PositionSide.Long) SetStop(Position,StopLoss_L,StopType, StopMode);

      if(Position.Side == PositionSide.Long) {
         double target_price = 0;//Calculation here to determine at what price you want to exit@target
         buyOrder = BuyLimitOrder(Qty, target_price, "Exit (Take Profit)");
      } else {
         double target_price = 0;//Calculation here to determine at what price you want to exit@target
         sellOrder = SellLimitOrder(Qty, target_price, "Exit (Take Profit)");
      }
   }
 
   public override void OnStopExecuted(Stop stop) {
      ClosePosition();
   }     

   [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;
}


This code never updates the targets, you'll probably want to do that in OnBar

_________________
Asking smart qustions:
http://www.catb.org/~esr/faqs/smart-que ... html#intro

I don't mind when people PM me to see if I have the time to help, but please post in the forum FIRST, and then grab my attention so others can benefit aswell.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 12, 2008 4:00 pm 
Offline

Joined: Wed Jan 09, 2008 12:19 pm
Posts: 49
Letharion, Thank you so much !!


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 01, 2009 8:54 pm 
Offline

Joined: Thu Aug 21, 2008 5:16 am
Posts: 46
Anton,
I applied a very simple 3% trailing stop in my strategy but the stategy doesn't do trailing stop when it runs. Here is my code for the trailing stop:

public override void OnPositionOpened()
{

SetStop(0.03, StopType.Trailing, StopMode.Percent);
}



public override void OnStopExecuted(Stop stop)
{
ClosePosition();
}

Please help,

Thanks!

Tom


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 04, 2009 10:49 am 
Offline

Joined: Wed Oct 08, 2003 1:06 pm
Posts: 833
Hi,

I've tried your code and it works fine on my machine.

Maybe the order that closes the position is sent but is not filled somewhy. Could you check if the order exists in the Order Manager window. Or maybe print out some debug information in the OnStopExecuted handler to check if the stop gets executed or not.

Regards,
Sergey.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 04, 2009 6:39 pm 
Offline

Joined: Thu Aug 21, 2008 5:16 am
Posts: 46
Sergey,
I made the following change to the code and trailing stop started working:
From:
public override void OnPositionOpened()
{

SetStop(0.03, StopType.Trailing, StopMode.Percent);
}


To:
public override void OnPositionOpened()
{

SetStop(Position,0.03, StopType.Trailing, StopMode.Percent);
}

Could you tell me what is the difference here? According to the API guide, there should be no difference between these two.

Thanks,

Tom
Baraz Sergey wrote:
Hi,

I've tried your code and it works fine on my machine.

Maybe the order that closes the position is sent but is not filled somewhy. Could you check if the order exists in the Order Manager window. Or maybe print out some debug information in the OnStopExecuted handler to check if the stop gets executed or not.

Regards,
Sergey.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 05, 2009 12:16 pm 
Offline

Joined: Wed Oct 08, 2003 1:06 pm
Posts: 833
Hi,

Yes, you are right, there should not be differences, since SetStop method without Position as a parameter simply calls SetStop method passing strategy Position as the first parameter.

I would say it is completely impossible that the only difference between woring and non working versions is the Position parameter. So it will be be great and interesting if you post a complete sample here that reproduces the problem.

Regards,
Sergey.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 11, 2009 2:49 am 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
It seems to me that SetStop always calculates a stop price that is relative to the current bar close price. In other words, you cannot use the SetStop method to specify an arbitrary fixed stop price of your own choosing. And I take it that the StopType.Absolute does NOT mean "an absolute number of dollars", otherwise the following code would work.

Code:
    if (TrailingStop == null && bar.Close > SD.PosEntryPrice + 0.20) {
        double stopprice = SD.PosEntryPrice + 0.10;
        MyStop = p.SetStop (p.Position, stopprice, StopType.Fixed, StopMode.Absolute);
        fmt = "{0} {1} Exit stop set. Entry price {2:F2}, stop price {3:F2}";
        fmt += ", curprice = {4:F2}";
        CP.W (fmt, CP.BD (bar), sym, SD.PosEntryPrice, stopprice, bar.Close);
        CP.W (fmt, CP.BD (bar), sym, SD.PosEntryPrice, MyStop.StopPrice, bar.Close);
    }


Here is the output that it produced. The code fed SetStop a value of 48.72 for an "absolute" stop price value, but 0.20 actually ended up in the stop object itself.

Code:
2009-04-22 02:43:05 CL Exit stop set. Entry price 48.62, stop price 48.72, curprice = 48.87
2009-04-22 02:43:05 CL Exit stop set. Entry price 48.62, stop price  0.20, curprice = 48.87


(1) Where did the 0.20 come from?

(2) Could one of you SmartQuant guys please give clear definitions for the parameters in the SetStop function calls?

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC + 3 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group