Ghostrider

Post Reply
Fourxxxx

Ghostrider

Post by Fourxxxx »

Hope I am not out of line posting this here.

I think that the trailing stop is not working as expected.

These 2 lines adding or subtracting from the current StopLoss rather than the Bid.

3683

Code: Select all

NewStop = NormalizeDouble(sl + (TrailingStopPips / factor), Digits)
3703

Code: Select all

NewStop = NormalizeDouble(sl - (TrailingStopPips / factor), Digits);
With the changes here it seems to be working as intended.

Code: Select all

void TrailingStopLoss(int ticket)
{

   //Security check
   if (!BetterOrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
      return;
   
   if (OrderProfit() < 0) return;//Nothing to do
   string LineName = SlPrefix + DoubleToStr(OrderTicket(), 0);
   double sl = ObjectGet(LineName, OBJPROP_PRICE1);
   //if (CloseEnough(sl, 0) ) return;//No line, so nothing to do
   if (CloseEnough(sl, 0) ) sl = OrderStopLoss();
   double NewStop;
   bool modify=false;
   bool result;
   
    if (OrderType()==OP_BUY)
       {
          if (sl < OrderOpenPrice() ) return;//Not at breakeven yet
          // Increment sl by sl + TrailingStopPips.
          if (CloseEnough(sl, 0) ) sl = MathMax(OrderStopLoss(), OrderOpenPrice());
          
          //Check that the market is sufficiently in profit to continue
          if (Bid < OrderOpenPrice() + (trailStartsAtProfit / factor) )
            return;
            
          if (Bid >= sl + (TrailingStopPips / factor) )//George
          {
             NewStop = NormalizeDouble(Bid - (TrailingStopPips / factor), Digits); //FourXXX
             if (HiddenPips > 0) ObjectMove(LineName, 0, Time[0], NewStop);
             if (NewStop - OrderStopLoss() >= Point) modify = true;//George again. What a guy
          }//if (Bid >= MathMax(sl,OrderOpenPrice()) + (TrailingStopPips / factor) )//George
       }//if (OrderType()==OP_BUY)
       
       if (OrderType()==OP_SELL)
       {
          if (sl > OrderOpenPrice() ) return;//Not at breakeven yet
          // Decrement sl by sl - TrailingStopPips.
          if (CloseEnough(sl, 0) ) sl = MathMin(OrderStopLoss(), OrderOpenPrice());
          if (CloseEnough(sl, 0) ) sl = OrderOpenPrice();
          
          //Check that the market is sufficiently in profit to continue
          if (Bid > OrderOpenPrice() - (trailStartsAtProfit / factor) )
            return;
           
          
          if (Bid <= sl  - (TrailingStopPips / factor))//George
          {
             NewStop = NormalizeDouble(Bid + (TrailingStopPips / factor), Digits); //FourXXXX
             if (HiddenPips > 0) ObjectMove(LineName, 0, Time[0], NewStop);
             if (OrderStopLoss() - NewStop >= Point || OrderStopLoss() == 0) modify = true;//George again. What a guy   
          }//if (Bid <= MathMin(sl, OrderOpenPrice() ) - (TrailingStopPips / factor) )//George
       }//if (OrderType()==OP_SELL)


   //Move 'hard' stop loss whether hidden or not. Don't want to risk losing a breakeven through disconnect.
   if (modify)
   {
      while (IsTradeContextBusy() ) Sleep(100);
      result = ModifyOrder(OrderTicket(), OrderOpenPrice(), NewStop, OrderTakeProfit(), OrderExpiration(), clrNONE, __FUNCTION__, slm);
   }//if (modify)
      
} // End of TrailingStopLoss sub
User avatar
SteveHopwood
Owner
Posts: 9754
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Ghostrider

Post by SteveHopwood »

Fourxxxx » Wed Oct 28, 2020 11:47 am wrote:Hope I am not out of line posting this here.

I think that the trailing stop is not working as expected.

These 2 lines adding or subtracting from the current StopLoss rather than the Bid.

3683

Code: Select all

NewStop = NormalizeDouble(sl + (TrailingStopPips / factor), Digits)
3703

Code: Select all

NewStop = NormalizeDouble(sl - (TrailingStopPips / factor), Digits);
With the changes here it seems to be working as intended.

Code: Select all

void TrailingStopLoss(int ticket)
{

   //Security check
   if (!BetterOrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
      return;
   
   if (OrderProfit() < 0) return;//Nothing to do
   string LineName = SlPrefix + DoubleToStr(OrderTicket(), 0);
   double sl = ObjectGet(LineName, OBJPROP_PRICE1);
   //if (CloseEnough(sl, 0) ) return;//No line, so nothing to do
   if (CloseEnough(sl, 0) ) sl = OrderStopLoss();
   double NewStop;
   bool modify=false;
   bool result;
   
    if (OrderType()==OP_BUY)
       {
          if (sl < OrderOpenPrice() ) return;//Not at breakeven yet
          // Increment sl by sl + TrailingStopPips.
          if (CloseEnough(sl, 0) ) sl = MathMax(OrderStopLoss(), OrderOpenPrice());
          
          //Check that the market is sufficiently in profit to continue
          if (Bid < OrderOpenPrice() + (trailStartsAtProfit / factor) )
            return;
            
          if (Bid >= sl + (TrailingStopPips / factor) )//George
          {
             NewStop = NormalizeDouble(Bid - (TrailingStopPips / factor), Digits); //FourXXX
             if (HiddenPips > 0) ObjectMove(LineName, 0, Time[0], NewStop);
             if (NewStop - OrderStopLoss() >= Point) modify = true;//George again. What a guy
          }//if (Bid >= MathMax(sl,OrderOpenPrice()) + (TrailingStopPips / factor) )//George
       }//if (OrderType()==OP_BUY)
       
       if (OrderType()==OP_SELL)
       {
          if (sl > OrderOpenPrice() ) return;//Not at breakeven yet
          // Decrement sl by sl - TrailingStopPips.
          if (CloseEnough(sl, 0) ) sl = MathMin(OrderStopLoss(), OrderOpenPrice());
          if (CloseEnough(sl, 0) ) sl = OrderOpenPrice();
          
          //Check that the market is sufficiently in profit to continue
          if (Bid > OrderOpenPrice() - (trailStartsAtProfit / factor) )
            return;
           
          
          if (Bid <= sl  - (TrailingStopPips / factor))//George
          {
             NewStop = NormalizeDouble(Bid + (TrailingStopPips / factor), Digits); //FourXXXX
             if (HiddenPips > 0) ObjectMove(LineName, 0, Time[0], NewStop);
             if (OrderStopLoss() - NewStop >= Point || OrderStopLoss() == 0) modify = true;//George again. What a guy   
          }//if (Bid <= MathMin(sl, OrderOpenPrice() ) - (TrailingStopPips / factor) )//George
       }//if (OrderType()==OP_SELL)


   //Move 'hard' stop loss whether hidden or not. Don't want to risk losing a breakeven through disconnect.
   if (modify)
   {
      while (IsTradeContextBusy() ) Sleep(100);
      result = ModifyOrder(OrderTicket(), OrderOpenPrice(), NewStop, OrderTakeProfit(), OrderExpiration(), clrNONE, __FUNCTION__, slm);
   }//if (modify)
      
} // End of TrailingStopLoss sub
Thanks. :clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap:

I could not see how the code would work properly and meant to sort it out, then became involved in another project and forgot. I will make the change and update later. DIYers, just copy the function over the top of the existing one.

:xm: :rocket:
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. [email protected] is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
User avatar
SteveHopwood
Owner
Posts: 9754
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Ghostrider

Post by SteveHopwood »

V 1g is in post 1, with FourXXXX's fix for the faulty trailing stop. I must have been on drugs when I coded my version.

Easy DIY fix. Do a search for:
NewStop = NormalizeDouble(sl + (TrailingStopPips / factor), Digits);

Replace it with:
//Thanks to FourXXXX for sorting out this next line of code
NewStop = NormalizeDouble(Bid - (TrailingStopPips / factor), Digits); //FourXXX

Then do a search for:
NewStop = NormalizeDouble(sl - (TrailingStopPips / factor), Digits);

Replace it with:
//Thanks to FourXXXX for sorting out this next line of code
NewStop = NormalizeDouble(Bid + (TrailingStopPips / factor), Digits); //FourXXXX

:xm: :rocket:
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. [email protected] is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
User avatar
SteveHopwood
Owner
Posts: 9754
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Ghostrider

Post by SteveHopwood »

V 1his in post 1.

I am working with c1borg on a more accurate representation of Ghostrider's trading method. Referring back to this version I noticed that I had not included the StopLossDistancePips value into the stop loss calculation.

Easy DIY for those of you who prefer this:
Do a search for:
stop = CalculateStopLoss(OP_BUY, rectangleLow[arrayIndex]);

Replace it with:
stop = CalculateStopLoss(OP_BUY, rectangleLow[arrayIndex] - (StopLossDistance / factor) );

Then do a search for:
stop = CalculateStopLoss(OP_SELL, rectangleHigh[arrayIndex]);

Replace it with:
stop = CalculateStopLoss(OP_SELL, rectangleHigh[arrayIndex] + (StopLossDistance / factor) );

Brief update on progress. The H4 demo is a small loser. The H1 demo is in the green by $240 - 0.2 lots on a $20,000 deposit. There is an H1 demo with all the latest stuff thrown at it, balance in loss but equity in profit thanks to a massive AU sell that is currently at +125. Mind, I had forgotten to set the jumping stop so it probably would not have gotten so far.

That last one should maybe give us pause for thought. Maybe we should be allowing the trades a longer run? There are not many of them, after all.

:xm: :rocket:
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. [email protected] is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
wallywonka
Trader
Posts: 176
Joined: Thu May 12, 2016 7:46 am

Ghostrider

Post by wallywonka »

Agree on this Steve, had a demo run last week and it had no stop loss settings and it did quite well.. it would have stopped out a few times. I do like the stop loss just outside the trading zone though as some trades do continue through the support/resistance zones so it is good to get out of those trades.
Pipstubborn
Trader
Posts: 272
Joined: Thu Jul 12, 2012 5:17 pm

Ghostrider

Post by Pipstubborn »

Hi Wally,

Not sure what I'm doing wrong Just got one trade in the whole week, AUDUSD (in profit).
Would you mind to share your PRESET FILE with the new version V1 h?

Thanks in advance,
Pipstubborn
User avatar
SteveHopwood
Owner
Posts: 9754
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Ghostrider

Post by SteveHopwood »

Pipstubborn » Fri Oct 30, 2020 2:32 pm wrote:Hi Wally,

Not sure what I'm doing wrong Just got one trade in the whole week, AUDUSD (in profit).
Would you mind to share your PRESET FILE with the new version V1 h?

Thanks in advance,
Pipstubborn
This one is not a trading tart. If the ducks don't line up, they don't line up.

:xm: :rocket:
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. [email protected] is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
User avatar
SteveHopwood
Owner
Posts: 9754
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Ghostrider

Post by SteveHopwood »

V 1i is in post 1, responding to a request from Wally that I include the option to send stop orders following a trade trigger. The inputs are immediately under the 'General Inputs' section and should be obvious but details in the user guide if not.

A DIY job should not be too difficult. Go to the space in between the support/resistance variables and the trading time frame inputs and insert the inputs for the trade types:

Code: Select all

extern string  sep17="================================================================";
extern string  ttp="---- Trade Types ----";
extern bool    SendImmediateMarketOrder=true;
extern bool    SendStopOrder=false;
extern int     DistanceFromMarketPips=10;//No of pips away from market to set the stop
////////////////////////////////////////////////////////////////////////////////////////
double         distanceFromMarket=0;
////////////////////////////////////////////////////////////////////////////////////////
do a search for: trailStartsAtProfit = TrailStartsAtProfitPips;
Add this to the list
distanceFromMarket = DistanceFromMarketPips;

Do a search for: type=OP_BUY;

Replace the existing code block with:

Code: Select all

      if (SendImmediateMarketOrder)
      {
         type=OP_BUY;
         stype = " Buy ";
         price = Ask;
      }//if (SendImmediateMarketOrder)
      else
      {
         type=OP_BUYSTOP;
         stype = " Buy stop ";
         price = NormalizeDouble(Ask + (distanceFromMarket / factor), Digits);
      }//else
Do a search for: Do a search for: type=OP_SELL;
Replace that block of code with:

Code: Select all

      if (SendImmediateMarketOrder)
      {
         type=OP_SELL;
         stype = " Sell ";
         price = Bid;
      }//if (SendImmediateMarketOrder)
      else
      {
         type=OP_SELLSTOP;
         stype = " Sell stop ";
         price = NormalizeDouble(Bid - (distanceFromMarket / factor), Digits);
      }//else
  

:xm: :rocket:
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. [email protected] is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
wallywonka
Trader
Posts: 176
Joined: Thu May 12, 2016 7:46 am

Ghostrider

Post by wallywonka »

Cheers for adding this in Steve, it would have saved me from a couple of bad trades last week!

:clap: :clap: :clap:
User avatar
SteveHopwood
Owner
Posts: 9754
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Ghostrider

Post by SteveHopwood »

wallywonka » Sun Nov 01, 2020 11:33 am wrote:Cheers for adding this in Steve, it would have saved me from a couple of bad trades last week!

:clap: :clap: :clap:
You are most welcome Wally. You are becoming a most helpful member of SHF. :clap: :clap: :clap: :clap: :clap:

:xm: :rocket:
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. [email protected] is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
Post Reply

Return to “Automated trading systems”