Topic: Working on FST

I'm going to work hard this week on FST.

I actually started from yesterday with some refactoring and preparing FST, dll, and expert for Martingale.

My goals are:
- Adding Martingale money management as in FSB;
- Investigating and fixing eventual bug with repeating orders from FST when SL or TP are not correctly set from MT4;
- Fixing issue with copy info from Status tab or Strategy overview  from Strategy tab. Currently Ctr-C copies the whole strategy to the clipboard. I'll limit strategy copy only to the Copy button in the tool strip. This is not an issue since most probably we copy a strategy from FSB and past it at FST.
- Providing some solution for automatic start of multiple instances and setting strategy, ID and start trading. Later I want to make a separate console with info about running strategies, some basic stats and simple controls as: Stop All, Start All or individual start and stop trading.
- Faster and flawless stop of FST. Now FST sends usage statistics to the server at closing. But this process continues longer some times (several seconds). The issue is that when multiple instances is not allowed and we decide to start FST again,  we  see error message that FST is always running.
- Adding some functionality for repeating non-executed orders several times or for a predefined period of time. This is very important for closing orders. A possibility is to repeat close orders at every minute or 30 seconds for an interval of 15 minutes or longer. It will be good to make these params adjustable.
- Major code cleanup and some refactoring. JedBrains provides ReSharper addon for VS2010 for free for freeware and opensource projects. I'll use it to cleanup FST code as I did for FSB.

I'll try to provide regularly nightly builds with the new features and I hope to prepare a beta version till the middle of July.

Any feedback, bug reports and ideas are highly welcomed.

Re: Working on FST

More ToDo:
- Writing log file automatically. Log files will be properly named with the connection ID, symbol, period, magic number and time stamp. I'll introduce a"Log" subfolder.
- Adding option to installation program for switching on / off installation of MS VC++ distributional package.

Re: Working on FST

Current problems:
- How to collect number of consecutive losses.
This parameter is required by Martingale. Since FST is stateless, consecutive losses must be counted and stored in the expert. I already made a manner of transferring additional params from MT to FST on every tick.

If you know some mql4 code for counting consecutive losses, please share.

I'm working on that now and hope to publish a testing release today.

Re: Working on FST

Expert zeroes ConsecutiveLosses when we reduce ore reverse a position.
When we close a position, ConsecutiveLosses  are zeroed when the close is at a profit and increased when the close is at a loss.

The problem is that expert doesn't count the cases when the position is closed at StopLoss or TakeProfit from the broker.

It seams that I have to add code for detecting  StopLoss  and TakeProfit activation and to modify ConsecutiveLosses  properly. If I do this, I can return that info to FST and to display a journal messages for that events.

Re: Working on FST

I added some code a couple of years ago so if it hit a maximum number of consecutive losses, FST would disconnect (to stop the strategy from trading). It looks like: if the order profit > 0, count as win, consecutive losers resets to 0;  else, increase consecutive loser count;;; then if consecutive losers is greater than parameter max losers, then disconnect.

It might have the problem of getting confused on adding or reducing to a position, or how to count stop losses and break evens.

code:

///
/// MT4-FST Server
///
int Server()
{
    string oldrequest = "";
    string expertID   = "";
    if (Connection_ID > 0)
        expertID = "(ID = " + Connection_ID + ") ";
    datetime tickTime = MarketInfo(Symbol(), MODE_TIME);
    datetime barTime = Time[0];
    string message = expertID + TimeToStr(TimeLocal(), TIME_DATE | TIME_SECONDS) + " Forex Strategy Trader is disconnected.";
    Comment(message);

    while (!IsStopped())
    {
        // KROG modification -- count previous orders, be sure less than max number of allowed losses
        // if more, then return -1 to end server
        
         int LossCount=0;
         int OrdHistory=OrdersHistoryTotal();
         // Print("ordersHistoryInit: " + ordersHistoryInit + " OrdHistory: " + OrdHistory + " LossCount: " + LossCount);
         if(OrdHistory>0) {
           for(int cnt=OrdHistory-ordersHistoryInit; cnt>=1; cnt--){
              if(OrderSelect(OrdHistory-cnt,SELECT_BY_POS,MODE_HISTORY)){
                 if (OrderSymbol()==Symbol() && OrderMagicNumber()==Expert_Magic) {
                    if (OrderProfit()>=0) {
                       LossCount=0;
                    }
                    else {
                       LossCount++;
                    }
                  }
               }
            } 
         }
         if (LossCount >= MaxLosses) {
            message = expertID + TimeToStr(TimeLocal(), TIME_DATE | TIME_SECONDS) + "Max Losses (" + LossCount + "/" + MaxLosses + ") hit, disconnecting FST";
            Comment(message);
            Print("Max Losses hit: Loss Count: " + LossCount + " MaxLosses: " + MaxLosses);
            return (-1);
         }

         // end KROG modification        
    
        LastError = 0;
        RefreshRates();

hope that helps

Re: Working on FST

I still cannot decide how to check for SL and TP activation. I have some ideas but still need testing. I'm going to update FST on Monday so we can test it on the next week.

I'm planning a short vacation (5-6 days) from 18th of July, so I really hope to update FST to next beta till then.

Hope Hackers' God will send me some inspiration.

Re: Working on FST

Update on auto start proposed here.

I used the idea of @acerguest for FST starting automation.
The batch file for autostarting will be included in the distribution.
Here is it's code:

:: This batch file runs FST automatically, sets connection ID,
:: sets a strategy name to load and starts (or not) autotrade;
:: Example:
:: start "" "Forex Strategy Trader.exe"   100   "yes"   "Test Trade"
:: Where:
::       100 is the ConectionID,
::       "yes" means start autotrade when connected,
::       "no" means do not start autotrading,
::       "Test Trade" is the strategy name without extension.

start "" "Forex Strategy Trader.exe"   100   "yes"   "Test Trade"
start "" "Forex Strategy Trader.exe"   110   "yes"   "Test Trade"
start "" "Forex Strategy Trader.exe"   120   "yes"   "Test Trade"
start "" "Forex Strategy Trader.exe"   130   "no"    "Test Trade"
start "" "Forex Strategy Trader.exe"   140   "no"    "Test Trade"

It will be available for testing on Monday together with other changes on FST.

Re: Working on FST

Code for detecting activation of StopLoss and TakeProfit

///
/// Detects if position was closed from SL or TP.
/// Must be called at every new tick.
///
void DetectSLTPActivation(string symbol)
{
    
    // Save position values from previous tick.
    double oldStopLoss   = PositionStopLoss;
    double oldTakeProfit = PositionTakeProfit;
    double oldProfit     = PositionProfit;
    int    oldType       = PositionType;

    // Update position values.
    SetAggregatePosition(symbol);

    // Compare updated values with previous tick values.
    if (oldType != OP_SQUARE && PositionType == OP_SQUARE)
    {   // Position was closed this tick must be due to SL or TP.
        double closePrice;
        double activatedSL = 0;
        double activatedTP = 0;
        if (oldType == OP_BUY)
            closePrice = MarketInfo(symbol, MODE_BID);
        else if (oldType == OP_SELL)
            closePrice = MarketInfo(symbol, MODE_ASK);

        string stopMessage = "No SL or TP activation";
        if (MathAbs(oldStopLoss - closePrice) < 2 * PipsValue)
        {   // Activated Stop Loss
            activatedSL = closePrice;
            stopMessage = "Activated StopLoss=" + activatedSL;
        }
        else if (MathAbs(oldTakeProfit - closePrice) < 2 * PipsValue)
        {   // Activated Take Profit
            activatedTP = closePrice;
            stopMessage = "Activated TakeProfit=" + activatedTP;
        }

          // For Martingale (if used)
        ConsecutiveLosses = IF_I(oldProfit < 0, ConsecutiveLosses + 1, 0);

        string message = stopMessage +
            ", Position closed at " + closePrice +
            ", Profit=" + oldProfit +
            ", ConsecutiveLosses=" + ConsecutiveLosses;

        if (Write_Log_File) WriteLogLine(message);
        Print(message);
    }
}

It compares current tick position values with previous tick values. If the position was closed, the code checks if current price is near to position's SL or TP. If yes, it assumes that the closing was due to activation of SL or TP.
We'll log this info and send it to FST for logging and drawing on the chart.
Also Martingale ConsecutiveLosses variable is be updated according to the profit / loss.

Re: Working on FST

Update:

Following tags remain open after publishing FSTv1.5.1NB.

- Automatic log file for FST;
- Repeating non executed closing orders (with an on/off option);

To fix:
- Dealing with activated SL and TP that are not properly detected from expert.

Documentation ToDO:
- Trade Settings;
- Logical Groups;

ETA next NB with log file Wednesday.

Re: Working on FST

Hi.
I`m testing 1.5.3.1, autostart seems to not work:

start "" "Forex Strategy Trader.exe"   140   "no"    "Test Trade"

got parse error, when converting to double.

Re: Working on FST

acerguest wrote:

Hi.
I`m testing 1.5.3.1, autostart seems to not work:

start "" "Forex Strategy Trader.exe"   140   "no"    "Test Trade"

got parse error, when converting to double.

Did you covert to double ConnectionID?
I'll add int.TryParse to prevent crashes.

Re: Working on FST

Don`t understand the question. I used compiled version, I didn`t have yet time to check the source code.

Re: Working on FST

What do you mean with "when converting to double." ?

Is the text "when converting to double." a part of the errormessage?
thomas

Re: Working on FST

Yes, that was a part of error message (but might be not the exact words).

15 (edited by acerguest 2012-07-17 23:37:37)

Re: Working on FST

I`m sorry, I wrongly interpreted this exception to be autostart issue - but it`s not.
Just installed fresh 1.5.3b, updated Mt4, and when FST is connecting to MT4, it throws this exception below. Broker Oanda & GoMarkets.

Ok probably I know what`s the problem, as it`s conversion issue, it must be good/old -> different Culture. Do you use US culture (I`m from Europe) ?


************** Exception Text **************
System.FormatException: Input string was not in a correct format.
   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)
   at System.Double.Parse(String s, NumberStyles style, NumberFormatInfo info)
   at Forex_Strategy_Trader.Actions.ParseAndSetParametrs(String parameters)
   at Forex_Strategy_Trader.Actions.TimerPingTick(Object sender, EventArgs e)
   at System.Windows.Forms.Timer.OnTick(EventArgs e)
   at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Re: Working on FST

After publishing FST 1.5.3.3 Beta I have to find a way for implementing the requested repeat of failed close orders feature.

Repeating Failed Close Orders
My thoughts are to make FST checking the return status of close orders. If close order returns an error or we still have a position after that, FST must repeat the close order. It's clear but:
- how frequently FST should repeat failed close orders: At every minute, At every new bar...
- When to cancel resending of close orders: after a specified time...
- do we need to stop strategy activity until the position gets closed (I mean adding and so on)
Please comment on that topic.

Fixing Martingale.
Current issue is failing of catching activated Stop loss or Take profits some times. It means that consecutive losses stats may not be correct. It is not a big issue, but we have to make FST as better as we can. Any thoughts on that matter?

I'm going to release stable FST after fixing these topics. Am I missing something?

I'll be in Bulgaria till the end of August. FST and FSB must become rock solid till than.

Re: Working on FST

Repeating Failed Close Orders

I think the failure to close an order is a critical issue. There can be nothing worse than an open position going against you that you have no control over. A bit like in the early days when you couldn't get hold of your broker and no-one else wanted to get involved with a fellow brokers book. Here are my thoughts.

1. If the close order is sent and it fails I think the order should be continuously retried until canceled by operator intervention.  At the same time a flashing warning should be displayed on FST as a visual indication of a problem and maybe even send an email indicating an order failure.

2. How long should it retry for? Maybe every 30 seconds would seem reasonable. Maybe for an hour, after that the market would either be closed or there is another problem.

3. I think strategy activity should be stopped until the order is closed. You cannot keep trading a strategy when half of it is broken.

4. I do occasionally get a message that says market closed when it isn't and the next minute the order goes through so getting a market closed response shouldn't be an indication to stop sending the close order.

Re: Working on FST

In the interests of making FST rock solid I have another issue you might want to consider. FST doesn't check that the time frame or currency pair match that of the MT4 chart. I was caught with this when I changed my AUDUSD strategy from a 30 minute to a 15 minute time frame but forgot to change the MT4 chart. FST didn't know and merrily went on trading a demo account to nothing!

Re: Working on FST

Thank you SpiderMan for the reasonable request. I'm going to work on that matter today.


I was caught with this when I changed my AUDUSD strategy from a 30 minute to a 15 minute time frame but forgot to change the MT4 chart.

If the strategy comes from FSB, it has a time frame and a symbol record. I can add a warning and additional confirmation if one tries to start trading on different chart.

If strategy was set directly in FST it can also record info for the market it was initially started to trade.
Normally FST stop trading if the user changes the chart, but currently there is no warning or confirmation when tarding is started on different chart.

I can also add a record if a strategy was first tested on a Demo account. If it wasn't, FST can show a confirmation dialog.

Re: Working on FST

FST will show a warning message and will play an error sound when detects a failed close order.

http://i.imgur.com/8x0US.png

FST will resend close order at every 30 seconds until the warning message is closed or the position is closed.
The position can be also close manually.