Help with coding, something really strange

Post Reply
User avatar
LittleCaro
Trader
Posts: 63
Joined: Tue May 19, 2015 7:09 am

Help with coding, something really strange

Post by LittleCaro »

Hello guys,

I'm facing a really strange thing, that's several days i'm on it, and well ...

Here is the thing : i create something really basic with Steve's shell.

I want to set pending order + x pips to the high of the previous candle, the thing it :

It's working with High[0] and High[2], BUT it's not working with High[1] the one that i'm interested in.

The expert says error 130 invalid stops, and that's the same stops used with High[0] and High[2]


Maybe some of our geniuses can look at it.

Here is the code :

Code: Select all

 //Long 
   if (SendLong)
   {
       
     
      type=OP_BUYLIMIT;
      stype = " Buy Limit ";
      price = High[1] + (PipsAdjust/factor);
         
      if (!SendAlertNotTrade)
      {
         
         stop = CalculateStopLoss(OP_BUYLIMIT, price);
         
         
         take = CalculateTakeProfit(OP_BUYLIMIT, price);
         
         
         //Lot size calculated by risk
         if (!CloseEnough(RiskPercent, 0)) SendLots = CalculateLotSize(price, NormalizeDouble(stop + (HiddenPips / factor), Digits) );

         
         
      }//if (!SendAlertNotTrade)
      
      SendTrade = true;
And the code for calculating stoploss :

Code: Select all

double CalculateStopLoss(int type, double price)
{
   //Returns the stop loss for use in LookForTradingOpps and InsertMissingStopLoss
   double stop;

    
    
   RefreshRates();
   
   
   if (type == OP_BUYLIMIT)
   {
      if (!CloseEnough(StopLoss, 0) ) 
      {
         stop = price - (StopLoss / factor);
         HiddenStopLoss = stop;
      }//if (!CloseEnough(StopLoss, 0) ) 

      if (HiddenPips > 0 && stop > 0) stop = NormalizeDouble(stop - (HiddenPips / factor), Digits);
   }//if (type == OP_BUY)
Thanks guys !
User avatar
LittleCaro
Trader
Posts: 63
Joined: Tue May 19, 2015 7:09 am

Help with coding, something really strange

Post by LittleCaro »

Hey guys,

When i was under my shower, i find partly a solution :

It's the setting : PostTradeAttemptWaitSecond, if i put it to zero, it works.

But now i have another problem, the limit order stays active for only 1 bar if he isn't triggerded, after he gets deleted.

You put a security to delete the pendings ? I looked through the code and find nothing ?
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.

Help with coding, something really strange

Post by SteveHopwood »

LittleCaro » Mon Jun 08, 2020 3:35 pm wrote:Hello guys,

I'm facing a really strange thing, that's several days i'm on it, and well ...

Here is the thing : i create something really basic with Steve's shell.

I want to set pending order + x pips to the high of the previous candle, the thing it :

It's working with High[0] and High[2], BUT it's not working with High[1] the one that i'm interested in.

The expert says error 130 invalid stops, and that's the same stops used with High[0] and High[2]


Maybe some of our geniuses can look at it.

Here is the code :

Code: Select all

 //Long 
   if (SendLong)
   {
       
     
      type=OP_BUYLIMIT;
      stype = " Buy Limit ";
      price = High[1] + (PipsAdjust/factor);
         
      if (!SendAlertNotTrade)
      {
         
         stop = CalculateStopLoss(OP_BUYLIMIT, price);
         
         
         take = CalculateTakeProfit(OP_BUYLIMIT, price);
         
         
         //Lot size calculated by risk
         if (!CloseEnough(RiskPercent, 0)) SendLots = CalculateLotSize(price, NormalizeDouble(stop + (HiddenPips / factor), Digits) );

         
         
      }//if (!SendAlertNotTrade)
      
      SendTrade = true;
And the code for calculating stoploss :

Thanks guys !
Good for you for getting your hands dirty by looking under the bonnet of CrapQl4. :clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap:

Re this:
price = High[1] + (PipsAdjust/factor);

Supposing the market had fallen by the time your EA tries to execute this code. You could be trying to send a limit order at an impossible price because the market is already lower than your limit price. You need to check that 'price' is below the current market price so that a limit order is possible. From there, you either abort the trade or send a stop order.

Assuming you are happy to send a stop order, then the construct would be this:
type = BUYLIMIT;
price = High[1] + (PipsAdjust/factor);
if (price > Ask)//In English to follow
{
  • Abort the function if you will not allow stop orders instead of limit orders.
  • Adjust the type code etc if you are happy to accept stop orders.
}

Up an at 'em, Tiger. I hope this helps. Ask away if not.

: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.

Help with coding, something really strange

Post by SteveHopwood »

LittleCaro » Mon Jun 08, 2020 5:25 pm wrote:Hey guys,

When i was under my shower, i find partly a solution :

It's the setting : PostTradeAttemptWaitSecond, if i put it to zero, it works.

But now i have another problem, the limit order stays active for only 1 bar if he isn't triggerded, after he gets deleted.

You put a security to delete the pendings ? I looked through the code and find nothing ?
Not that I can think of. Try my latest and see, then I will take a look if need be.

: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
LittleCaro
Trader
Posts: 63
Joined: Tue May 19, 2015 7:09 am

Help with coding, something really strange

Post by LittleCaro »

Hello Steve,

Thanks a lot for all your help.

I restarted from zero, with your latest shell code.

And all is working good.

I also realized that i made a mistake between stop and limit orders.

Sometimes i'm so silly :smile:


Thanks for all !
tzaro1
Posts: 1
Joined: Wed Nov 03, 2021 1:41 pm

Help with coding, something really strange

Post by tzaro1 »

LittleCaro » Tue Jun 09, 2020 5:12 pm wrote:Hello Steve,

Thanks a lot for all your help.

I restarted from zero, with your latest shell code.

And all is working good.

I also realized that i made a mistake between stop and limit orders.

Sometimes i'm so silly :smile:


Thanks for all !
Hi, glad you got it worked out. Regarding 1 bar expiry, the limit/ stop orders in Empty4 use an additional parameter variable of the type 'datetime'. It is the 2nd to last one and between magic number and color. Hope that helps.
Post Reply

Return to “Coders Hangout”